def _contour_t(mu, Cov, nu, axes=None, scale=4, transpose=False, colors='k'): """ """ if axes is None: axes = plt.gca() if np.shape(mu) != (2, ) or np.shape(Cov) != (2, 2) or np.shape(nu) != (): print(np.shape(mu), np.shape(Cov), np.shape(nu)) raise ValueError("Only 2-d t-distribution allowed") if transpose: mu = mu[[1, 0]] Cov = Cov[np.ix_([1, 0], [1, 0])] s = np.sqrt(np.diag(Cov)) x0 = np.linspace(mu[0] - scale * s[0], mu[0] + scale * s[0], num=100) x1 = np.linspace(mu[1] - scale * s[1], mu[1] + scale * s[1], num=100) X0X1 = misc.grid(x0, x1) Y = X0X1 - mu L = linalg.chol(Cov) logdet_Cov = linalg.chol_logdet(L) Z = linalg.chol_solve(L, Y) Z = linalg.inner(Y, Z, ndim=1) lpdf = random.t_logpdf(Z, logdet_Cov, nu, 2) p = np.exp(lpdf) shape = (np.size(x0), np.size(x1)) X0 = np.reshape(X0X1[:, 0], shape) X1 = np.reshape(X0X1[:, 1], shape) P = np.reshape(p, shape) return axes.contour(X0, X1, P, colors=colors)
def _contour_t(mu, Cov, nu, axes=None, scale=4, transpose=False, colors='k'): """ """ if axes is None: axes = plt.gca() if np.shape(mu) != (2,) or np.shape(Cov) != (2,2) or np.shape(nu) != (): print(np.shape(mu), np.shape(Cov), np.shape(nu)) raise ValueError("Only 2-d t-distribution allowed") if transpose: mu = mu[[1,0]] Cov = Cov[np.ix_([1,0],[1,0])] s = np.sqrt(np.diag(Cov)) x0 = np.linspace(mu[0]-scale*s[0], mu[0]+scale*s[0], num=100) x1 = np.linspace(mu[1]-scale*s[1], mu[1]+scale*s[1], num=100) X0X1 = misc.grid(x0, x1) Y = X0X1 - mu L = linalg.chol(Cov) logdet_Cov = linalg.chol_logdet(L) Z = linalg.chol_solve(L, Y) Z = linalg.inner(Y, Z, ndim=1) lpdf = random.t_logpdf(Z, logdet_Cov, nu, 2) p = np.exp(lpdf) shape = (np.size(x0), np.size(x1)) X0 = np.reshape(X0X1[:,0], shape) X1 = np.reshape(X0X1[:,1], shape) P = np.reshape(p, shape) return axes.contour(X0, X1, P, colors=colors)
def run(N=50, K=5, D=2): # Generate data N1 = np.floor(0.5*N) N2 = N - N1 y = np.vstack([np.random.normal(0, 0.5, size=(N1,D)), np.random.normal(10, 0.5, size=(N2,D))]) # Construct model Q = gaussianmix_model(N,K,D) # Observe data Q['Y'].observe(y) # Run inference Q.update(repeat=30) # Run predictive model zh = nodes.Categorical(Q['alpha'], name='zh') Yh = nodes.Mixture(zh, nodes.Gaussian, Q['X'], Q['Lambda'], name='Yh') zh.update() # Plot predictive pdf N1 = 400 N2 = 400 x1 = np.linspace(-3, 15, N1) x2 = np.linspace(-3, 15, N2) xh = misc.grid(x1, x2) lpdf = Yh.integrated_logpdf_from_parents(xh, 0) pdf = np.reshape(np.exp(lpdf), (N2,N1)) plt.clf() plt.contourf(x1, x2, pdf, 100) plt.scatter(y[:,0], y[:,1]) print('integrated pdf:', np.sum(pdf)*(18*18)/(N1*N2))
def contour(Z, x, y, n=None, **kwargs): """ Plot 2-D probability density function of a 2-D variable. Parameters ---------- Z : node or function Stochastic node or log pdf function x : array Grid points on x axis y : array Grid points on y axis """ XY = misc.grid(x, y) try: lpdf = Z.logpdf(XY) except AttributeError: lpdf = Z(XY) p = np.exp(lpdf) shape = (np.size(x), np.size(y)) X = np.reshape(XY[:,0], shape) Y = np.reshape(XY[:,1], shape) P = np.reshape(p, shape) if n is not None: levels = np.linspace(0, np.amax(P), num=n+2)[1:-1] return plt.contour(X, Y, P, levels, **kwargs) else: return plt.contour(X, Y, P, **kwargs)
def contour(Z, x, y, n=None, axes=None, fig=None, **kwargs): """ Plot 2-D probability density function of a 2-D variable. Parameters ---------- Z : node or function Stochastic node or log pdf function x : array Grid points on x axis y : array Grid points on y axis """ # TODO: Make it possible to plot a plated variable using _subplots function. if axes is None and fig is None: axes = plt.gca() else: if fig is None: fig = plt.gcf() axes = fig.add_subplot(111) XY = misc.grid(x, y) try: lpdf = Z.logpdf(XY) except AttributeError: lpdf = Z(XY) p = np.exp(lpdf) shape = (np.size(x), np.size(y)) X = np.reshape(XY[:, 0], shape) Y = np.reshape(XY[:, 1], shape) P = np.reshape(p, shape) if n is not None: levels = np.linspace(0, np.amax(P), num=n + 2)[1:-1] return axes.contour(X, Y, P, levels, **kwargs) else: return axes.contour(X, Y, P, **kwargs)
def contour(Z, x, y, n=None, axes=None, fig=None, **kwargs): """ Plot 2-D probability density function of a 2-D variable. Parameters ---------- Z : node or function Stochastic node or log pdf function x : array Grid points on x axis y : array Grid points on y axis """ # TODO: Make it possible to plot a plated variable using _subplots function. if axes is None and fig is None: axes = plt.gca() else: if fig is None: fig = plt.gcf() axes = fig.add_subplot(111) XY = misc.grid(x, y) try: lpdf = Z.logpdf(XY) except AttributeError: lpdf = Z(XY) p = np.exp(lpdf) shape = (np.size(x), np.size(y)) X = np.reshape(XY[:,0], shape) Y = np.reshape(XY[:,1], shape) P = np.reshape(p, shape) if n is not None: levels = np.linspace(0, np.amax(P), num=n+2)[1:-1] return axes.contour(X, Y, P, levels, **kwargs) else: return axes.contour(X, Y, P, **kwargs)