Example #1
0
MODEL_FILENAME = 'temp_weights_eight_schools.h5'

d = 3
DATA_SHAPE = (2500, 3)
q = GaussianWithReparametrization(d=3, shape=DATA_SHAPE)
q(tf.zeros(DATA_SHAPE))
q.load_weights(MODEL_FILENAME)
z, mu, log_var = q(tf.zeros(DATA_SHAPE))

thetas, mu, tau = z[:, 0], z[:, 1], z[:, 2]

# Prior
N = 5000
mu_prior = np.random.normal(loc=0, scale=5, size=N)
tau_prior = halfcauchy.rvs(loc=0, scale=5, size=N)
thetas_prior = np.random.normal(loc=mu_prior, scale=tau_prior, size=N)
mask_tau = (np.log(tau_prior) > -2) & (np.log(tau_prior) < 2.8)

plt.figure()
plt.scatter(np.log(tau_prior[mask_tau]),
            thetas_prior[mask_tau],
            color='gray',
            alpha=0.6)
plt.scatter(tf.math.log(tau), thetas, color='crimson', alpha=0.6)
plt.xlabel(r'$log(\tau)$')
plt.ylabel(r'$\theta$')
plt.legend(['True Posterior', 'q'])

npdf = lambda x, m, s: np.exp(-(x - m)**2 / (2 * (s**2))) / np.sqrt(2 * np.pi *
                                                                    (s**2))
Example #2
0
# Display the probability density function (``pdf``):

x = np.linspace(halfcauchy.ppf(0.01), halfcauchy.ppf(0.99), 100)
ax.plot(x, halfcauchy.pdf(x), 'r-', lw=5, alpha=0.6, label='halfcauchy pdf')

# Alternatively, the distribution object can be called (as a function)
# to fix the shape, location and scale parameters. This returns a "frozen"
# RV object holding the given parameters fixed.

# Freeze the distribution and display the frozen ``pdf``:

rv = halfcauchy()
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

# Check accuracy of ``cdf`` and ``ppf``:

vals = halfcauchy.ppf([0.001, 0.5, 0.999])
np.allclose([0.001, 0.5, 0.999], halfcauchy.cdf(vals))
# True

# Generate random numbers:

r = halfcauchy.rvs(size=1000)

# And compare the histogram:

ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
ax.legend(loc='best', frameon=False)
plt.show()
Example #3
0
def main():

    # generate data
    np.random.seed(0)
    n = 1
    m = 10000
    mu = norm.rvs(0, 1, m)
    sigma = halfcauchy.rvs(0, 1, m)
    y = norm.rvs(mu, sigma, (n, m))

    # set up model
    with pm.Model():

        mu_ = pm.Normal("mu", 0, 1)
        sigma_ = pm.HalfCauchy("sigma", 1)
        yt_ = pm.Normal("yt", 0, 1, shape=n)
        pm.Deterministic("y", mu_ + yt_ * sigma_)
        # y_ = pm.Normal("y", mu_, sigma_, shape=n)

        # sample and save samples
        trace = pm.sample(m, chains=1)
        mu_samples = trace["mu"][:]
        sigma_samples = trace["sigma"][:]
        yt_samples = trace["yt"].T[:]
        y_samples = trace["y"].T[:]

    # plot 2-D figures
    sc = 5
    fs = rcParams["figure.figsize"]
    rcParams["figure.figsize"] = (fs[0], fs[0])
    rcParams["lines.linewidth"] = 2
    rcParams["font.size"] = 14
    fig, axes = plt.subplots(2, 2, constrained_layout=True)

    ax = axes[0, 0]
    ax.scatter(
        yt_samples[0], mu_samples, marker=".", alpha=0.05, rasterized=True, color="r"
    )
    ax.set_xlim(-sc, sc)
    ax.set_ylim(-sc, sc)
    ax.set_ylabel("$\mu$")
    ax.set_xticklabels([])

    ax = axes[0, 1]
    ax.scatter(
        y_samples[0], mu_samples, marker=".", alpha=0.05, rasterized=True, color="r"
    )
    ax.set_xlim(-sc, sc)
    ax.set_ylim(-sc, sc)
    ax.set_yticklabels([])
    ax.set_xticklabels([])

    ax = axes[1, 0]
    ax.scatter(
        yt_samples[0], sigma_samples, marker=".", alpha=0.05, rasterized=True, color="r"
    )
    ax.set_xlim(-sc, sc)
    ax.set_ylim(0, sc / 2)
    ax.set_xlabel(r"$\tilde{y}_0$")
    ax.set_ylabel("$\sigma$")

    ax = axes[1, 1]
    ax.scatter(
        y_samples[0], sigma_samples, marker=".", alpha=0.05, rasterized=True, color="r"
    )
    ax.set_xlim(-sc, sc)
    ax.set_ylim(0, sc / 2)
    ax.set_yticklabels([])
    ax.set_xlabel("$y_0$")

    # save
    plt.savefig("../images/realistic-funnel-b.svg", bbox_inches=0, transparent=True)
Example #4
0
import matplotlib.pyplot as plt
import numpy as np

from scipy.stats import halfcauchy

N = 1000
mu = np.random.normal(loc=0, scale=5, size=N)
tau = halfcauchy.rvs(loc=0, scale=5, size=N)
theta = np.random.normal(loc=mu, scale=tau, size=N)

mask_tau = (np.log(tau) > -2) & (np.log(tau) < 3)

plt.scatter(np.log(tau[mask_tau]), theta[mask_tau], color='gray', alpha=0.6)

plt.show()