def setUp(self): np.random.seed(1) # generate data N = 400 s_x = 0.05 s_y = 0.1 X = (sp.linspace(0,2,N)+s_x*sp.randn(N))[:,sp.newaxis] Y = sp.sin(X)+s_y*sp.randn(N,1) Y-= Y.mean(0) Y/= Y.std(0) Xstar = sp.linspace(0,2,1000)[:,sp.newaxis] # define mean term F = 1.*(sp.rand(N,2)<0.2) mean = lin_mean(Y,F) # define covariance matrices covar1 = SQExpCov(X,Xstar=Xstar) covar2 = FixedCov(sp.eye(N)) covar = SumCov(covar1,covar2) # define gp self._gp = GP(covar=covar,mean=mean)
def setUp(self): np.random.seed(1) # generate data N = 400 s_x = 0.05 s_y = 0.1 X = (sp.linspace(0, 2, N) + s_x * sp.randn(N))[:, sp.newaxis] Y = sp.sin(X) + s_y * sp.randn(N, 1) Y -= Y.mean(0) Y /= Y.std(0) Xstar = sp.linspace(0, 2, 1000)[:, sp.newaxis] # define mean term F = 1.0 * (sp.rand(N, 2) < 0.2) mean = lin_mean(Y, F) # define covariance matrices covar1 = SQExpCov(X, Xstar=Xstar) covar2 = FixedCov(sp.eye(N)) covar = SumCov(covar1, covar2) # define gp self._gp = GP(covar=covar, mean=mean)
def setUp(self): np.random.seed(1) N = 400 s_x = 0.05 s_y = 0.1 X = (np.linspace(0, 2, N) + s_x * np.random.randn(N))[:, np.newaxis] Y = np.sin(X) + s_y * np.random.randn(N, 1) #pl.plot(x,y,'x') # define mean term F = 1. * (np.random.rand(N, 2) < 0.2) mean = lin_mean(Y, F) # define covariance matrices covar1 = sqexp(X) covar2 = fixed(np.eye(N)) covar = sumcov(covar1, covar2)
def setUp(self): np.random.seed(1) N = 400 s_x = 0.05 s_y = 0.1 X = (np.linspace(0, 2, N) + s_x * np.random.randn(N))[:, np.newaxis] Y = np.sin(X) + s_y * np.random.randn(N, 1) # pl.plot(x,y,'x') # define mean term F = 1.0 * (np.random.rand(N, 2) < 0.2) mean = lin_mean(Y, F) # define covariance matrices covar1 = sqexp(X) covar2 = fixed(np.eye(N)) covar = sumcov(covar1, covar2)
def VD(self, DGE, IGE, IEE, cageEffect): """ defines covariance for variance decomposition.""" #defines mean mean = lin_mean(self.pheno,self.covs) #define cagemate assignment - required for SGE, SEE, and cage effects. Z is N focal x N_cm and has 0s in cells Z_i,i (i.e. an animal is not its own cage mate) same_cage = 1. * (self.cage==self.cage_cm) diff_inds = 1. * (self.sampleID[:,sp.newaxis]!=self.sampleID_cm) Z = same_cage * diff_inds #define the overall genetic covariance matrix if DGE or IGE: #scales kinship (DGE component) to sample variance 1 sf_K = covar_rescaling_factor(self.kinship) self.kinship *= sf_K #now create and scale SGE and DGE/SGE covariance components if IGE: #first SGE component: ZKcmZ' in this code (ZKZ' in paper) _ZKcmZ = sp.dot(Z,sp.dot(self.kinship_cm,Z.T)) sf_ZKcmZ = covar_rescaling_factor(_ZKcmZ) self.kinship_cm *= sf_ZKcmZ #second DGE/SGE covariance: self.kinship_cross *= sp.sqrt(sf_K * sf_ZKcmZ) if DGE and not IGE: self._genoCov = FixedCov(self.kinship) elif IGE and not DGE: self._genoCov = FixedCov(_ZKcmZ) elif DGE and IGE: self._genoCov = DirIndirCov(self.kinship,Z,kinship_cm=self.kinship_cm,kinship_cross=self.kinship_cross) else: self._genoCov = None #define the overall environmental covariance matrix #there is always DEE #env naturally has sample variance 1 so no need to scale it if IEE: #_ZZ = ZIcmZ' _ZZ = sp.dot(Z,Z.T) sf_ZZ = covar_rescaling_factor(_ZZ) self.env_cm *= sf_ZZ self.env_cross *= sp.sqrt(1 * sf_ZZ) self._envCov = DirIndirCov(self.env,Z,kinship_cm=self.env_cm,kinship_cross=self.env_cross) else: self._envCov = FixedCov(self.env) ##define cage effect covariance matrix if cageEffect: N = self.pheno.shape[0] uCage = sp.unique(self.cage) #W, the cage design matrix, is N x n_cages (where N is number of focal animals) W = sp.zeros((N,uCage.shape[0])) for cv_i, cv in enumerate(uCage): W[:,cv_i] = 1.*(self.cage[:,0]==cv) #WW, the cage effect covariance matrix, is N x N and has 1s in cells WW_i,i WW = sp.dot(W,W.T) #this is equivalent to getting covar_rescaling_factor first and then multiplying, as done for other matrices above WW = covar_rescale(WW) self._cageCov = FixedCov(WW) else: self._cageCov = None # define overall covariance matrix as sum of genetic, environmental and cage covariance matrices if self._genoCov is None: if self._cageCov is None: self.covar = SumCov(self._envCov) else: self.covar = SumCov(self._envCov,self._cageCov) else: if self._cageCov is None: self.covar = SumCov(self._genoCov,self._envCov) else: self.covar = SumCov(self._genoCov,self._envCov,self._cageCov) ## define gp self._gp = GP(covar=self.covar,mean=mean)
sp.random.seed(1) if __name__ == "__main__": # generate data N = 400 X = sp.linspace(0,2,N)[:,sp.newaxis] v_noise = 0.01 Y = sp.sin(X) + sp.sqrt(v_noise) * sp.randn(N, 1) # for out-of-sample preditions Xstar = sp.linspace(0,2,1000)[:,sp.newaxis] # define mean term W = 1. * (sp.rand(N, 2) < 0.2) mean = lin_mean(Y, W) # define covariance matrices sqexp = SQExpCov(X, Xstar = Xstar) noise = FixedCov(sp.eye(N)) covar = SumCov(sqexp, noise) # define gp gp = GP(covar=covar,mean=mean) # initialize params sqexp.scale = 1e-4 sqexp.length = 1 noise.scale = Y.var() # optimize gp.optimize(calc_ste=True) # predict out-of-sample