<jupyter_start><jupyter_text>Visual diagnosticsWe will discuss:* Trace plots* Autocorrelation plots* Rank plots  Trace plots    az.plot_trace(trace)* MCMC samples should not be sensitive to the starting point (ergodicity)    * So if you sample more than one chain (starting from different places) you should get the same result* MCMC samples should have the lowest possible autocorrelation      * No long regions of monoticityTrace plots can help diagnose:* Multimodal posteriors* Bad intialization* Difficult topologies (such as Neal's funnel)  Pathological traces  How to fix it* More samples* Burn-in* Change the sampler!* Reparametrize the model* Read PyMC3 warning messages and suggestions!  Autocorrelation plot    az.plot_autocorr(trace)* Ideally a sample should be independent and identically distributed (iid)* By definition MCMC samples are correlated* In practice, we want samples will low autocorrelation<jupyter_code>bad_chains = np.linspace(0, 1, 1000).reshape(2, -1)
az.plot_autocorr(bad_chains);
good_chains = stats.uniform.rvs(0, 1,size=(2,500))
az.plot_autocorr(good_chains);<jupyter_output><empty_output><jupyter_text>How to fix it* More samples (this becomes less and less practicall as the autocorrelation increases)* Reparametrize the model* Follow PyMC3 warning messages and suggestions!  Rank plot    az.plot_rank()* Rank plots are histograms of the ranked posterior draws (ranked over all chains) plotted separately for each chain.* If all of the chains are targeting the same posterior, we expect the ranks in each chain to be uniform* If rank plots of all chains look similar, this indicates good mixing of the chains* Basically deviations from uniformity are signaling a chain that is not sampling all the regions properly* This is a recently proposed visual test, author argue superiority over trace plots.[<NAME> et al](https://arxiv.org/abs/1903.08008)<jupyter_code>original_data = np.random.beta(2, 2, 1000)
ranked_data = stats.rankdata(original_data)

_, ax = plt.subplots(1, 2, figsize=(12, 4), sharey=True)
ax[0].plot(original_data, ranked_data, 'b.')
ax[0].set_xlabel('data values')
ax[0].set_ylabel('rank')
ax[0].set_xlim([0, 1])
ax[1].hist(ranked_data, bins=10, rwidth=0.9, orientation='horizontal')
ax[1].set_xlabel('frecuency');
az.plot_rank(good_chains);
az.plot_rank(bad_chains);
dada = np.random.normal(0, 1, (2, 1000))
dada[:,0:100] = 0
az.plot_rank(dada);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0934156378600823, 'fraction_numerical': 0.024691358024691357, 'mean_word_length': 6.15, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '537873', 'license': 'MIT', 'n_tokens_mistral': 792, 'n_tokens_neox': 722, 'n_words': 271, 'path': 'content/Section_02/Visual_diagnostics.ipynb', 'repo_name': 'Sahanduiuc/Exploratory-Analysis-of-Bayesian-Models', 'size': '2435'}