Simple function and a small if problem, please try to help me
1 view (last 30 days)
Show older comments
Hello Experts,
I have the following function where the user chooses the section and with the given parameters gets a graph.
What I can't understand is why the function doesn't enter to section 2 while section 1 works perfectly?
Here is the code:
function Q1(rho,K1,K2,section)
% SDE's of 2 shares:
% dS1 = S1*(r1dt + s1dW1) ; dS2 = S2*(r2dt + s2dW2)
% Check if rho is a column vector if not, transpose it:
if size(rho,2) > 1
rho = transpose(rho);
end
% Given parameters:
r1 = 0.0001;
r2 = 0.0001;
s1 = 0.0001;
s2 = 0.0002;
% Number of simulations:
M = 100000;
% End of period is set to be t = 100 days
t = 100;
% Initial values S1(0) = S2(0) = 1:
S1_0 = 1;
S2_0 = 1;
% Generating independent normally distributed vectors, here
% concentrated at the end of the period where t = 100.
W2 = sqrt(t)*randn(M,1);
V = sqrt(t)*randn(M,1);
if section == 1
ReasonablePrice = zeros(length(rho),1);
PayoffStd = zeros(length(rho),1);
PayoffError = zeros(length(rho),1);
elseif section == 2
ProfitMean = zeros(length(rho),1);
ProfitStd = zeros(length(rho),1);
ProfitError = zeros(length(rho),1);
end
for j = 1:length(rho)
% Making W1 and W2 dependent while W and V are independent,
% cov(W1,W2) = rho(j)*Var(W2) + 0 = rho^2(j)*t.
W1 = rho(j)*W2 + V;
S1 = S1_0*exp((r1 - 0.5*s1^2)*t + s1*W1(:,1));
S2 = S2_0*exp((r2 - 0.5*s2^2)*t + s2*W2(:,1));
if section == 1
% Payoff for all of M simulations using logical expression to find
% where S2 > S1:
Payoff = (S2-S1).*(S2 > S1);
% Calculating the reasonable price: E[max(S2(100) - S1(100),0)]:
ReasonablePrice(j,1) = mean(Payoff);
PayoffStd(j,1) = std(Payoff);
PayoffError(j,1) = PayoffStd(j,1)/sqrt(M);
elseif section == 2
Profit = max(S1 - K1,S2 - K2).*(S1 > K1).*(S1 - K2);
ProfitMean(j) = mean(Profit);
ProfitStd(j) = std(Profit);
ProfitError(j) = ProfitStd(j)/sqrt(M);
end
end
if section == 1
figure(1);
plot(rho,ReasonablePrice);
xlabel('rho');
ylabel('Price');
elseif section == 2
ProfitMean = rho;
plot(rho,ProfitMean);
xlabel('rho');
ylabel('ProfitMean');
end
0 Comments
Accepted Answer
Image Analyst
on 4 Dec 2013
What happens if you set a break point on the "if" line and inspect section's value when it stops there? http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
2 Comments
Image Analyst
on 4 Dec 2013
Hopefully you used the debugger like I recommended, rather than just asking yourself, to reveal what the values were. So, is this solved? Or do you still have problems? If solved, please mark as Accepted to close it out.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!