def plot_posterior(prior, Nt=100):
    """
    Plot the true (analytic) posterior density function along with the likelihood,
    prior and true value of theta.

    Parameters:
    -----------
    prior : array_like ([dist_type, param1, param2])
        Vector defining the prior distribution through the dist_type ('normal'
        or 'unif'), and two parameters (floats, mean and std for Gaussian, LHS
        and range for Uniform).
    Nt : int
        Number of points at which to evaluate each density function.

    Returns:
    --------
    Plot the true (analytic) posterior density function along with the likelihood,
    prior and true value of theta.
    """

    # Theta values for evaluating the posterior.
    tv = np.linspace(0,3,Nt)

    # Evaluate posterior, prior and likelihood.
    pv, p0v, Lv = posterior(tv, prior, ip_data)

    # Normalise.
    p0v = p0v/np.trapz(p0v,tv)
    Lv = Lv/np.trapz(Lv,tv)
    pv = pv/np.trapz(pv,tv)

    # Plot.
    fig, ax = myfigure()
    ax.plot(tv, pv, label='posterior')
    ax.plot(tv, Lv, label='likelihood')
    ax.plot(tv, p0v, label='prior')
    plt.axvline(x=np.log(ip_data['lam']), color='c', label='truth')
    ax.set_xlabel('theta')
    ax.set_ylabel('probability density')
    leg = ax.legend();
    plt.show(block=False)
def hist_plot(hist, bin_centres):
    """
    Plot a histogram of the input Markov chain.

    Parameters:
    -----------
    hist : array
        Probability density values.
    bin_centres : array
        Bin centre values.

    Returns:
    --------
    Plot the input histogram.
    """

    # Plot histogram (manually).
    fig, ax = myfigure()
    ax.plot(bin_centres, hist, label='posterior')
    plt.axvline(x=np.log(ip_data['lam']), color='r', label='truth')
    ax.set_xlabel('theta')
    ax.set_ylabel('probability density')
    leg = ax.legend();
    plt.show()
Exemplo n.º 3
0
pv = np.zeros((3, Nt))

# Evaluate posterior.
for n in range(Nt):
    pv[0, n], p0v[0, n], dummy1 = util.posterior(tv[n], prior1, ip_data)
    pv[1, n], p0v[1, n], dummy2 = util.posterior(tv[n], prior2, ip_data)
    pv[2, n], p0v[2, n], dummy3 = util.posterior(tv[n], prior3, ip_data)

# Normalise.
for i in range(3):
    p0v[i] = p0v[i] / np.trapz(p0v[i], tv)
    pv[i] = pv[i] / np.trapz(pv[i], tv)

## PLOT

fig, ax = myfigure()
ax.plot(tv, pv[0], '-b', label='posterior1')
ax.plot(tv, p0v[0], ':b', label='prior1')

ax.plot(tv, pv[1], '-r', label='posterior2')
ax.plot(tv, p0v[1], ':r', label='prior2')

ax.plot(tv, pv[2], '-g', label='posterior3')
ax.plot(tv, p0v[2], ':g', label='prior3')

plt.axvline(x=np.log(ip_data['lam']), color='c', label='truth')

ax.set_xlabel('theta')
ax.set_ylabel('probability density')
leg = ax.legend()
plt.show()
    ## ----------
    ## PLOT DATA:
    ## ----------

    # Spatial locations (plotting).
    x = np.linspace(0,1,50)

    # Matrix representation of noisy data.
    d_mat = np.reshape(ip_data['d'],(ip_data['nt'],ip_data['nx']))

    # Labels for y-axes.
    ylabs = ['u(x,0)', 'u(x,T/3)','u(x,2T/3)','u(x,T)']

    # Initiate 2x2 plot.
    fig, ax = myfigure(nrows=2, ncols=2)

    for i,axi in enumerate(ax):

        if i == 0:
            # Initial data.
            axi.plot(x, np.squeeze(uxt(x,0,ip_data['lam'])))
        else:
            # Measurement times data.
            axi.plot(x, np.squeeze(uxt(x,ip_data['t_obs'][i-1],ip_data['lam'])))
            axi.plot(ip_data['x_obs'], d_mat[i-1,:], 'xr')

        axi.set_xlabel('x')
        axi.set_ylabel(ylabs[i])
        axi.axis([0, 1, 0, 3])