本實驗通過程序模擬采集大量的樣本數據來驗證辛欽大數定理。
實驗環境:
本實驗采用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); } }