def setUp(self): SP.random.seed(1) self.n = 10 self.n_dim = 10 X = SP.rand(self.n, self.n_dim) self.C = limix.CCovLinearARD(self.n_dim) self.name = 'CCovLinearARD' self.C.setX(X) self.n_params = self.C.getNumberParams() K = self.C.K() params = SP.exp(SP.randn(self.n_params)) self.C.setParams(params)
def setUp(self): SP.random.seed(1) self.n=10 n_dim1=8 n_dim2=12 self.C=limix.CProductCF() self.C.addCovariance(limix.CCovSqexpARD(n_dim1)); self.C.addCovariance(limix.CCovLinearARD(n_dim2)); self.n_dim=self.C.getNumberDimensions() X=SP.rand(self.n,self.n_dim) self.C.setX(X) self.name = 'CProductCF' self.n_params=self.C.getNumberParams() params=SP.exp(SP.randn(self.n_params)) self.C.setParams(params)
def setUp(self): SP.random.seed(1) self.n=10 n_dim2=12 K0 = SP.eye(self.n) self.C=limix.CSumCF() #sum of fixed CF and linearARD covar1 = limix.CFixedCF(K0) covar2 = limix.CCovLinearARD(n_dim2) self.C.addCovariance(covar1) self.C.addCovariance(covar2) self.n_dim=self.C.getNumberDimensions() self.X=SP.rand(self.n,self.n_dim) self.C.setX(self.X) self.name = 'CSumCF' self.n_params=self.C.getNumberParams() params=SP.exp(SP.randn(self.n_params)) self.C.setParams(params)
def setUp(self): SP.random.seed(1) n1=3 n2=5 n_dim1=8 n_dim2=12 X1 = SP.rand(n1,n_dim1) X2 = SP.rand(n2,n_dim2) C1 = limix.CCovSqexpARD(n_dim1); C1.setX(X1) C2 = limix.CCovLinearARD(n_dim2); C2.setX(X2) self.C = limix.CKroneckerCF() self.C.setRowCovariance(C1) self.C.setColCovariance(C2) self.n = self.C.Kdim() self.n_dim=self.C.getNumberDimensions() self.name = 'CKroneckerCF' self.n_params=self.C.getNumberParams() params=SP.exp(SP.randn(self.n_params)) self.C.setParams(params)
def setUp(self): SP.random.seed(1) nr=3 nc=5 n_dim1=8 n_dim2=12 #truncation of soft kronecker self.n_trunk = 10 Xr = SP.rand(nr,n_dim1) Xc = SP.rand(nc,n_dim2) Cr = limix.CCovSqexpARD(n_dim1); Cr.setX(Xr) Cc = limix.CCovLinearARD(n_dim2); Cc.setX(Xc) self.C = limix.CKroneckerCF() self.C.setRowCovariance(Cr) self.C.setColCovariance(Cc) #set kronecker index self.kronecker_index = limix.CKroneckerCF.createKroneckerIndex(nc,nr) self.n = self.C.Kdim() self.n_dim=self.C.getNumberDimensions() self.name = 'CKroneckerCF' self.n_params=self.C.getNumberParams() params=SP.exp(SP.randn(self.n_params)) self.C.setParams(params)
def train(self,rank=20,Kpop=True,LinearARD=False): """train panama module""" if 0: covar = limix.CCovLinearISO(rank) ll = limix.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.CGPHyperParams() hyperparams['covar'] = covar_params hyperparams['lik'] = lik_params hyperparams['X'] = X0 constrainU = limix.CGPHyperParams() constrainL = limix.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.CSumCF() if LinearARD: covar_1 = limix.CCovLinearARD(rank) covar_params = [] for d in range(rank): covar_params.append(1/sp.sqrt(d+2)) else: covar_1 = limix.CCovLinearISO(rank) covar_params = [1.0] covar.addCovariance(covar_1) if self.use_Kpop: covar_2 = limix.CFixedCF(self.Kpop) covar.addCovariance(covar_2) covar_params.append(1.0) ll = limix.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.CGPHyperParams() hyperparams['covar'] = covar_params hyperparams['lik'] = lik_params hyperparams['X'] = X0 constrainU = limix.CGPHyperParams() constrainL = limix.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.CGPbase(covar,ll) gp.setY(self.Y) gp.setX(X0) lml0 = gp.LML(hyperparams) dlml0 = gp.LMLgrad(hyperparams) gpopt = limix.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() if self.use_Kpop: self.Ktot += covar_2.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['Kpop'] = sp.array([covar_2.K().diagonal().mean()]) V['noise'] = gp.getParams()['lik']**2 self.varianceComps = V
hyperparams['covar_c'] = covar_c hyperparams['lik'] = lik hyperparams['x_r'] = X0r hyperparams['x_c'] = X0c kgp = kronecker_gplvm.KroneckerGPLVM(covar_func_r=covariance_r_, covar_func_c=covariance_c_, likelihood=likelihood_) kgp.setData(x_r=X0r, x_c=X0c, y=Y) #gradcheck=True #[hyperparams_o,opt_lml_o] = opt.opt_hyper(kgp,hyperparams,gradcheck=gradcheck) #limix if ard: covariance_r = limix.CCovLinearARD(Kr) covariance_c = limix.CCovLinearARD(Kc) else: covariance_r = limix.CCovLinearISO(Kr) covariance_c = limix.CCovLinearISO(Kc) gp = limix.CGPkronecker(covariance_r, covariance_c) gp.setY(Y) gp.setX_r(X0r) gp.setX_c(X0c) params = limix.CGPHyperParams() params['covar_r'] = covar_r params['covar_c'] = covar_c params['lik'] = lik params['X_r'] = X0r params['X_c'] = X0c
# http://www.apache.org/licenses/LICENSE-2.0 # #Unless required by applicable law or agreed to in writing, software #distributed under the License is distributed on an "AS IS" BASIS, #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #See the License for the specific language governing permissions and #limitations under the License. import sys sys.path.append('./..') sys.path.append('./../../build/src/interfaces/python') import limix import scipy as SP # LINEAR COVARIANCE MATRIX C = limix.CCovLinearARD(2) """ # NON-COMPATIBLE PARAM BOUNDS # 1. Wrong number of components LB=SP.array([0.]) UB=float('inf')*SP.ones(2) C.setParamBounds(LB,UB) # 2. Upper < Lower LB=+1.*SP.ones(2) UB=-1.*SP.ones(2) C.setParamBounds(LB,UB) """ LB = SP.array([0., 5.]) UB = SP.array([float('inf'), 10.]) C.setParamBounds(LB, UB) print C.getParamBounds0()