def test_ellipse_plotter(): f = plt.figure(figsize=(7, 7)) for i, Sig in enumerate([np.array([[1.0, 0.0], [0.0, 0.25]]), np.array([[0.25, 0.0], [0.0, 1.0]]), np.array([[1.0, 0.8], [0.8, 1.0]]), np.array([[1.0, -0.8], [-0.8, 1.0]])]): ax = f.add_subplot(2, 2, i+1) ax.set_xlim(-1, 1) ax.set_ylim(-1, 1) plot_ellipse([0., 0.], Sig) ax.set_title("$\Sigma$={}".format(Sig)) f.tight_layout() f.savefig("plots/ellipse.png")
def test_NormInvWish(): mu_0 = np.r_[0.3, -0.2] d = len(mu_0) Lam_0 = np.linalg.inv(np.array([[2, 1.1], [1.1, 1.2]])) kappa_0 = 2.1 nu_0 = 8 model = dpmm.NormInvWish(mu_0, kappa_0, Lam_0, nu_0) # First check some numerics Nsample = 5000 samples = model.sample(size=Nsample) mu_samples = [s[0] for s in samples] cov_samples = [s[1] for s in samples] mean = np.mean(mu_samples, axis=0) std = np.std(mu_samples, axis=0)/np.sqrt(Nsample) print "NormInvWish mu_0 = {}".format(mu_0) print "NormInvWish E(mu) = {} +/- {}".format(mean, std) mean_cov = np.mean(cov_samples, axis=0) std_cov = np.std(cov_samples, axis=0)/np.sqrt(Nsample) print "NormInvWish (Lam_0)^(-1)/(nu_0-d-1) = \n{}".format(np.linalg.inv(Lam_0)/(nu_0-d-1)) print "NormInvWish E(Sig) = \n{}\n +/-\n{}".format(mean_cov, std_cov) # Now try some plots with different values of kappa_0 and nu_0 f = plt.figure(figsize=(7, 7)) for i, (kappa_0, nu_0) in enumerate(zip([0.4, 0.4, 6.5, 6.5], [10, 4, 10, 4])): model = dpmm.NormInvWish(mu_0, kappa_0, Lam_0, nu_0) samples = model.sample(size=25) ax = f.add_subplot(2, 2, i+1) for sample in samples: mu, Sig = sample plot_ellipse(mu, Sig, ax=ax, facecolor='none', edgecolor='k', alpha=0.2) plot_ellipse(mu_0, np.linalg.inv(Lam_0)/(nu_0-d-1), ax=ax, facecolor='none', edgecolor='r') ax.set_xlim(-3, 3) ax.set_ylim(-3, 3) ax.axvline(mu_0[0], c='r', alpha=0.1) ax.axhline(mu_0[1], c='r', alpha=0.1) ax.set_title(r"$\kappa_0$={}, $\nu_0$={}".format(kappa_0, nu_0)) print np.mean([s[1] for s in samples], axis=0) f.savefig("plots/NormInvWish_samples.png")