1、eviews中的蒙特卡洛模拟程序模拟程序案例例 1,在做抛掷一枚质地均匀的硬币的试验中发“正面朝上”的事件(用 1 表示)和“正面朝下”的事件 A(用 0 表示)的情况。历史上一些学者得到的具体试验结果如下:现在需要利用 eviews 来模拟上述三位学者的实验。算法分析:上述三学者的实验均为二项分布的实验,可以直接利用 eviews 产生二项分布随机数的函数rbinom(n,p).编程如下:work u 1 2048series resultfor !i=0 to 1smpl 1 2048series xx(1)=0for !cou=1 to 2048x(!cou)=rbinom(!i,0.5
2、)nextnextx.hist例 2(投掷骰子)(1)投掷一颗质地均匀的骰子,令 X 表示其出现的点数,分析各点数出现的频率的稳定性及变化规律;(2)利用统计的方法,根据“频率的稳定性”规律求投掷一枚质地不均匀的骰子出现某点数的概率;(3)演示随机变量 X 的数学期望的统计意义。算法分析:根据逆变换法产生来自分布函数F(x)的随机数,就要求出 F-1(y),其中 F-1(y)infx:F(x)y.0y1.质地均匀的骰子各点数出现的频率的分布函数是F(x)=p(xx)=(i=1)/6,i-1xi,i=1,2,7可求得F-1(y)=infx:F(x)y.0y1=i-1,(i-1)/6yi/6,i=
3、1,6因而,可先由产生均匀分布随机数的函数 runif(0,1)抽取 y 值,再来计算 F-1(y)值即可。程序实现:work u 1 1000smpl 1 1000series x series yseries a1series a2series a3series a4series a5series a6for !i=1 to 1000a1(!i)=1/6a2(!i)=2/6a3(!i)=3/6a4(!i)=4/6a5(!i)=5/6a6(!i)=1x(!i)=runif(0,1)if x(!i)=a1(!i) and x(!i)=a2(!i) and x(!i)=a3(!i) and x(
4、!i)=a4(!i) and x(!i)=a5(!i) and x(!i)a6(!i) then y(!i)=6else y(!i)=7endifendifendifendifendifendifnexty.hist1.通过已知总体模型得到多组样本数据,进行多次回归,验证回归结果的特征、性质最小二乘法的无偏性work u 1 10vector(10) v1v1.fill 80, 100,120,140,160,180,200,220,240,260mtos(v1,x)!b1=25!b2=0.5matrix(100,2) ffor !k=1 to 100series u=3*nrndseries
5、 y=!b1+!b2*x+uequation eq.ls y=c(1)+c(2)* xf(!k,1)=c(1)f(!k,2)=c(2)nextshow fexpand 1 100smpl 1 100mtos(f,gr)freeze ser01.qqplotfreeze ser01.histfreeze ser02.qqplotfreeze ser02.histmatrix(1,2) mm(1,1)=mean(ser01)m(1,2)=mean(ser02)show m蒙特卡洛模拟程序:(最终调试成功)store monte carle results in a serieschecked 4/
6、1/2004set work to number of monte carle replicationswfcreate mcarle u 1 100create data series for xnote: x is fixed in repeated samplesonly first 10 observations are used (remaining 90 obs missing)series x x.fill 80,100,120,140,160,180,200,220,240,260set true parameter values!beta1=2.5!beta2=0.5set
7、seed for random number generatorrndseed 123456assign number of replications to a control variable!reps=100begin loopfor !I=1 to !reps set sample to estimation sample smpl 1 10 simulate y data (only for 10 obs) series y=!beta1+!beta2*x+3*nrnd regress y on a constant and x Equation eq1.ls y c xset sam
8、ple to one observationsmpl !I !iand store each coefficient estimate in a series series b1=eq1.coefs(1)series b2=eq1.coefs(2)nextend of loopset sample to full samplesmpl 1 100show kernel density eatimate for each coeffreeze(gra1) b1.distplot kerneldrow vertical dashline at true parameter valuegra1.dr
9、aw(dashline,bottom,rgb(156,156,156) !beta1show gra1freeze(gra2) b2.distplot kerneldraw vertical dashline at true parameter valuegra2.draw(dashline,bottom,rgb(156,156,156) !beta2show gra2一元回归参数的分布:Subroutine moni (scalar n, scalar sum, scalar param1, scalar param2, scalar type)For !m=1 to nX(!m)=rnd*
10、100NextFor !n=1 to sumIf type=0 thenFor !m=1 to nU(!m)=nrndNextEndifIf type=1 thenFor !m=1 to nIf rnd0.5 thenU(!m)=rndElseU(!m)=rnd*(-1)EndifNextEndifIf type=2 thenU(1)=nrndFor !m=2 to nU(!m)=u(!m-1)+nrndNextEndifIf type=3 thenFor !m=1 to nIf rnd=2freeze(statby_%1) %1.statby(nomean,nostd)cao_%1nextfor %1 r0 b0 t0 dw0 r20 f0 r1 b1 t1 dw1 r21 f1 r2 b2 t2 dw2 r22 f2freeze(hist_%1) %1.h
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1