def _sample_effects_block_garcia(self): self._update_mm_equations() C = self.mmeq_C # sample loc_star # -- fixed locations are left as 0 since they don't affect result (GC&S claim) sigma2b = self.theta[self.sigma2_b_idx] self.loc_star[self.num_fixed:,0] = gt.rmvn(0,cholL=self.cholG,sigma=sigma2b) # Sample y* sigma2e = self.theta[self.sigma2_e_idx] Ie = np.matrix(np.identity(self.num_y), copy=False) e_star = gt.rmvn(0,cholLU=Ie,lower=True,sigma=sigma2e) self.y_star = self.W*self.loc_star + e_star cholC = np.asmatrix(scipy.linalg.cholesky(C,lower=True)) rhs = self.W.T * (self.y - self.y_star) self.loc_tilde = np.asmatrix(scipy.linalg.cho_solve((cholC,True),rhs)) # Sampling result self.theta[0:self.num_locations,0] = self.loc_star - self.loc_tilde
def lmm_sample(beta,Gs,sigma2=[],X=[5,30]): p = len(beta) beta = np.matrix(beta).reshape(p,1) c = len(Gs) qs = [np.shape(g)[0] for g in Gs] q = sum(qs) n = qs[0] if (isinstance(X,list)): # X contains list of ranges rangex0 = X[0] rangex1 = X[1] X = np.matrix(np.zeros((n,p))) X[:,0] = 1 X[:,1] = rangex0 + (rangex1-rangex0)*np.random.rand(n).reshape(n,1) else: assert(isinstance(X,np.matrix)) (n1,p1) = np.shape(X) assert((n1==n)and(p1==n)) # Create Z: n x q --- Sparse # sparse(y) zs = [sparse.eye(n)] data = [1 for u in range(0,n)] for qi in qs[1:]: # col = [random.randint(0,qi-1) for i in range(0,n)] col = [i%qi for i in range(0,n)] zi = sparse.csc_matrix((data,(range(0,n),col)),shape=(n,qi)) zs.append(zi) Z = sparse.hstack(zs) assert(len(sigma2)==(c+1)) # Sample random effects us = [] cholLs=[] for g,s2 in zip(Gs,sigma2[:-1]): cholL = np.linalg.cholesky(g) cholLs.append(cholL) ui = gt.rmvn(0,cholL=cholL,sigma=s2) us.append(ui) u = np.vstack(us) # Sample residual sigma2_e = sigma2[-1] R = np.asmatrix(np.eye(n)) cholL = np.linalg.cholesky(R) e = gt.rmvn(0,cholL=cholL,sigma=sigma2_e) # Calculate y y = X*beta + Z*u + e return (y,X,Z,cholLs,u,e)
def _sample_effects_block(self): self._update_mm_equations() C = self.mmeq_C invC = np.linalg.inv(C) # Compute location estimators - expected values # Equation C * loc = rhs -> loc = C^{-1}*rhs self.loc_hat = invC * self.mmeq_rhs # Sample from multivariate normal self.theta[0:self.num_locations,0] = gt.rmvn(self.loc_hat,V=invC,sigma=self.theta[self.sigma2_e_idx])
def lmmsample(tree): # The script #tree = PhyloTree(treeFile, schema='newick') #tree.normalize() G = tree.getV() (n,n) = np.shape(G) num_fixed = 2 # Create Beta beta = np.matrix([15,2.5]).reshape(num_fixed,1) # Create X rangex0 = 5 rangex1 = 30 X = np.matrix(np.zeros((n,num_fixed))) X[:,0] = 1 X[:,1] = rangex0 + (rangex1-rangex0)*np.random.rand(n).reshape(n,1) # Create Z Z = np.asmatrix(np.eye(n)) # Sample b ~ N(0,sigma2_b * V) sigma2_b = 5 cholL = np.linalg.cholesky(G) b = gt.rmvn(0,cholL=cholL,sigma=sigma2_b) # Sample residual e ~ N(0, sigma2_e * R) sigma2_e = 1.0 R = np.asmatrix(np.eye(n)) cholL = np.linalg.cholesky(R) e = gt.rmvn(0,cholL=cholL,sigma=sigma2_e) # Calculate y y = X*beta + Z*b + e #yl = y.ravel().tolist()[0] #xl = X[:,1].ravel().tolist()[0] #plt.plot(xl,yl,'ro') #ymax = max(yl) #plt.axis([0,rangex1,0, 10*(int(ymax/10)+1)]) #plt.show() return (y,X,beta,Z,b,G,cholL,sigma2_b,R,sigma2_e)
def _sample_effects_block_chol(self): # self._update_mm_equations() C = self.mmeq_C L = np.linalg.cholesky(C) # L = np.asmatrix(scipy.linalg.cholesky(C,lower=True)) # print 'before solve' self.theta_hat = np.asmatrix(scipy.linalg.cho_solve((L, True), self.mmeq_rhs)) # print 'after solve' # invChol = np.linalg.inv(cholC) sigma2_e0 = self.sigma2_e[0, 0] self.theta[:, 0] = gt.rmvn(self.theta_hat, cholL=L, inverse=True, sigma=sigma2_e0)
def _sample_effects_block_chol(self): self._update_mm_equations() C = self.mmeq_C L = np.linalg.cholesky(C) #L = np.asmatrix(scipy.linalg.cholesky(C,lower=True)) print 'before solve' print L print self.mmeq_rhs self.loc_hat = np.asmatrix(scipy.linalg.cho_solve((L,True),self.mmeq_rhs)) print "first loc hat" print self.loc_hat #print 'after solve' # invChol = np.linalg.inv(cholC) sigma2e = self.theta[self.sigma2_e_idx] self.theta[0:self.num_locations,0] = gt.rmvn(self.loc_hat,cholL=L,inverse=True,sigma=sigma2e) print 'sample' print self.theta[0:self.num_locations,0] exit(0)