def setUp(self): SP.random.seed(1) self.n = 4 self.rank = 2 self.C = dlimix_legacy.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 setUp(self): SP.random.seed(1) self.n = 4 self.rank = 1 d = SP.rand(self.n) + 1 C0 = dlimix_legacy.CSumCF() C0.addCovariance(dlimix_legacy.CLowRankCF(self.n, self.rank)) C0.addCovariance(dlimix_legacy.CDiagonalCF(self.n)) self.C = dlimix_legacy.CFixedDiagonalCF(C0, d) self.name = 'CFixedDiagonalCF' self.n_params = self.C.getNumberParams() params = 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, d=None): """ 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 = dlimix_legacy.CSumCF() if trait_covar_type == 'freeform': cov.addCovariance(dlimix_legacy.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 is not 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(dlimix_legacy.CFixedCF(fixed_trait_covar)) diag = sp.zeros(1) elif trait_covar_type == 'diag': cov.addCovariance(dlimix_legacy.CDiagonalCF(self.P)) diag = sp.ones(self.P) elif trait_covar_type == 'lowrank': cov.addCovariance(dlimix_legacy.CLowRankCF(self.P, rank)) diag = sp.zeros(self.P * rank) elif trait_covar_type == 'lowrank_id': cov.addCovariance(dlimix_legacy.CLowRankCF(self.P, rank)) cov.addCovariance(dlimix_legacy.CFixedCF(sp.eye(self.P))) diag = sp.concatenate([sp.zeros(self.P * rank), sp.ones(1)]) elif trait_covar_type == 'lowrank_diag': cov.addCovariance(dlimix_legacy.CLowRankCF(self.P, rank)) cov.addCovariance(dlimix_legacy.CDiagonalCF(self.P)) diag = sp.concatenate([sp.zeros(self.P * rank), sp.ones(self.P)]) elif trait_covar_type == 'lowrank_diag1': assert d.shape[0] == self.P, 'dimension mismatch for d' cov1 = dlimix_legacy.CSumCF() cov1.addCovariance(dlimix_legacy.CLowRankCF(self.P, rank)) cov1.addCovariance(dlimix_legacy.CDiagonalCF(self.P)) cov.addCovariance(dlimix_legacy.CFixedDiagonalCF(cov1, d)) diag = sp.concatenate([sp.zeros(self.P * rank), sp.ones(self.P)]) elif trait_covar_type == 'freeform1': assert d.shape[0] == self.P, 'dimension mismatch for d' cov.addCovariance( dlimix_legacy.CFixedDiagonalCF( dlimix_legacy.CFreeFormCF(self.P), d)) L = sp.eye(self.P) diag = sp.concatenate([L[i, :(i + 1)] for i in range(self.P)]) elif trait_covar_type == 'block': cov.addCovariance(dlimix_legacy.CFixedCF(sp.ones( (self.P, self.P)))) diag = sp.zeros(1) elif trait_covar_type == 'block_id': cov.addCovariance(dlimix_legacy.CFixedCF(sp.ones( (self.P, self.P)))) cov.addCovariance(dlimix_legacy.CFixedCF(sp.eye(self.P))) diag = sp.concatenate([sp.zeros(1), sp.ones(1)]) elif trait_covar_type == 'block_diag': cov.addCovariance(dlimix_legacy.CFixedCF(sp.ones( (self.P, self.P)))) cov.addCovariance(dlimix_legacy.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 = dlimix_legacy.CFixedCF(sp.eye(self.P)) _cov.setParams(sp.array([sp.sqrt(jitter)])) _cov.setParamMask(sp.zeros(1)) cov.addCovariance(_cov) return cov, diag