def integrand(y, x): #centered on x0,y0, delta around that e1 = np.exp(pymc.lognormal_like(y+y0, np.log(m*(x+x0)), 1./sig**2)) e2 = np.exp(pymc.mv_normal_cov_like(np.array([x,y]), np.array([0,0]), cov)) return e1*e2
def smooth_rate(f=rate, age_indices=age_indices, C=C): log_rate = pl.log(pl.maximum(f, NEARLY_ZERO)) return mc.mv_normal_cov_like(log_rate[age_indices] - log_rate[age_indices].mean(), pl.zeros_like(age_indices), C=C)
def fit(self, N, tol=.01): """ Stores approximate likelihood parameters mu, V and returns approximation of log p(D). """ # Make initial observations (usually trivial) self.C = self.C_pri.copy() self.M = self.M_pri.copy() self.coefs = simps_coefs(N) for i in xrange(self.Nx): self.observe(i) if np.any(np.isinf(self.C)) or np.any(np.isinf(self.M)): # Pdb(color_scheme='Linux').set_trace() raise RuntimeError, 'C or M is infinite.' if np.any(np.diag(self.C)<0): # Pdb(color_scheme='Linux').set_trace() raise RuntimeError, 'C has negative diagonal.' # To help convergence, use same iid normal samples # in all iterations. # self.normal_samps = np.random.normal(size=N_samps) # self.nug_samps = np.random.normal(size=N_samps) # Record last values of mu and V to assess convergence. last_mu = np.copy(self.mu) last_V = np.copy(self.V) sw = 0 dmu = np.Inf dV = np.Inf # print self.mu, self.V while max(dmu, dV) > tol: sw += 1 # Update self.update_sweep(N) # Assess convergence and possibly terminate. dmu = np.max(np.abs((self.mu - last_mu)/self.mu)) if np.isnan(dmu): dmu = np.max(np.abs((self.mu - last_mu)*10)) dV = np.max(np.abs((self.V - last_V)/self.V)) if sw > 10000 and sw % 100 == 0: print 'Too many iterations, randomizing.' # Pdb(color_scheme='Linux').set_trace() print 'mu: %s'%self.mu print 'V: %s'%self.V self.V = last_V + np.random.random(size=len(self.V)) * dV * self.V self.mu = last_mu + np.random.random(size=len(self.mu)) * dmu * self.mu last_V[:] = self.V[:] last_mu[:] = self.mu[:] # print self.mu, self.V V = np.array((self.V+self.nug)) V_ind = V + np.diag(self.C_pri) C_joint = self.C_pri+np.diag(V) if np.all(V_ind>0): joint_term = pm.mv_normal_cov_like(self.mu, self.M_pri, C_joint) ind_term = pm.normal_like(self.mu, self.M_pri, 1/V_ind) log_ratio = joint_term-ind_term # Protect against negative 'variances' (which are acceptable) else: V = V.astype('complex') V_ind = V_ind.astype('complex') C_joint = C_joint.astype('complex') dev=self.mu-self.M_pri ind_term = -.5*np.sum((np.log(2.*np.pi*V_ind) + dev**2/V_ind)) val,vec = np.linalg.eig(C_joint) sq = np.asarray(np.dot(dev,vec/np.sqrt(val))).ravel() joint_term = -.5*(np.sum((np.log(2.*np.pi*val))) + np.dot(sq, sq)) log_ratio = np.real(joint_term-ind_term) if np.sum(self.lp) + log_ratio > 10000: print 'Warning, HUGE p' if np.any(np.isnan(log_ratio)): from IPython.Debugger import Pdb Pdb(color_scheme='Linux').set_trace() return np.sum(self.lp) + log_ratio
def parent_similarity(x=model.vars[data_type]['mu_age'], mu=mu, C=C, knots=knots): return mc.mv_normal_cov_like(x[knots], mu[knots], C[:, knots][knots, :])
def fit(self, N, tol=.01): """ Stores approximate likelihood parameters mu, V and returns approximation of log p(D). """ # Make initial observations (usually trivial) self.C = self.C_pri.copy() self.M = self.M_pri.copy() self.coefs = simps_coefs(N) for i in xrange(self.Nx): self.observe(i) if np.any(np.isinf(self.C)) or np.any(np.isinf(self.M)): # Pdb(color_scheme='Linux').set_trace() raise RuntimeError, 'C or M is infinite.' if np.any(np.diag(self.C) < 0): # Pdb(color_scheme='Linux').set_trace() raise RuntimeError, 'C has negative diagonal.' # To help convergence, use same iid normal samples # in all iterations. # self.normal_samps = np.random.normal(size=N_samps) # self.nug_samps = np.random.normal(size=N_samps) # Record last values of mu and V to assess convergence. last_mu = np.copy(self.mu) last_V = np.copy(self.V) sw = 0 dmu = np.Inf dV = np.Inf # print self.mu, self.V while max(dmu, dV) > tol: sw += 1 # Update self.update_sweep(N) # Assess convergence and possibly terminate. dmu = np.max(np.abs((self.mu - last_mu) / self.mu)) if np.isnan(dmu): dmu = np.max(np.abs((self.mu - last_mu) * 10)) dV = np.max(np.abs((self.V - last_V) / self.V)) if sw > 10000 and sw % 100 == 0: print 'Too many iterations, randomizing.' # Pdb(color_scheme='Linux').set_trace() print 'mu: %s' % self.mu print 'V: %s' % self.V self.V = last_V + np.random.random( size=len(self.V)) * dV * self.V self.mu = last_mu + np.random.random( size=len(self.mu)) * dmu * self.mu last_V[:] = self.V[:] last_mu[:] = self.mu[:] # print self.mu, self.V V = np.array((self.V + self.nug)) V_ind = V + np.diag(self.C_pri) C_joint = self.C_pri + np.diag(V) if np.all(V_ind > 0): joint_term = pm.mv_normal_cov_like(self.mu, self.M_pri, C_joint) ind_term = pm.normal_like(self.mu, self.M_pri, 1 / V_ind) log_ratio = joint_term - ind_term # Protect against negative 'variances' (which are acceptable) else: V = V.astype('complex') V_ind = V_ind.astype('complex') C_joint = C_joint.astype('complex') dev = self.mu - self.M_pri ind_term = -.5 * np.sum( (np.log(2. * np.pi * V_ind) + dev**2 / V_ind)) val, vec = np.linalg.eig(C_joint) sq = np.asarray(np.dot(dev, vec / np.sqrt(val))).ravel() joint_term = -.5 * (np.sum( (np.log(2. * np.pi * val))) + np.dot(sq, sq)) log_ratio = np.real(joint_term - ind_term) if np.sum(self.lp) + log_ratio > 10000: print 'Warning, HUGE p' if np.any(np.isnan(log_ratio)): from IPython.Debugger import Pdb Pdb(color_scheme='Linux').set_trace() return np.sum(self.lp) + log_ratio
def smooth_rate(f=rate, age_indices=age_indices, C=C): log_rate = np.log(f + 1.e-8) return mc.mv_normal_cov_like(log_rate[age_indices], -10*np.ones_like(age_indices), C=C)
def parent_similarity(x=model.vars[data_type]['mu_age'], mu=mu, C=C, knots=knots): return mc.mv_normal_cov_like(x[knots], mu[knots], C[:,knots][knots,:])
def true_evidence(Q=Q, M=M, vals=vals, vars=vars): C = np.array(Q.todense().I + np.diag(vars)) return pm.mv_normal_cov_like(vals, M, C)
def true_evidence(Q=Q, M=M, vals=vals, vars=vars): C = np.array(Q.todense().I+np.diag(vars)) return pm.mv_normal_cov_like(vals, M, C)