矩母函数:
伯努利实验:
模型:扔硬币,正面朝上的概率为
,正面朝下的概率
( 0 1 分布、两点分布)
分布函数:
似然函数:
二项分布:
模型:n次伯努利试验成功了x次
分布函数:
matplotlib可视化验证二项分布的性质:
import numpy as np
import matplotlib.pyplot as plt
plt.xlim((0,100))
plt.ylim((0,0.27))
sample1 = np.random.binomial(10, 0.5, size=1000)
sample2 = np.random.binomial(100, 0.5, size=1000)
sample3 = np.random.binomial(110, 0.5, size=1000)
pillar=100
s1=plt.hist(sample1, rwidth=0.9, alpha=0.6, density=True, label="n1",bins=pillar,range=[0,pillar])
plt.plot(s1[1][0:pillar],s1[0],color='blue')
s2=plt.hist(sample2, rwidth=0.9, alpha=0.6, density=True, label="n2",bins=pillar,range=[0,pillar])
plt.plot(s2[1][0:pillar],s2[0],color='orange')
s3=plt.hist(sample3, rwidth=0.9, alpha=0.6, density=True, label="n3",bins=pillar,range=[0,pillar])
plt.plot(s3[1][0:pillar],s3[0],color='g')
s4=plt.hist(sample1 + sample2, rwidth=0.9, alpha=0.6, density=True, label="n1+n2",bins=pillar,range=[0,pillar])
plt.plot(s4[1][0:pillar],s4[0],color='r')
plt.legend()
plt.show()
pyecharts:
import numpy as np
from pyecharts.charts import *
import collections
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
from pyecharts.globals import ThemeType
sample1 = np.random.binomial(10, 0.5, size=1000)
sample2 = np.random.binomial(100, 0.5, size=1000)
sample3 = np.random.binomial(110, 0.5, size=1000)
list_x0 = sorted(dict(collections.Counter(sample1 + sample2)))
list_y0 = [dict(collections.Counter(sample1 + sample2))[i] for i in list_x0]
list_y0 = [i / sum(list_y0) for i in list_y0]
list_x1 = sorted(dict(collections.Counter(sample1)))
list_y1 = [dict(collections.Counter(sample1))[i] for i in list_x1]
list_y1 = [i / sum(list_y1) for i in list_y1]
list_x2 = sorted(dict(collections.Counter(sample2)))
list_y2 = [dict(collections.Counter(sample2))[i] for i in list_x2]
list_y2 = [i / sum(list_y2) for i in list_y2]
list_x3 = sorted(dict(collections.Counter(sample3)))
list_y3 = [dict(collections.Counter(sample3))[i] for i in list_x3]
list_y3 = [i / sum(list_y3) for i in list_y3]
line = (
Line(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis([str(i) for i in list_x3])
.add_yaxis("n1+n2", list_y0, is_smooth=True)
.add_yaxis("n1", list_y1, is_smooth=True)
.add_yaxis("n2", list_y2, is_smooth=True)
.add_yaxis("n3", list_y3, is_smooth=True)
.set_global_opts(legend_opts=opts.LegendOpts(orient="vertical", pos_right='10%'),
title_opts=opts.TitleOpts(title="Bernoulli", pos_left="center"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)
make_snapshot(snapshot, line.render(), "Bernoulli.png")