Ejemplo n.º 1
0
 def test_beta_approx_1d(self):
     x0 = np.array([0.9])
     true_dist = beta(1000, 1000)
     approx = approx_dist(true_dist.logpdf, x0)
     self.assertAlmostEqual(float(approx.mean[0]), true_dist.mean())
     self.assertAlmostEqual(float(approx.cov[0]),
                            true_dist.var(),
                            delta=1e-5)
Ejemplo n.º 2
0
    def test_binomial_observation_posterior(self):
        x0 = np.array([0.25])

        def posterior_logpdf(v):
            if 0 < v < 1:
                return binom.logpmf(2000, 3000, v)
            return -np.inf

        approx = approx_dist(posterior_logpdf, x0)
        true_dist = beta(2001, 1001)
        self.assertAlmostEqual(float(approx.mean[0]),
                               true_dist.mean(),
                               delta=1e-3)
        self.assertAlmostEqual(float(approx.cov[0]),
                               true_dist.var(),
                               delta=1e-5)
Ejemplo n.º 3
0
    def test_linear_regression_posterior(self):
        def lin_regression_lnprob(v, x=None, y=None):
            a, b, s = v
            if s <= 0:
                return -np.inf
            y_hat = a + b * x
            lp = np.sum(norm.logpdf(y, y_hat, s))
            return lp

        x = np.linspace(0, 5000, 1000)
        y = 5 - 10 * x + np.random.normal(0, 2, 1000)
        L = partial(lin_regression_lnprob, x=x, y=y)

        approx = approx_dist(L, np.array([0, 0, 1]))
        self.assertAlmostEqual(float(approx.mean[0]), 5, delta=1e-1)
        self.assertAlmostEqual(float(approx.mean[1]), -10, delta=1e-1)
        self.assertAlmostEqual(float(approx.mean[2]), 2, delta=1e-1)
    a, b, s = v
    if s <= 0:
        return -np.inf
    y_hat = a + b * x
    lp = np.sum(norm.logpdf(y, y_hat, s))
    return lp


TRUE_A = 0.1
TRUE_B = -10
TRUE_S = 1.3
x = np.linspace(0, 1, 10000)
y = TRUE_A + TRUE_B * x + np.random.normal(0, TRUE_S, 10000)
L = partial(lin_regression_lnprob, x=x, y=y)

result = approx_dist(L, np.array([0, 0, 1]))
samples = result.rvs(1000)
plt.scatter(samples[:, 0], samples[:, 1], alpha=0.1)
plt.scatter([np.mean(samples[:, 0])], [np.mean(samples[:, 1])],
            marker='x',
            color='red',
            label='Posterior mean')
plt.title('Posterior samples for intercept and coefficient of linear model')
plt.xlabel('Intercept')
plt.ylabel('Coefficient')
plt.axvline(TRUE_A, color='orange', label='True intercept')
plt.axhline(TRUE_B, label='True Coefficient')
plt.legend()
plt.show()

sns.distplot(samples[:, 2])
Ejemplo n.º 5
0
 def test_norm_approx_1d(self):
     x0 = np.array([1])
     true_dist = norm(0, 1)
     approx = approx_dist(true_dist.logpdf, x0)
     self.assertAlmostEqual(float(approx.mean[0]), 0)
     self.assertAlmostEqual(float(approx.cov[0]), 1)