Plotting error bar plot error bar plot for Monte Carlo simulation
3 次查看(过去 30 天)
显示 更早的评论
I have the following code which does a monte carlo simulation for option pricing.
For some reason, when I run it once and run it again my Sfinal doesnt change, therefore the graph is the same. But if I just run Sfinal in the command window, I get a different vector each time.
Is it possible to adapt the code so it gives a different simulation for the graph each time?
Once working I need to adapt for barrier options.
(code from https://stackoverflow.com/questions/49960122/plotting-confidence-intervals-in-matlab/54843312#54843312)
randn('state',100)
S0=10; X=9; H=11; sigma=0.1; r=0.06; T=1; %H=12;
N=200; Dt=T/N;
M = [2^2,2^3,2^4,2^5,2^6,2^7,2^11,2^12,2^13,2^14,2^15,2^16,2^17,2^18,2^19,2^20]; %Number of iterations
hold on;
for k=1:numel(M)
Sfinal = S0*exp((r-0.5*sigma^2)*T+sigma*sqrt(T)*randn(M(k),1)); %for barrier, change
%Smax=max(Sfinal);
%if Smax>H
V = exp(-r*T)*max(Sfinal-X,0);
%end
aM = mean(V);
bM = std(V);
plot(M(k),aM,'x');
errorbar(M(k), aM, 1.96*bM/sqrt(M(k)));
end
chvar = repmat(ch08(10,9,0.06,0.1,1),1,numel(M));
plot(M, chvar,'--.k');
title('Monte Carlo Approximations');
xlabel('Samples'); % x-axis label
ylabel('Option value approximation'); % y-axis label
set(gca,'XScale', 'log','YScale', 'log');
axis([10^1.5 10^6 10^0.1 10^0.3]);
set(gca,'YTick',[10^0.1 10^0.2 10^0.3]);
set(gca,'YTickLabel',{'10^{0.1}','10^{0.2}','10^{0.3}'});
回答(1 个)
Geoff Hayes
2019-2-23
Katie - your above code calls
randn('state',100)
which will always "use" the same state of the generator...so the same sequence of random numbers is generated (at least that is my understanding of what this does). Try removing this line of code and then each time you call your script, you should get a different result (much like when you call Sfinal from the command line). See rand('state',...) and Replace Discouraged Syntaxes of rand and randn for details.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!