예제 #1
0
    def setUp(self):
        sp.random.seed(1)

        # generate data
        n = 10
        f = 2
        X = 1. * (sp.rand(n, f) < 0.2)
        X -= X.mean(0)
        X /= X.std(0)
        kinship = sp.dot(X, X.T)
        kinship /= kinship.diagonal().mean()
        design = sp.zeros((n, n))
        for i in range(n // 2):
            design[2 * i, 2 * i + 1] = 1
            design[2 * i + 1, 2 * i] = 1

        # define covariance
        self.C = DirIndirCov(kinship, design)
        self.C.setRandomParams()
    def VD(self, DGE, IGE, IEE, cageEffect):
        """ defines covariance for variance decomposition."""

        #defines mean
        mean = lin_mean(self.pheno,self.covs)

        #define cagemate assignment - required for SGE, SEE, and cage effects. Z is N focal x N_cm and has 0s in cells Z_i,i (i.e. an animal is not its own cage mate)
        same_cage = 1. * (self.cage==self.cage_cm)
        diff_inds = 1. * (self.sampleID[:,sp.newaxis]!=self.sampleID_cm)
        Z = same_cage * diff_inds

        #define the overall genetic covariance matrix
        if DGE or IGE:
            #scales kinship (DGE component) to sample variance 1
            sf_K = covar_rescaling_factor(self.kinship)
            self.kinship *= sf_K

            #now create and scale SGE and DGE/SGE covariance components
            if IGE:
                #first SGE component: ZKcmZ' in this code (ZKZ' in paper)
                _ZKcmZ = sp.dot(Z,sp.dot(self.kinship_cm,Z.T))
                sf_ZKcmZ = covar_rescaling_factor(_ZKcmZ)
                self.kinship_cm *= sf_ZKcmZ
                 #second DGE/SGE covariance:
                self.kinship_cross *= sp.sqrt(sf_K * sf_ZKcmZ)
        
        if DGE and not IGE:
            self._genoCov = FixedCov(self.kinship)
        elif IGE and not DGE:
            self._genoCov = FixedCov(_ZKcmZ)
        elif DGE and IGE:
            self._genoCov = DirIndirCov(self.kinship,Z,kinship_cm=self.kinship_cm,kinship_cross=self.kinship_cross)
        else:
            self._genoCov = None


        #define the overall environmental covariance matrix
        #there is always DEE
        #env naturally has sample variance 1 so no need to scale it
        if IEE:
            #_ZZ = ZIcmZ'
            _ZZ  = sp.dot(Z,Z.T)
            sf_ZZ = covar_rescaling_factor(_ZZ)
            self.env_cm *= sf_ZZ
            self.env_cross *= sp.sqrt(1 * sf_ZZ)

            self._envCov = DirIndirCov(self.env,Z,kinship_cm=self.env_cm,kinship_cross=self.env_cross)
        else:
            self._envCov = FixedCov(self.env)

        ##define cage effect covariance matrix
        if cageEffect:
            N = self.pheno.shape[0]
            uCage = sp.unique(self.cage)
            #W, the cage design matrix, is N x n_cages (where N is number of focal animals) 
            W = sp.zeros((N,uCage.shape[0]))
            for cv_i, cv in enumerate(uCage):
                W[:,cv_i] = 1.*(self.cage[:,0]==cv)
            #WW, the cage effect covariance matrix, is N x N and has 1s in cells WW_i,i
            WW = sp.dot(W,W.T)
            #this is equivalent to getting covar_rescaling_factor first and then multiplying, as done for other matrices above
            WW = covar_rescale(WW)
            self._cageCov = FixedCov(WW)
        else:
            self._cageCov = None

        # define overall covariance matrix as sum of genetic, environmental and cage covariance matrices
        if self._genoCov is None:
            if self._cageCov is None:
                self.covar = SumCov(self._envCov)
            else:
                self.covar = SumCov(self._envCov,self._cageCov)
        else:
            if self._cageCov is None:
                self.covar = SumCov(self._genoCov,self._envCov)
            else:
                self.covar = SumCov(self._genoCov,self._envCov,self._cageCov)

        ## define gp
        self._gp = GP(covar=self.covar,mean=mean)