Tasting the Bayes

Bayesian Data Analysis in Python

Michal Oleszak

Machine Learning Engineer

Binomial distribution

  • A discrete distribution, can only take one of two values:
    • Success (1)
    • Failure (0)
  • One parameter: probability of success.
  • Task: given a list of draws (successes and failures), estimate the probability of success.
Bayesian Data Analysis in Python

Binomial distribution in Python

Number of successes in 100 trials:

import numpy as np
np.random.binomial(100, 0.5)
51

 

np.random.binomial(100, 0.5)
44

Get draws from a binomial:

import numpy as np
np.random.binomial(1, 0.5, size=5)
array([1, 0, 0, 1, 1])
Bayesian Data Analysis in Python

Heads probability

  • get_heads_prob() - a custom function
  • input: a list of coin tosses
  • output: a list, the distribution of the probability of success

 

import numpy as np
tosses = np.random.binomial(1, 0.5, size=1000)
print(tosses)
[1 0 0 0 1 1 0 1 1 ... ]
Bayesian Data Analysis in Python

Heads probability

heads_prob = get_heads_prob(tosses)
print(heads_prob)
[0.47815295 0.51679212 0.51684779 ... ]
import matplotlib.pyplot as plt
import seaborn as sns

sns.kdeplot(heads_prob, 
            shade=True, 
            label="heads probabilty")
plt.show()

A symmetric, bell-shaped curve peaking around the X-axis value of 0.51.

Bayesian Data Analysis in Python

Let's toss some coins!

Bayesian Data Analysis in Python

Preparing Video For Download...