Simple function and a small if problem, please try to help me
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!