def NK_priors(para): #([0.8,0.8,1.6000,.8000,.99,1.4,0.016,0.0183,1.6700,0.3,1.01,.117,.032,.9,.85,1.15,.993,1.17]) para = cp.asnumpy(para) shape = np.zeros(11) scale = np.zeros(11) c = np.zeros(2) d = np.zeros(2) mu = np.array([.125, .015, 0.031, .2, .3, 1.2, .99, .5, 1.6, .2, .15]) sig = np.array([.09, .011, .022, .1, .2, .15, .001, .2, .15, .15, .1]) for i in range(11): shape[i] = (mu[i] / sig[i])**2 scale[i] = sig[i]**2 / mu[i] shape[3] = (mu[3] / sig[3])**2 + 2 scale[3] = (shape[3] - 1) * mu[3] c[0] = (mu[6]**2) * ((1 - mu[6]) / ((sig[6])**2) - (1 / mu[6])) d[0] = c[0] * ((1 / mu[6]) - 1) c[1] = (mu[7]**2) * ((1 - mu[7]) / ((sig[7])**2) - (1 / mu[7])) d[1] = c[1] * ((1 / mu[7]) - 1) P1 = gamma.logpdf((1 / para[1]), a=shape[0], scale=scale[0]) P6 = gamma.logpdf(para[6], a=shape[1], scale=scale[1]) P5 = gamma.logpdf(para[5], a=shape[4], scale=scale[4]) P10 = gamma.logpdf(para[10], a=shape[5], scale=scale[5]) P2 = gamma.logpdf(para[2], a=shape[8], scale=scale[8]) P3 = gamma.logpdf(para[3], a=shape[9], scale=scale[9]) P7 = gamma.logpdf(para[7], a=shape[2], scale=scale[2]) P9 = norm.logpdf(para[9], .3, .05**2) # P4 = gamma.logpdf(para[4],a=shape[6],scale=.001) # muU = .99 # sigU = .01 # alphaaz = ((1-muU)/sigU** - 1/muU)*muU**2 # Betaaz = alphaaz*(1/muU-1) # P4 = beta.logpdf(para[4],alphaaz,Betaaz) P13 = gamma.logpdf(para[13], a=shape[7], scale=scale[7]) shape[3] = (mu[3] / sig[3])**2 - 2 scale[3] = (shape[3] - 1) * (mu[3]) shape[10] = (mu[10] / sig[10])**2 - 2 scale[10] = (shape[10] - 1) * (mu[10]) P8 = invgamma.logpdf(para[8], a=shape[3], scale=scale[3]) P15 = invgamma.logpdf(para[15], a=shape[3], scale=scale[3]) P16 = invgamma.logpdf(para[16], a=shape[3], scale=scale[3]) #np.log(np.exp(np.log(2) - np.log(math.gamma(.5*b)) + .5*b*np.log(b*(a**2)*.5) - ((b+1)*.5)*np.log(x**2) - (b*a**2)*(x**-2)*.5)) prioc = P1 + P6 + P2 + P3 + P9 + P5 + P10 + P8 + P15 + P16 + P13 + P7 #+P4 return prioc
def simple_model_prior_logpdf(values): means = np.sum([uniform_logpdf(val, START, END) for val in values[components:(2*components)]]) scales = np.sum([invgamma.logpdf(val, A, LOC, SCALE) for val in values[(2*components):(3*components)]]) weights = multivariate_normal.logpdf(values[:components], mean=np.zeros(components), cov=COV_ALPHA*np.eye(components)) return weights + means + scales
def test_inverse_gamma_log_pdf(self): from scipy.stats import invgamma alpha = 3.0 beta = 2.0 for x in np.linspace(0.001,10,25): expected = invgamma.logpdf(x,alpha,scale=beta) got = inverse_gamma_log_pdf(x,alpha,beta) # print x,got,expected self.assertAlmostEqual(got,expected,places=6)
def simple_model_prior_logpdf(values): means = np.sum([ uniform_logpdf(val, START, END) for val in values[components:(2 * components)] ]) scales = np.sum([ invgamma.logpdf(val, A, LOC, SCALE) for val in values[(2 * components):(3 * components)] ]) weights = multivariate_normal.logpdf(values[:components], mean=np.zeros(components), cov=COV_ALPHA * np.eye(components)) return weights + means + scales
def __calc_likelihood(self): self.__calc_pdf() lh = np.sum(self.pdf, axis=0) eps = 1e-300 lh = np.where(lh < eps, eps, lh) logsumlh = np.sum(np.log(lh)) if self.map_sigma: lh_map = np.sum( invgamma.logpdf(np.diag(self.__sigma), self.alpha, scale=self.beta)) return logsumlh + lh_map else: return logsumlh
def log_like(x, m_z_stack, C_z_stack): """ Calculate the log-likelihood for a set of parameters x. """ A = m_z_stack[:, 0].flat G = m_z_stack[:, 1].flat h = x[:2].reshape(2) v = x[2] a_var = 100 J = np.eye(2, dtype=float) J[1, 0] = h[1] - h[0] C_1 = J.dot(np.array([[a_var, 0.0], [0.0, v]], dtype=float)).dot(J.T) C = C_1 + C_z_stack C_inv = np.linalg.inv(C) m1 = (h[1] - h[0]) * A + h[0] w = -0.5 * (m1 - G)**2 * C_inv[:, 1, 1] sign, logdet = np.linalg.slogdet(C) w += -0.5 * sign * logdet w = np.sum(w) # # Priors v_a_p = 0.01 v_b_p = 0.01 w += invgamma.logpdf(v, a=v_a_p, scale=v_b_p) # Correlated prior off_diag = np.where(~np.eye(2, dtype=bool)) corr = np.eye(2, dtype=float) corr[off_diag] = 0.5 std = 100 C_prior = np.full((2, 2), std, dtype=float) C_prior *= C_prior.T C_prior *= corr h_m_p = np.zeros(2, dtype=float) h_cov_p = C_prior w += multivariate_normal.logpdf(h, mean=h_m_p, cov=h_cov_p) if w == np.inf: root_logger.debug("Likelihood too big...") exit() return w
def log_prior(self): """ Compute the prior probability of F, mu0, and lmbda """ from graphistician.internals.utils import \ normal_inverse_wishart_log_prob, \ inverse_wishart_log_prob lp = 0 # Log prior of F under spherical Gaussian prior from scipy.stats import norm, invgamma lp += invgamma.logpdf(self.eta, 1, 1) lp += norm.logpdf(self.b, 0, 1) lp += norm.logpdf(self.L, 0, 1).sum() lp += inverse_wishart_log_prob(self.cov) lp += normal_inverse_wishart_log_prob(self._self_gaussian) # Log prior of mu_0 and mu_self return lp
def test_logpdf(self): shapes = np.arange(0.1, 20, 0.5) scales = np.arange(0.1, 10, 0.5) xs = np.arange(0.001, 10, 0.5) test_params = cartesian([xs, shapes, scales]).astype(np.float64) python_results = np.zeros(test_params.shape[0]) for ind in range(test_params.shape[0]): x = test_params[ind, 0] shape = test_params[ind, 1] scale = test_params[ind, 2] python_results[ind] = invgamma.logpdf(x, shape, scale=scale) opencl_results = invgamma_logpdf().evaluate( { 'x': test_params[:, 0], 'shape': test_params[:, 1], 'scale': test_params[..., 2] }, test_params.shape[0]) assert_allclose(opencl_results, python_results, atol=1e-7, rtol=1e-7)
def linvgamma(x,a,beta): return invgamma.logpdf(x,a,scale=beta)
def compute_marg_likelihood_and_NSE_galaxies(y, iters, init, hypers): ''' Compute the marginal likelihood from the Gibbs Sampler output according to Chib (1995) y : (array-like) endogeneous variables iters: (int) length of the MCMC init: (array-like) initialisation parameters hypers: (array-like) hyper-parameters returns: (float) the marginal likelihood/normalizing constant ''' # Initialisation d = init['d'] mu_params, sigma_square_params, q_params = init['mu_params'], init[ 'sigma_square_params'], init['q_params'] mu, sigma_square, q, mu_hat, B, n_for_estim_sigma, delta, n_for_estim_q = GibbsSampler_galaxies( y, iters, init, hypers) mu_star = np.array(mu).mean(axis=0) sigma_square_star = np.array(sigma_square).mean(axis=0) q_star = np.array(q).mean(axis=0) ## Marginal likelihood computation P7, right column # First term: y_given_mu_and_sigma2_stars_pdf = np.stack([ norm.pdf(x=y, loc=mu_star[i], scale=sigma_square_star[i]) for i in range(d) ])[:, :, 0].T log_like = np.log( (q_star * y_given_mu_and_sigma2_stars_pdf).sum(axis=1)).sum() # Second term mu_prior = multivariate_normal.logpdf( x=mu_star, mean=mu_params[0], cov=mu_params[1]).sum( ) # Sum because of a the use of logpdf instead of pdf sigma_square_prior = invgamma.logpdf(x=sigma_square_star, a=sigma_square_params[0], scale=np.sqrt( sigma_square_params[1])).sum() q_square_prior = dirichlet.logpdf(x=q_star, alpha=q_params).sum() log_prior = mu_prior + sigma_square_prior + q_square_prior # Third term conditional_densities_mu = np.array([ np.prod(multivariate_normal.pdf(x=mu_star, mean=mu_hat[i], cov=B[i])) for i in range(iters) ]) conditional_densities_sigma = np.array([np.prod(invgamma.pdf(x=sigma_square_star, a=(sigma_square_params[0]+n_for_estim_sigma[i])/2,\ scale=(sigma_square_params[1]+delta[i])/2)) for i in range(iters)]) conditional_densities_q = np.array([ dirichlet.pdf(x=q_star, alpha=q_params + n_for_estim_q[i]) for i in range(iters) ]) conditional_densities = conditional_densities_mu * conditional_densities_sigma * conditional_densities_q log_posterior = np.log(conditional_densities.mean()) log_marg_likelihood = log_like + log_prior - log_posterior #Numerical Standard Error Computation h = np.array([ conditional_densities_mu, conditional_densities_sigma, conditional_densities_q ]) h_hat = np.array([ np.mean(conditional_densities_mu), np.mean(conditional_densities_sigma), np.mean(conditional_densities_q) ]) var = compute_var_h_hat(h, h_hat) NSE = np.dot(np.dot((1 / h_hat).reshape(1, -1), var), (1 / h_hat).reshape(-1, 1))[0, 0] return log_marg_likelihood, NSE
loglik = np.zeros((len(phi_grid), 10)) for r, phi_c in enumerate(phi_grid): for c in range(10): loglik[r,c] = stocVol_bootstrap(y, N, phi=phi_c, sigma=sigma, beta=beta)['loglik'] _ = plt.boxplot(loglik.T, labels = phi_grid) _ = plt.title("Ten loglik estimates (y-axis) per phi value (x-axis)") # %% [markdown] # # H.3 (b) # According to this webpage we get the equivalent InvGamma distribution by writing the following: # `scipy.stats.invgamma(alpha, loc=0, scale=beta)` # https://distribution-explorer.github.io/continuous/inverse_gamma.html #%% logprior_sigma2 = lambda sigma2 :invgamma.logpdf(sigma2, 0.01, loc=0, scale=0.01) logprior_beta2 = lambda beta2 :invgamma.logpdf(beta2, 0.01, loc=0, scale=0.01) def compute_logprob(sigma2, beta2, N): loglik = stocVol_bootstrap(y, N, phi=phi, sigma=np.sqrt(sigma2), beta=np.sqrt(beta2))['loglik'] return loglik + logprior_sigma2(sigma2) + logprior_beta2(beta2) num_samples = 2000 N = 100 step_size = 0.008 trace = { 'sigma2' : np.zeros((num_samples,)), 'beta2' : np.zeros((num_samples,)), 'posterior' : np.zeros((num_samples,)), 'accept_ratio' : np.zeros((num_samples,)), }
def AdPMCMC_M1(NSteps, T, AdaptiveRate, Y, L, N_0): # draw uniforms for accept-reject procedure Uniforms = np.random.uniform(0, 1, NSteps) # inverse gamma prior hyperparameters AlphaEps = AlphaW = T/2 BetaEps = BetaW = 2*(AlphaEps - 1)/10 # trajectory history NHist = [] # Parameters initialization B_0 = np.random.normal(0, 1) B_1 = np.random.normal(0, 1) SigmaEps = np.sqrt(invgamma.rvs(a=AlphaEps, scale=BetaEps)) SigmaW = np.sqrt(invgamma.rvs(a=AlphaW, scale=BetaW)) N_0 = N_0 Theta = np.array([B_0, B_1, SigmaEps, SigmaW, N_0]) ThetaHist = [Theta] for j in tqdm(range(NSteps), desc="Running AdPMCMC"): # draw candidate theta wrt non adaptive proposal if np.random.rand() < 1 - AdaptiveRate or j < 500: ThetaProp = non_adaptive_theta_proposal(Theta) else: ThetaProp = adaptive_theta_proposal(ThetaHist) # unpack theta B_0, B_1, SigmaEps, SigmaW, N_0 = ThetaProp # To avoid negative std deviation errors SigmaEps, SigmaW = np.abs(SigmaEps), np.abs(SigmaW) ThetaProp = np.array([B_0, B_1, SigmaEps, SigmaW, N_0]) # run SMC LogParticles, LogW, LogMarginalLikelihood = SMC( Y, T, L, N_0, 'M1', SigmaEps, SigmaW, B_0, B_1) # sample a candidate path idx = np.random.choice(a=L, size=1, p=np.exp(LogW[-1,:]))[0] CandidatePath = LogParticles[:, idx] # compute acceptance proba if j == 0: # always accept first draw (initialization) alpha = 1 else: # last accepted theta PrevB_0, PrevB_1, PrevSigmaEps, PrevSigmaW, PrevN_0 = Theta # SMC marginal likelihood ratio MarginalLikelihoodRatio = np.exp(LogMarginalLikelihood - PrevLogMarginalLikelihood) # b_0 prior ratio MarginalB_0_ratio = np.exp(0.5*(PrevB_0**2 - B_0**2)) # b_1 prior ratio MarginalB_1_ratio = np.exp(0.5*(PrevB_1**2 - B_1**2)) # sigma_eps prior ratio MarginalEpsRatio = np.exp(invgamma.logpdf(x=SigmaEps**2, a=AlphaEps, scale=BetaEps) - invgamma.logpdf(x=PrevSigmaEps**2, a=AlphaEps, scale=BetaEps)) # sigma_w prior ratio MarginalWRatio = np.exp(invgamma.logpdf(x=SigmaW**2, a=AlphaW, scale=BetaW) - invgamma.logpdf(x=PrevSigmaW**2, a=AlphaW, scale=BetaW)) alpha = (MarginalLikelihoodRatio * MarginalB_0_ratio * MarginalB_1_ratio * MarginalEpsRatio * MarginalWRatio) # accept-reject if Uniforms[j] < alpha: Theta = ThetaProp PrevLogMarginalLikelihood = LogMarginalLikelihood ThetaHist.append(Theta) NHist.append(CandidatePath) if j==1000 and len(ThetaHist) < 5 : raise ValueError("Too few accepted samples") print("Number of samples : {}".format(len(ThetaHist))) return ThetaHist
def _logp(self, value, shape, scale): return np.sum(invgamma.logpdf(value, shape, scale=scale))
def log_likelihood(self, x_list, params=None, **kwargs): params = self.params_handler('load', params, **kwargs) ll = 0 for x in x_list: ll += invgamma.logpdf(x, params.shape, 0.0, params.scale) return ll