<jupyter_start><jupyter_text>How long is the Blacklist? (With PyMC3)> A Bayesian approach using PyMC3- toc: true - badges: true- comments: true- categories: [Bayesian, PyMC3]- image: images/2020-05-20-How-Long-Is-The-Blacklist-With-PyMC3/header.jpg  An Introduction to PyMC3Last post, I took a first principles approach to computationally solving a Bayesian analysis problem. In practice, problems can be far more complex. Fortunately, sophisticated software libraries already exist that can be leveraged in order to solve equally complex problems. While there are a number of options available, my current favourite is [PyMC3](https://docs.pymc.io/). If you want to be inspired by what's possible, I strongly suggest checking out the fantastic set of [examples](https://docs.pymc.io/nb_examples/index.html) on the PyMC3 site.   Putting it into practiceLet's start as we did last time, by taking a sample of the first 10 elements from the blacklist.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [15, 15]

# E: the data
y = np.array([52, 145, 84, 161, 85, 152,
       47, 109, 16, 16, 106, 101,
       64, 73, 57, 83, 88, 135,
       119,120,121, 122, 42, 8, 8, 104, 112,
       89, 82, 74, 114, 22, 12,
       21, 21, 67, 71, 93, 94,
       75, 7, 97, 117, 62, 87,
       55, 11, 38, 80, 72, 43,
       50, 86, 31, 108, 24, 24,
       95, 132, 103, 77, 113, 78,
       32, 32, 41, 18, 14, 14,
       79, 66, 65, 81, 105, 53,
       98, 98, 111, 163, 102,
       34, 107, 59, 10, 61, 29,
       46, 4, 4, 30, 37, 76, 44,
       54, 90, 48, 13, 118, 100,
       56, 63, 51, 68, 19, 25,
       23, 13, 110, 26, 17, 33,
       20, 124, 146, 147, 131, 91, 116,
       58, 99, 160, 20, 20, 9,
        6, 115, 69, 136, 92, 128,
       60, 15, 27, 27, 151, 138,
       130, 125, 162, 159, 3, 137,
       155, 144, 126, 158, 149,
       150])
y_sample = y[0:10]
plt.stem(np.sort(y_sample), use_line_collection=True)
plt.show()<jupyter_output><empty_output><jupyter_text>Now this is when things start getting intesting. Firstly going to lay out all the code we need to solve this problem, all 7 lines.<jupyter_code>import pymc3 as pm

model = pm.Model()
with model:
    # prior - P(N): N ~ uniform(max(y), 500)
    N = pm.DiscreteUniform("N", lower=y.max(), upper=500)

    # likelihood - P(D|N): y ~ uniform(0, N)
    y_obs = pm.DiscreteUniform("y_obs", lower=0, upper=N, observed=y)
    
    trace = pm.sample(10_000, start={"N": y.max()}) 

pm.plots.plot_posterior(trace)<jupyter_output>Multiprocess sampling (2 chains in 2 jobs)
Metropolis: [N]
Sampling 2 chains, 0 divergences: 100%|██████████| 21000/21000 [00:05<00:00, 4179.11draws/s]
The number of effective samples is smaller than 10% for some parameters.
{'fraction_non_alphanumeric': 0.13243831640058054, 'fraction_numerical': 0.14840348330914369, 'mean_word_length': 3.2546296296296298, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '118369', 'license': 'Apache-2.0', 'n_tokens_mistral': 1346, 'n_tokens_neox': 967, 'n_words': 373, 'path': '_notebooks/2020-05-20-How-Long-Is-The-Blacklist-With-PyMC3.ipynb', 'repo_name': 'CGCooke/Blog', 'size': '2756'}