Different answers at different computers but same code
이전 댓글 표시
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
답변 (5개)
Walter Roberson
2014년 2월 10일
편집: Walter Roberson
2014년 2월 10일
1 개 추천
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
2014년 2월 11일
0 개 추천
댓글 수: 1
dpb
2014년 2월 13일
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
2014년 2월 11일
0 개 추천
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
Andreas Goser
2014년 2월 14일
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.
카테고리
도움말 센터 및 File Exchange에서 Array Geometries and Analysis에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!