def compute_gamma(self,A,E,zz_tn_prev,zz_tn,gamma_tn): """ Replaces the current value of the gamma parameters with its updated value, and returns the mean square difference between the two. """ # This is the main bottleneck of the code. # Would be faster if: # - implemented in C # - roots() was also implemented in C d_z = len(gamma_tn) product_matrix_matrix(zz_tn_prev,A.T,self.mat_d_z_d_z) product_matrix_matrix(A,self.mat_d_z_d_z,self.mat_d_z_d_z2) getdiag(self.mat_d_z_d_z2,self.AzzA_prev) G = diag(zz_tn)+2*self.gamma_prior_beta H = self.AzzA_prev a1 = 2.0*(self.gamma_prior_alpha+1.0) a2 = (4.0*self.gamma_prior_alpha+5.0)*E + H - G a3 = ((2.0*self.gamma_prior_alpha+3)*E-2.0*G)*E a4 = -G*E**2 Q = ((3.0*a3/a1)-((a2/a1)**2))/9 R = (9*a1*a2*a3-27*a4*(a1**2)-2*(a2**3))/(54*a1**3) ##delta = Q**3+R**2 #rho = sqrt(-Q**3) #theta = arccos(R/rho) theta = arccos(sign(R)*minimum(exp(log(abs(R))-3.0/2.0*log(-Q)),1.0)) #print theta1, theta #JJ = pow(rho,1.0/3) HH = sqrt(-Q) am = 2*HH*cos(theta/3)-a2/(3.0*a1) am = maximum(abs(am),0.00001) gamma_mean_diff = sum((am-gamma_tn)**2)/d_z gamma_tn[:] = am return gamma_mean_diff
def compute_gamma(self, A, E, zz_tn_prev, zz_tn, gamma_tn): """ Replaces the current value of the gamma parameters with its updated value, and returns the mean square difference between the two. """ # This is the main bottleneck of the code. # Would be faster if: # - implemented in C # - roots() was also implemented in C d_z = len(gamma_tn) product_matrix_matrix(zz_tn_prev, A.T, self.mat_d_z_d_z) product_matrix_matrix(A, self.mat_d_z_d_z, self.mat_d_z_d_z2) getdiag(self.mat_d_z_d_z2, self.AzzA_prev) G = diag(zz_tn) + 2 * self.gamma_prior_beta H = self.AzzA_prev a1 = 2.0 * (self.gamma_prior_alpha + 1.0) a2 = (4.0 * self.gamma_prior_alpha + 5.0) * E + H - G a3 = ((2.0 * self.gamma_prior_alpha + 3) * E - 2.0 * G) * E a4 = -G * E**2 Q = ((3.0 * a3 / a1) - ((a2 / a1)**2)) / 9 R = (9 * a1 * a2 * a3 - 27 * a4 * (a1**2) - 2 * (a2**3)) / (54 * a1**3) ##delta = Q**3+R**2 #rho = sqrt(-Q**3) #theta = arccos(R/rho) theta = arccos( sign(R) * minimum(exp(log(abs(R)) - 3.0 / 2.0 * log(-Q)), 1.0)) #print theta1, theta #JJ = pow(rho,1.0/3) HH = sqrt(-Q) am = 2 * HH * cos(theta / 3) - a2 / (3.0 * a1) am = maximum(abs(am), 0.00001) gamma_mean_diff = sum((am - gamma_tn)**2) / d_z gamma_tn[:] = am return gamma_mean_diff
def log_det_diff2_log_gamma(self,A,E,zz_tn_prev,zz_tn,gamma_tn): d_z = len(gamma_tn) product_matrix_matrix(zz_tn_prev,A.T,self.mat_d_z_d_z) product_matrix_matrix(A,self.mat_d_z_d_z,self.mat_d_z_d_z2) getdiag(self.mat_d_z_d_z2,self.AzzA_prev) G = 0.5*diag(zz_tn)+self.gamma_prior_beta H = 0.5*self.AzzA_prev gamma_E_1 = (gamma_tn+E) gamma_E_2 = gamma_E_1*gamma_E_1 gamma_E_3 = gamma_E_2*gamma_E_1 return sum(log(G/gamma_tn+H*gamma_tn*(E-gamma_tn)/gamma_E_3-0.5*E*gamma_tn/gamma_E_2))
def log_det_diff2_log_gamma(self, A, E, zz_tn_prev, zz_tn, gamma_tn): d_z = len(gamma_tn) product_matrix_matrix(zz_tn_prev, A.T, self.mat_d_z_d_z) product_matrix_matrix(A, self.mat_d_z_d_z, self.mat_d_z_d_z2) getdiag(self.mat_d_z_d_z2, self.AzzA_prev) G = 0.5 * diag(zz_tn) + self.gamma_prior_beta H = 0.5 * self.AzzA_prev gamma_E_1 = (gamma_tn + E) gamma_E_2 = gamma_E_1 * gamma_E_1 gamma_E_3 = gamma_E_2 * gamma_E_1 return sum( log(G / gamma_tn + H * gamma_tn * (E - gamma_tn) / gamma_E_3 - 0.5 * E * gamma_tn / gamma_E_2))
def multivariate_norm_log_pdf(self,x,mu,cov): # -0.5 * (dot(x-mu,dot(inv(cov),x-mu)) + len(x)*log(2*pi) + log(det(cov))) self.vec_d_y[:] = x self.vec_d_y[:] -= mu solve(cov,reshape(self.vec_d_y,(-1,1)),reshape(self.vec_d_y2,(-1,1)), self.covf,self.colvecf,self.pivotscov) ret = dot(self.vec_d_y,self.vec_d_y2) ret += len(x)*log(2*pi) lu(cov,self.pcov,self.Lcov,self.Ucov,self.covf,self.pivotscov) getdiag(self.Ucov,self.vec_d_y) absolute(self.vec_d_y,self.vec_d_y2) log(self.vec_d_y2,self.vec_d_y) ret += sum(self.vec_d_y) ret *= -0.5 return ret
def multivariate_norm_log_pdf(self, x, mu, cov): # -0.5 * (dot(x-mu,dot(inv(cov),x-mu)) + len(x)*log(2*pi) + log(det(cov))) self.vec_d_y[:] = x self.vec_d_y[:] -= mu solve(cov, reshape(self.vec_d_y, (-1, 1)), reshape(self.vec_d_y2, (-1, 1)), self.covf, self.colvecf, self.pivotscov) ret = dot(self.vec_d_y, self.vec_d_y2) ret += len(x) * log(2 * pi) lu(cov, self.pcov, self.Lcov, self.Ucov, self.covf, self.pivotscov) getdiag(self.Ucov, self.vec_d_y) absolute(self.vec_d_y, self.vec_d_y2) log(self.vec_d_y2, self.vec_d_y) ret += sum(self.vec_d_y) ret *= -0.5 return ret
def multivariate_norm_log_pdf(self,x,mu,cov): #return -0.5 * (dot(x-mu,dot(inv(cov),x-mu)) + len(x)*log(2*pi) + sum(log(eigvals(cov)))) #return_old = -0.5 * (dot(x-mu,solve(cov,x-mu)) + len(x)*log(2*pi) + sum(log(abs(diag(scipy.linalg.lu(cov)[2]))))) self.vec_d_y[:] = x self.vec_d_y[:] -= mu solve(cov,reshape(self.vec_d_y,(-1,1)),reshape(self.vec_d_y2,(-1,1)), self.covf,self.colvecf,self.pivotscov) ret = dot(self.vec_d_y,self.vec_d_y2) ret += len(x)*log(2*pi) lu(cov,self.pcov,self.Lcov,self.Ucov,self.covf,self.pivotscov) getdiag(self.Ucov,self.vec_d_y) absolute(self.vec_d_y,self.vec_d_y2) log(self.vec_d_y2,self.vec_d_y) ret += sum(self.vec_d_y) ret *= -0.5 return ret
def multivariate_norm_log_pdf(self, x, mu, cov): #return -0.5 * (dot(x-mu,dot(inv(cov),x-mu)) + len(x)*log(2*pi) + sum(log(eigvals(cov)))) #return_old = -0.5 * (dot(x-mu,solve(cov,x-mu)) + len(x)*log(2*pi) + sum(log(abs(diag(scipy.linalg.lu(cov)[2]))))) self.vec_d_y[:] = x self.vec_d_y[:] -= mu solve(cov, reshape(self.vec_d_y, (-1, 1)), reshape(self.vec_d_y2, (-1, 1)), self.covf, self.colvecf, self.pivotscov) ret = dot(self.vec_d_y, self.vec_d_y2) ret += len(x) * log(2 * pi) lu(cov, self.pcov, self.Lcov, self.Ucov, self.covf, self.pivotscov) getdiag(self.Ucov, self.vec_d_y) absolute(self.vec_d_y, self.vec_d_y2) log(self.vec_d_y2, self.vec_d_y) ret += sum(self.vec_d_y) ret *= -0.5 return ret