def drawSparsityPat(self, distr="prior"): """ draws the B matrix with ones whererver the corresponding element in the B matrix is nonzero and zeros otherwise. """ if distr=="prior": B = self.getBasisFunctionsMatrix(distr="prior") elif distr=="posterior": B = self.getBasisFunctionsMatrix(distr="posterior") ind = np.where(np.abs(B)>1e-10) B1 = B; B1[ind] = 1 Brr = B1 #mt.dispMat(B1, cmap='binary', title="%s sparsity pattern" % distr, colorbar=False) mt.dispMat(Brr, cmap='binary', title="%s sparsity pattern" % distr, colorbar=False)
def MRALikelihood(params): kappa, sigma, me_scale = params cov = lambda _locs1, _locs2: sigma * mt.Matern32( _locs1, _locs2, l=np.abs(kappa)) mraTree = MRATree(locs, M, J, r0, critDepth, cov, y_obs, me_scale) lik = mraTree.getLikelihood() return (lik)
def likelihood(kappa): #def likelihood(params): """ Evaluates the likelihood of the model for a given set of parameters params=(kappa, sigma, R) """ #par = dict(zip(('kappa', 'sigma', 'R'), np.abs(params))) #logger.debug("kappa=%f, sig=%f, R=%f" % (par['kappa'], par['sigma'], par['R'])) cov = lambda _locs1, _locs2: mt.ExpCovFun(_locs1, _locs2, l=kappa) #cov = lambda _locs1, _locs2: mt.Matern32(_locs1, _locs2, l=par['kappa'], sig=1.0) mraTree = MRATree(locs, M, J, r0, critDepth, cov, y_obs, me_scale) #mraTree = MRATree(locs, M, J, r0, critDepth, cov, y_obs, np.abs(par['R'])) lik = mraTree.getLikelihood() return (lik)
def TrueLikelihood(kappa): cov = lambda _locs1: mt.ExpCovFun(_locs1, _locs1, l=np.abs(kappa)) obs = y_obs[obs_inds].ravel() obs_mat = np.matrix(obs).T obs_locs = locs[obs_inds, :] varY = cov(obs_locs) + np.eye(len(obs)) * me_scale sign, logdet = np.linalg.slogdet(varY) const = len(obs) * np.log(2 * np.pi) quad_form = obs_mat.T * lng.inv(varY) * obs_mat hand_lik = logdet + quad_form full_hand_lik = -0.5 * (const + hand_lik) model = mvn(mean=np.zeros(len(obs)), cov=varY) numpy_lik = model.logpdf(obs) #print("full_hand_lik: %f" % full_hand_lik) #print("numpy_lik: %f" % numpy_lik) #return(numpy_lik) return (logdet + quad_form)
if __name__ == '__main__': logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%H:%M:%S', level=logging.INFO) logging.info("===Unit tests for MRA===") ##### #1. MRA in 1d exponential with 0 resolutions is the same as kriging ##### dim_x = 2 M=0; J=1; r0=dim_x frac_obs = 0.5 sig = 1.0 me_scale=1e-10 kappa = 1.0 locs = mt.genLocations(dim_x) Sig = sig*mt.ExpCovFun(locs, l=kappa) SigC = np.matrix(lng.cholesky(Sig)) x_raw = np.matrix(np.random.normal(size=(locs.shape[0],1))) x = SigC.T * x_raw R = me_scale Rc = np.sqrt(R) if isinstance(me_scale, float) else np.linalg.cholesky(R) eps = Rc * np.matrix(np.random.normal(size=(locs.shape[0],1))) y = x + eps obs_inds = np.random.choice(dim_x, int(dim_x*frac_obs), replace=False) obs_inds=np.sort(obs_inds) y_obs = np.empty(np.shape(y)); y_obs[:] = np.NAN; y_obs[obs_inds] = y[obs_inds]
dim_y = 100 M = 5 J = 4 r0 = 35 critDepth = 0 ### simulate data ### sig = 1.0 me_scale = 1e-1 kappa = 0.3 logger.info('=== simulating data ===') logger.info('simulating locations') if dim_y == 1: locs = mt.genLocations(dim_x) else: locs = mt.genLocations2d(Nx=dim_x, Ny=dim_y) logger.info('calculating covariance matrix') if test_small: Sig = sig * mt.ExpCovFun(locs, l=kappa) #Sig = sig*mt.Matern32(locs, l=kappa, sig=sig) SigC = np.matrix(lng.cholesky(Sig)) else: #Sig = sig*mt.ExpCovFun(locs, l=kappa) #SigC = np.matrix(lng.cholesky(Sig)) #np.save("SigC.npy", SigC) SigC = np.load("SigC.npy") x_raw = np.matrix(np.random.normal(size=(locs.shape[0], 1)))
locs[:,1] = locs[:,1] - np.min(locs[:,1]) locs[:,0] = locs[:,0]/np.max(locs[:,0]) locs[:,1] = locs[:,1]/np.max(locs[:,1]) Nx = len(np.unique(locs[:,0])); Ny = len(np.unique(locs[:,1])) obs_mean = np.nanmean(y_obs) y_obs = y_obs - obs_mean y = y - obs_mean obs_inds = np.isfinite(y_obs).flatten() R = me_scale y_disp = y_obs.reshape((Nx, Ny)) mt.dispMat(y_disp, cmap="Spectral", title="observations") logging.info("MRA started") start = time.time() cov = lambda _locs1, _locs2: mt.ExpCovFun(_locs1, _locs2, l=0.3) start = time.time() mraTree = MRATree(locs, M, J, r0, critDepth, cov, y_obs, R) yP, sdP = mraTree.predict() sdP = sdP.reshape((Nx, Ny)) #sdP = np.flipud(sdP.reshape((Nx, Ny)))
def drawBasisFunctions(self, distr="prior", order="root"): """ plots basis functions """ cmaps = [plt.cm.Blues, plt.cm.copper, plt.cm.Blues, plt.cm.copper]#plt.cm.Greys, plt.cm.YlOrBr, plt.cm.Purples, plt.cm.RdPu] ### 1D version if np.shape(self.locs)[1]==1 and self.M>0: B = self.getBasisFunctionsMatrix(groupByResolution=True, order=order, distr=distr) nodes = self.getNodesBFS(groupByResolution=True) fig = plt.figure(figsize=(8,6)) for m in range(self.M+1): Bm = B[m] cmap = cmaps[m]; ncol = Bm.shape[1]; offset=0.3*ncol ax = fig.add_subplot(self.M+1, 1, m+1) ax.set_ylim(top=1.1) ax.set_xlim(np.min(self.locs), np.max(self.locs)) # draw partition lines for node_idx, node in enumerate(nodes[m]): if np.min(node.locs)>np.min(self.locs): ax.axvline(x=node.locs[0], linestyle='dashed', color='k', linewidth=1) # draw functions for col in range(Bm.shape[1]): color = cmap(((offset+col)/(ncol+offset))) ax.plot(self.locs, Bm[:,col], color=color, linewidth=2) ax.tick_params(labelsize='large') ax.plot(self.locs, np.zeros(len(self.locs)), color='black', linewidth=2) if m<(len(B)-2): ax.get_xaxis().set_visible(False) ax.set_title("resolution: %d" % m, fontsize='x-large') plt.tight_layout() plt.show() ### 2D version if np.shape(self.locs)[1]==2 and self.M: dim_x = len(np.unique(self.locs[:,0])) dim_y = len(np.unique(self.locs[:,1])) B = self.getBasisFunctionsMatrix(groupByResolution=True) for m, Bm in enumerate(B): if Bm.shape[1]>36: continue nrows, ncols = mt.get_layout(m, self.J, self.r) fig = plt.figure() grid = AxesGrid(fig, 111, nrows_ncols=(nrows, ncols), axes_pad=0.1, share_all=True, label_mode="L", cbar_location="right", cbar_mode="single") for func, ax in zip(Bm.T, grid): im = ax.imshow(np.array(func).reshape((dim_x, dim_y)), vmax=1, vmin=-0.1, cmap="coolwarm") ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.suptitle("resolution: %d" % m, fontsize="x-large") grid.cbar_axes[0].colorbar(im) plt.show()
dim_x = 2 dim_y = 1 M = 1 J = 1 r0 = 1 critDepth = M + 1 ### simulate data ### sig = 1.0 me_scale = 1e-4 kappa = 0.3 if dim_y == 1: locs = mt.genLocations(dim_x) else: locs = mt.genLocations2d(Nx=dim_x, Ny=dim_y) Sig = sig * mt.ExpCovFun(locs, l=kappa) SigC = np.matrix(lng.cholesky(Sig)) x_raw = np.matrix(np.random.normal(size=(locs.shape[0], 1))) x = SigC.T * x_raw Rc = np.sqrt(me_scale) eps = Rc * np.matrix(np.random.normal(size=(locs.shape[0], 1))) y = x + eps obs_inds = np.random.choice(dim_x * dim_y, int(dim_x * dim_y * frac_obs),