楼主: tangjunbo
15636 17

[程序分享] 用MATLAB进行蒙特卡洛模拟? [推广有奖]

  • 0关注
  • 0粉丝

大专生

96%

还不是VIP/贵宾

-

威望
0
论坛币
21 个
通用积分
0.0092
学术水平
0 点
热心指数
1 点
信用等级
0 点
经验
534 点
帖子
89
精华
0
在线时间
48 小时
注册时间
2009-12-29
最后登录
2018-10-9

楼主
tangjunbo 发表于 2011-5-10 19:41:50 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
请问有谁知道用MATLAT如何进行蒙特卡洛模拟?我要做毕业论文,处理上证指数。跪求,谢谢!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:MATLAB 蒙特卡洛模拟 matla atlab 蒙特卡洛 MATLAB 模拟 蒙特卡洛

沙发
yucongy 发表于 2011-5-10 20:02:31
蒙特卡洛模拟?用R语言吧,比较方便,也不怎么需要编程

藤椅
maximum 发表于 2011-5-10 20:07:27
如果连这个都不会,还是别做了。

板凳
tangjunbo 发表于 2011-5-11 21:56:07
请不要灌水啊,哈哈。不会才问吗!

报纸
tulipsliu 在职认证  发表于 2011-5-21 19:50:07
不知道你是问哪一方面的。
在最新版本的matlab里,econometrics toolbox 里的 期权定价,信用风险分析等,这些demo都是蒙特靠罗模拟的。
劳动经济学

地板
tulipsliu 在职认证  发表于 2011-5-21 19:50:49
我贴一个我去年改装的GARCH 的蒙特卡洛模拟的程序吧。数据应该是调用demo里的。安装了matlab,就有那个数据的。
可以直接调用。
劳动经济学

7
tulipsliu 在职认证  发表于 2011-5-21 19:53:50
哦,处理上证指数哦。哈哈。
劳动经济学

8
tulipsliu 在职认证  发表于 2011-5-21 19:55:39
conn=yahoo;
enddate=datestr(today);
SZindex=fetch(‘000001.ss’,'close','01/01/2001',enddate,d);
忘记最后一个参数了,好像设置为每一日的数据,应该有单引号,也好像没有,可以 help fetch。
劳动经济学

9
tulipsliu 在职认证  发表于 2011-5-21 20:00:44
  1. conn=yahoo;
  2. SZindexprice=fetch(conn,'000001.ss','close','01/01/2001',05/23/2011',d);
  3. retSZindex=price2ret(SZindexprice);
  4. markpound = retSZindex;
  5. [H,pValue,Stat,CriticalValue] = lbqtest(markpound-mean(markpound),[10 15 20]',0.05);
  6. [H1,pValue,Stat,CriticalValue] = lbqtest((markpound-mean(markpound)).^2,[10 15 20]',0.05);
  7. [coeff,errors,LLF,innovations,sigmas,summary] = garchfit(markpound);
  8. spec = garchset('VarianceModel','GJR','R',1,'M',1,'P',1,'Q',1);
  9. spec = garchset(spec,'Display','off','Distribution','T');
  10. [coeff,errros,LLF,eFit,sFit] = garchfit(spec,markpound);
  11. [eFit,sFit] = garchinfer(coeff,markpound);

  12. spec1=garchset;
  13. [coeff1,erros1,LLF1,eFit1,sFit1]=garchfit(spec1,markpound);


  14. horizon = 20;
  15. nPaths = 20000;
  16. strm = RandStream('mt19937ar','Seed',5489);
  17. RandStream.setDefaultStream(strm);
  18. [eSim,sSim,ySim] = garchsim(coeff,horizon,nPaths,[],[],[],eFit,sFit,markpound);
  19. Y=ret2price(ySim);
  20. [eSim1,sSim1,ySim1]=garchsim(coeff1,horizon,nPaths,[],[],[],eFit1,sFit1,markpound);
  21. Y1=ret2price(ySim1);

  22. subplot(2,1,1),plot(Y)
  23. subplot(2,1,2),plot(Y1)

  24. %% expirement hist plot usd prctile
  25. exret=mean(Y1)*100;
复制代码
劳动经济学

10
tulipsliu 在职认证  发表于 2011-5-21 20:08:21
  1. %% This program is writed by Danniel tulip lew
  2. %   Danny have this program`s copyright  @2010

  3. %% The first step:Loading data to whcih will be used to analysis
  4. CleanUp;

  5. load mygarchdata

  6. markpound = SSret;
  7. figure
  8. plot(markpound)
  9. title('市场序列')

  10. adfval = adftest(SSret);
  11. disp('The test value of Augmented Dickey-Fuller test for unit root');
  12. disp('------------------------------------------------------------');
  13. disp(adfval)

  14. kpssval = kpsstest(SSret);
  15. disp('The test value of kpsstes');
  16. disp('---------------------------');
  17. disp(kpssval)

  18. [h,pValue,stat,cValue] = archtest(SSret,1:5,0.01);
  19. disp('Engle test for residual heteroscedasticity');
  20. disp('---------------------------------------------');
  21. disp(h)
  22. disp('----------------------------------------------');
  23. disp(pValue);

  24. %% Bulding GARCH Model and fit the parameters
  25. spec = garchset('VarianceModel','GJR','R',1,'M',1,'P',1,'Q',1);
  26. spec = garchset(spec,'Display','off','Distribution','T');
  27. [coeff,errros,LLF,~,~] = garchfit(spec,markpound);
  28. [eFit,sFit] = garchinfer(coeff,markpound);

  29. spec11=garchset;
  30. spec11=garchset(spec11,'Display','off');
  31. [coeff1,erros1,LLF1,eFit1,sFit1]=garchfit(spec11,markpound);

  32. %% Monte carlo simulation and visulise the simulation result using
  33. horizon = 30;
  34. nPaths = 10000;
  35. strm = RandStream('mt19937ar','Seed',5489);
  36. RandStream.setDefaultStream(strm);
  37. [eSim,sSim,ySim] = garchsim(coeff,horizon,nPaths,[],[],[],eFit,sFit,markpound);
  38. Y=ret2tick(ySim);
  39. [eSim1,sSim1,ySim1]=garchsim(coeff1,horizon,nPaths,[],[],[],eFit1,sFit1,markpound);
  40. Y1=ret2tick(ySim1);
  41. % plot result
  42. figure
  43. subplot(2,1,1),plot(Y)
  44. subplot(2,1,2),plot(Y1)

  45. %% Garch simulation and garch predict  
  46. NumPeriods=30;
  47. fprintf('Garchpred result: Sigma and Mena\n');
  48. [SigmaForecast,MeanForecast,sigmaTotal,meanRMSE] = ...
  49. garchpred(coeff,markpound,NumPeriods);
  50. disp('Sigma')
  51. disp(SigmaForecast)
  52. disp('Mean')
  53. disp(MeanForecast)
  54.    

  55. %% Standardized residuel

  56. figure
  57. autocorr(markpound);
  58. % Compare Sigmas
  59. figure
  60. plot(SigmaForecast,'.-b')
  61. hold('on')
  62. grid('on')
  63. plot(sqrt(mean(sSim.^2,2)),'.r')
  64. title('Forecast of STD of Residuals')
  65. legend('forecast results','simulation results')
  66. xlabel('Forecast Period')
  67. ylabel('Standard Deviation')

  68. % Compare Returns
  69. figure
  70. plot(MeanForecast,'.-b')
  71. hold('on')
  72. grid('on')
  73. plot(mean(ySim,2),'.r')
  74. title('Forecast of Returns')
  75. legend('forecast results','simulation results',4)
  76. xlabel('Forecast Period')
  77. ylabel('Return')

  78. % Compare Standard Errors
  79. figure
  80. plot(meanRMSE,'.-b')
  81. hold('on')
  82. grid('on')
  83. plot(std(ySim'),'.r')
  84. title('Standard Error of Forecast of Returns')
  85. legend('forecast results','simulation results')
  86. xlabel('Forecast Period')
  87. ylabel('Standard Deviation')



  88. %% Hypothesis test: | lrationtest |
  89. % A likelihood ratio test is another way to compare the model which is
  90. % beest
  91. [H,pValue]=lratiotest(LLF,LLF1,1);
  92. fprintf('Result of the GARCH model compare using "lrationtest"\n');
  93. if H==0
  94.         disp('The GARCH(1,1) model is suported at the 5% significant level')
  95.        else
  96.         disp('The GJR(1,1) model is suported at the 5% significant level')
  97. end

  98. %-----------------------------EOF------------------------------------------
复制代码
已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
Sunknownay + 100 + 10 + 1 + 1 + 1 热心帮助其他会员

总评分: 经验 + 100  论坛币 + 10  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

劳动经济学

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-4 07:37