def _initParams_regressOut(self, Ycc, X, varXX): """ initialize the gp parameters 1) the variance of Kcc as Ycc.var(0).mean() 2) X with the provided 3) variance of interaction (if label is True) will be set to ~0 4) residual to residual """ X *= SP.sqrt(varXX / (X**2).mean()) Y1 = self.Y - Ycc a = SP.array([SP.sqrt(Ycc.var(0).mean())]) b = 1e-3 * SP.ones(1) c = Y1.var(0).mean() - varXX c = SP.maximum(1e-1, c) c = SP.array([SP.sqrt(c)]) # gp hyper params params = limix.CGPHyperParams() if self.interaction: params['covar'] = SP.concatenate( [a, X.reshape(self.N * self.k, order='F'), SP.ones(1), b]) else: params['covar'] = SP.concatenate( [a, X.reshape(self.N * self.k, order='F')]) params['lik'] = c return params
def _initParams_fast(self): """ initialize the gp parameters 1) project Y on the known factor X0 -> Y0 average variance of Y0 is used to initialize the variance explained by X0 2) considers the residual Y1 = Y-Y0 (this equivals to regress out X0) 3) perform PCA on cov(Y1) and considers the first k PC for initializing X 4) the variance of all other PCs is used to initialize the noise 5) the variance explained by interaction is set to a small random number """ Xd = LA.pinv(self.X0) Y0 = self.X0.dot(Xd.dot(self.Y)) Y1 = self.Y - Y0 YY = SP.cov(Y1) S, U = LA.eigh(YY) X = U[:, -self.k:] * SP.sqrt(S[-self.k:]) a = SP.array([SP.sqrt(Y0.var(0).mean())]) b = 1e-3 * SP.randn(1) c = SP.array([SP.sqrt((YY - SP.dot(X, X.T)).diagonal().mean())]) # gp hyper params params = limix.CGPHyperParams() if self.interaction: params['covar'] = SP.concatenate( [a, X.reshape(self.N * self.k, order='F'), SP.ones(1), b]) else: params['covar'] = SP.concatenate( [a, X.reshape(self.N * self.k, order='F')]) params['lik'] = c return params
def setUp(self): SP.random.seed(1) #1. simulate self.settings = {'K': 5, 'N': 100, 'D': 80} self.simulation = self.simulate() N = self.settings['N'] K = self.settings['K'] D = self.settings['D'] #2. setup GP covar = dlimix_legacy.CCovLinearISO(K) ll = dlimix_legacy.CLikNormalIso() #create hyperparm covar_params = SP.array([1.0]) lik_params = SP.array([1.0]) hyperparams = dlimix_legacy.CGPHyperParams() hyperparams['covar'] = covar_params hyperparams['lik'] = lik_params hyperparams['X'] = self.simulation['X0'] #cretae GP self.gp = dlimix_legacy.CGPbase(covar, ll) #set data self.gp.setY(self.simulation['Y']) self.gp.setX(self.simulation['X0']) self.gp.setParams(hyperparams) pass
def _initParams_random(self): """ initialize the gp parameters randomly """ # gp hyper params params = limix.CGPHyperParams() if self.interaction: params['covar'] = SP.concatenate( [SP.randn(self.N * self.k + 1), SP.ones(1), SP.randn(1)]) else: params['covar'] = SP.randn(self.N * self.k + 1) params['lik'] = SP.randn(1) return params
def _initParams_null(self, varX0X0, nois): """ initialize from null model """ X = 1e-3 * SP.randn(self.N, self.k) a = SP.array([SP.sqrt(varX0X0)]) b = 1e-3 * SP.ones(1) c = SP.array([SP.sqrt(nois)]) # gp hyper params params = limix.CGPHyperParams() if self.interaction: params['covar'] = SP.concatenate( [a, X.reshape(self.N * self.k, order='F'), SP.ones(1), b]) else: params['covar'] = SP.concatenate( [a, X.reshape(self.N * self.k, order='F')]) params['lik'] = c return params
def setUp(self): SP.random.seed(1) #1. simulate self.settings = {'K': 5, 'N': 100, 'D': 80} self.simulation = self.simulate() N = self.settings['N'] K = self.settings['K'] D = self.settings['D'] #2. setup GP K0 = SP.dot(self.simulation['S'], self.simulation['S'].T) K0[:] = 0 covar1 = dlimix_legacy.CFixedCF(K0) covar2 = dlimix_legacy.CCovLinearISO(K) covar = dlimix_legacy.CSumCF() covar.addCovariance(covar1) covar.addCovariance(covar2) ll = dlimix_legacy.CLikNormalIso() #create hyperparm covar_params = SP.array([0.0, 1.0]) lik_params = SP.array([0.1]) hyperparams = dlimix_legacy.CGPHyperParams() hyperparams['covar'] = covar_params hyperparams['lik'] = lik_params hyperparams['X'] = self.simulation['X0'] #cretae GP self.gp = dlimix_legacy.CGPbase(covar, ll) #set data self.gp.setY(self.simulation['Y']) self.gp.setX(self.simulation['X0']) self.gp.setParams(hyperparams) pass
def train(self, rank=20, Kpop=True, LinearARD=False): """train panama module""" if 0: covar = limix_legacy.CCovLinearISO(rank) ll = limix_legacy.CLikNormalIso() X0 = sp.random.randn(self.N, rank) X0 = PCA(self.Y, rank)[0] X0 /= sp.sqrt(rank) covar_params = sp.array([1.0]) lik_params = sp.array([1.0]) hyperparams = limix_legacy.CGPHyperParams() hyperparams['covar'] = covar_params hyperparams['lik'] = lik_params hyperparams['X'] = X0 constrainU = limix_legacy.CGPHyperParams() constrainL = limix_legacy.CGPHyperParams() constrainU['covar'] = +5 * sp.ones_like(covar_params) constrainL['covar'] = 0 * sp.ones_like(covar_params) constrainU['lik'] = +5 * sp.ones_like(lik_params) constrainL['lik'] = 0 * sp.ones_like(lik_params) if 1: covar = limix_legacy.CSumCF() if LinearARD: covar_1 = limix_legacy.CCovLinearARD(rank) covar_params = [] for d in range(rank): covar_params.append(1 / sp.sqrt(d + 2)) else: covar_1 = limix_legacy.CCovLinearISO(rank) covar_params = [1.0] covar.addCovariance(covar_1) for K in self.Ks: covar.addCovariance(limix_legacy.CFixedCF(K)) covar_params.append(1.0) ll = limix_legacy.CLikNormalIso() X0 = PCA(self.Y, rank)[0] X0 /= sp.sqrt(rank) covar_params = sp.array(covar_params) lik_params = sp.array([1.0]) hyperparams = limix_legacy.CGPHyperParams() hyperparams['covar'] = covar_params hyperparams['lik'] = lik_params hyperparams['X'] = X0 constrainU = limix_legacy.CGPHyperParams() constrainL = limix_legacy.CGPHyperParams() constrainU['covar'] = +5 * sp.ones_like(covar_params) constrainL['covar'] = -5 * sp.ones_like(covar_params) constrainU['lik'] = +5 * sp.ones_like(lik_params) gp = limix_legacy.CGPbase(covar, ll) gp.setY(self.Y) gp.setX(X0) lml0 = gp.LML(hyperparams) dlml0 = gp.LMLgrad(hyperparams) gpopt = limix_legacy.CGPopt(gp) gpopt.setOptBoundLower(constrainL) gpopt.setOptBoundUpper(constrainU) t1 = time.time() gpopt.opt() t2 = time.time() #Kpanama self.Xpanama = covar_1.getX() if LinearARD: self.Xpanama /= self.Xpanama.std(0) self.Kpanama = covar_1.K() # self.Kpanama/= self.Kpanama.diagonal().mean() # Ktot self.Ktot = covar_1.K() for c_i in range(len(self.Ks)): self.Ktot += covar.getCovariance(c_i + 1).K() # self.Ktot/= self.Ktot.diagonal().mean() #store variances V = {} if LinearARD: V['LinearARD'] = covar_1.getParams()**2 * covar_1.getX().var(0) else: V['Kpanama'] = sp.array([covar_1.K().diagonal().mean()]) # if self.use_Kpop: # V['Ks'] = sp.array([covar.getCovariance(c_i+1).K().diagonal().mean() for c_i in range(len(self.Ks))]) V['Ks'] = sp.array( [covar.getCovariance(c_i + 1).K() for c_i in range(len(self.Ks))]) V['noise'] = gp.getParams()['lik']**2 self.varianceComps = V # predictions Ki = la.inv(ll.K() + covar.K()) self.Ypanama = sp.dot(covar_1.K(), sp.dot(Ki, self.Y)) self.LL = ll.K()