Clear Filters
Clear Filters

Model calibration for different dates

4 views (last 30 days)
Tevor N
Tevor N on 2 Mar 2023
Answered: Peter Perkins on 13 Mar 2023
Hi guys,
I want to calibrate the Heston model (a financial option pricing model) daily for more than 1000 days and store the optimal parameters for each day in a matrix. I have a large dataframe made with pandas where I have all the necessary information for the price calculation in the columns (i.e. Strike Price, Maturity etc), with also a 'DATE' column. I have +- 1400 option observations per day, so I want to calibrate the Heston model for observations with the same 'DATE' value. My dataframe has over a million observations and I want to calibrate it for options with the same 'DATE' value. I have already loaded this dataframe in MATLAB and daily calibration happens quite fast, but now I want to calibrate my whole dataset with all the dates.
My idea was to make some kind of loop over my different dates such that when the calibration for one particular date is done, MATLAB loops to the next date and starts calibrating it, until daily calibration is done for all my days. I do not really know how I should approach this. With Python I work often with 'groupby, on='DATE'', but I am not very experienced with MATLAB.
The code below works for one date, so I load option data for one date (which are vectors of length 1400) and calibrate it, which works fine. Now I want to load my whole dataset (more than a million observations), group them by date and calibrate it with some sort of loop.
Thanks in advance for the help!
startparameters = x; %[0.054 0.046 -0.53 2.3 0.0825]
options = optimoptions('lsqnonlin', 'Display', 'iter');
xopt = lsqnonlin(@(x) option_function2(distr, S0, K, x, T, CP, sv1, sv2, sv3, rf, q, N, L) - MarketPrice,...
startparameters, ... %start values
... % rho,theta,kappa,sigma
[ -1+eps eps eps eps eps 0 0 0 0 0 0 0 0 ], ... % lower bound for parameter vector
[ 1+eps inf inf inf inf 0 0 0 0 0 0 0 0 ], ... % upper bound for parameter vector

Answers (2)

Jasvin on 6 Mar 2023
You can use the findgroups() and splitapply() functions in tandem to achieve the desired functionality.
Their use case is defined in this documentation page,
So first you call findgroups() on your dataset's Date column to obtain the group numbers for each entry and the code that you wrote which works for one date can be converted to a function which you pass in as a function handle to the spltiapply() call.

Peter Perkins
Peter Perkins on 13 Mar 2023
I would think that the best way to do this would be to put your data into a timetable, and use the rowfun function with the date as your grouping variable set to the date. If your timestamps are intraday, you'd need to create that grouping variable using dateshift.


Find more on Financial Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!