def test_numpy_rvs_shape_compatibility(self): np.random.seed(2846) alpha = np.array([1.0, 2.0, 3.0]) x = np.random.dirichlet(alpha, size=7) assert_equal(x.shape, (7, 3)) assert_raises(ValueError, dirichlet.pdf, x, alpha) assert_raises(ValueError, dirichlet.logpdf, x, alpha) dirichlet.pdf(x.T, alpha) dirichlet.pdf(x.T[:-1], alpha) dirichlet.logpdf(x.T, alpha) dirichlet.logpdf(x.T[:-1], alpha)
def test_point_to_pdf_vectorization(self): """Test point_to_pdf. Check vectorization of the computation of the pdf. """ n_points = 2 points = self.dirichlet.random_point(n_points) pdfs = self.dirichlet.point_to_pdf(points) alpha = gs.ones(self.dim) samples = self.dirichlet.sample(alpha, self.n_samples) result = pdfs(samples) pdf1 = [dirichlet.pdf(x, points[0, :]) for x in samples] pdf2 = [dirichlet.pdf(x, points[1, :]) for x in samples] expected = gs.stack([gs.array(pdf1), gs.array(pdf2)], axis=0) self.assertAllClose(result, expected)
def dpdf(v1, v2, alphavec): if (v1 + v2) > 1: out = np.nan else: vec = v1 * Corner1 + v2 * Corner2 + (1.0 - v1 - v2) * Corner3 out = dirichlet.pdf(vec, alphavec) return (out)
def test_point_to_pdf(self): """Test point_to_pdf. Check the computation of the pdf. """ point = self.dirichlet.random_point() pdf = self.dirichlet.point_to_pdf(point) alpha = gs.ones(self.dim) samples = self.dirichlet.sample(alpha, self.n_samples) result = pdf(samples) expected = [dirichlet.pdf(x, point) for x in samples] self.assertAllClose(result, expected)
def test_point_to_pdf(self, dim, point, n_samples): point = gs.to_ndarray(point, 2) n_points = point.shape[0] pdf = self.space(dim).point_to_pdf(point) alpha = gs.ones(dim) samples = self.space(dim).sample(alpha, n_samples) result = pdf(samples) pdf = [] for i in range(n_points): pdf.append( gs.array([dirichlet.pdf(x, point[i, :]) for x in samples])) expected = gs.squeeze(gs.stack(pdf, axis=0)) self.assertAllClose(result, expected)
def test_frozen_dirichlet(): np.random.seed(2846) n = np.random.randint(1, 32) alpha = np.random.uniform(10e-10, 100, n) d = dirichlet(alpha) assert_equal(d.var(), dirichlet.var(alpha)) assert_equal(d.mean(), dirichlet.mean(alpha)) assert_equal(d.entropy(), dirichlet.entropy(alpha)) num_tests = 10 for i in range(num_tests): x = np.random.uniform(10e-10, 100, n) x /= np.sum(x) assert_equal(d.pdf(x[:-1]), dirichlet.pdf(x[:-1], alpha)) assert_equal(d.logpdf(x[:-1]), dirichlet.logpdf(x[:-1], alpha))
def pdf(x): """Generate parameterized function for normal pdf. Parameters ---------- x : array-like, shape=[n_points, dim] Points of the simplex at which to compute the probability density function. Returns ------- pdf_at_x : array-like, shape=[..., n_points] Values of pdf at x for each value of the parameters provided by point. """ pdf_at_x = [] for param in point: pdf_at_x.append(gs.array([dirichlet.pdf(pt, param) for pt in x])) pdf_at_x = gs.squeeze(gs.stack(pdf_at_x, axis=0)) return pdf_at_x
def test_alpha_correct_depth(self): alpha = np.array([1.0, 2.0, 3.0]) x = np.ones((3, 7)) / 3 dirichlet.pdf(x, alpha) dirichlet.logpdf(x, alpha)
import numpy as np from scipy.stats import dirichlet for x in np.linspace(.1, 3, 20): print(x, dirichlet.pdf(np.array([.9, .05, .05]), np.ones(3) / x))
def prior_density(x): assert x.shape[1] == m, "wrong dimension" output = np.zeros(x.shape[0]) for i in range(len(output)): output[i] = dirichlet.pdf(x=x[i], alpha=np.ones((m,))) return output.reshape(-1)
def integrand(p1, p2, b1, b2, b3): if b3 == 0: #return 5000000*p1**(b1-1) * p2**(b2-1) return dirichlet.pdf([p1, 1-p1], [b1, b2]) #return 5000000*p1**(b1-1) * p2**(b2-1) * (1-p1-p2)**(b3-1) return dirichlet.pdf([p1, p2, 1-p1-p2], [b1, b2, b3])
quantiles = np.vstack((quantiles_av, quantiles_op)) alpha1 = np.array([0.1, 5]) alpha2 = np.array([5, 0.1]) alpha3 = np.array([0.1, 0.1]) alpha4 = np.array([5, 5]) alpha5 = np.array([2, 4]) alpha6 = np.array([1, 1]) res1 = np.zeros([1, quantiles.shape[1]]) res2 = np.zeros([1, quantiles.shape[1]]) res3 = np.zeros([1, quantiles.shape[1]]) res4 = np.zeros([1, quantiles.shape[1]]) res5 = np.zeros([1, quantiles.shape[1]]) res6 = np.zeros([1, quantiles.shape[1]]) for i in range(quantiles.shape[1]): res1[0][i] = dirichlet.pdf(quantiles[:, i], alpha1) res2[0][i] = dirichlet.pdf(quantiles[:, i], alpha2) res3[0][i] = dirichlet.pdf(quantiles[:, i], alpha3) res4[0][i] = dirichlet.pdf(quantiles[:, i], alpha4) res5[0][i] = dirichlet.pdf(quantiles[:, i], alpha5) res6[0][i] = dirichlet.pdf(quantiles[:, i], alpha6) res1 = res1 / np.sum(res1) res2 = res2 / np.sum(res2) res3 = res3 / np.sum(res3) res4 = res4 / np.sum(res4) res5 = res5 / np.sum(res5) res6 = res6 / np.sum(res6) plt.plot(quantiles_av, res1[0], label=r'$\alpha=[0.1, 5]$') plt.plot(quantiles_av, res2[0], label=r'$\alpha=[5, 0.1]$') plt.plot(quantiles_av, res3[0], label=r'$\alpha=[0.1, 0.1]$')
def Beta(y): return dirichlet.pdf([y, 1 - y], alpha)
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
def pdf(self): """P(theta | alpha): parallel to ft_binomial.""" return lambda x: dirichlet.pdf(x, self.alpha)
# \end{equation} # # # In[15]: # http://blog.bogatron.net/blog/2014/02/02/visualizing-dirichlet-distributions/ from scipy.stats import dirichlet import matplotlib.pyplot as plt import numpy as np import matplotlib.tri as tri quantiles = np.array([0.2, 0.2, 0.6]) # specify quantiles alpha = np.array([0.4, 5, 15]) # specify concentration parameters dirichlet.pdf(quantiles, alpha) # In[9]: corners = np.array([[0, 0], [1, 0], [0.5, 0.75**0.5]]) triangle = tri.Triangulation(corners[:, 0], corners[:, 1]) # Mid-points of triangle sides opposite of each corner midpoints = [(corners[(i + 1) % 3] + corners[(i + 2) % 3]) / 2.0 for i in range(3)] def xy2bc(xy, tol=1.e-3): '''Converts 2D Cartesian coordinates to barycentric.''' s = [(corners[i] - midpoints[i]).dot(xy - midpoints[i]) / 0.75 for i in range(3)] return np.clip(s, tol, 1.0 - tol)
import numpy as np from scipy.stats import dirichlet import matplotlib.pyplot as plt import matplotlib.tri as mtri plt.rcParams['figure.figsize'] = (8, 8) # In[152]: x = np.linspace(0, 1, 100) y = np.linspace(0, 1, 100) xx, yy = np.meshgrid(x, y) zz = 1 - xx - yy zz temp_loc = np.dstack((xx, yy, zz)) loc = temp_loc[(zz > 0) & (xx > 0) & (yy > 0)] # In[167]: alpha = np.array([0.1, 0.1, 0.1]) new_pos = loc[0, :] new_pos density = [dirichlet.pdf(single_loc, alpha) for single_loc in loc] # In[163]: triang = mtri.Triangulation(loc[:, 0], loc[:, 1]) # In[168]: plt.tricontourf(triang, density)
''' @Author: Runsen @微信公众号: 润森笔记 @博客: https://blog.csdn.net/weixin_44510615 @Date: 2020/5/10 ''' from scipy.stats import beta, dirichlet import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 1, 100) a_array = [1, 2, 5] b_array = [1, 2, 5] fig, axarr = plt.subplots(len(a_array), len(b_array)) for i, a in enumerate(a_array): for j, b in enumerate(b_array): axarr[i, j].plot(x, beta.pdf(x, a, b), 'r', lw=1, alpha=0.6, label='a=' + str(a) + ',b=' + str(b)) axarr[i, j].legend(loc='upper left', fontsize=8) plt.show() print(dirichlet.pdf([0.6, 0.3, 0.1], [3, 2, 1]))
def pdf(self, x): x=x/x.sum() # enforce simplex constraint return dirichlet.pdf(x=x,alpha=self._alpha)
def fun(x, y): quantiles = np.array([x, y, 1 - x - y]) z = dirichlet.pdf(quantiles, alpha) return z