本实验通过程序模拟采集大量的样本数据来验证辛钦大数定理。
实验环境:
本实验采用Java语言编程,开发环境为Eclipse,图像生成使用JFreeChart类。
一,验证辛钦大数定理
由辛钦大数定理描述为:
辛钦大数定理(弱大数定理) 设随机变量序列 X1, X2, … 相互独立,服从同一分布,具有数学期望E(Xi) = μ, i = 1, 2, …, 则对于任意正数ε ,有
即
实验思路:
实验产生的随机变量Xi服从均匀分布与(0-1)分布,即X~U(0,1)或X~b(1,0.5)首先随机产生5000(0,1)内,已知X服从均匀分布或(0-1)分布,所以均值E(X)=(a+b)/2=0.5。且随机变量的方差相等,统计样本容量为n的样本算术平均值,n以10为步长线性增加,画出()的图像,将其与y=0.5的图像对比,可得,当n越来越大时,趋向于均值E(X)=0.5,即
实验画得如下图一:
图一
由图可看出,当数据点足够多时
实验程序如下,程序已经加上注释:
import java.awt.Color; import java.util.Random; import java.util.SortedSet; import java.util.TreeSet; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.function.Function2D; import org.jfree.data.function.NormalDistributionFunction2D; import org.jfree.data.general.DatasetGroup; import org.jfree.data.general.DatasetUtilities; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; public class KhinchinBigDataTheorem { /********************************* *样本点集 ********************************/ private static XYSeriesCollection dataset=new XYSeriesCollection(); /********************************** * getXYSeriesCollection() * 获得样本点XY坐标点集XYSeriesCollection * @return *********************************/ public static XYSeriesCollection getXYSeriesCollection(){ XYSeries series= new XYSeries("Khinchin"); int sampleSize=5000; //验证样本容量 int bin=10; //以步长为bin进行样本概率统计 int poltSize=sampleSize/bin; //样本分成的区间数 double[] sampleProbability=new double[poltSize]; //每个区间内出现的点得数量的矩阵 double[] XAxis=new double[poltSize]; //每个区间所采取的Xi(X轴坐标点)的矩阵 for (int i = 0; i < XAxis.length; i++) { sampleProbability[i]=0; XAxis[i]=0; } /*************************************************** * 产生500000个(0,1)内均匀分布与(0-1)分布的样本点 * 画出样本数量从少到多的算术平均值趋向于均值的差距 ***************************************************/ double u=0.5; //样本服从的均值 double[] samplePoints=new double[sampleSize]; //分布的样本点 int su=0; for (int i = 0; i < samplePoints.length; i++) { //交替产生均匀分布与(0-1)分布样本点 if (i%2==0) { samplePoints[i]=new Random().nextDouble(); }else { samplePoints[i]=generator(0.5); } } double sum=0; for (int i = 0; i < samplePoints.length; i++) { sum+=samplePoints[i]; if (i%bin==0) { XAxis[i/bin]=i; sampleProbability[i/bin]=sum/(i+1); //System.out.println(sampleProbability[i/bin]); } } for (int i = 0; i < poltSize ; i++) { series.add(XAxis[i], sampleProbability[i]); } dataset.addSeries(series); return dataset; } /********************************************** * 产生概率为0.5的(0-1)分布点 * @param p * @return **********************************************/ public static int generator(double p){ Random random=new Random(); double g=random.nextDouble(); int i=0; if(g<p){ i=1; }else { i=0; } return i; } public XYSeriesCollection dataset1; public JFreeChart chart; public XYPlot plot; public KhinchinBigDataTheorem() { //KhinchinBigDataTheorem centerLimit=new KhinchinBigDataTheorem(); dataset1=getXYSeriesCollection(); //获取样本数据集 XYSeriesCollection dataset=new XYSeriesCollection(); XYSeries series= new XYSeries("0.5 Line"); for (int i = 0; i < 500; i++) { series.add(i*10.0, 0.5); } dataset.addSeries(series); chart = ChartFactory.createXYLineChart("MultiAxis", "X axis", "First Y Axis", dataset1, PlotOrientation.VERTICAL, true, true, false); plot = chart.getXYPlot(); plot.setDataset(1, dataset); XYLineAndShapeRenderer render2 = new XYLineAndShapeRenderer(); render2.setSeriesPaint(0, Color.BLUE); plot.setRenderer(1, render2); } public static void main(String[] agrs) { KhinchinBigDataTheorem obj = new KhinchinBigDataTheorem(); ChartFrame frame = new ChartFrame("多坐标轴", obj.chart); frame.pack(); frame.setVisible(true); } }