Different answers at different computers but same code

24 views (last 30 days)
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

Answers (5)

Walter Roberson
Walter Roberson on 10 Feb 2014
Edited: Walter Roberson on 10 Feb 2014
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.

dpb
dpb on 10 Feb 2014
Edited: dpb on 10 Feb 2014
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
Michael on 11 Feb 2014
but that is such a huge difference that I would expect if the simulations were less than 5000 but at 500,000 I don't think it should be that far apart.
  1 Comment
dpb
dpb on 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.

Sign in to comment.


Andreas Goser
Andreas Goser on 11 Feb 2014
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 Comment
Andreas Goser
Andreas Goser on 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.

Sign in to comment.


Michael
Michael on 13 Feb 2014
MY version and computer:
R2013a Student Version (8.1.0.604) 32-bit (Win32)
computer:
Windows 7 Pro 64 bit 12gb RAM with intel i7 2.2gHz Visual Studio 2012
School Setup:
R2012b version (8.0.0.783) 64-bit (win64)
computer:
Windows 7 enterprise 64-bit 4Gb ram indel cor 2 duo 2.93 compilers they have 4 different ones on the machine but i believe that Visual Studio 2010 is the default
I hope this helps let me know if i need more information.

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!