Thread Subject: I need help with least square model fitting, Please!

Subject: I need help with least square model fitting, Please!

From: DekkusamaNT T.

Date: 8 Nov, 2009 08:17:03

Message: 1 of 2

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

Subject: I need help with least square model fitting, Please!

From: Bruno Luong

Date: 8 Nov, 2009 09:40:03

Message: 2 of 2

Debugging if a part of a life of a programmer. Learn how to do it.

Type

DBSTOP IF ERROR

then rerun your code. When the errors occurs, the prompt appears. Examine your variables and expressions to see what dimension is mismatched.

Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
least square DekkusamaNT T. 8 Nov, 2009 03:19:04
risk neutral de... DekkusamaNT T. 8 Nov, 2009 03:19:04
lognormal DekkusamaNT T. 8 Nov, 2009 03:19:04
option pricing DekkusamaNT T. 8 Nov, 2009 03:19:04
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com