File Exchange

image thumbnail

Heston Model Calibration and Simulation

version 1.1 (6.79 KB) by

Calibrated the Heston Model to market Option prices



View License

This code calibrates the heston model to any dataset of the form
of the marketdata.txt file.

Provides analytical heston and MCMC heston pricing of Option

To see an example, run the hestoncalibrationexample.m code

Comments and Ratings (21)

Dear Moeti,

Thank you very much for your file, it is very useful! However, I see that it only shows the Heston pricing for a call option. Do you have by any chance the Heston pricing model for a put option? In other words, do you have the "HestonPut" file?

Thank you very much for your sharing!

Zhongzhi Hu

Alternatively, you can also enforce the Feller condition as a constraint by using fmincon:
where Feller is a the constraint function.

Zhongzhi Hu

Adressing to Moeti, boulala666 and mj:

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.


nice code. but is the problem fixed that "mj" mentioned?


jie (view profile)

very good


mj (view profile)

 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

Carlos AY


Where is the "MCMC heston pricing" part of pricing because it seems that there is only the analytical approach, or am I missing something here?

Many thanks for anyone who could help me.


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


TingFu (view profile)

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"?

Moeti Ncube

Moeti Ncube (view profile)

Yankun, sorry I could never get your code to work. Perhaps you can send folder in zip format with your revision and I will try to run again.


Yankun (view profile)

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.


Yankun (view profile)

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

Moeti Ncube

Moeti Ncube (view profile)

Hi, and thanks for your questions

Some of the variables you discussed are vectors in the code:

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).

me1 k

me1 k (view profile)

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.

tbfanso ??

Oh,I see.Thanks for you help!

Moeti Ncube

Moeti Ncube (view profile)

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);

Moeti Ncube

Moeti Ncube (view profile)

Hi, and thanks for comments.

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.

Hope this helps.

tbfanso ??

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?

Thank you for your time



Corrected volatility simulation to 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);

MATLAB Release
MATLAB 7.8 (R2009a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video