def setUp(self): SP.random.seed(1) self.n = 4 self.rank = 2 self.C = limix.CLowRankCF(self.n, self.rank) self.name = 'CLowRankCF' self.n_params = self.C.getNumberParams() params = SP.exp(SP.randn(self.n_params)) self.C.setParams(params)
def __init__(self, Y=None, X0=None, k=1, standardize=False, interaction=True): """ Y: data [NxG] X0: known latent factors [Nxk0] k: number of latent factors to infer """ assert Y != None, 'gpCLVM:: set Y!' assert X0 != None, 'gpCLVM:: set X0!' self.cache = {} self.interaction = interaction # read data self._setY(Y, standardize=standardize) self._setX0(X0) self.k = k # covariance for known latex factor self.C0 = limix.CFixedCF(self.K0) # covariance for unknow latent factor self.C = limix.CProductCF() self.Ca = limix.CLowRankCF(self.N, self.k) self.C.addCovariance(self.Ca) if self.interaction == True: self.Cb1 = limix.CFixedCF(SP.ones((self.N, self.N))) self.Cb1.setParamMask(SP.zeros(1)) self.Cb2 = limix.CFixedCF(self.K0) self.Cb = limix.CSumCF() self.Cb.addCovariance(self.Cb1) self.Cb.addCovariance(self.Cb2) self.C.addCovariance(self.Cb) # total covariance covar = limix.CSumCF() covar.addCovariance(self.C0) covar.addCovariance(self.C) # likelihood self.ll = limix.CLikNormalIso() # init GP and hyper params self.gp = limix.CGPbase(covar, self.ll) self.gp.setY(self.Y)
def _buildTraitCovar(self, trait_covar_type='lowrank_diag', rank=1, fixed_trait_covar=None, jitter=1e-4): """ Internal functions that builds the trait covariance matrix using the LIMIX framework Args: trait_covar_type: type of covaraince to use. Default 'freeform'. possible values are rank: rank of a possible lowrank component (default 1) fixed_trait_covar: PxP matrix for the (predefined) trait-to-trait covariance matrix if fixed type is used jitter: diagonal contribution added to trait-to-trait covariance matrices for regularization Returns: LIMIX::PCovarianceFunction for Trait covariance matrix vector labelling Cholesky parameters for different initializations """ cov = limix.CSumCF() if trait_covar_type == 'freeform': cov.addCovariance(limix.CFreeFormCF(self.P)) L = sp.eye(self.P) diag = sp.concatenate([L[i, :(i + 1)] for i in range(self.P)]) elif trait_covar_type == 'fixed': assert fixed_trait_covar != None, 'VarianceDecomposition:: set fixed_trait_covar' assert fixed_trait_covar.shape[ 0] == self.N, 'VarianceDecomposition:: Incompatible shape for fixed_trait_covar' assert fixed_trait_covar.shape[ 1] == self.N, 'VarianceDecomposition:: Incompatible shape for fixed_trait_covar' cov.addCovariance(limix.CFixedCF(fixed_trait_covar)) diag = sp.zeros(1) elif trait_covar_type == 'diag': cov.addCovariance(limix.CDiagonalCF(self.P)) diag = sp.ones(self.P) elif trait_covar_type == 'lowrank': cov.addCovariance(limix.CLowRankCF(self.P, rank)) diag = sp.zeros(self.P * rank) elif trait_covar_type == 'lowrank_id': cov.addCovariance(limix.CLowRankCF(self.P, rank)) cov.addCovariance(limix.CFixedCF(sp.eye(self.P))) diag = sp.concatenate([sp.zeros(self.P * rank), sp.ones(1)]) elif trait_covar_type == 'lowrank_diag': cov.addCovariance(limix.CLowRankCF(self.P, rank)) cov.addCovariance(limix.CDiagonalCF(self.P)) diag = sp.concatenate([sp.zeros(self.P * rank), sp.ones(self.P)]) elif trait_covar_type == 'block': cov.addCovariance(limix.CFixedCF(sp.ones((self.P, self.P)))) diag = sp.zeros(1) elif trait_covar_type == 'block_id': cov.addCovariance(limix.CFixedCF(sp.ones((self.P, self.P)))) cov.addCovariance(limix.CFixedCF(sp.eye(self.P))) diag = sp.concatenate([sp.zeros(1), sp.ones(1)]) elif trait_covar_type == 'block_diag': cov.addCovariance(limix.CFixedCF(sp.ones((self.P, self.P)))) cov.addCovariance(limix.CDiagonalCF(self.P)) diag = sp.concatenate([sp.zeros(1), sp.ones(self.P)]) else: assert True == False, 'VarianceDecomposition:: trait_covar_type not valid' if jitter > 0: _cov = limix.CFixedCF(sp.eye(self.P)) _cov.setParams(sp.array([sp.sqrt(jitter)])) _cov.setParamMask(sp.zeros(1)) cov.addCovariance(_cov) return cov, diag
def addMultiTraitTerm(self, K=None, covar_type='freeform', is_noise=False, normalize=True, Ks=None, offset=1e-4, rank=1, covar_K0=None): """ add multi trait random effects term. The inter-trait covariance is parametrized by covar_type, where parameters are optimized. Args: K: Individual-individual (Intra-Trait) Covariance Matrix [N, N] (K is normalised in the C++ code such that K.trace()=N) covar_type: type of covaraince to use. Default 'freeform'. possible values are 'freeform': free form optimization, 'fixed': use a fixed matrix specified in covar_K0, 'diag': optimize a diagonal matrix, 'lowrank': optimize a low rank matrix. The rank of the lowrank part is specified in the variable rank, 'lowrank_id': optimize a low rank matrix plus the weight of a constant diagonal matrix. The rank of the lowrank part is specified in the variable rank, 'lowrank_diag': optimize a low rank matrix plus a free diagonal matrix. The rank of the lowrank part is specified in the variable rank, 'block': optimize the weight of a constant P x P block matrix of ones, 'block_id': optimize the weight of a constant P x P block matrix of ones plus the weight of a constant diagonal matrix, 'block_diag': optimize the weight of a constant P x P block matrix of ones plus a free diagonal matrix, is_noise: Boolean indicator specifying if the matrix is homoscedastic noise (weighted identity covariance) (default False) normalize: Boolean indicator specifying if K is normalized such that K.trace()=N. Ks: NxNtest cross covariance for predictions offset: diagonal contribution added to trait-to-trait covariance matrices for regularization rank: rank of a possible lowrank component (default 1) covar_K0: PxP matrix for the (predefined) trait-to-trait covariance matrix if fixed type is used """ assert self.P > 1, 'CVarianceDecomposition:: Incompatible number of traits' assert K != None or is_noise, 'CVarianceDecomposition:: Specify covariance structure' assert offset >= 0, 'CVarianceDecomposition:: offset must be >=0' #TODO: check that covar_K0 is correct if fixed typeCF is used.. if is_noise: assert self.noisPos == None, 'CVarianceDecomposition:: noise term already exists' K = SP.eye(self.N) self.noisPos = self.n_terms else: assert K.shape[ 0] == self.N, 'CVarianceDecomposition:: Incompatible shape' assert K.shape[ 1] == self.N, 'CVarianceDecomposition:: Incompatible shape' if Ks != None: assert Ks.shape[ 0] == self.N, 'CVarianceDecomposition:: Incompatible shape' if normalize: Norm = 1 / K.diagonal().mean() K *= Norm if Ks != None: Ks *= Norm cov = limix.CSumCF() if covar_type == 'freeform': cov.addCovariance(limix.CFreeFormCF(self.P)) L = SP.eye(self.P) diag = SP.concatenate([L[i, :(i + 1)] for i in range(self.P)]) elif covar_type == 'fixed': cov.addCovariance(limix.CFixedCF(covar_K0)) diag = SP.zeros(1) elif covar_type == 'diag': cov.addCovariance(limix.CDiagonalCF(self.P)) diag = SP.ones(self.P) elif covar_type == 'lowrank': cov.addCovariance(limix.CLowRankCF(self.P, rank)) diag = SP.zeros(self.P * rank) elif covar_type == 'lowrank_id': cov.addCovariance(limix.CLowRankCF(self.P, rank)) cov.addCovariance(limix.CFixedCF(SP.eye(self.P))) diag = SP.concatenate([SP.zeros(self.P * rank), SP.ones(1)]) elif covar_type == 'lowrank_diag': cov.addCovariance(limix.CLowRankCF(self.P, rank)) cov.addCovariance(limix.CDiagonalCF(self.P)) diag = SP.concatenate([SP.zeros(self.P * rank), SP.ones(self.P)]) elif covar_type == 'block': cov.addCovariance(limix.CFixedCF(SP.ones((self.P, self.P)))) diag = SP.zeros(1) elif covar_type == 'block_id': cov.addCovariance(limix.CFixedCF(SP.ones((self.P, self.P)))) cov.addCovariance(limix.CFixedCF(SP.eye(self.P))) diag = SP.concatenate([SP.zeros(1), SP.ones(1)]) elif covar_type == 'block_diag': cov.addCovariance(limix.CFixedCF(SP.ones((self.P, self.P)))) cov.addCovariance(limix.CDiagonalCF(self.P)) diag = SP.concatenate([SP.zeros(1), SP.ones(self.P)]) else: assert True == False, 'CVarianceDecomposition:: covar_type not valid' if offset > 0: _cov = limix.CFixedCF(SP.eye(self.P)) _cov.setParams(SP.array([SP.sqrt(offset)])) _cov.setParamMask(SP.zeros(1)) cov.addCovariance(_cov) self.offset.append(offset) self.covar_type.append(covar_type) self.diag.append(diag) self.vd.addTerm(cov, K) if Ks != None: self.setKstar(self.n_terms, Ks) self.n_terms += 1 self.gp = None self.init = False self.fast = False self.optimum = None self.cache['Sigma'] = None self.cache['Hessian'] = None self.cache['Lparams'] = None self.cache['paramsST'] = None