Different answers at different computers but same code
Mostra commenti meno recenti
At home I am running the student version R2013a and running the same code at school on R2012a full license, and I get substantially different values for the same code. Can anyone help me understand why this could be?
Home:
ans =
10.5385
School:
ans = 9.2312
Program:
N=252; %Number of Trading periods per T
M=50000; % Number of simulations
T=1; % evaluation period
Mu=.08; % Mean return set to r if risk neutral pricing
Sigma=.25; %stock volatility
So=100; %initial value
B=95; %Barrier
K=90; %Strike
r=.08; %Discount Rate
dt = T/N; %change in time
St=zeros(N,M); %pre allocating to save time
St(1,1:M)=So; %initial value for each loop
t=T; %discounting time frame
C=zeros(1,M); %pre allocate
for j=1:M
for i=2:N
St(i,j)=St(i-1,j)*exp((Mu-Sigma^2/2)*dt+Sigma*sqrt(dt)*randn);
end
end
for k=1:M
if min(St(:,k))<B && St(N,k)>K
C(k)=exp(-r*t)*(St(N,k)-K);
else
C(k)=0;
end
end
mean(C)
std(C)
if true
% code
end
Risposte (5)
Walter Roberson
il 10 Feb 2014
Modificato: Walter Roberson
il 10 Feb 2014
1 voto
You have randn in your St formula. randn is a random number generator. Unless you specifically use the same random seed using rng(), you are going to get different answers on different runs.
Welcome to the world of Monte Carlo simulation...your two runs are using different sequences of random numbers.
...
St(i,j)=St(i-1,j)*exp((Mu-Sigma^2/2)*dt+Sigma*sqrt(dt)*randn);
You've got a random normal generate here. Unless you set the initial conditions and use the same generator for the two cases the results are bound to be different.
See
doc rng
for discussion on how to set.
Michael
il 11 Feb 2014
0 voti
1 Commento
dpb
il 13 Feb 2014
Have you started by recording a set of the returned values from randn() for the two after setting the generator?
I note you've one 32- and one 64-bit machine--I don't know what difference that might make--I would presume the default Matlab data type is a double on both but is the internal precision different such that the exp() part is causing troubles, perhaps? Just lobbing out some more or less random thoughts.
Andreas Goser
il 11 Feb 2014
0 voti
When I run this code with different set of random numbers, the results typically end up between 10.3 and 10.6. So I conclude with the assumption it is about either the MATLAB version or the general environment.
Can you provide information about the operation systems and architectures (32/64 bit) for the two environments?
1 Commento
Andreas Goser
il 14 Feb 2014
I agree with the comment from dpb and it was exactly the reason why I have asked. If you use certain algorithms on 32 bs 64 bit, the results are simply different. This is how it is. Question is what do you do with this information now. One need to understand the application in order to give you a suggestion on how to change your code to make it safer with reagrds to those numerical effects.
Michael
il 13 Feb 2014
0 voti
Categorie
Scopri di più su Array Geometries and Analysis in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!