小马哥课堂-统计学-大数定理

Law of large numbers(大数定理)

the law of large numbers (LLN) is a theorem that describes the result of performing the same experiment a large number of times. According to the law,the average of the results obtained from a large number of trials should be close to the expected value,and will tend to become closer as more trials are performed.

当试验次数足够大时,样本均值趋向于总体均值。

实例

以掷骰子为例,已知总体均值为3.5,随着实验次数增加,样本均值将发生怎样的变化呢?

#!/usr/bin/env python3                          
#-*- coding:utf-8 -*-
#############################################
#File Name: lln.py
#Brief: 以掷骰子为例,演示随着试验次数增加,样本均值趋于总体均值
#Author: frank
#Email: [email protected]
#Created Time:2018-08-07 22:29:00
#Blog: http://www.cnblogs.com/black-mamba
#Github: https://github.com/suonikeyinsuxiao
#############################################
import numpy as np
import matplotlib.pyplot as plt

#总实验次数
total_num = 1000
#骰子的6面
num_sides = 6

plt.figure(1)

#设置x轴的范围
xmin,xmax = plt.xlim(0,total_num)
print("xmin:{},xmax:{}".format(xmin,xmax))

#设置y轴的范围
ymin,ymax = plt.ylim(1,6)
print("ymin:{},ymax:{}".format(ymin,ymax))

#设置坐标轴的名称
plt.xlabel(‘number of trials‘)
plt.ylabel(‘average‘)



#画均值线
plt.plot((0,total_num),(3.5,3.5),c=‘b‘)

l = []
n_s = range(1,total_num)

#number of trials:n,mean of sample:average
for n in  n_s:
    average = np.mean(np.random.randint(1,7,n))
    l.append(average)
    #print("n:{},average:{}".format(n,average))
    #plt.scatter(n,average,c=‘r‘,s=1,alpha=0.5) 

plt.plot(n_s,l,alpha=0.5,c=‘red‘)


plt.figure(2)
#设置x轴的范围
ymin,ymax = plt.ylim(0,total_num)
print("ymin:{},ymax))
#设置y轴的范围
xmin,xmax = plt.xlim(1,6)
print("xmin:{},xmax))

#设置坐标轴的名称
plt.xlabel(‘number of trials‘)
plt.ylabel(‘average‘)

plt.plot((3.5,(0,c=‘b‘)

#plt.plot(l,n_s,c=‘red‘)
for n in  n_s:
    average = np.mean(np.random.randint(1,average))
    plt.scatter(average,n,alpha=0.5) 

plt.show()

分享图片

分享图片

相关文章

自1998年我国取消了福利分房的政策后,房地产市场迅速开展蓬...
文章目录获取数据查看数据结构获取数据下载数据可以直接通过...
网上商城系统MySql数据库设计
26个来源的气象数据获取代码
在进入21世纪以来,中国电信业告别了20世纪最后阶段的高速发...