| 所在主题: | |
| 文件名: mcmc.txt | |
| 资料下载链接地址: https://bbs.pinggu.org/a-1564446.html | |
| 附件大小: | |
|
因为输入的参数都是要从excel表格里导入的向量,而且其中两部分的参数维数还不同,再加上又需要蒙特卡洛方法(即生成10000个路径),所以我只能编两个for循环:
下面是我写的code: function [price] = mcmc(S,K,B,T,sigma,v,theta,r,d,M,N,seed) %M is the number of time steps, %N is the number of paths, M = 100; N = 10000; MCpayoff = zeros(N,M+1); S=zeros(N,M+1); rand('seed',0); %S is the current price of the underlying asset, %K is the exercise price of the option, %B is the barrier of the option, %r is the risk-free rate, %d is the dividend, %T is the time to maturity, %sigma is the volatility of the asset price, NUM = xlsread('EuropeanUpAndOutCall.xls'); MCprice = zeros(26,5); for i = 1:26 B = NUM(i,1); K = NUM(i,2); %这两个参数的数据是26行的向量, for ii = 1:5 T = NUM(ii,3); sigma = NUM(ii,4); v = NUM(ii,5); theta = NUM(ii,6); r = NUM(ii,7); d = NUM(ii,8); S0 = NUM(ii,9); %这几个参数却是5行的向量, %choose a time discretization; Deltat=T/M; %simulate a random number of gamma processes,independent of all other yet %simulated random number, G = gamrnd(Deltat/v,v,N,M); %simulate a standard normally distributed random number Z_i, Z = normrnd(0,1,N,M); %the return is deltaX = theta*G+sigma*sqrt(G).*Z; X0 = zeros(N,M); X = X0+cumsum(deltaX,2); %the stock prices S(:,1) = S0*ones(N,1); t = (Deltat:Deltat:T); t = repmat(t,N,1); w = (1/v)*log(1-theta*v-sigma^2*v/2); S(:,2:M+1) = S0.*exp(r*t+X+w*t); %the payoff for a Down and Out Put Option ST = S(:,M+1); Max = max(S,[],2); for j=1:length(Max) ifMax(j,1) < B && K < S(j,M+1)%因为此处需要10000个元素的列向量的每个元素与B,K(26个元素的列向量)的每个元素比较,又设了个for循环和if循环 MCpayoff(j,1) = S(j,M+1)-K; else MCpayoff(j,1) = 0; end; end; %the option price is MCprice = exp(-r*T)*(1/N)*sum(MCpayoff(:,1));%此处是得出10000个路径的平均值,之后要针对有5个元素的列向量循环一次,然后是26个元素的列向量再循环一次。。 end end 我的问题是:类似这种代入参数维数不同的情况,有没有什么简化的方法?如果没有,循环的顺序是否先内再外呢?好复杂啊,我这个新手撑不住了。。光end都end了好几个,已经分不清了。。 |
|
熟悉论坛请点击新手指南
|
|
| 下载说明 | |
|
1、论坛支持迅雷和网际快车等p2p多线程软件下载,请在上面选择下载通道单击右健下载即可。 2、论坛会定期自动批量更新下载地址,所以请不要浪费时间盗链论坛资源,盗链地址会很快失效。 3、本站为非盈利性质的学术交流网站,鼓励和保护原创作品,拒绝未经版权人许可的上传行为。本站如接到版权人发出的合格侵权通知,将积极的采取必要措施;同时,本站也将在技术手段和能力范围内,履行版权保护的注意义务。 (如有侵权,欢迎举报) |
|
京ICP备16021002号-2 京B2-20170662号
京公网安备 11010802022788号
论坛法律顾问:王进律师
知识产权保护声明
免责及隐私声明