Plotting error bar plot error bar plot for Monte Carlo simulation

3 vues (au cours des 30 derniers jours)
Katie Brewer
Katie Brewer le 23 Fév 2019
Commenté : Rena Berman le 2 Avr 2019
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}'});

Réponses (1)

Geoff Hayes
Geoff Hayes le 23 Fév 2019
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.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by