eviews中的蒙特卡洛模拟程序.docx
《eviews中的蒙特卡洛模拟程序.docx》由会员分享,可在线阅读,更多相关《eviews中的蒙特卡洛模拟程序.docx(15页珍藏版)》请在冰豆网上搜索。
![eviews中的蒙特卡洛模拟程序.docx](https://file1.bdocx.com/fileroot1/2023-1/23/6ebc0954-84b1-427b-bd71-324048909890/6ebc0954-84b1-427b-bd71-3240489098901.gif)
eviews中的蒙特卡洛模拟程序
eviews中的蒙特卡洛模拟程序
1.通过已知总体模型得到多组样本数据,进行多次回归,验证回归结果的特征、性质
最小二乘法的无偏性
workfilemcu110
vector(10)v1
v1.fill80,100,120,140,160,180,200,220,240,260
mtos(v1,x)
!
b1=25
!
b2=0.5
matrix(100,2)f
for!
k=1to100
seriesu=3*nrnd
seriesy=!
b1+!
b2*x+u
equationeq.lsy=c
(1)+c
(2)*x
f(!
k,1)=c
(1)
f(!
k,2)=c
(2)
next
showf
expand1100
smpl1100
mtos(f,gr)
freezeser01.qqplot
freezeser01.hist
freezeser02.qqplot
freezeser02.hist
matrix(1,2)m
m(1,1)=@mean(ser01)
m(1,2)=@mean(ser02)
showm
蒙特卡洛模拟程序:
(最终调试成功)
'storemontecarleresultsinaseries
'checked4/1/2004
'setworkfilerangetonumberofmontecarlereplications
wfcreatemcarleu1100
'createdataseriesforx
'note:
xisfixedinrepeatedsamples
'onlyfirst10observationsareused(remaining90obsmissing)
seriesx
x.fill80,100,120,140,160,180,200,220,240,260
'settrueparametervalues
!
beta1=2.5
!
beta2=0.5
'setseedforrandomnumbergenerator
rndseed123456
'assignnumberofreplicationstoacontrolvariable
!
reps=100
'beginloop
for!
I=1to!
reps
'setsampletoestimationsample
smpl110
'simulateydata(onlyfor10obs)
seriesy=!
beta1+!
beta2*x+3*nrnd
'regressyonaconstantandx
Equationeq1.lsycx
'setsampletooneobservation
smpl!
I!
i
'andstoreeachcoefficientestimateinaseries
seriesb1=eq1.@coefs
(1)
seriesb2=eq1.@coefs
(2)
next
'endofloop
'setsampletofullsample
smpl1100
'showkerneldensityeatimateforeachcoef
freeze(gra1)b1.distplotkernel
'drowverticaldashlineattrueparametervalue
gra1.draw(dashline,bottom,rgb(156,156,156))!
beta1
showgra1
freeze(gra2)b2.distplotkernel
'drawverticaldashlineattrueparametervalue
gra2.draw(dashline,bottom,rgb(156,156,156))!
beta2
showgra2
一元回归参数的分布:
Subroutinemoni(scalarn,scalarsum,scalarparam1,scalarparam2,scalartype)
For!
m=1ton
X(!
m)=@rnd*100
Next
For!
n=1tosum
Iftype=0then
For!
m=1ton
U(!
m)=@nrnd
Next
Endif
Iftype=1then
For!
m=1ton
If@rnd<0.5then
U(!
m)=@rnd
Else
U(!
m)=@rnd*(-1)
Endif
Next
Endif
Iftype=2then
U
(1)=@nrnd
For!
m=2ton
U(!
m)=u(!
m-1)+@nrnd
Next
Endif
Iftype=3then
For!
m=1ton
If@rnd<0.5then
U(!
m)=@nrnd
Else
U(!
m)=@nrnd*2
Endif
Next
Endif
For!
m=2ton
Y(!
m)=param1+param2*x(!
m)+u(!
m)
Next
Equationeq.lsycx
B1(!
n)=eq.@coefs
(1)
B2(!
n)=eq.@coefs
(2)
Next
Endsub
Workfilemoniu110000
Seriesb1
Seriesb2
Seriesx
Seriesy
Seriesu
Callmoni(100,10000,20,0.8,3)
B1.hist
B2.hist
生成季度虚拟变量的程序:
wfcreatedumtestq19701990
%start="1972:
1"
%end="1979:
4"
for!
i=@dtoo(%start)to@dtoo(%end)
%obsstr=@otod(!
i)
if(@mid(%obsstr,5,1)=":
")then
%name="d_"+@left(%obsstr,4)+"_"+@mid(%obsstr,6)
else
%name="d_"+%obsstr
endif
smpl@all
series{%name}=0
smpl{%obsstr}{%obsstr}
series{%name}=1
next
下面这个执行不了:
!
n=10000
wfcreatecaou1!
n
matrix(!
n,18)m
groupg
for%1r0b0t0dw0r20f0r1b1t1dw1r21f1r2b2t2dw2r22f2
series{%1}=0
g.add{%1}
next
for!
k=1to!
n
smpl1122
for%1y0x0y1x1y2x2
series{%1}=@rnorm
next
smpl2122
x1=x1(-1)+@rnorm
y1=y1(-1)+@rnorm
smpl2122
x2=2*x2(-1)-x2(-2)+@rnorm
y2=2*y2(-1)-y2(-2)+@rnorm
smpl1122
for%1%2%31y0x07y1x113y2x2
m(!
k,{%1})=@cor({%2},{%3})
equationeq.ls{%2}c{%3}
m(!
k,{%1}+1)=eq.c
(2)
m(!
k,{%1}+2)=eq.@tstats
(2)
m(!
k,{%1}+3)=eq.@dw
m(!
k,{%1}+4)=eq.@r2
m(!
k,{%1}+5)=eq.@f
next
next
smpl1!
T
mtos(m,g)
for%1t0t1t2
seriescao_{%1}=@abs({%1})>=2
freeze(statby_{%1}){%1}.statby(nomean,nostd)cao_{%1}
next
for%1r0b0t0dw0r20f0r1b1t1dw1r21f1r2b2t2dw2r22f2
freeze(hist_{%1}){%1}.hist
next
伪回归相关系数模拟(不行)
workfilecorru1500
seriesresult
for!
i=1to500
smpl1100
seriesx=nrnd
seriesy=nrnd
seriesxx
seriesyy
scalarsum1=0
scalarsum2=0
for!
counter=1to100
sum1=sum1+x(!
counter)
sum2=sum2+y(!
counter)
xx(!
counter)=sum1
yy(!
counter)=sum2
next
next
scalarr=@cor(xx,yy)
result(!
i)=r
时变贝塔系数的模拟(执行不了):
'BETA.PRG(3/7/2007)
'Timevaryingbeta
'demonstratesseveralways
'toobtainbetabetweenassets
'1)constantbeta
'2)rollingbetabyregression/movingcov/var
'3)usingstatespace
'4)usingmultivariateARCH
'Checked3/20/2007
'changepathtoprogrampath
%path=@runpath
cd%path
'loadworkfile
loadfx.wf1
'dependentvariablesofseriesmustbecontinuous
smpl@all
seriesy1=@pch(index)
seriesy2=@pch(jy)
'------------------------------------------------------------------------
'calculatetheconstantbetausingOLS
'------------------------------------------------------------------------
smpl1990@last
equationconstant_beta.lsy2cy1
seriesbeta_const=c
(2)
'------------------------------------------------------------------------
'calculatingtimevaryingbetawithrollingregression
'forabi-variatecasecanusemovingcov/varinstead
'ofOLSregression
'------------------------------------------------------------------------
!
ssize=200
seriesbeta_roll=@movcov(y1,y2,!
ssize)/@movvar(y1,!
ssize)
'codeforrunningarollingregression:
'commentedoutrightnow
'!
length=@obs(y1)
'equationroll_beta.lsy2cy1
'showroll_beta
'for!
i=1to!
length-!
ssize+1
'smpl@first+!
i-1@first+!
i+!
ssize-2
'equationroll_beta.lsy2cy1
'smpl@first+!
i+!
ssize-2@first+!
i+!
ssize-2
'beta_roll=roll_beta.@coefs
(2)
'next
'------------------------------------------------------------------------
'calculatebetawithStateSpace
'viaatime-varyingcoefficientforY1
'------------------------------------------------------------------------
smpl1990@last
sspacessbeta
ssbeta.appendy2=c
(1)+sv1*y1+[var=exp(c
(2))]
ssbeta.append@statesv1=sv1(-1)
ssbeta.ml
ssbeta.makestatesbeta_*
renamebeta_sv1beta_ss
'------------------------------------------------------------------------
'calculatebetawithsystemARCH
'byestimatingthecovarianceandvarianceof
'thetwoseriesusingMultivariateARCH
'------------------------------------------------------------------------
systemarbeta
arbeta.appendy1=c
(1)
arbeta.appendy2=c
(2)
arbeta.arch@Diagvechc(indef)arch(1,indef)garch(1,indef)
arbeta.makegarch(name=arch)
seriesbeta_arch=arch01_02/arch01
'------------------------------------------------------------------------
'displaythedifferentbetas
'------------------------------------------------------------------------
groupbetas_ls_rollbeta_constbeta_roll
groupbetas_roll_ss_archbeta_rollbeta_ssbeta_arch
showbetas_ls_roll.line
showbetas_roll_ss_arch.line
在生成服从某种分布(标准正态分布白噪声序列)的随机序列的基础上生成各种ARIMA序列:
AR
(1)、MA
(1)、ARMA(1,1)、ARIMA(1,1,1)
⏹Workfilerandom2u11000
⏹Seriesu=@nrnd
⏹U
(1)=0
⏹Seriesx1
⏹X1
(1)=0
⏹Seriesx2
⏹X2
(1)=0
⏹Seriesx3
⏹X3
(1)=0
⏹Seriesx4
⏹X4
(1)=0
⏹Smpl21000
⏹x1=0.8*x1(-1)+u
⏹X2=u+0.8*u(-1)
⏹X3=0.8*x3(-1)+u+0.8*u(-1)
⏹X4=x4(-1)+x3
⏹Smpl@all
⏹groupmyx1x2x3
⏹showmy.line
⏹workfilesimuu11000
⏹seriesx1=@runif(0,1)
⏹seriesx2=@runif(0,1)
⏹seriesx3=@runif(0,1)
⏹groupgx1x2x3
⏹g.line
⏹g.bar
⏹scatx1x2
⏹sortx1
⏹genry=10+2*x1
⏹y.line
⏹freezey
⏹y.hist
⏹scalaryy=@mean(y)
⏹scalaryy1=@skew(y)
⏹scalaryy3=@kurt(y)