Alternatively, you can also enforce the Feller condition as a constraint by using fmincon:
x=fmincon(@costf2,x0,[],[],[],[],lb,ub,@Feller);
where Feller is a the constraint function.
It is indeed essential that the obtained parameters satisfy the Feller condition. In the main program I edit the following: in x0, lb and ub I added another element x(6). Make sure that the lower bound for x(6) is 0. In costf2.m I added x(6)=2*x(2)*x(3)-x(4)^2; %Feller condition.
Thanks a lot Moeti for your code! It was very helpful for my Msc. thesis in Financial Econometrics.
Dear Moeti,
In Heston model, If the parameters obey 2*kappa*theta> sig (known as the Feller condition) then the process volatility is strictly positive. When I run your codes even using your marketdata, calibrated parameters violate the feller condition. Could you please explain why it is so and probably fix it if possible.
Thanks in advance
Hey every one I am trying to run this code as it is but I get the message " Undefined function 'lsqnonlin' for input arguments of type'function_handle'.
Error in hestoncalibrationexample (line 36)
x = lsqnonlin(@costf2,x0,lb,ub);
Any body can help me with this problem?
thanks in advance
Dear Moeti Ncube
Thanks for your code, it does help me a lot!
But I have a question for results.
In general, the simulated price should be very close to the Heston model price, right?
But why it seem that there is a gap between "simhes" and "modhes"?
Sorry, I made a mistake.
I mean your HestonCall.m doesn't work correctly when parameters such as St,K,r,T,vt,kap,th,sig,rho,lda are vectors instead of scalars.
Dear Moeti Ncube
Thanks for your code.
But I think your HestonCalibration.m doesn't work correctly when parameters such as St,K,r,T,vt,kap,th,sig,rho,lda are vectors instead of scalars.
I've mailed you my modification based on your code.
Thank you.
Dear Moeti NCube
I have read several discussions of
pricing and calibration of Heston model and I am wondering if you have encountered the problems alluded to in the following comments on calibrating Heston and pricing Heston calls given parameters:
(1) pricing the option using Heston' formulas " gives rise to an inherent numerical instability as a
consequence of which most implementations of Heston’s formulæ are not robust for moderate to long dated maturities or strong mean reversion."
(2) the calibration can get bogged down in local minima and can take a long time
Some of the variables you discussed are vectors in the code:
K=strike=marketdata(:,1)
T=marketdata(:,4)/365;
So if you want to modify the strikes and maturities consider you should adjust the marketdata.txt file
St and r is the current value of underlying and risk free rate respectively which will always be scalars. You can't have multiple values for the underlying spot price or risk free rate (technically).
Hi moeti, thnx for the code. It gave me the opportunity to understand better the calibration procedure.
However, I am wondering how can the HestonCall(St,K,r,T,vt,kap,th,sig,rho,lda) for example work for vectors.
I mean how can I input St,K,r,T as vectors? What modifications are needed to be done in the code? Thank you in advance.
Correction: volatility equation should be the following
vhes(j+1)=vhes(j)*exp(((kappa*(theta - vhes(j))-0.5*vsigma^2)*dt)/vhes(j) + vsigma*(1/sqrt(vhes(j)))*sqrt(dt)*r2);
The second column is not needed in the calibration as it was a column I created, for data extraction purposes, to represent how far away from the spot month the contract was. So for example if the value is '1' this mean this contract is 1 month away from expiry, if it has a value of '2', this means the contract is 2 months away from expiry.
Hi,moeti
I have read your great code seriously,but I have a some questions.First,about the marketdata the calibration used,the marketdata's first column is strike,and third is implied volatility,the fourth is time to maturity(days),but it seem that the third column data not used in the calibration? and what is the third column data?