필터 지우기
필터 지우기

Simple function and a small if problem, please try to help me

조회 수: 2 (최근 30일)
Steve
Steve 2013년 12월 4일
댓글: Image Analyst 2013년 12월 4일
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

채택된 답변

Image Analyst
Image Analyst 2013년 12월 4일
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
Steve
Steve 2013년 12월 4일
Dear Image Analyst,
Just asked my self what are the values of the S1,S2 and saw they are around 1.01 ===> that's why the function gave me 0 when I ran it with K1 = 2,K2 = 3. Now all I need is to find the appropriate values to show a good graph.
Image Analyst
Image Analyst 2013년 12월 4일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by