|
I am so new to matlab and I don't understand how it works yet.
My problem is about using optimization problem (at least, I think the problem is there)
The thing is, I tried to construct risk neutral density which is a mixture of 3 log-normal distribution.
I followed method showed in Gianluca Fusai and Andrea Roncoroni, Implementing Models in Quantitative Finance, Chapter 12. The model assumed risk neutral density in Log-normal form.
It minimizes sum of square differences between observed market price and price derived from model from both call and put options.
The variables that need to be estimated are parameters in the estimation model, alphas, betas, and weights.
I was able to write m-file for calculating theoretical option prices but when I wrote optimization script and run it, the following errors occurred, saying something about Matrix dimension on command line,
which run smoothly if I didnot insert the optimization lines.
I post my scripts below the error message. I know it's quite a mess, bear with me.
Could anyone help pointing out what I did wrong here? Any books, any examples I can refer to?
How can I get over it? I've been trying to solve this for 2 whole days already, browsing through Matlab's Help and Documentations,
but nothing seemed to work.... HELP!!
---------------------------------------------------------------------------------------------------------------------------------------------------------
??? Error using ==> times
Matrix dimensions must agree.
Error in ==> calrnd5>calprice at 29
y = typep.*(exp(-r.*T/252).*...
Error in ==> calrnd5>iverrors at 46
y =calprice(r,T, K, zeta0,typep, typec)-MktP;
Error in ==> lsqnonlin at 202
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Error in ==> calrnd5 at 52
x = lsqnonlin(@iverrors,zeta0,[], [], opts,r,T, K, typep,typec, MktP);
Caused by:
Failure in initial user-supplied objective function evaluation. LSQNONLIN cannot continue.
-------------------------------------------------------------------------------------------------------------------------------------------------------
function xxx = calrnd5
clear;
clc;
load calrnd
K = data(:,1);
r = data(:,5);
T = data(:,2);
typec = data(:,4);
typep = data(:,7);
MktP = data(:,3);
alpha = 9.5+zeros(3,1);
beta = 0.2+zeros(3,1);
w = 1/3+zeros(3,1);
zeta0 = [alpha' beta' w'];
z = calprice(r,T, K, zeta0,typep, typec)
xz = iverrors(r,T, K, zeta0,typep, typec,MktP)
function y = calprice(r,T, K, zeta0,typep, typec)
% calculate option price, typep and typec are just indicators for put and call
% K is a vector of exercise price, r is vector of risk free rate, T is vector of time to maturity in days. They're all in 137x1 dimension
%zeta0 is row vector of parameters I want to estimate, comprising of 3 alphas, 3 betas, and 3 weights, respectively.
% the function form for call price is call = exp(-r*T)*(w1*(exp(alpha1+(beta1^2)/2 * N(d1) - K * N(d2)) + w2(....) + w3(...)
%d(i) is function of alphas and betas
y = typep.*(exp(-r.*T/252).*...
(zeta0(7)*(K.*normcdf(-((-log(K)+ zeta0(1) +zeta0(4)^2)./zeta0(4) - zeta0(4)))...
-exp(zeta0(1)+(zeta0(4).^2)/2).*normcdf(-((-log(K)+ zeta0(1) +zeta0(4)^2)./zeta0(4))))...
+ zeta0(8)*(K.*normcdf(-((-log(K)+ zeta0(2)+zeta0(5)^2)./zeta0(5) - zeta0(5)))...
-exp(zeta0(2)+(zeta0(5).^2)/2).*normcdf(-((-log(K)+ zeta0(2)+zeta0(5)^2)./zeta0(5))))...
+ (1-zeta0(7)-zeta0(8))*(K.*normcdf(-((-log(K)+ zeta0(3)+zeta0(6)^2)./zeta0(6) - zeta0(6)))...
-exp(zeta0(3)+(zeta0(6).^2)/2).*normcdf(-((-log(K))+ zeta0(3)+zeta0(6)^2)./zeta0(6)))))...
+ typec.*(exp(-r.*T/252).*...
(zeta0(7)*(exp(zeta0(1)+(zeta0(4).^2)/2).*normcdf((-log(K)+ zeta0(1) +zeta0(4)^2)./zeta0(4))...
-K.*normcdf(((-log(K)+ zeta0(1) +zeta0(4)^2)./zeta0(4) - zeta0(4))))...
+ zeta0(8)*(exp(zeta0(2)+(zeta0(5).^2)/2).*normcdf((-log(K)+ zeta0(2)+zeta0(5)^2)./zeta0(5))...
-K.*normcdf(((-log(K)+ zeta0(2)+zeta0(5)^2)./zeta0(5) - zeta0(5))))...
+ (1-zeta0(7)-zeta0(8))*(exp(zeta0(3)+(zeta0(6).^2)/2).*normcdf((-log(K)+ zeta0(3)+zeta0(6)^2)./zeta0(6))...
-K.*normcdf(((-log(K)+ zeta0(3)+zeta0(6)^2)./zeta0(6) - zeta0(6))))));
end
function y= iverrors(r,T, K, zeta0,typep, typec,MktP)
y =calprice(r,T, K, zeta0,typep, typec)-MktP;
end
opts = optimset('Display','iter','TolFun',0.00001);
x = lsqnonlin(@iverrors,zeta0,[], [], opts,r,T, K, typep,typec, MktP);
x
end
|