def MCMCadams(size, lam_init, rho_init, T_init, thismtPP, nInsDelMov, kappa, delta, L, mu_lam, sigma2, p, a, b, n, V, mu_init, mean_mu, var_mu, diagnostics, res, thin, GP_mom_scale, range_mom_scale): ### independent type prior mean ### initialize GP thisGP = GP(zeroMean, expCov(1, rho_init)) ### location container initialization K = thismtPP.K totLocInit = np.concatenate( (thismtPP.locs, PPP.randomHomog(lam=int(lam_init // (K + 1))).loc), 0) nObs = thismtPP.nObs locations = bdmatrix(int(10 * lam_init), totLocInit, nObs, "locations") # initial size is a bit of black magic ### cov matrix initialization Rmat = dsymatrix(int(10 * lam_init), thisGP.covMatrix(totLocInit), nObs) ### GP values container initialization # ### try to initiate GP in logical position # mean_init = np.zeros(shape=(locations.nThin+nObs,K)) # mean_init[:nObs,:] = np.transpose(np.linalg.cholesky(np.linalg.inv(T_init))@thismtPP.typeMatrix) # #### values = bdmatrix( int(10 * lam_init), matrix_normal.rvs(rowcov=Rmat.sliceMatrix(), colcov=np.linalg.inv(T_init)) + mu_init, nObs, "values") ### parameters containers lams = np.empty(shape=(size)) rhos = np.empty(shape=(size)) Ts = np.empty(shape=(size, K, K)) mus = np.empty(shape=(size, 1, K)) Nthins = np.empty(shape=(size)) ### independent type prior mean Vm1 = np.linalg.inv(V) ### lams[0] = lam_init rhos[0] = rho_init Ts[0] = T_init mus[0] = mu_init Nthins[0] = locations.nThin ### instantiate containers for diagnostics if diagnostics: danceLocs = np.empty(shape=(int(size // thin), int(10 * lam_init), 2)) GPvaluesAtObs = np.empty(shape=(int(size // thin), nObs, K)) fieldsGrid = np.empty(shape=(int(size // thin), res**2, K + 1)) danceLocs[0, :int(nObs + Nthins[0])] = locations.totLoc() GPvaluesAtObs[0] = values.obsLoc() gridLoc = makeGrid([0, 1], [0, 1], res) s_11 = thisGP.cov(gridLoc, gridLoc) S_21 = thisGP.cov(locations.totLoc(), gridLoc) S_12S_22m1 = np.dot(np.transpose(S_21), Rmat.inver) muGrid = np.dot(S_12S_22m1, values.totLoc() - mus[0]) + mus[0] spatSig = s_11 - np.dot(S_12S_22m1, S_21) A = np.linalg.cholesky(Ts[0]) Am = sp.linalg.solve_triangular(A, np.identity(K), lower=True) newVal = np.linalg.cholesky(spatSig) @ np.random.normal( size=(res**2, K)) @ Am + muGrid fieldsGrid[0] = lams[0] * np.array([multExpit(val) for val in newVal]) i = 1 diagnb = 1 while i < size: j = 0 while j < nInsDelMov: birthDeathMove(lams[i - 1], kappa, thisGP, locations, values, Rmat, Ts[i - 1], mus[i - 1]) j += 1 Nthins[i] = locations.nThin # # locTot_prime = np.concatenate((locThin_prime,thisPPP.loc)) # valTot_prime = np.concatenate((valThin_prime,obsVal[i-1])) # nthin = locThin_prime.shape[0] # # Sigma = thisGP.covMatrix(locTot_prime) # A = np.linalg.cholesky(Sigmas[i]) # ntot = A.shape[0] # whiteVal_prime = sp.linalg.solve_triangular(A,np.identity(ntot),lower=True)@valTot_prime # functionSampler(delta,L,values,Sigma) rhos[i] = functionRangeSampler(delta, L, values, Rmat, rhos[i - 1], Ts[i - 1], mus[i - 1], thismtPP.typeMatrix, a, b, GP_mom_scale, range_mom_scale) Ts[i] = typePrecisionSampler(n, Vm1, values, Rmat, mus[i - 1]) mus[i] = typeMeanSampler(values, Rmat, Ts[i], mean_mu, var_mu) thisGP = GP(zeroMean, expCov(1, rhos[i])) # valTot_prime = A @ whiteVal_prime # thinLoc[i] = locThin_prime # thinVal[i] = valTot_prime[:nthin,:] # obsVal[i] = valTot_prime[nthin:,:] # ntot = valTot_prime.shape[0] lams[i] = intensitySampler(mu_lam, sigma2, values.nThin + values.nObs) if diagnostics and i % thin == 0: danceLocs[diagnb, :int(nObs + Nthins[i])] = locations.totLoc() GPvaluesAtObs[diagnb] = values.obsLoc() s_11 = thisGP.cov(gridLoc, gridLoc) S_21 = thisGP.cov(locations.totLoc(), gridLoc) S_12S_22m1 = np.dot(np.transpose(S_21), Rmat.inver) muGrid = np.dot(S_12S_22m1, values.totLoc() - mus[i]) + mus[i] spatSig = s_11 - np.dot(S_12S_22m1, S_21) A = np.linalg.cholesky(Ts[i]) Am = sp.linalg.solve_triangular(A, np.identity(K), lower=True) newVal = np.linalg.cholesky(spatSig) @ np.random.normal( size=(res**2, K)) @ Am + muGrid fieldsGrid[diagnb] = lams[i] * np.array( [multExpit(val) for val in newVal]) diagnb += 1 if p: ### next sample locations.nextSamp() values.nextSamp() print(i) i += 1 if diagnostics: ### dancing locations plot mpdf = PdfPages('0thinLocs.pdf') diagnb = 0 while (diagnb < int(size // thin)): fig = plt.figure() ax = fig.add_subplot(111) ax.set_aspect('equal') plt.plot(danceLocs[diagnb, nObs:int(nObs + Nthins[diagnb * thin]), 0], danceLocs[diagnb, nObs:int(nObs + Nthins[diagnb * thin]), 1], 'o', c=(0.75, 0.75, 0.75)) for pp in thismtPP.pps: plt.plot(pp.loc[:, 0], pp.loc[:, 1], 'o') plt.xlim(0, 1) plt.ylim(0, 1) # plt.show() mpdf.savefig(bbox_inches='tight') plt.close(fig) diagnb += 1 mpdf.close() ### GP traces at observerd locations fig, axs = plt.subplots(nObs, figsize=(10, 1.5 * nObs)) obsNB = 0 colNB = 0 while (obsNB < nObs): colNB = 0 while (colNB < K): if thismtPP.typeMatrix[colNB, obsNB] == 1: axs[obsNB].plot(GPvaluesAtObs[:, obsNB, colNB], linewidth=2) else: axs[obsNB].plot(GPvaluesAtObs[:, obsNB, colNB], linestyle="dashed") colNB += 1 obsNB += 1 # plt.show() fig.savefig("0GPtraces.pdf", bbox_inches='tight') plt.close(fig) ### mean intensities meanFields = np.mean(fieldsGrid, axis=0, dtype=np.float32) maxi = np.max(meanFields) mini = np.min(meanFields) k = 0 while k < K + 1: fig = plt.figure() ax = fig.add_subplot(111) ax.set_aspect('equal') plt.xlim(0, 1) plt.ylim(0, 1) imGP = np.transpose(meanFields[:, k].reshape(res, res)) x = np.linspace(0, 1, res + 1) y = np.linspace(0, 1, res + 1) X, Y = np.meshgrid(x, y) # fig = plt.figure() # axs[k] = fig.add_subplot(111) ax.set_aspect('equal') ff = ax.pcolormesh(X, Y, imGP, cmap='gray', vmin=mini, vmax=maxi) fig.colorbar(ff) for pp in thismtPP.pps: ax.plot(pp.loc[:, 0], pp.loc[:, 1], 'o', c="tab:orange") # plt.scatter(pointpo.loc[:,0],pointpo.loc[:,1], color= "black", s=1) # plt.show() fig.savefig("0IntFields" + str(k) + ".pdf", bbox_inches='tight') plt.close(fig) k += 1 # fig.savefig("0IntFields.pdf", bbox_inches='tight') # plt.close(fig) return (lams, rhos, Ts, mus, Nthins)
resGP = np.empty(shape=(size,res**2,K+1)) # meanGP = np.zeros(shape=(res**2,1)) i=0 j=0 t0 = time.time() while(i < size-1): locations = np.loadtxt("locations"+str(i)+".csv", delimiter=",") values = np.loadtxt("values"+str(i)+".csv", delimiter=",") # np.savetxt("resGP"+str(i)+".csv",lams[i+1]*expit(newGP.rCondGP(gridLoc,locations,np.transpose([values]))) ,delimiter=",") newGP = GP(zeroMean,expCov(1,rhos[i+1])) ## propose new value from MGP(.|totVal) s_11 = newGP.cov(gridLoc,gridLoc) S_21 = newGP.cov(locations,gridLoc) S_22 = newGP.cov(locations,locations) S_12S_22m1 = np.dot(np.transpose(S_21),np.linalg.inv(S_22)) mu = np.dot(S_12S_22m1,values) spatSig = s_11 - np.dot(S_12S_22m1,S_21) A = np.linalg.cholesky(Ts[i+1]) K = A.shape[0] Am = sp.linalg.solve_triangular(A,np.identity(K),lower=True) newVal = np.linalg.cholesky(spatSig)@np.random.normal(size=(res**2,K))@Am+mu