Plot Issues of Simulation

1 次查看(过去 30 天)
carlo mario pirisi
Hi i'm struggling doing the following thing:
i did a simulation of a Stock Price, and saved the numbers in a matrix,
here is the first part of the code that i used to simulate a geometric brownian motion:
r=0.05
%parameters
mu = r; sigma = 0.5; S0 = 12 %S0=starting value stock
T = 10 % time
Dt = 1/T;
SIM=5% here is how many trajectories i want to simulate
BARR=8 %threshold
STORE_SIM=zeros(Dt,SIM); %create matrix for store values
now i did the simulation and created to others matrix to store the column vectors that had gone beyond the threshold at least once (or just once), and another matrix where the values in columns vectors never touched the threshold.
Here is how i proceeded:
for i=1:SIM
DW = sqrt(Dt)*randn(1,T); % brownian increments
W = cumsum(DW);
Strue = S0*exp((mu-0.5*sigma^2)*([Dt:Dt:T])+sigma*W);
STORE_SIM(:,i)=Strue %storing values (by colums);
%now i create the different matrices for the 2 type of vectors
idx = STORE_SIM>BARR;
to_del=all(idx,1);
STORE_SIM_1=STORE_SIM;
STORE_SIM_1(:,to_del)=0;
%and the second type
index = STORE_SIM<BARR
logic=any(index)
NOTACTIVE=STORE_SIM
NOTACTIVE(:,logic)=0
end
and here's the plot:
figure(1)
hold on
for i=1:SIM
plot(NOTACTIVE(:,i),'--')
end
hold on
for i=1:SIM
plot(STORE_SIM_1(:,i),'k')
end
hold on
plot([0,10],[8,8],'k')
Now my issues are:
1)how to make not missing the first value of the stock price S0=12? because it starts from the first time moment of simulation and not from the "time zero".
2)how to make thicker the line of the threshold, so that would be more visible?
3)can i add a name like "Threshold" just a little bit over the line?
Thanks in advance

回答(1 个)

Pooja Sethia
Pooja Sethia 2018-2-13
Hi Carlo, Following are the solutions that might work for you.
Issue 1) I did not understand this query properly but if you want to use S0 value with other matrix so that it does not miss out. To do this 'cat' function is useful i.e. you can append S0 to some matrix.
ex. cat(2,S0,arrayname);
Issue 2) To change the thickness of threshold line, you can set 'Linewidth' property of the plot.
ex. set(findobj(gca,'Type','Line'),'Linewidth',2)
Issue 3) You can use 'annotation' function.
ex. plot(1:10)
dim = [.2 .5 .3 .3];
str = 'Straight Line Plot from 1 to 10';
annotation('textbox',dim,'String',str,'FitBoxToText','on');

类别

Help CenterFile Exchange 中查找有关 Monte-Carlo 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by