微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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()

分享图片

分享图片

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐