def az_mu_sigma_plot(stan_fit):
    """
        Function to demonstrate pystan theta convergence result through R_hat table, autocorrelation (3 chians), and trace plot
        """
    az.plot_trace(stan_fit, var_names=['sigma2', 'mu'], filter_vars="like")
    az.plot_autocorr(stan_fit, var_names=['sigma2', "mu"])
    az.plot_pair(stan_fit, var_names=['sigma2', "mu"], divergences=True)
Example #2
0
def bayes_multiple_detector_I(t, s, n, tracename):
    with pm.Model() as abrupt_model:
        sigma = pm.Normal('sigma', mu=30, sigma=5)
        # sigma = pm.Uniform('sigma', 5, 15)
        mu = pm.Uniform("mu1", -30, 30)
        tau = pm.DiscreteUniform("tau" + "1", t.min(), t.max())

        for i in np.arange(2, n + 2):
            _mu = pm.Uniform("mu" + str(i), -100, 0)
            mu = T.switch(tau >= t, mu, _mu)
            if (i < (n + 1)):
                tau = pm.DiscreteUniform("tau" + str(i), tau, t.max())
        # add random walk
        # sigma_rw = pm.Uniform("sigma_rw", 0, 10)
        g_rw = pm.GaussianRandomWalk("g_rw", tau=1, shape=len(s))
        s_obs = pm.Normal("s_obs", mu=g_rw + mu, sigma=sigma, observed=s)
    # g = pm.model_to_graphviz(abrupt_model)
    # g.view()
    with abrupt_model:
        pm.find_MAP()
        trace = pm.sample(5000, tune=1000)
        az.plot_trace(trace)
        plt.show()
        az.plot_autocorr(trace)
        plt.show()
        az.to_netcdf(trace, getpath('tracepath') + tracename)
        pm.summary(trace)
    return trace
def az_v_theta_plot(stan_fit):
    """
        Function to demonstrate pystan theta convergence result through R_hat table, autocorrelation (3 chians), and trace plot
        """
    az.plot_trace(stan_fit, var_names=['v', 'theta'], filter_vars="like")
    print(stan_fit.stansummary())
    az.plot_autocorr(stan_fit, var_names=["v", 'theta'])
    az.plot_pair(stan_fit, var_names=["v", 'theta'], divergences=True)
Example #4
0
def az_v_sigma2_plot(stan_fit, var_list=['v', 'sigma2']):
    """
        Function to demonstrate pystan v convergence result through R_hat table, autocorrelation (3 chians), and trace plot
        """

    #        print(az.summary(stan_fit, var_names=["v","sigma2",'W'], filter_vars="like"))
    print(az.summary(stan_fit, var_names=var_list + ['W']))
    #        az.plot_trace(stan_fit, var_names=['v','sigma2'], filter_vars="like")
    az.plot_trace(stan_fit, var_names=var_list)
    az.plot_autocorr(stan_fit, var_names=var_list)

    az.plot_pair(stan_fit, var_names=var_list, divergences=True)
def create_diagnostic_plots(idx,pdf_filename,fit,diag_pars,niter,nchain):

    # Converting the Stan FIT object to Arviz InfereceData
    samples   = fit.extract(permuted=True) # Extracting parameter samples
    data      = az.from_pystan(fit)
    tmp       = data.posterior
    var_names = list(tmp.data_vars)

    # Filtering the list of parameters to plot
    unwanted  = {'losvd','spec','conv_spec','poly','bestfit','losvd_','losvd_mod','spec_pred','log_likelihood'}
    vars_main = [e for e in var_names if e not in unwanted]
   
    # Reading diagnostic parameters
    accept_stat, stepsize,  treedepth = np.zeros((niter,nchain)), np.zeros((niter,nchain)) , np.zeros((niter,nchain))
    n_leapfrog,  divergent, energy    = np.zeros((niter,nchain)), np.zeros((niter,nchain)) , np.zeros((niter,nchain))  
    for j in range(nchain):
        accept_stat[:,j] = diag_pars[j]['accept_stat__']
        stepsize[:,j]    = diag_pars[j]['stepsize__']
        treedepth[:,j]   = diag_pars[j]['treedepth__']
        n_leapfrog[:,j]  = diag_pars[j]['n_leapfrog__']
        divergent[:,j]   = diag_pars[j]['divergent__']
        energy[:,j]      = diag_pars[j]['energy__']    
 
    # Creating the plot in multiple PDF papges
    pdf_pages = PdfPages(pdf_filename)

    print(" - Sampler params")
    plot_sampler_params(idx,accept_stat,stepsize,treedepth,n_leapfrog,divergent,energy)
    pdf_pages.savefig()
    print(" - Chains")
    plot_chains(samples,vars_main)
    pdf_pages.savefig()
   #  print(" - Trace plot [Main params]")
   #  az.plot_trace(data, var_names=vars_main)
   #  pdf_pages.savefig()
   #  print(" - Trace plot [LOSVD]")
   #  az.plot_trace(data, var_names=['losvd'])
   #  pdf_pages.savefig()
    print(" - Pair plot")
    az.plot_pair(data, var_names=vars_main, divergences=True, kind='kde', fill_last=False)
    pdf_pages.savefig()
    print(" - Autocorr plot")
    az.plot_autocorr(data, var_names=vars_main)
    pdf_pages.savefig()
    print(" - Energy plot")
    az.plot_energy(data)
    pdf_pages.savefig()
    pdf_pages.close()   

    return
Example #6
0
def efficient_autocorr(trace, var_names, figstem, max_panel=6):
    """
    Wrap the arviz call for correlation to speed things up by splitting into smaller figures bundled as a scrollable PDF.
    """

    N = len(var_names)
    n = 0
    i = 0
    while n <= N:
        plot_vars = var_names[n : n + max_panel]

        az.plot_autocorr(trace, var_names=plot_vars)
        plt.savefig(figstem.format(i))
        plt.close("all")
        n += max_panel
        i += 1
Example #7
0
def plot_autocorr(trace, title):
    """Generate an autocorr plot with Arviz"""
    axs = az.plot_autocorr(trace, max_lag=20)
    fig = plt.gcf()
    fig.suptitle(title, fontsize=16)
    # ax0, ax1 = axs[0,0], axs[0,1]
    return axs
Example #8
0
def plot_param_diagnostics(mod,
                           incl_noise_params=False,
                           incl_trend_params=False,
                           incl_smooth_params=False,
                           which='trace',
                           **kwargs):
    """
    Parameters
    -----------
    mod : orbit model object
    which : str, {'density', 'trace', 'pair', 'autocorr', 'posterior', 'forest'}
    incl_noise_params : bool
        if plot noise parameters; default False
    incl_trend_params : bool
        if plot trend parameters; default False
    incl_smooth_params : bool
        if plot smoothing parameters; default False
    **kwargs :
        other parameters passed to arviz functions

    Returns
    -------
        matplotlib axes object
    """
    posterior_samples = get_arviz_plot_dict(
        mod,
        incl_noise_params=incl_noise_params,
        incl_trend_params=incl_trend_params,
        incl_smooth_params=incl_smooth_params)

    if which == "trace":
        axes = az.plot_trace(posterior_samples, **kwargs)
    elif which == "density":
        axes = az.plot_density(posterior_samples, **kwargs)
    elif which == "posterior":
        axes = az.plot_posterior(posterior_samples, **kwargs)
    elif which == "pair":
        axes = az.plot_pair(posterior_samples, **kwargs)
    elif which == "autocorr":
        axes = az.plot_autocorr(posterior_samples, **kwargs)
    elif which == "forest":
        axes = az.plot_forest(posterior_samples, **kwargs)
    else:
        raise Exception(
            "please use one of 'trace', 'density', 'posterior', 'pair', 'autocorr', 'forest' for kind."
        )

    return axes
Example #9
0
def mcmc_diagnostic_plots(posterior, sample_stats, it):

    az_trace = az.from_dict(posterior=posterior, sample_stats=sample_stats)
    """
    # 2 parameters or more for these pair plots
    if len(az_trace.posterior.data_vars) > 1:
        ax = az.plot_pair(az_trace, kind="hexbin", gridsize=30, marginals=True)
        fig = ax.ravel()[0].figure
        plt.ylim((5000, 30000))
        plt.xlim((1e-10, 1e-7))
        fig.savefig(f"./results/pair_plot_it{it}.png")
        plt.clf()

        ax = az.plot_pair(
            az_trace,
            kind=["scatter", "kde"],
            kde_kwargs={"fill_last": False},
            point_estimate="mean",
            marginals=True,
        )
        fig = ax.ravel()[0].figure
        fig.savefig(f"./results/point_estimate_plot_it{it}.png")
        plt.clf()
    """

    ax = az.plot_trace(az_trace, divergences=False)
    fig = ax.ravel()[0].figure
    fig.savefig(f"./results/trace_plot_it{it}.png")
    plt.clf()

    ax = az.plot_posterior(az_trace)
    fig = ax.ravel()[0].figure
    fig.savefig(f"./results/posterior_plot_it{it}.png")
    plt.clf()

    lag = np.minimum(len(list(posterior.values())[0]), 100)
    ax = az.plot_autocorr(az_trace, max_lag=lag)
    fig = ax.ravel()[0].figure
    fig.savefig(f"./results/autocorr_plot_it{it}.png")
    plt.clf()

    ax = az.plot_ess(az_trace, kind="evolution")
    fig = ax.ravel()[0].figure
    fig.savefig(f"./results/ess_evolution_plot_it{it}.png")
    plt.clf()
    plt.close()
Example #10
0
 def plot_auto_corr(self, **kwargs):
     """Plot the autocorrelation function of each posterior parameter."""
     return az.plot_autocorr(self.data, **kwargs)
Example #11
0
    os.makedirs(plotdir)

trace = pm.load_trace(directory="chains/close/rv", model=m.model)

# view summary
df = az.summary(trace, var_names=m.all_vars)
print(df)

# write summary to disk
f = open(f"{plotdir}summary.txt", "w")
df.to_string(f)
f.close()

with az.rc_context(rc={"plot.max_subplots": 60}):
    # autocorrelation
    az.plot_autocorr(trace, var_names=m.sample_vars)
    plt.savefig(f"{plotdir}autocorr.png")

    # make a traceplot
    az.plot_trace(trace, var_names=m.all_vars)
    plt.savefig(f"{plotdir}trace.png")

# make a nice corner plot of the variables we care about
samples = pm.trace_to_dataframe(
    trace,
    varnames=[
        "P",
        "KAa",
        "KAb",
        "e",
        "gamma",
The following figure shows examples of problematic samples:

<center><img src="../img/pathological_traces.png" width=600></center>

On the first row we see that the MCMC chains has large autocorrelation, you can see the trace (left column) shows long regions of monoticity (the lines parallel to the x-axis). This could be a consequence of a multimodal posterior with barrier between modes of very low probability. Thus the samples has trouble to freely  move from mode to mode. Another explanation could be high correlation between parameters, this can also be problematic for some samplers specially Metropolis. In such cases the multimodality could be _apparent_ and not a real feature of our posterior.

On the second row we see two chains that started from two very different position and eventually converge to the same distribution. The first $\approx$ 25 samples could bias our results so we can just remove them (*burn-in*).

ON the last row, we see two chains exploring two different regions of the parameter space. From the trace it seems they are in fact approaching each other at a slow rate and the maybe eventually reach the same stationary distribution. 

## Autocorrelation plot

<br>

    az.plot_autocorr()

As we discussed in the Numerical Diagnostics section, we can see autocorrelation as a factor that decrease the actual amount of information contained in a sample. So we want to reduce autocorrelation as much as possible.

bad_chains = np.linspace(0, 1, 1000).reshape(2, -1)
az.plot_autocorr(bad_chains)

The autocorrelation plot shows the _degree of autocorrelation_ by default it used a maximum window of 100. The previous figure, corresponding to `bad_chains` show a very high autocorrelation while the next figure corresponding to `good_chains` show a very low autocorrelation.

good_chains = stats.uniform.rvs(0, 1, size=(2, 500))
az.plot_autocorr(good_chains)

## Rank plot

<br>
np.random.seed(0)
with Centered_eight:
    trace_centered = pm.sample(1000, chains=4)

print(pm.summary(trace_centered).round(2))
# Effective sample size is << 4*1000, especially for tau
# Also, PyMC3 gives various warnings about not mixing

# Display the total number and percentage of divergent chains
diverging = trace_centered['diverging']
print('Number of Divergent Chains: {}'.format(diverging.nonzero()[0].size))
diverging_pct = diverging.nonzero()[0].size / len(trace_centered) * 100
print('Percentage of Divergent Chains: {:.1f}'.format(diverging_pct))

az.plot_autocorr(trace_centered, var_names=['mu', 'tau'])

az.plot_forest(trace_centered, var_names="theta", credible_interval=0.95)

# Non-Centered model

with pm.Model() as NonCentered_eight:
    mu = pm.Normal('mu', mu=0, sigma=10)
    tau = pm.HalfCauchy('tau', beta=5)
    theta_offset = pm.Normal('theta_offset', mu=0, sigma=1, shape=J)
    theta = pm.Deterministic('theta', mu + tau * theta_offset)
    #theta = pm.Normal('theta', mu=mu, sigma=tau, shape=J)
    obs = pm.Normal('obs', mu=theta, sigma=sigma, observed=y)

np.random.seed(0)
with NonCentered_eight:
Example #14
0
 def autocorr_plot(self, chain_id, **kwargs):
     return az.plot_autocorr(self.chains, var_names=chain_id,  **kwargs)
Example #15
0
    alpha = pm.Uniform('alpha', 0, 5)
    log_sigma = pm.Uniform('log_sigma', -10, 10)
    mu = alpha
    y_obs = pm.Normal('y_obs', mu=mu, sigma=np.exp(log_sigma), observed=d['kcal.per.g'])

pm.find_MAP(model=m6_11, method='BFGS')

with m6_11:
    trace = pm.sample(2000, return_inferencedata=True, chains=2)
pm.summary(trace)
az.summary(trace)
#pm.gelman_rubin(trace)
with m6_11:
    az.plot_trace(trace)
plt.show()
az.plot_autocorr(trace)
plt.show()
az.plot_density(trace)
plt.show()
az.plot_forest(trace)
plt.show()

# might need to multiply by -2 to compare with McElreath
with m6_11:
    print(pm.waic(trace))
    print(pm.loo(trace))


#m6_13 = pm.Model()
with pm.Model() as m6_13:
    alpha = pm.Uniform('alpha', 0, 5)
Example #16
0
az.summary(trace.posterior['intercept'])
pm.traceplot(trace)
plt.show()
az.plot_trace(trace)
az.plot_trace(trace, var_names=['intercept'])
plt.show()
az.plot_trace(trace, var_names=['sigma'])
plt.show()

az.plot_forest(trace, r_hat=True)
plt.show()
az.plot_posterior(trace)
plt.show()
az.plot_energy(trace)
plt.show()
az.plot_autocorr(trace, var_names=["intercept", "sigma"])
plt.show()


# map() by hand
def negPost(x, *args):
    y = args[0]
    p = -1 * sum(norm.logpdf(y, x[0], x[1]))[0] + norm.logpdf(
        x[0], 178, 20) + uniform.logpdf(x[1], 0, 50)
    return p


negPost((178, 3), d2)
secondDerivPost = nd.Derivative(negPost, n=2, full_output=True)

modePost1 = minimize(negPost,
Example #17
0
for pi in range(len(paraname)):
    plt.subplot(3, 1, pi+1)
    plt.tight_layout()
    [plt.plot(iter_range + 1, ms[iter_range,ci,pi], color=palette[ci]) for ci in range(ms.shape[1])]
    plt.title(paraname[pi])
plt.show()

#
for pi in range(len(paraname)):
    plt.subplot(3, 1, pi+1)
    plt.tight_layout()
    [sns.kdeplot(ms[iter_range,ci,pi], color=palette[ci]) for ci in range(ms.shape[1])]
    plt.title(paraname[pi])
plt.show()

mcmc_result.plot()

plt.show()

# using arviz instead of bayesplot
#az.plot_density(data=mcmc_result, var_names=['mu']);
az.plot_trace(data=mcmc_result)
plt.show()

az.plot_forest(data=mcmc_result, kind='ridgeplot', combined=True)
plt.show()

az.plot_autocorr(data=mcmc_result)
plt.show()

Example #18
0
az.plot_trace(trace_cm, var_names=['a'], divergences='top')


# %%
az.plot_trace(trace_ncm, var_names=['a'])

# %%
az.plot_forest(trace_cm, var_names=['a'], r_hat=True, ess=True)

# %%
summaries = pd.concat([az.summary(trace_cm, var_names=['a']), az.summary(trace_ncm, var_names=['a'])])
summaries.index = ['centered', 'non_centered']
summaries

# %%
az.plot_autocorr(trace_cm, var_names=['a'])

# %%
az.plot_autocorr(trace_ncm, var_names=['a'])

# %%
_, ax = plt.subplots(1, 2, sharey=True, sharex=True, figsize=(10, 5), constrained_layout=True)

for idx, tr in enumerate([trace_cm, trace_ncm]):
    az.plot_pair(tr, var_names=['b', 'a'], coords={'b_dim_0':[0]}, kind='scatter',
                 divergences=True, contour=False, divergences_kwargs={'color':'C1'},
                 ax=ax[idx])
    ax[idx].set_title(['centered', 'non-centered'][idx])

# %%
az.plot_parallel(trace_cm)
Example #19
0
"""
Autocorrelation Plot
====================

_thumb: .8, .8
"""
import arviz as az

data = az.load_arviz_data("centered_eight")
ax = az.plot_autocorr(data, var_names="tau", backend="bokeh", show=True)
    beta_eff[i] = get_beta_eff(i, beta_start, beta_end, k, 90)

plt.plot(np.arange(0, 180), beta_eff)
plt.xlabel('Time (days)')
plt.ylabel('Beta (effective)')
plt.savefig('../results/plots/beta_over_time.pdf')
plt.clf()
"""
seir.plot_incidence()
plt.plot(np.arange(0,180),daily_cases.newcountconfirmed[20:200],label='observed')
plt.legend()
plt.tight_layout()
plt.savefig('../results/plots/model_fit.pdf')
"""

inference_data = az.from_cmdstan('../results/outputs/*.csv')
az.plot_trace(inference_data)
plt.savefig('../results/plots/model_trace.pdf')
az.plot_joint(inference_data,
              var_names=['beta_start', 'beta_end'],
              kind='kde',
              figsize=(6, 6))
plt.tight_layout()
plt.savefig('../results/plots/model_joint_betas.pdf')
az.plot_posterior(inference_data)
plt.savefig('../results/plots/model_posterior.pdf')
az.plot_autocorr(inference_data, combined=True)
plt.savefig('../results/plots/model_autocorrelation.pdf')
az.plot_rank(inference_data)
plt.savefig('../results/plots/model_rank.pdf')
Example #21
0
"""
Autocorrelation Plot
====================

_thumb: .8, .8
"""
import matplotlib.pyplot as plt
import arviz as az

az.style.use("arviz-darkgrid")

data = az.load_arviz_data("centered_eight")
az.plot_autocorr(data, var_names=("tau", "mu"))

plt.show()
    
np.random.seed(0)
with Centered_eight:
    trace_centered = pm.sample(1000, chains=4)
    
pm.summary(trace_centered).round(2)
# Effective sample size is << 4*1000, especially for tau
# Also, PyMC3 gives various warnings about not mixing

# Display the total number and percentage of divergent chains
diverging = trace_centered['diverging']
print('Number of Divergent Chains: {}'.format(diverging.nonzero()[0].size))
diverging_pct = diverging.nonzero()[0].size / len(trace_centered) * 100
print('Percentage of Divergent Chains: {:.1f}'.format(diverging_pct))

az.plot_autocorr(trace_centered, var_names=['mu_alpha', 'sigma_alpha']);
 
az.plot_forest(trace_centered, var_names="alpha", credible_interval=0.95);
    

# Non-Centered model

with pm.Model() as NonCentered_eight:
    mu_alpha = pm.Normal('mu_alpha', mu=0, sigma=5)
    sigma_alpha = pm.HalfCauchy('sigma_alpha', beta=5)
    alpha_offset = pm.Normal('alpha_offset', mu=0, sigma=1, shape=J)
    alpha = pm.Deterministic('alpha', mu_alpha + sigma_alpha * alpha_offset)
    #alpha = pm.Normal('alpha', mu=mu_alpha, sigma=sigma_alpha, shape=J)
    obs = pm.Normal('obs', mu=alpha, sigma=sigma, observed=y)
    
             marginal_kwargs=kde_kwargs,
             textsize=20,
             figsize=(24, 18),
             point_estimate="median",
             kind=['scatter', 'kde'])

az.plot_pair(data,
             var_names=['b', 'mu'],
             marginals=True,
             marginal_kwargs=kde_kwargs,
             textsize=20,
             figsize=(24, 18),
             point_estimate="median",
             kind=['scatter', 'kde'])

az.plot_autocorr(data, var_names=('kv', 'k0', 'kf', 'k2', 'b', 'mu', 'phif'))

# Load in permuted traces from PyStan run
predictive_df = pd.read_csv('stanwound_fit_predictive_permuted.csv')
stress_mean_predicted_phif_4 = np.empty([0, 3])
stress_mean_predicted_phif_20 = np.empty([0, 3])
stress_mean_predicted_phif_40 = np.empty([0, 3])
stress_mean_predicted_phif_RS = np.empty([0, 3])
stress_predicted_phif_4 = np.empty([0, 3])
stress_predicted_phif_20 = np.empty([0, 3])
stress_predicted_phif_40 = np.empty([0, 3])
stress_predicted_phif_RS = np.empty([0, 3])
von_mises_prob_mean_predicted_phif_4 = np.empty([0, 3])
von_mises_prob_mean_predicted_phif_20 = np.empty([0, 3])
von_mises_prob_mean_predicted_phif_40 = np.empty([0, 3])
von_mises_prob_mean_predicted_phif_RS = np.empty([0, 3])
Example #24
0
def conduct_bayesian_norm(xj,
                          zj,
                          nj,
                          mu_init,
                          beta_init,
                          draws,
                          target_accept,
                          plot_flag=True):
    """
    A function to conduct Bayesian updating of fragility models.
    (Optional): Produce MCMC-related plots (trace). Default: True
    Notes:
    Here intensity measure is the wind speed: A normalizing factor (max wind speed) is used to improve
    numerical stability.

    Prior distributions are designated according to the assumption that the Bayesian analysis will utilize
    wind fragility functions from HAZUS (see priors for mu and beta). See De Brujin et al. (2020) for more details.

    De Bruijn J. et al. (2020). "Using rapid damage observations from social media for Bayesian updating of hurricane
    vulnerability functions: A case study of Hurricane Dorian." Nat.Hazards Earth Syst. Sci. Discuss. [preprint],
    https://doi.org/10.5194/nhess-2020-282.

    The likelihood function is modeled using a Binomial distribution see Lallemant et al. (2015) for more details.

    :param xj: An array or list of observed intensity measure values for the damage measure.
    :param zj: An array or list of failure observations (number of failed buildings/components) for the given damage
                and intensity measure.
    :param nj: An array or list of the total # of buildings for the given damage measure and intensity measure
    :param mu_init: The mean value of the prior distribution for the logarithmic mean.
    :param beta_init: The mean value of the prior distribution for the logarithmic std. dev.
    :param num_samples: (Optional) The number of samples to conduct MCMC.
    :param plot_flag: (Optional) Produce the trace plot for the MCMC and updated distributions for parameters.
    :return: updated_values: A dictionary with each parameter's updated mean and standard deviation.
    """
    # Step 1: Normalize the intensity measure:
    norm_analysis = True
    if norm_analysis:
        norm_factor = max(xj)
        xj = xj / norm_factor
        mu_init = mu_init / norm_factor
        mu_std_dev = 15 / norm_factor
        #nj = nj/15
        #zj = zj/15
        #beta_init = beta_init/norm_factor
        #beta_std_dev = 0.03/norm_factor
    else:
        mu_std_dev = 15
    beta_std_dev = 0.03
    # Step 2: Build the Bayesian model in PyMC3:
    with pm.Model() as model:
        # Step 3a: Set up the prior
        # Here we assume Normal distributions for both parameters of the fragility
        # Note: Parameters for mu are also normalized for compatibility with intensity measure values.
        # See De Brujin et al. for more information regarding the initialization of prior distributions.
        #mu = pm.Normal('mu', mu_init/norm_factor, 15/norm_factor)
        BoundedNormal = pm.Bound(pm.Normal, lower=0.0)
        #x = BoundedNormal('x', mu=1.0, sigma=3.0)
        theta1 = BoundedNormal('theta1', mu_init, mu_std_dev)
        theta2 = BoundedNormal('theta2', beta_init, beta_std_dev)

        # Step 3b: Set up the likelihood function:
        # The likelihood in this model is represented via a Binomial distribution.
        # See Lallemant et al. (2015) for MLE derivation.
        # Define fragility function equation:
        def normal_cdf(theta1, theta2, xj):
            """Compute the log of the cumulative density function of the normal."""
            return 0.5 * (1 + tt.erf(
                (tt.log(xj / theta1)) / (theta2 * tt.sqrt(2))))

        # Define the likelihood:
        like = pm.Binomial('like',
                           p=normal_cdf(theta1, theta2, xj),
                           observed=zj,
                           n=nj)
        # Uncomment to do an initial check of parameter values (lookout for like = +/-inf)
        #for RV in model.basic_RVs:
        #   print(RV.name, RV.logp(model.test_point))
        # Step 3c: Determine the posterior
        # Note: can manually change number of cores if more computational power is available.
        trace_idata = pm.sample(draws,
                                cores=1,
                                return_inferencedata=True,
                                random_seed=72,
                                target_accept=target_accept)
        #trace = pm.sample(8000, cores=1, return_inferencedata=True, tune=2000, random_seed=52)  #tune=2000
        # (Optional): Plotting the trace and updated distributions for parameters:
        if plot_flag:
            from matplotlib import rcParams
            rcParams['font.family'] = "Times New Roman"
            rcParams.update({'font.size': 16})
            az.plot_trace(trace_idata, chain_prop={'color': ['blue', 'red']})
            # Plot the autocorrelation to check convergence:
            ax_corr = az.plot_autocorr(trace_idata, combined=True)
            ax_corr[0].set_title(r'$\theta_1$')
            ax_corr[1].set_title(r'$\theta_2$')
            ax_corr[0].set_ylabel('Autocorrelation')
            ax_corr[0].set_xlabel('Lag')
            ax_corr[1].set_xlabel('Lag')
        # Step 4: Generate summary statistics for the MCMC:
        print('Summary statistics for the MCMC:')
        print(az.summary(
            trace_idata))  # Note: can output this DataFrame if needed
        df = az.summary(trace_idata, hdi_prob=0.95)
        # Step 5: Sample from the posterior and save updated values for mean and std. dev:
        ppc = pm.sample_posterior_predictive(
            trace_idata, var_names=['theta1', 'theta2', 'like'])
        # Re-scale values for logarithmic mean:
        if norm_analysis:
            df['mean']['theta1'] = df['mean']['theta1'] * norm_factor
            df['sd']['theta1'] = df['sd']['theta1'] * norm_factor
            ppc['theta1'] = ppc['theta1'] * norm_factor
        else:
            pass
        # Export analysis results:
        summary_dict = {}
        for row in df.index:
            for col in df.columns:
                new_key = row + col
                if 'hdi' in new_key and 'theta1' in new_key and norm_analysis:
                    new_val = df[col][row] * norm_factor
                else:
                    new_val = df[col][row]
                summary_dict[new_key] = new_val
        # Export MCMC details:
        mcmc_dict = {'draws': draws, 'target_accept': target_accept}
        for key in mcmc_dict:
            summary_dict[key] = mcmc_dict[key]
        df_summary = pd.DataFrame(summary_dict, index=[0], dtype='object')
        # Sample directly from the posterior to create figures:
        updated_values = {
            'theta1': {
                'mean': ppc['theta1'].mean(),
                'std dev': np.std(ppc['theta1'])
            },
            'theta2': {
                'mean': ppc['theta2'].mean(),
                'std dev': np.std(ppc['theta2'])
            }
        }
        if plot_flag:
            # Plot prior and updated distributions for parameters:
            # mu
            fig, ax = plt.subplots()
            ax.hist(ppc['theta1'] / 2.237,
                    bins=25,
                    density=True,
                    alpha=0.4,
                    color='cornflowerblue',
                    label='posterior samples')
            ax.set_xlim(50 / 2.237, 200 / 2.237)
            xmin, xmax = ax.set_xlim()
            x = np.linspace(xmin, xmax, 100)
            if norm_analysis:
                p_prior = norm.pdf(x, mu_init * norm_factor / 2.237,
                                   mu_std_dev * norm_factor / 2.237)
            else:
                p_prior = norm.pdf(x, mu_init / 2.237, mu_std_dev / 2.237)
            p_new = norm.pdf(x, updated_values['theta1']['mean'] / 2.237,
                             updated_values['theta1']['std dev'] / 2.237)
            ax.plot(x, p_prior, 'k', linewidth=2, label='prior')
            ax.plot(x,
                    p_new,
                    'r',
                    linewidth=2,
                    label='updated',
                    linestyle='dashed')
            ax.set_title('Prior and updated distributions for ' +
                         r'$\theta_1$')
            ax.set_xlabel(r'$\theta_1$')
            ax.set_ylabel('Probability')
            ax.legend()
            plt.show()
            # beta
            fig2, ax2 = plt.subplots()
            ax2.hist(ppc['theta2'],
                     bins=25,
                     density=True,
                     alpha=0.4,
                     color='cornflowerblue',
                     label='posterior samples')
            ax2.set_xlim(0, 0.3)
            xmin2, xmax2 = ax2.set_xlim()
            x2 = np.linspace(xmin2, xmax2, 100)
            p_prior2 = norm.pdf(x2, beta_init, 0.03)
            p_new2 = norm.pdf(x2, updated_values['theta2']['mean'],
                              updated_values['theta2']['std dev'])
            ax2.plot(x2, p_prior2, 'k', linewidth=2, label='prior')
            ax2.plot(x2,
                     p_new2,
                     'r',
                     linewidth=2,
                     label='updated',
                     linestyle='dashed')
            ax2.set_title('Prior and updated distributions for ' +
                          r'$\theta_2$')
            ax2.set_xlabel(r'$\theta_2$')
            ax2.set_ylabel('Probability')
            ax2.legend()
            plt.show()
            # Create forestplots:
            # ax_forest = az.plot_forest(trace_idata.posterior['theta1']*norm_factor/2.237, hdi_prob=0.95, combined=True, var_names=['theta1'])
            # ax_forest[0].set_xlim(45, 75)
            # plt.show()
            # ax_forest2 = az.plot_forest(trace_idata, hdi_prob=0.95, combined=True,
            #                             var_names=['theta2'])
            # ax_forest2[0].set_xlim(0, 0.5)
            # plt.show()
            # Calculate failure probabilities for prior, updated:
            im = np.arange(70, 200, 2)
            # Mean of simulation-based fragility:
            if norm_analysis:
                pf_sim = pf(im, mu_init * norm_factor, beta_init)
            else:
                pf_sim = pf(im, mu_init, beta_init)
            # Mean of updated fragility:
            pf_mean = pf(im, ppc['theta1'].mean(), ppc['theta2'].mean())
            # Calculate entire distribution of pfs using posterior samples:
            pf_ppc = []
            for i in range(0, len(ppc['theta1'])):
                y = pf(im, ppc['theta1'][i], ppc['theta2'][i])
                pf_ppc.append(y)
            # Plot the credible intervals, mean outcome of prediction, mean of simulation-based:
            fig3, ax3 = plt.subplots()
            ax3.set_clip_on(False)
            ax3.set_ylim(0, 1.2)
            ax3.spines['right'].set_visible(False)
            ax3.spines['top'].set_visible(False)
            az.plot_hdi(im / 2.237,
                        pf_ppc,
                        hdi_prob=0.95,
                        fill_kwargs={
                            'alpha': 0.1,
                            'color': 'paleturquoise',
                            'label': '95% credible interval'
                        })
            ax3.plot(im / 2.237,
                     pf_mean,
                     label='mean of prediction',
                     color='r',
                     linestyle='dashed')
            ax3.plot(im / 2.237,
                     pf_sim,
                     label='mean of simulation-based',
                     color='k')
            # Plot the observations:
            if norm_analysis:
                ax3.scatter(xj * norm_factor / 2.237,
                            zj / nj,
                            color='darkviolet',
                            label='observations',
                            zorder=5,
                            s=70)
            else:
                ax3.scatter(xj / 2.237,
                            zj / nj,
                            color='darkviolet',
                            label='observations',
                            zorder=5,
                            s=70)
            ax3.set_xlabel('Wind Speed [m/s]')
            ax3.set_ylabel('Probability of Failure')
            ax3.legend()
            plt.show()
    return df_summary
Example #25
0
# ==> 10 independently drawn, joint samples at `index_points`.
# ==> 10 independently drawn, noisy joint samples at `index_points`
t2 = time.perf_counter()
print()

(observations_.numpy()[0] - X1)

acf = tfp.stats.auto_correlation(
    amplitudes, axis=-1, max_lags=None, center=True, normalize=True,
    name='auto_correlation'
)

var1 = np.array([amplitudes,
      length_scales,
      observation_noise_variances])
az.plot_autocorr(var1)
'''
, var_names=("amplitudes",
      "length_scales",
      "observation_noise_variances"))'''

numElems = len(Y)
sample = samples[0] + mean_fn(observation_index_points[:,0], r.t2bv(10**(observation_index_points[:,1])), a ,b ,c ,d)
idx = np.round(np.linspace(0, len(np.array(sample).reshape(numElems**2)) - 1, numElems)).astype(int)
# Picks equal spaced elements from (longer) prediction array so that its shape of data

mu_test = (np.array(sample).reshape(numElems**2)[idx])
sd_test = (np.array(var).reshape(numElems**2)[idx]) 

vals = np.sort([mu_test, sd_test], axis=1)
Example #26
0
"""
Autocorrelation Plot
====================

_thumb: .8, .8
"""
import arviz as az

az.style.use('arviz-darkgrid')

data = az.load_arviz_data('centered_eight')
az.plot_autocorr(data, var_names=('tau', 'mu'))
Example #27
0
def chain_validate_arviz(inferred, variables):
    for var in variables:
        az.plot_autocorr(inferred, var_names=[var])