Borrar filtros
Borrar filtros

Function return values help?

3 visualizaciones (últimos 30 días)
Steve
Steve el 7 de Dic. de 2013
Comentada: Walter Roberson el 7 de Dic. de 2013
Hello Experts,
I have the function that gets a section (1,2,3 or 4) and gives results of calculation: function [VMean, VStd, VError, MeanDeltaV, MeanVegaV] = Q2(section)
When I enter section = 1 or any other number 1-4 I want the function to return only the section's variables results. For example section 1 calculates VMean, VStd, VError but not MeanDeltaV, MeanVegaV. Please tell me how to make it possible. The full code is:
function [VMean, VStd, VError, MeanDeltaV, MeanVegaV] = Q2(section)
% Spot price satisfies the SDE: dS = S*(rdt + sdW)
% Given parameters:
r = 0.1; % Drift
s = 0.4; % Volatility
K = 1.1; % Strike
T = 60/252; % Time to maturity
M = 50000; % Number of simulations
if section == 1
% Sampling twice a day, there are 60 days ==> 60*2 = 120 samples.
N = 120;
h = T/N;
elseif section == 2 || section == 3 || section == 4
% Sampling 10 times a day, there are 60 days ==> 60*10 = 600 samples.
N = 600;
h = T/N;
end
% Initial stock price S(0) = 1:
S = ones(M,N+1);
% S(t) = S(0)*exp((r - 0.5*s^2)*t + s*W(t)) = the exact price:
W = randn(M,N);
A = (r - 0.5*s^2)*h;
B = s*sqrt(h);
% According to the section (1 or 2), building the stock prices matrix:
for j = 1:N
S(:,j+1) = S(:,j).*exp(A + B*W(:,j));
end
% Find option values - using max function, find for each simulation a
% maximum value for all the samples = max(S,[],2) ==> substract K and find
% the maximum between the vector received and 0. Multiply by discount
% factor to get the option prices vector:
V = exp(-r*T)*max(max(S,[],2) - K,0);
VMean = NaN;
VStd = NaN;
VError = NaN;
if (section == 1 || section == 2)
VMean = mean(V);
VStd = std(V);
VError = VStd/sqrt(M);
% If the number of price samples (N) increases the mean value of
% the option price increases: twice a day VMean = 0.887,
% 10 times a day 0.0925.
end
% Initializing epsilon values for the derivatives: Delta and Vega Greeeks
% of the option price V:
epsilon = transpose(0.001:0.0001:0.009);
% Using Monte - Carlo to calculate the mean value of Delta and Vega of
% option price V:
MeanDeltaV = NaN;
MeanVegaV = NaN;
% Initialization:
DeltaV = zeros(length(epsilon),1);
VegaV = zeros(length(epsilon),1);
if section == 3
Splus = zeros(M,N+1);
Sminus = zeros(M,N+1);
% Calculating the Delta of option price V:
for u = 1:length(epsilon)
% For different epsilon values, calculating the Delta:
for k = 1:N
% Splus and Sminus are the matrices of share prices:
Splus(:,k+1) = (S(:,k) + epsilon(u)).*exp(A + B*W(:,j));
Sminus(:,k+1) = (S(:,k) - epsilon(u)).*exp(A + B*W(:,j));
% Vplus and Vminus are the option prices vectors for all M
% simulations on the k'th sample:
Vplus = exp(-r*T)*max(max(Splus,[],2) - K,0);
Vminus = exp(-r*T)*max(max(Sminus,[],2) - K,0);
% Calculating the dV:
dV = Vplus - Vminus;
end
% DeltaV = dV/dS(0), (calculated dS(0) using Splus and Sminus):
DeltaV(u) = mean(dV)/(2*epsilon(u));
end
% The mean value of Delta using Monte - Carlo:
MeanDeltaV = mean(DeltaV);
elseif section == 4
Ssplus = zeros(M,N+1);
Ssminus = zeros(M,N+1);
% Calculating the Vega of option price V:
for u = 1:length(epsilon)
% For different epsilon values, calculating the Vega:
for k = 1:N
% For each sample, calculating the parts of the power of the
% share price who satisfy GBM:
Aplus = (r - 0.5*(s + epsilon(u))^2)*h;
Bplus = (s + epsilon(u))*sqrt(h);
Aminus = (r - 0.5*(s - epsilon(u))^2)*h;
Bminus = (s - epsilon(u))*sqrt(h);
% Splus and Sminus are the vectors of share prices for all M
% simulations on the k'th sample:
Ssplus(:,k+1) = S(:,k).*exp(Aplus + Bplus*W(:,j));
Ssminus(:,k+1) = S(:,k).*exp(Aminus + Bminus*W(:,j));
% Vplus and Vminus are the option prices vectors for all M
% simulations on the k'th sample:
Vplus = exp(-r*T)*max(max(Ssplus,[],2) - K,0);
Vminus = exp(-r*T)*max(max(Ssminus,[],2) - K,0);
% Calculating the dV:
dV = Vplus - Vminus;
end
% VegaV = dV/ds, (calculated ds using s + epsilon - (s - epsilon)
% for all possible epsilon values):
VegaV(u) = mean(dV)/(2*epsilon(u));
end
% The mean value of Vega using Monte - Carlo:
MeanVegaV = mean(VegaV);
end
end

Respuestas (1)

Walter Roberson
Walter Roberson el 7 de Dic. de 2013
You need to look at nargout . If the user has given (say) 3 output variables, then you need to assign something to at least the first 3 output variables.
  2 comentarios
Steve
Steve el 7 de Dic. de 2013
Dear Walter, can you please figure out why this function take a lot of time to complete if section == 3? As I don't want to duplicate the same code. And thank you very much for the comment.
Walter Roberson
Walter Roberson el 7 de Dic. de 2013
How long is "a lot of time" ?

Iniciar sesión para comentar.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by