数学笔记2:概率分布
1、离散随机变量概率分布
古典概型
import numpy as np
from numpy.random import random_sample
from matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('white')
from matplotlib import pylab
def weighted_values(values,probabilities,size):
bins = np.add.accumulate(probabilities)
return values[np.digitize(random_sample(size),bins)]
values = np.array([1,2,3,4,5,6])
probabilities = np.array([1/6, 1/6, 1/6, 1/6, 1/6, 1/6])
samples = weighted_values(values, probabilities, 100)
for value in values:
counts.append(len(len(sample[sample==values]))
plt.figure(figsize=(11,6))
plt.grid(True)
plt.title("Classical models of probability")
plt.xlable("values")
plt.ylable("Occurences")
plt.bar(values,counts)
plt.show()
二项分布&贝努利试验
from scipy.stats import binom
rv = binom(10, 0.7)
x = np.arange(-1, 12, 1)
y = rv.pmf(x)
plt.figure(figsize=(11,6))
plt.grid(True)
plt.title("binomial distribution")
plt.xlable("Value")
plt.ylable("P(X=x)")
plt.bar(x-0.2, y, width=0.4)
plt.show()
2、连续随机变量概率分布
均匀分布
a = 0.0
b = 8.0
x = np.linspace(a, b, 100)
y = [1/(b-a) for i in x]
正态分布
# 均值为0
mu1 = 0
mu2 = 0
# 方差分别为1和2
sigmal1 = 1
sigmal2 = 2
x = np.linspace(-8, 8, 200)
# 正态分布概率密度函数
y1 = (1/(sigmal1 * np.sqrt(2*np.pi)))*np.exp(-(x-mu1)/(2*sigmal*sigmal))
y2 = (1/(sigmal2 * np.sqrt(2*np.pi)))*np.exp(-(x-mu2)/(2*sigma2*sigma2))