eviews中的蒙特卡洛模拟程序文件_第1页
eviews中的蒙特卡洛模拟程序文件_第2页
eviews中的蒙特卡洛模拟程序文件_第3页
eviews中的蒙特卡洛模拟程序文件_第4页
eviews中的蒙特卡洛模拟程序文件_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、模拟程序案例例1,在做抛掷一枚质地均匀的硬币的试验中发“正面朝上”的事件(用 1表示)和“正面朝下”的事件A (用0表示)的情况。历史上一些学者得到的具体试验结果如下:现在需要利用eviews来模拟上述三位学者的实验。算法分析:上述三学者的实验均为二项分布的实验,可以直接利用eviews产生二项分布随机数的函数rbinom (n, p).编程如下:workfile binom u 1 2048 series result for !i=0 to 1 smpl 1 2048 series x x(1)=0for !cou=1 to 2048 x(!cou)=rbinom(!i,0.5) next

2、 nextSeries: XSample 1 2048Observations 2048Mean0.507813Median1.000000Maximum1.000000Minimum0.000000Std. Dev.0.500061Skewness-0.031254Kurtosis1.000977Jarque-Bera341.3334Probability0.000000 x.hist 1,2001,000 -800 _600 -400 -200 -0-,1,11,10.00.10.20.30.40.50.60.70.80.91.0例2 (投掷骰子)(1)投掷一颗质地均匀的骰子,令 X表示其

3、出现的点数,分析各点数出 现的频率的稳定性及变化规律;(2)利用统计的方法,根据“频率的稳定性”规律求投掷一 枚质地不均匀的骰子出现某点数的概率;(3)演示随机变量X的数学期望的统计意义。算法分析:根据逆变换法产生来自分布函数 F (x)的随机数,就要求出F-1 (y),其中F-1 (y) =infx: F (x)y.0&y& 1.质地均匀的骰子各点数出现的频率的分布函数是F (x) =p (xx) = (i=1) /6, i-1 xy.0y 1=i-1 , (i-1) /6yi/6, i=1 ,,6因而,可先由产生均匀分布随机数的函数runif (0, 1)抽取y值,再来计算F-1 (y)值

4、即可。程序实现:workfile binom u 1 1000smpl 1 1000series xseries 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(!i)=a4(!i) and x(!i)=a5(!i) and x(!i)a6(!i

5、) then y(!i)=6 else y(!i)=7endifendifendifendifendifendifnexty.histSeries: YSample 1 1000Observations 1000Mean Median Maximum Minimum Std. Dev. Skewness KurtosisJarque-Bera Probability3.5240003.0000006.0000001.0000001.680550 -0.002354 1.76118863.944890.0000001.通过已知总体模型得到多组样本数据,进行多次回归,验证回归结果的特征、性质 最小

6、二乘法的无偏性workfile mc u 1 10vector(10) v1vl.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 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.

7、histfreeze ser02.qqplotfreeze ser02.histmatrix(1,2) mm(1,1)=mean(ser01)m(1,2)=mean(ser02)show ma ELONfo sownnauQ.46.46.47 .48.49.50.51.52.53.54.55a ELONf o54321098755555544432161618202224262830308 6 42 2 222o 82 1Quantiles of SER02Quantiles of SER01蒙特卡洛模拟程序:(最终调试成功)store monte carle results in a ser

8、ieschecked 4/1/2004set workfile range 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 xx.fill 80,100,120,140,160,180,200,220,240,260set true parameter values!

9、beta1=2.5!beta2=0.5set seed for random number generatorrndseed 123456assign number of replications to a control variable!reps=100begin loopfor !I=1 to !repsset sample to estimation samplesmpl 1 10simulate y data (only for 10 obs)series y=!beta1 + !beta2*x+3*nrndregress y on a constant and xEquation

10、eq1.ls y c xset sample to one observationsmpl !I !iand store each coefficient estimate in a seriesseries b1=eq1.coefs(1) series b2=eq1.coefs(2) next end of loopset sample to full samplesmpl 1 100show kernel density eatimate for each coef freeze(gra1) b1.distplot kernel!beta1!beta2drow vertical dashl

11、ine at true parameter value gra1.draw(dashline,bottom,rgb(156,156,156) show gra1freeze(gra2) b2.distplot kerneldraw vertical dashline at true parameter value gra2.draw(dashline,bottom,rgb(156,156,156) show gra2一元回归参数的分布:Subroutine moni (scalar n, scalar sum, scalar param1, scalar param2, scalar type

12、) For !m=1 to nX(!m)=rnd*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 then For !m=1 to nIf rnd=2freeze(statby_%1) %1.statb

13、y(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.histnext伪回归相关系数模拟(不行)workfile corr u 1 500 series resultfor !i=1 to 500smpl 1 100series x=nrnd series y=nrnd series xx series yyscalar sum1=0scalar sum2=0 for !counter=1 to 100 sum1=sum1+x(!

14、counter) sum2=sum2+y(!counter) xx(!counter)=sum1 yy(!counter)=sum2 next nextscalar r=cor(xx,yy) result(!i)=r时变贝塔系数的模拟(执行不了):BETA.PRG (3/7/2007) Time varying beta demonstrates several ways to obtain beta between assets1) constant beta2) rolling beta by regression/moving cov/var3) using state space4)

15、using multivariate ARCHChecked 3/20/2007change path to program path%path = runpathcd %pathload workfileload 仅.wf1dependent variables of series must be continuous smpl allseries y1 = pch(index)series y2 = pch(jy) calculate the constant beta using OLSsmpl 1990 lastequation constant_beta.ls y2 c y1seri

16、es beta_const=c(2)calculating time varying beta with rolling regression for a bi-variate case can use moving cov/var instead of OLS regression!ssize = 200series beta_roll=movcov(y1,y2,!ssize)/movvar(y1,!ssize)code for running a rolling regression:commented out right now!length = obs(y1)equation roll

17、_beta.ls y2 c y1show roll_betafor !i = 1 to !length-!ssize+1smpl first+!i-1 first+!i+!ssize-2equation roll_beta.ls y2 c y1smpl first+!i+!ssize-2 first+!i+!ssize-2beta_roll = roll_beta.coefs(2)nextcalculate beta with State Spacevia a time-varying coefficient for Y1 smpl 1990 lastsspace ssbetassbeta.a

18、ppend y2=c(1)+sv1*y1+var=exp(c(2)ssbeta.append state sv1 = sv1(-1)ssbeta.mlssbeta.makestates beta_* rename beta_sv1 beta_sscalculate beta with system ARCHby estimating the covariance and variance of the two series using Multivariate ARCHsystem arbetaarbeta.append y1 = c(1)arbeta.append y2 = c(2)arbeta.arch Diagvech c(indef) arch(1,indef) garch(1,indef)arbeta.makegarch(name=arch)series beta_arch = arch01_02/arch01 display the different betasgroup betas_ls_roll beta_const beta_rollgroup betas_roll_ss_arch beta_roll beta_ss

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论