def update_delta_v(self): options = dict() options['maxiter'] = 50 res = scipy.optimize.minimize(_f_delta, jac=False, x0=numpy.random.random(), method='L-BFGS-B',args=(self.aux.P_v,self.aux.Q_v,self.param.M_v,self.param.C_v),bounds=[(0.001,None)] ,options=options) if res.success and res.nit > 0: # and res.jac < 1E-4 print 'update delta' self.param.delta_v = res.x self.aux.inv_Sigma_v = self.aux.PQinvPt_v + numpy.diag(numpy.full((self.obs.G),self.param.delta_v)) self.aux.Sigma_v = aux_variable.matrix_inv(numpy.full((self.obs.G),self.param.delta_v),self.aux.P_v,self.aux.Q_v_inv) self.aux.logdet_Sigma_v = numpy.linalg.slogdet(self.aux.Sigma_v)[1]
def update_q_u(self,k): m_u_k_old = self.param.M_u[:,k].copy() c_u_k_partial = numpy.zeros(self.obs.D) m_u_partial = numpy.zeros(self.obs.D) which_observed = set() for n in xrange(self.obs.N): for g in xrange(self.obs.G): if numpy.sum(self.mat_mask[n][:,g])>0: which_observed.add(n) c_u_k_partial += self.mat_mask[n][:,g] * self.param.tau[n]* (self.param.a[n]**2) * (self.param.M_v[g,k]**2 + self.param.C_v[k,g,g]) m_u_partial += self.param.tau[n]*self.param.a[n]*self.param.M_v[g,k]* self.mat_mask[n][:,g]*(self.aux.R[n][:,g]+self.param.a[n]*self.param.M_v[g,k]*self.param.M_u[:,k]) if len(which_observed) > 0: # self.param.C_u[k] = numpy.linalg.solve((numpy.diag(c_u_k_partial) + self.aux.inv_Sigma_u ),numpy.identity(self.obs.D)) self.param.C_u[k] = aux_variable.matrix_inv(c_u_k_partial+self.param.delta_u,self.aux.P_u,self.aux.Q_u_inv) self.aux.S_u[:,k] = numpy.diag(self.param.C_u[k]) self.param.M_u[:,k] = self.param.C_u[k].dot(m_u_partial) # Update aux for n in which_observed: # for g in xrange(self.obs.G): # self.aux.R[n][:,g] = self.aux.R[n][:,g]-self.param.a[n]*(self.param.M_u[:,k]-m_u_k_old)*(self.param.M_v[g,k]) self.aux.R[n] = self.aux.R[n]-self.param.a[n]*numpy.outer((self.param.M_u[:,k]-m_u_k_old),self.param.M_v[:,k])
def update_q_v(self,k): m_v_k_old = self.param.M_v[:,k].copy() c_v_k_partial = numpy.zeros(self.obs.G) m_v_partial = numpy.zeros(self.obs.G) which_observed = set() for n in xrange(self.obs.N): for d in xrange(self.obs.D): if numpy.sum(self.mat_mask[n][d,:])>0: which_observed.add(n) c_v_k_partial += self.mat_mask[n][d,:] * self.param.tau[n]* (self.param.a[n]**2) * (self.param.M_u[d,k]**2 + self.param.C_u[k,d,d]) m_v_partial += self.param.tau[n]*self.param.a[n]*self.param.M_u[d,k]* self.mat_mask[n][d,:]*(self.aux.R[n][d,:]+self.param.a[n]*self.param.M_u[d,k]*self.param.M_v[:,k]) if len(which_observed) > 0: # self.param.C_v[k] = numpy.linalg.solve((numpy.diag(c_v_k_partial) + self.aux.inv_Sigma_v ),numpy.identity(self.obs.G)) self.param.C_v[k] = aux_variable.matrix_inv(c_v_k_partial+self.param.delta_v,self.aux.P_v,self.aux.Q_v_inv) self.aux.S_v[:,k] = numpy.diag(self.param.C_v[k]) self.param.M_v[:,k] = self.param.C_v[k].dot(m_v_partial) # Update aux for n in which_observed: # for d in xrange(self.obs.D): # self.aux.R[n][d,:] = self.aux.R[n][d,:]-self.param.a[n]*(self.param.M_v[:,k]-m_v_k_old)*(self.param.M_u[d,k]) self.aux.R[n] = self.aux.R[n]-self.param.a[n]*numpy.outer(self.param.M_u[:,k],(self.param.M_v[:,k]-m_v_k_old))
def __init__(self,obs,user_param,mat_mask): self.obs = obs self.hist_ELBO = [] self.hist_sum_rmse = [] self.hist_convergence = [] self.hist_rmse = [[]] * self.obs.N self.converged = False self.user_param = user_param self.mat_mask = mat_mask self.isProgramError = False #============================================================================== # Initialise parameters #============================================================================== self.param = parameter(self.obs,user_param) self.param.rand_init(self.obs,self.user_param) #============================================================================== # Initialise auxillary variables #============================================================================== print 'Aux initialising ...' self.aux = aux_variable.aux_variable(self.obs,self.user_param) if self.obs.decomposed_u: self.aux.P_u = self.obs.P_u.copy() self.aux.Q_u = self.obs.Q_u.copy() self.aux.Q_u_inv = self.obs.Q_u_inv.copy() else: if self.obs.Sigma_u.shape[0] == self.obs.Sigma_u.shape[1]: self.aux.P_u,self.aux.Q_u,self.aux.Q_u_inv,self.param.delta_u = aux_variable.svd(self.obs.Sigma_u,self.user_param.R[0]) else: self.aux.P_u,self.aux.Q_u_inv,self.aux.Q_u,self.param.delta_u = aux_variable.svd(numpy.linalg.pinv(self.obs.Sigma_u.T)) self.aux.PQinvPt_u = self.aux.P_u.dot(self.aux.Q_u_inv).dot(self.aux.P_u.T) self.aux.inv_Sigma_u = self.aux.PQinvPt_u + numpy.diag(numpy.full((self.obs.D),self.param.delta_u)) self.aux.Sigma_u = aux_variable.matrix_inv(numpy.full((self.obs.D),self.param.delta_u),self.aux.P_u,self.aux.Q_u_inv) self.aux.logdet_Sigma_u = numpy.linalg.slogdet(self.aux.Sigma_u)[1] if self.obs.decomposed_v: self.aux.P_v = self.obs.P_v.copy() self.aux.Q_v = self.obs.Q_v.copy() self.aux.Q_v_inv = self.obs.Q_v_inv.copy() else: if self.obs.Sigma_v.shape[0] == self.obs.Sigma_v.shape[1]: self.aux.P_v,self.aux.Q_v,self.aux.Q_v_inv,self.param.delta_v = aux_variable.svd(self.obs.Sigma_v.T,self.user_param.R[1]) else: self.aux.P_v,self.aux.Q_v_inv,self.aux.Q_v,self.param.delta_v = aux_variable.svd(numpy.linalg.pinv(self.obs.Sigma_v.T)) self.aux.PQinvPt_v = self.aux.P_v.dot(self.aux.Q_v_inv).dot(self.aux.P_v.T) self.aux.inv_Sigma_v = self.aux.PQinvPt_v + numpy.diag(numpy.full((self.obs.G),self.param.delta_v)) self.aux.Sigma_v = aux_variable.matrix_inv(numpy.full((self.obs.G),self.param.delta_v),self.aux.P_v,self.aux.Q_v_inv) self.aux.logdet_Sigma_v = numpy.linalg.slogdet(self.aux.Sigma_v)[1] for n in xrange(self.obs.N): self.aux.R[n] = (self.param.Z[n] - aux_variable.get_F(self.param,n)) * self.mat_mask[n] # Signed zeros for k in xrange(self.user_param.K): self.aux.S_u[:,k] = numpy.diag(self.param.C_u[k]) self.aux.S_v[:,k] = numpy.diag(self.param.C_v[k]) #============================================================================== # Initialise warping fucntion object #============================================================================== self.warping = [[]] * self.obs.N for n in xrange(self.obs.N): self.warping[n] = warping_functions.composite_warping(self.user_param.I_list,self.user_param.func_type_list)