def jac(x): gam_acute, alpha_acute, tau_acute, sigma_acute = x tau = np.exp(tau_acute) alpha = np.exp(alpha_acute) gamma = np.exp(gam_acute) sigma = np.exp(sigma_acute) KS = gpkernel.RBF(sep, gamma) KT = gpkernel.Matern32(obst, tau=tau) WSWT = (W @ KS @ W.T) Kw = alpha * KT * (WSWT) covd = sigma**2 * np.eye(Ni) Cov = covd + Kw sign, logdet = np.linalg.slogdet(Cov) P = scipy.linalg.inv(Cov) Xlc = P @ lc ## gamma dgam_acute = gpkernel.d_RBF(sep, gamma) dKw = alpha * KT * (W @ dgam_acute @ W.T) dLdgam = np.trace(P @ dKw) - Xlc @ dKw @ Xlc ## alpha dLdalpha = np.trace(P @ Kw) - Xlc @ Kw @ Xlc ## tau dtau_acute = gpkernel.d_Matern32(obst, tau) dKw = alpha * dtau_acute * (WSWT) dLdtau = np.trace(P @ dKw) - Xlc @ dKw @ Xlc ## dLdsigma = np.trace(P @ covd) - Xlc @ covd @ Xlc grad = np.array([dLdgam, dLdalpha, dLdtau, dLdsigma]) return grad
def log_likelihood(theta, d, covd): p_zeta,p_Thetaeq,p_gamma,p_alpha=theta WI,WV=mocklc.comp_weight(nside,p_zeta,inc,p_Thetaeq,Thetav,Phiv) Wp=WV[:,:]*WI[:,:] #KS=p_alpha*gpkernel.Matern32(sep,p_gamma) KS=p_alpha*gpkernel.RBF(sep,p_gamma) Cov = covd + Wp@[email protected] sign,logdet=np.linalg.slogdet(Cov) Pi_d=scipy.linalg.solve(Cov,d,assume_a="pos") prop = -0.5*logdet-0.5*d@Pi_d #-0.5*np.shape(cov)[0]*np.log(2.0*np.pi) return prop
def log_likelihood(theta, d): p_gamma,p_alpha,p_tau,p_sigma=theta #KS=p_alpha*gpkernel.Matern32(sep,p_gamma) KS=gpkernel.RBF(sep,p_gamma) KT=gpkernel.Matern32(obst,tau=p_tau) WSWT=(W@[email protected]) Kw=p_alpha*KT*(WSWT) ###### covd=p_sigma**2*np.eye(Ni) Cov = covd + Kw try: sign,logdet=np.linalg.slogdet(Cov) Pi_d=scipy.linalg.solve(Cov,d,assume_a="pos") prop = -0.5*logdet-0.5*d@Pi_d #-0.5*np.shape(cov)[0]*np.log(2.0*np.pi) except: prop = -np.inf return prop
def log_likelihood(theta, d, covd): p_zeta, p_Thetaeq, p_gamma, p_alpha, p_tau, p_Pspin = theta wspin = 2 * np.pi / p_Pspin Phiv = np.mod(wspin * obst, 2 * np.pi) WI, WV = mocklc.comp_weight(nside, p_zeta, inc, p_Thetaeq, Thetav, Phiv) Wp = WV[:, :] * WI[:, :] #KS=p_alpha*gpkernel.Matern32(sep,p_gamma) KS = gpkernel.RBF(sep, p_gamma) KT = gpkernel.Matern32(obst, tau=p_tau) WSWT = (Wp @ KS @ Wp.T) Kw = p_alpha * KT * (WSWT) ###### Cov = covd + Kw try: sign, logdet = np.linalg.slogdet(Cov) Pi_d = scipy.linalg.solve(Cov, d, assume_a="pos") prop = -0.5 * logdet - 0.5 * d @ Pi_d #-0.5*np.shape(cov)[0]*np.log(2.0*np.pi) return prop except: return -np.inf
def func(x): gam_acute, alpha_acute, tau_acute, sigma_acute = x tau = np.exp(tau_acute) alpha = np.exp(alpha_acute) gamma = np.exp(gam_acute) sigma = np.exp(sigma_acute) print(tau, alpha, gamma, sigma) KS = gpkernel.RBF(sep, gamma) KT = gpkernel.Matern32(obst, tau=tau) WSWT = (W @ KS @ W.T) Kw = alpha * KT * (WSWT) covd = sigma**2 * np.eye(Ni) Cov = covd + Kw sign, logdet = np.linalg.slogdet(Cov) try: Xlc = scipy.linalg.solve(Cov, lc, assume_a="pos") nlev = logdet + lc @ Xlc except: nlev = np.inf return nlev
## dLdsigma = np.trace(P @ covd) - Xlc @ covd @ Xlc grad = np.array([dLdgam, dLdalpha, dLdtau, dLdsigma]) return grad ts = time.time() #res=scipy.optimize.minimize(func, x0,method="powell") res = scipy.optimize.minimize(func, x0, method="L-BFGS-B", jac=jac, tol=1.e-12) gamast = np.exp(res["x"][0]) alphaast = np.exp(res["x"][1]) tauast = np.exp(res["x"][2]) sigmaast = np.exp(res["x"][3]) print("gamma,alpha,tau,sigma=", gamast / np.pi * 180, "deg", alphaast, tauast, "d", sigmaast) print("gamma,alpha,tau,sigma=", gamast, "radian", alphaast, tauast, "d", sigmaast) # Final map import rundynamic_cpu as rundynamic import plotdymap KS = gpkernel.RBF(sep, gamast) KT = gpkernel.Matern32(obst, tauast) Pid = sigmaast**-2 * np.eye(Ni) #Data precision matrix Aast = rundynamic.Mean_DYSOT(W, KS, KT, alphaast, lc, Pid) plotdymap.plotseqmap(Aast, frames, "mapev", title=title, Earth=True) #,vmin=0.05,vmax=0.35)
sep = sepmat.calc_sepmatrix(nside) ## compute Ns, Npar = np.shape(flat_samples) Aast = [] #Nsample=10 Nsample = Nj randmap = [] rm = False frames = [0, int(Ni / 2), Ni - 1] randmap = [] Aast = np.zeros((Ni, Nj)) for n in tqdm.tqdm(range(0, Nsample)): zeta, Thetaeq, gamma, alpha, tau, Pspin = flat_samples[n, :] KS = gpkernel.RBF(sep, gamma) KT = gpkernel.Matern32(obst, tau) Pid = sigma**-2 * np.eye(Ni) #Data precision matrix Sigmad = sigma**2 * np.eye(Ni) #weight recomputing wspin = 2 * np.pi / Pspin Phiv = np.mod(wspin * obst, 2 * np.pi) WI, WV = mocklc.comp_weight(nside, zeta, inc, Thetaeq, Thetav, Phiv) W = WV[:, :] * WI[:, :] #Aasteach=rundynamic.Mean_DYSOT(W,KS,KT,alpha,lc,Pid) P, Aasteach = rundynamic.PMean_DYSOT(W, KS, KT, alpha, lc, Pid, Sigmad) Aast = Aast + Aasteach #RANDOMIZED MAP
fig = corner.corner(flat_samples, labels=labels, truths=[zeta, Thetaeq, None, None]) plt.savefig("corner" + tag + ".png") plt.savefig("corner" + tag + ".pdf") sigma = np.mean(lc) * 0.01 ## kernel Ni, Nj = np.shape(W) nside = hp.npix2nside(Nj) sep = sepmat.calc_sepmatrix(nside) ## comute Ns, Npar = np.shape(flat_samples) mu = [] Nave = 100 #Nave=Ns for i in tqdm.tqdm(range(0, Nave)): zeta, Thetaeq, gamma, alpha = flat_samples[i, :] KS = alpha * gpkernel.RBF(sep, gamma) Pid = sigma**-2 * np.eye(Ni) #Data precision matrix mueach = runstatic.Mean_STSOT(W, KS, lc, Pid) mu.append(mueach) mu = np.array(mu) mu = np.mean(mu, axis=0) hp.mollview(mu, title="", flip="geo", cmap=plt.cm.pink, min=0, max=1.0) plt.savefig("mu.pdf") plt.show()