Exemple #1
0
    def setUp(self):
        np.random.seed(1)

        # define phenotype
        N = 200
        P = 2
        self.Y = sp.randn(N, P)
        # define fixed effects
        self.F = []
        self.A = []
        self.F.append(1. * (sp.rand(N, 2) < 0.5))
        self.A.append(sp.eye(P))
        # define row caoriance
        f = 10
        X = 1. * (sp.rand(N, f) < 0.2)
        self.R = covar_rescale(sp.dot(X, X.T))
        self.R += 1e-4 * sp.eye(N)
        # define col covariances
        self.Cg = FreeFormCov(P)
        self.Cn = FreeFormCov(P)
        self.Cg.setCovariance(0.5 * sp.cov(self.Y.T))
        self.Cn.setCovariance(0.5 * sp.cov(self.Y.T))
        # define gp
        self.gp = GP2KronSum(Y=self.Y,
                             F=self.F,
                             A=self.A,
                             Cg=self.Cg,
                             Cn=self.Cn,
                             R=self.R)
Exemple #2
0
 def setUp(self):
     SP.random.seed(1)
     self.n = 4
     self.C = FreeFormCov(self.n)
     self.name = 'freeform'
     self.n_params = self.C.getNumberParams()
     params = SP.randn(self.n_params)
     self.C.setParams(params)
Exemple #3
0
 def test_too_expensive_exceptions(self):
     Cg = FreeFormCov(5001)
     Cn = FreeFormCov(5001)
     C = Cov2KronSum(Cg=Cg, Cn=Cn, R=self.R)
     with self.assertRaises(TooExpensiveOperationError):
         C.L()
     with self.assertRaises(TooExpensiveOperationError):
         C.K()
     with self.assertRaises(TooExpensiveOperationError):
         C.K_grad_i(0)
Exemple #4
0
 def setUp(self):
     sp.random.seed(1)
     # define row caoriance
     dim_r = 4
     X = sp.rand(dim_r, dim_r)
     self.R = covar_rescale(sp.dot(X,X.T))
     # define col covariances
     dim_c = 2
     Cg = FreeFormCov(dim_c)
     Cn = FreeFormCov(dim_c)
     self.C = Cov2KronSum(Cg = Cg, Cn = Cn, R = self.R)
     self.name = 'cov2kronSum'
     self.C.setRandomParams()
Exemple #5
0
 def setUp(self):
     sp.random.seed(1)
     # define row caoriance
     dim_r = 4
     rank_r = 2
     G = sp.rand(dim_r, rank_r)
     X = sp.rand(dim_r, dim_r)
     R = covar_rescale(sp.dot(X, X.T))
     # define col covariances
     dim_c = 2
     Cg = FreeFormCov(dim_c)
     Cn = FreeFormCov(dim_c)
     self.C = Cov3KronSumLR(Cn=Cn, Cg=Cg, R=R, G=G, rank=1)
     self.name = 'cov3kronSumLR'
     self.C.setRandomParams()
Exemple #6
0
 def setUp(self):
     SP.random.seed(1)
     self.n=4
     self.C = FreeFormCov(self.n)
     self.name = 'freeform'
     self.n_params=self.C.getNumberParams()
     params=SP.randn(self.n_params)
     self.C.setParams(params)
Exemple #7
0
 def setUp(self):
     np.random.seed(1)
     dim_r = 10
     dim_c = 3
     X = sp.rand(dim_r, dim_r)
     R = covar_rescale(sp.dot(X,X.T))
     C = FreeFormCov(dim_c)
     self._cov = KronCov(C, R)
     self._Iok = sp.randn(self._cov.dim)<0.9
Exemple #8
0
    def test_too_expensive_exceptions(self):
        dim_r = 100
        rank_r = 2
        G = sp.rand(dim_r, rank_r)
        X = sp.rand(dim_r, dim_r)
        R = covar_rescale(sp.dot(X, X.T))

        dim_c = 5001
        Cg = FreeFormCov(dim_c)
        Cn = FreeFormCov(dim_c)
        C = Cov3KronSumLR(Cn=Cn, Cg=Cg, R=R, G=G, rank=1)

        with self.assertRaises(TooExpensiveOperationError):
            C.L()
        with self.assertRaises(TooExpensiveOperationError):
            C.K()
        with self.assertRaises(TooExpensiveOperationError):
            C.K_grad_i(0)
Exemple #9
0
    def rank(self, value):
        self._rank = value
        if value is None:
            self.cat_cov = FreeFormCov(self.n_cats, jitter=self.jitter)
        else:
            assert value <= self.n_cats, 'rank cant be higher than number of categories'
            self.cat_cov = LowRankCov(self.n_cats, value)

        self.clear_all()
        self._notify()
Exemple #10
0
 def setUp(self):
     sp.random.seed(2)
     # define row caoriance
     n = 200
     f = 10
     P = 3
     X = 1. * (sp.rand(n, f) < 0.2)
     # define col covariances
     Cn = FreeFormCov(P)
     self.C = Cov2KronSumLR(Cn=Cn, G=X, rank=1)
     self.name = 'cov2kronSumLR'
     self.C.setRandomParams()
Exemple #11
0
 def test_too_expensive_exceptions(self):
     n = 5001
     f = 10
     Cn = FreeFormCov(5001)
     X = 1. * (sp.rand(n, f) < 0.2)
     C = Cov2KronSumLR(Cn=Cn, G=X, rank=1)
     with self.assertRaises(TooExpensiveOperationError):
         C.L()
     with self.assertRaises(TooExpensiveOperationError):
         C.R()
     with self.assertRaises(TooExpensiveOperationError):
         C.K()
     with self.assertRaises(TooExpensiveOperationError):
         C.K_grad_i(0)
Exemple #12
0
    def setUp(self):
        np.random.seed(1)

        # define phenotype
        N = 10 
        P = 3
        Y = sp.randn(N,P)

        # pheno with missing data
        Ym = Y.copy()
        Im = sp.rand(N, P)<0.2
        Ym[Im] = sp.nan

        # define fixed effects
        F = []; A = []
        F.append(1.*(sp.rand(N,2)<0.5))
        A.append(sp.eye(P))
        mean = MeanKronSum(Y, F=F, A=A)
        mean_m = MeanKronSum(Ym, F=F, A=A)

        # define row caoriance
        f = 10
        X = 1.*(sp.rand(N, f)<0.2)
        R = covar_rescale(sp.dot(X,X.T))
        R+= 1e-4 * sp.eye(N)

        # define col covariances
        Cg = FreeFormCov(P)
        Cn = FreeFormCov(P)
        Cg.setRandomParams()
        Cn.setRandomParams()

        # define covariance matrices
        covar1 = KronCov(Cg, R)
        covar2 = KronCov(Cn, sp.eye(N))
        covar  = SumCov(covar1,covar2)

        # define covariance matrice with missing data
        Iok = (~Im).reshape(N * P, order='F')
        covar1_m = KronCov(copy.copy(Cg), R, Iok=Iok)
        covar2_m = KronCov(copy.copy(Cn), sp.eye(N), Iok=Iok)
        covar_m  = SumCov(covar1_m,covar2_m)

        # define gp
        self._gp = GP(covar=covar, mean=mean)
        self._gpm = GP(covar=covar_m, mean=mean_m)
        self._gp2ks = GP2KronSum(Y=Y, F=F, A=A, Cg=Cg, Cn=Cn, R=R) 
Exemple #13
0
def define_gp(Y, Xr, mean, Ie, type):
    P = 2
    if type == 'null':
        _Cr = FixedCov(sp.ones([2, 2]))
        _Cr.scale = 1e-9
        _Cr.act_scale = False
        covar = CategoricalLR(_Cr, sp.ones((Xr.shape[0], 1)), Ie)
    else:
        if type == 'block': _Cr = FixedCov(sp.ones((P, P)))
        elif type == 'rank1': _Cr = LowRankCov(P, 1)
        elif type == 'full': _Cr = FreeFormCov(P)
        else: print('poppo')
        covar = CategoricalLR(_Cr, Xr, Ie)
    _gp = GP(covar=covar, mean=mean)
    return _gp
    def setUp(self):
        np.random.seed(1)

        # define phenotype
        N = 200
        P = 2
        self.Y = sp.randn(N, P)
        # define fixed effects
        self.F = []; self.A = []
        self.F.append(1.*(sp.rand(N,2)<0.5))
        self.A.append(sp.eye(P))
        # define row caoriance
        f = 10
        X = 1.*(sp.rand(N, f)<0.2)
        self.R  = covar_rescale(sp.dot(X,X.T))
        self.R += 1e-4 * sp.eye(N)
        # define col covariances
        self.Cg = FreeFormCov(P)
        self.Cn = FreeFormCov(P)
        self.Cg.setCovariance(0.5 * sp.cov(self.Y.T))
        self.Cn.setCovariance(0.5 * sp.cov(self.Y.T))
        # define gp
        self.gp = GP2KronSum(Y=self.Y, F=self.F, A=self.A, Cg=self.Cg,
                             Cn=self.Cn, R=self.R)
Exemple #15
0
    def setUp(self):
        np.random.seed(1)

        # define phenotype
        N = 200
        P = 2
        Y = sp.randn(N, P)
        # define row caoriance
        f = 10
        G = 1. * (sp.rand(N, f) < 0.2)
        X = 1. * (sp.rand(N, f) < 0.2)
        R = covar_rescale(sp.dot(X, X.T))
        R += 1e-4 * sp.eye(N)
        # define col covariances
        Cg = FreeFormCov(P)
        self._Cg = Cg
        Cn = FreeFormCov(P)
        Cg.setCovariance(0.5 * sp.cov(Y.T))
        Cn.setCovariance(0.5 * sp.cov(Y.T))
        # define gp
        self.gp = GP3KronSumLR(Y=Y, Cg=Cg, Cn=Cn, R=R, G=G, rank=1)
Exemple #16
0
    def setUp(self):
        np.random.seed(1)

        # define phenotype
        N = 200
        P = 2
        Y = sp.randn(N, P)
        # define fixed effects
        F = []
        A = []
        F.append(1. * (sp.rand(N, 2) < 0.5))
        A.append(sp.eye(P))
        # define row caoriance
        f = 10
        G = 1. * (sp.rand(N, f) < 0.2)
        # define col covariances
        Cr = FreeFormCov(P)
        self._Cr = Cr
        Cn = FreeFormCov(P)
        Cr.setCovariance(0.5 * sp.cov(Y.T))
        Cn.setCovariance(0.5 * sp.cov(Y.T))
        # define gp
        self.gp = GP2KronSumLR(Y=Y, F=F, A=A, Cn=Cn, G=G)
Exemple #17
0
    def setUp(self):
        np.random.seed(1)

        # define phenotype
        N = 200
        P = 2
        Y = sp.randn(N,P)
        # define row caoriance
        f = 10
        G = 1.*(sp.rand(N, f)<0.2)
        X = 1.*(sp.rand(N, f)<0.2)
        R = covar_rescale(sp.dot(X,X.T))
        R+= 1e-4 * sp.eye(N)
        # define col covariances
        Cg = FreeFormCov(P)
        self._Cg = Cg
        Cn = FreeFormCov(P)
        Cg.setCovariance(0.5 * sp.cov(Y.T))
        Cn.setCovariance(0.5 * sp.cov(Y.T))
        # define gp
        self.gp = GP3KronSumLR(Y = Y, Cg = Cg, Cn = Cn, R = R, G = G, rank = 1)
Exemple #18
0
    def setUp(self):
        np.random.seed(1)

        # define phenotype
        N = 200
        P = 2
        Y = sp.randn(N,P)
        # define fixed effects
        F = []; A = []
        F.append(1.*(sp.rand(N,2)<0.5))
        A.append(sp.eye(P))
        # define row caoriance
        f = 10
        G = 1.*(sp.rand(N, f)<0.2)
        # define col covariances
        Cr = FreeFormCov(P)
        self._Cr = Cr
        Cn = FreeFormCov(P)
        Cr.setCovariance(0.5 * sp.cov(Y.T))
        Cn.setCovariance(0.5 * sp.cov(Y.T))
        # define gp
        self.gp = GP2KronSumLR(Y = Y, F = F, A = A, Cn = Cn, G = G)
Exemple #19
0
class TestFreeForm(unittest.TestCase):
    def setUp(self):
        SP.random.seed(1)
        self.n = 4
        self.C = FreeFormCov(self.n)
        self.name = 'freeform'
        self.n_params = self.C.getNumberParams()
        params = SP.randn(self.n_params)
        self.C.setParams(params)

    def test_grad(self):
        def func(x, i):
            self.C.setParams(x)
            return self.C.K()

        def grad(x, i):
            self.C.setParams(x)
            return self.C.K_grad_i(i)

        x0 = self.C.getParams()
        err = mcheck_grad(func, grad, x0)

        np.testing.assert_almost_equal(err, 0., decimal=6)

    def test_param_activation(self):
        self.assertEqual(len(self.C.getParams()), 10)
        self.C.act_K = False
        self.assertEqual(len(self.C.getParams()), 0)

        self.C.setParams(np.array([]))
        with self.assertRaises(ValueError):
            self.C.setParams(np.array([0]))

        with self.assertRaises(ValueError):
            self.C.K_grad_i(0)

    def test_Khess(self):

        cov = self.C

        for j in range(cov.getNumberParams()):

            def func(x, i):
                cov.setParams(x)
                return cov.K_grad_i(j)

            def grad(x, i):
                cov.setParams(x)
                return cov.K_hess_i_j(j, i)

            x0 = cov.getParams()
            err = mcheck_grad(func, grad, x0)
            np.testing.assert_almost_equal(err, 0.)
Exemple #20
0
 def __init__(self,
              Y=None,
              R=None,
              S_R=None,
              U_R=None,
              traitID=None,
              F=None,
              rank=1):
     """
     Args:
         Y:          [N, P] phenotype matrix
         F:          [N, K] matrix of fixed effect design.
                     K is the number of per-trait covariates.
         R:          [N, N] genetic relatedness matrix between individuals.
                     In alternative to R, S_R and U_R can be provided.
                     If not specified a model without relatedness component is considered.
         S_R:        N vector of eigenvalues of R
         U_R:        [N, N] eigenvector matrix of R
         traiID:     P vector of the IDs of the phenotypes to analyze (optional)
         rank:       rank of the trait covariance matrix of the variance component to be tested (default is 1)
     """
     # data
     noneNone = S_R is not None and U_R is not None
     self.bgRE = R is not None or noneNone
     # fixed effect
     msg = 'The current implementation of the full rank mtSet'
     msg += ' does not support covariates.'
     msg += ' We reccommend to regress out covariates and'
     msg += ' subsequently quantile normalize the phenotypes'
     msg += ' to a normal distribution prior to use mtSet.'
     msg += ' This can be done within the LIMIX framework using'
     msg += ' the methods limix.utils.preprocess.regressOut and'
     msg += ' limix.utils.preprocess.gaussianize'
     assert not (F is not None and self.bgRE), msg
     if F is not None:
         F = remove_dependent_cols(F)
         A = sp.eye(Y.shape[1])
     else:
         A = None
     #traitID
     if traitID is None:
         traitID = sp.array(['trait %d' % p for p in range(Y.shape[1])])
     self.setTraitID(traitID)
     #init covariance matrices and gp
     Cg = FreeFormCov(Y.shape[1])
     Cn = FreeFormCov(Y.shape[1])
     G = 1. * (sp.rand(Y.shape[0], 1) < 0.2)
     if self.bgRE:
         self._gp = GP3KronSumLR(Y=Y,
                                 Cg=Cg,
                                 Cn=Cn,
                                 R=R,
                                 S_R=S_R,
                                 U_R=U_R,
                                 G=G,
                                 rank=1)
     else:
         self._gp = GP2KronSumLR(Y=Y, Cn=Cn, G=G, F=F, A=A)
     # null model params
     self.null = None
     # calls itself for column-by-column trait analysis
     self.stSet = None
     self.nullST = None
     self.infoOpt = None
     self.infoOptST = None
     pass
Exemple #21
0
class TestFreeForm(unittest.TestCase):
    def setUp(self):
        SP.random.seed(1)
        self.n=4
        self.C = FreeFormCov(self.n)
        self.name = 'freeform'
        self.n_params=self.C.getNumberParams()
        params=SP.randn(self.n_params)
        self.C.setParams(params)

    def test_grad(self):
        def func(x, i):
            self.C.setParams(x)
            return self.C.K()

        def grad(x, i):
            self.C.setParams(x)
            return self.C.K_grad_i(i)

        x0 = self.C.getParams()
        err = mcheck_grad(func, grad, x0)

        np.testing.assert_almost_equal(err, 0., decimal=6)

    def test_param_activation(self):
        self.assertEqual(len(self.C.getParams()), 10)
        self.C.act_K = False
        self.assertEqual(len(self.C.getParams()), 0)

        self.C.setParams(np.array([]))
        with self.assertRaises(ValueError):
            self.C.setParams(np.array([0]))

        with self.assertRaises(ValueError):
            self.C.K_grad_i(0)
Exemple #22
0
from limix.core.gp import GP
from limix.utils.preprocess import covar_rescale
import time
import copy
import pdb

if __name__=='__main__':

    # define row caoriance
    N = 1000
    f = 10
    P = 3
    X = 1.*(sp.rand(N, f)<0.2)

    # define col covariances
    Cn = FreeFormCov(P)


    # define fixed effects and pheno
    F = 1.*(sp.rand(N,2)<0.5)
    A = sp.eye(P)
    Y = sp.randn(N, P)

    gp = GP2KronSumLR(Y = Y, F = F, A = A, Cn = Cn, G = X)
    gp.covar.Cr.setRandomParams()
    gp.optimize()

    if 0:
        # profile time
        for i in range(10):
            gp.covar.setRandomParams()
class TestGPBase(unittest.TestCase):
    def setUp(self):
        np.random.seed(1)

        # define phenotype
        N = 200
        P = 2
        self.Y = sp.randn(N, P)
        # define fixed effects
        self.F = []; self.A = []
        self.F.append(1.*(sp.rand(N,2)<0.5))
        self.A.append(sp.eye(P))
        # define row caoriance
        f = 10
        X = 1.*(sp.rand(N, f)<0.2)
        self.R  = covar_rescale(sp.dot(X,X.T))
        self.R += 1e-4 * sp.eye(N)
        # define col covariances
        self.Cg = FreeFormCov(P)
        self.Cn = FreeFormCov(P)
        self.Cg.setCovariance(0.5 * sp.cov(self.Y.T))
        self.Cn.setCovariance(0.5 * sp.cov(self.Y.T))
        # define gp
        self.gp = GP2KronSum(Y=self.Y, F=self.F, A=self.A, Cg=self.Cg,
                             Cn=self.Cn, R=self.R)

    @unittest.skip("someone has to fix it")
    def test_grad(self):

        gp = self.gp

        def func(x, i):
            params = gp.getParams()
            params['covar'] = x
            gp.setParams(params)
            return gp.LML()

        def grad(x, i):
            params = gp.getParams()
            params['covar'] = x
            gp.setParams(params)
            grad = gp.LML_grad()
            return grad['covar'][i]

        x0 = gp.getParams()['covar']
        err = mcheck_grad(func, grad, x0)
        np.testing.assert_almost_equal(err, 0., decimal=4)

    def test_grad_activation(self):

        gp = self.gp

        self.Cg._K_act = False

        def func(x, i):
            params = gp.getParams()
            params['covar'] = x
            gp.setParams(params)
            return gp.LML()

        def grad(x, i):
            params = gp.getParams()
            params['covar'] = x
            gp.setParams(params)
            grad = gp.LML_grad()
            return grad['covar'][i]

        x0 = gp.getParams()['covar']
        err = mcheck_grad(func, grad, x0)
        np.testing.assert_almost_equal(err, 0., decimal=4)

        self.Cg._K_act = True
        self.Cn._K_act = False

        def func(x, i):
            params = gp.getParams()
            params['covar'] = x
            gp.setParams(params)
            return gp.LML()

        def grad(x, i):
            params = gp.getParams()
            params['covar'] = x
            gp.setParams(params)
            grad = gp.LML_grad()
            return grad['covar'][i]

        x0 = gp.getParams()['covar']
        err = mcheck_grad(func, grad, x0)
        np.testing.assert_almost_equal(err, 0., decimal=4)

    def test_correct_inputs(self):
        np.asarray(None, dtype=float)
Exemple #24
0

def gen_data(N=100, P=4):
    f = 20
    G = 1. * (sp.rand(N, f) < 0.2)
    F = sp.rand(N, 2)
    Y = sp.randn(N, P)
    return Y, F, G


if __name__ == '__main__':

    P = 4

    # define col covariances
    Cg = FreeFormCov(P, jitter=0)
    Cn = FreeFormCov(P)
    Cg.setRandomParams()
    Cn.setRandomParams()

    out_file = './times_PC.hdf5'

    if not os.path.exists(out_file) or 'recalc' in sys.argv:
        Ns = sp.array([
            100, 150, 200, 300, 500, 800, 1200, 1600, 2000, 3000, 4000, 5000,
            6000, 8000, 10000, 12000, 14000, 16000, 20000, 24000, 32000, 40000
        ])
        n_rips = 5
        t = sp.zeros((Ns.shape[0], n_rips))
        t0 = sp.zeros((Ns.shape[0], n_rips))
        r = sp.zeros((Ns.shape[0], n_rips))
def gen_data(N=100, P=4):
    f = 20
    G = 1.*(sp.rand(N, f)<0.2)
    X = 1.*(sp.rand(N, f)<0.2)
    R = covar_rescale(sp.dot(X,X.T))
    R+= 1e-4 * sp.eye(N)
    S, U = la.eigh(R)
    Y = sp.randn(N, P)
    return Y, S, U, G 

if __name__=='__main__':

    P = 4

    # define col covariances
    Cg = FreeFormCov(P, jitter=0)
    Cn = FreeFormCov(P)
    Cg.setRandomParams()
    Cn.setRandomParams()

    out_file = './times.hdf5'

    if not os.path.exists(out_file) or 'recalc' in sys.argv:
        Ns = sp.array([100,150,200,300,500,800,1200,1600,2000,3000,4000,5000])
        n_rips = 5 
        t = sp.zeros((Ns.shape[0], n_rips))
        t0 = sp.zeros((Ns.shape[0], n_rips))
        r = sp.zeros((Ns.shape[0], n_rips))
        for ni, n in enumerate(Ns): 
            for ri in range(n_rips):
                print '.. %d individuals - rip %d' % (n, ri)
import mtSet.pycore.covariance as covariance
import time
import copy
import pdb
import h5py
import os
from limix.utils.util_functions import smartDumpDictHdf5

if __name__=='__main__':

    P = 4

    pdb.set_trace()

    # define col covariances
    C = FreeFormCov(P, jitter=0)
    C0 = covariance.freeform(P)

    t1 = 0
    t0 = 0

    for ti in range(1000):
        C.setRandomParams()
        C0.setParams(C.getParams())

        for i in range(int(0.5*P*(P+1))):
            _t0 = time.time()
            C0.Kgrad_param(i)
            _t1 = time.time()
            C.K_grad_i(i)
            _t2 = time.time()
Exemple #27
0
            Kiz = sp.dot(Ki, z)
            Hq0 = sp.zeros((3, 3))
            for i in range(3):
                for j in range(3):
                    Hq0[i, j] = sp.dot(z.T,
                                       sp.dot(KidK[i], sp.dot(KidK[j], Kiz)))
            pdb.set_trace()

    if 0:
        # compute score with hessian

        H = 0.5 * Ht  #-0.5*Ht+Hq

        Hi = la.inv(H)
        score1[time_i] = (SSS * sp.dot(Hi, SSS)).sum()
        C = FreeFormCov(2)

        def f1(x):
            C.setParams(x)
            b = C.K()[sp.tril_indices(2)]
            delta = (b - SSS)
            val = (delta * sp.dot(Hi, delta)).sum()
            db_dx0 = C.K_grad_i(0)[sp.tril_indices(2)]
            db_dx1 = C.K_grad_i(1)[sp.tril_indices(2)]
            db_dx2 = C.K_grad_i(2)[sp.tril_indices(2)]
            grad = 2 * sp.array([(delta * sp.dot(Hi, db_dx0)).sum(),
                                 (delta * sp.dot(Hi, db_dx1)).sum(),
                                 (delta * sp.dot(Hi, db_dx2)).sum()])
            return val, grad

        x_opt, dscore1, info = sp.optimize.fmin_l_bfgs_b(f1, sp.randn(3))
    # define col covariances

    out_file = './times_scaleP.hdf5'

    if not os.path.exists(out_file) or 'recalc' in sys.argv:
        Ps = sp.array([2, 3, 4, 5])
        n_rips = 5
        t = sp.zeros((Ps.shape[0], n_rips))
        t0 = sp.zeros((Ps.shape[0], n_rips))
        r = sp.zeros((Ps.shape[0], n_rips))
        for pi, p in enumerate(Ps):
            for ri in range(n_rips):
                print '.. %d traits - rip %d' % (p, ri)
                print '   .. generating data'
                Y, S, U, G = gen_data(N=N, P=p)
                Cg = FreeFormCov(p, jitter=0)
                Cn = FreeFormCov(p)

                # define GPs
                gp = GP3KronSumLR(Y=Y, Cg=Cg, Cn=Cn, S_R=S, U_R=U, G=G, rank=1)
                gp0 = gp3ks0(mean(Y),
                             covariance.freeform(p),
                             covariance.freeform(p),
                             S_XX=S,
                             U_XX=U,
                             rank=1)
                gp0.set_Xr(G)
                gp._reset_profiler()

                if 1:
                    gp.covar.setRandomParams()
Exemple #29
0
        RV = sp.dot(self.U_CstarGrad_n(i).T, self.Cn.USi2().T)
        RV += sp.dot(self.U_Cstar().T, self.Cn.USi2grad(i).T)
        return RV

    def Sgrad_g(self, i):
        return sp.kron(self.S_CstarGrad_g(i), self.Sr())

    def Sgrad_n(self, i):
        return sp.kron(self.S_CstarGrad_n(i), self.Sr())


if __name__ == '__main__':
    from limix.core.covar import FreeFormCov
    from limix.utils.preprocess import covar_rescale

    # define row caoriance
    dim_r = 10
    X = sp.rand(dim_r, dim_r)
    R = covar_rescale(sp.dot(X, X.T))

    # define col covariances
    dim_c = 3
    Cg = FreeFormCov(dim_c)
    Cn = FreeFormCov(dim_c)

    cov = Cov2KronSum(Cg=Cg, Cn=Cn, R=R)
    cov.setRandomParams()

    print cov.K()
    print cov.K_grad_i(0)
Exemple #30
0
from limix.core.gp import GP3KronSumLR
from limix.core.gp import GP
from limix.utils.preprocess import covar_rescale
import time
import copy
import pdb

if __name__ == '__main__':

    # define region and bg terms
    N = 500
    f = 10
    P = 3
    G = 1. * (sp.rand(N, f) < 0.2)
    X = 1. * (sp.rand(N, f) < 0.2)
    R = covar_rescale(sp.dot(X, X.T))
    R += 1e-4 * sp.eye(N)

    # define col covariances
    Cg = FreeFormCov(P)
    Cn = FreeFormCov(P)
    Cg.setRandomParams()
    Cn.setRandomParams()

    # define pheno
    Y = sp.randn(N, P)

    # define GP and optimize
    gp = GP3KronSumLR(Y=Y, Cg=Cg, Cn=Cn, R=R, G=G, rank=1)
    gp.optimize()
    # define fixed effects
    F = []; A = []
    F.append(1.*(sp.rand(N,2)<0.5))
    #F.append(1.*(sp.rand(N,1)<0.5))
    A.append(sp.eye(P))
    #A.append(sp.ones((1,P)))

    # define row caoriance
    f = 10
    X = 1.*(sp.rand(N, f)<0.2)
    R = covar_rescale(sp.dot(X,X.T))
    R+= 1e-4 * sp.eye(N)
    S_R, U_R = la.eigh(R)

    # define col covariances
    Cg = FreeFormCov(P)
    Cn = FreeFormCov(P)
    Cg.setRandomParams()
    Cn.setRandomParams()

    # define gp
    gp = GP2KronSum(Y=Y, F=F, A=A, Cg=Cg, Cn=Cn, S_R=S_R, U_R=U_R)
    pdb.set_trace()
    print gp.covar.Sr()
    print gp.LML()
    print gp.covar.Sr()
    print Cg.setRandomParams()

    t0 = time.time()
    print 'GP2KronSum.LML():', gp.LML()
    print 'Time elapsed:', time.time() - t0
Exemple #32
0
class TestGPBase(unittest.TestCase):
    def setUp(self):
        np.random.seed(1)

        # define phenotype
        N = 200
        P = 2
        self.Y = sp.randn(N, P)
        # define fixed effects
        self.F = []
        self.A = []
        self.F.append(1. * (sp.rand(N, 2) < 0.5))
        self.A.append(sp.eye(P))
        # define row caoriance
        f = 10
        X = 1. * (sp.rand(N, f) < 0.2)
        self.R = covar_rescale(sp.dot(X, X.T))
        self.R += 1e-4 * sp.eye(N)
        # define col covariances
        self.Cg = FreeFormCov(P)
        self.Cn = FreeFormCov(P)
        self.Cg.setCovariance(0.5 * sp.cov(self.Y.T))
        self.Cn.setCovariance(0.5 * sp.cov(self.Y.T))
        # define gp
        self.gp = GP2KronSum(Y=self.Y,
                             F=self.F,
                             A=self.A,
                             Cg=self.Cg,
                             Cn=self.Cn,
                             R=self.R)

    def test_grad(self):

        gp = self.gp

        def func(x, i):
            params = gp.getParams()
            params['covar'] = x
            gp.setParams(params)
            return gp.LML()

        def grad(x, i):
            params = gp.getParams()
            params['covar'] = x
            gp.setParams(params)
            grad = gp.LML_grad()
            return grad['covar'][i]

        x0 = gp.getParams()['covar']
        err = mcheck_grad(func, grad, x0)
        np.testing.assert_almost_equal(err, 0., decimal=4)

    def test_grad_activation(self):

        gp = self.gp

        self.Cg._K_act = False

        def func(x, i):
            params = gp.getParams()
            params['covar'] = x
            gp.setParams(params)
            return gp.LML()

        def grad(x, i):
            params = gp.getParams()
            params['covar'] = x
            gp.setParams(params)
            grad = gp.LML_grad()
            return grad['covar'][i]

        x0 = gp.getParams()['covar']
        err = mcheck_grad(func, grad, x0)
        np.testing.assert_almost_equal(err, 0., decimal=4)

        self.Cg._K_act = True
        self.Cn._K_act = False

        def func(x, i):
            params = gp.getParams()
            params['covar'] = x
            gp.setParams(params)
            return gp.LML()

        def grad(x, i):
            params = gp.getParams()
            params['covar'] = x
            gp.setParams(params)
            grad = gp.LML_grad()
            return grad['covar'][i]

        x0 = gp.getParams()['covar']
        err = mcheck_grad(func, grad, x0)
        np.testing.assert_almost_equal(err, 0., decimal=4)

    def test_correct_inputs(self):
        np.asarray(None, dtype=float)
Exemple #33
0
    def getInterParams(self):
        return self.C.getInterParams()

    def K_grad_interParam_i(self,i):
        R = sp.kron(self.C.K_grad_interParam_i(i), self.R)
        if self.Iok is not None:
            R = R[self.Iok][:,self.Iok]
        return R

    def setFIinv(self, value):
        self.C.setFIinv(value)

if __name__=='__main__':
    from limix.core.covar import FreeFormCov
    from limix.utils.preprocess import covar_rescale

    # define row caoriance
    dim_r = 10
    X = sp.rand(dim_r, dim_r)
    R = covar_rescale(sp.dot(X,X.T))

    # define col covariance
    dim_c = 3
    C = FreeFormCov(dim_c)

    cov = KronCov(C, R) 
    cov.setRandomParams()
    print((cov.K()))
    print((cov.K_grad_i(0)))