import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy.stats import binom
6 Minimum, Maxium, and Mixture
6.1 Cumulative Distriburtion Function (CDF)
我们先来看一下 Cumulative Distriburtion Function (CDF),用我们在 Chapter 4 中的例子。
Code
def normalize_array(arr):
return np.array([i/sum(arr) for i in arr])
def update_binom(heads, tosses, prior):
"""
heads: number of heads
tosses: total tosses
prior: prior distribution; should be a empiricaldist.pmf object (a Series)
"""
# 0/n, 1/n, 2/n ...
= np.array([i/(n-1) for i in range(n)])
likelihood_head = likelihood_head
coin_head_probabilities = binom.pmf(k = heads, n = tosses, p = coin_head_probabilities)
likelihood = prior.copy()
posterior *= likelihood
posterior return normalize_array(posterior)
# n: number of coins
= 1001
n = range(n)
x_axis = 250
tosses # number of heads out of 250 tosses
= 140
heads = np.array([1]*n)
prior = normalize_array(prior)
uniform = update_binom(heads, tosses, uniform) posterior
def get_cdf(arr):
"""Get cumulative distribution function
"""
= np.sum(arr)
total_sum = []
res sum = 0
for x in arr:
sum += x
# normaize to make sure the max in res is 1
sum/total_sum)
res.append(return res
= get_cdf(posterior) cdf
Code
=x_axis, y=cdf, label="CDF", color='orange', where='post')
plt.step(x# plt.scatter(x=x_axis, y = cdf, label="CDF", color = 'orange', s = 2)
= x_axis, y = posterior,
plt.scatter(x ="PMF", color="steelblue", s = 2)
label"Coin #")
plt.xlabel("Probability mass function")
plt.ylabel("CDF and PMF of posterior distribution of a uniform prior")
plt.title(
plt.legend() plt.show()
6.2 Mixture Distribution
假设袋子里有两种硬币 A 和 B。A 的数量占 60%。 硬币 A 的属性是,随机抛一下,正面朝上和背面朝上的概率都是 50%。硬币 B 的属性是,随机抛一下,正面朝上的概率是 70%,背面朝上的概率是 30%。
现在问你,随机抽一枚硬币,随机抛一下,正面朝上的概率是多少?应该很容易算。
= np.array([0.5, 0.7])
heads_prob = np.array([0.6, 0.4])
weights = (heads_prob * weights).sum()
res res
0.58
所以正面朝上的概率是 58%。