Repository navigation
Add generic rates model calibration, implemented for Hull-White - #500
Open
jjohnnystwsss wants to merge 1 commit into
Open
jjohnnystwsss wants to merge 1 commit into
jjohnnystwsss wants to merge 1 commit into
Conversation
RatesModelCalibration fits a model's parameters to the market prices of interest rate options with bounded least squares. A ModelCalibrationSpec names the model class, the fitted constructor arguments, their bounds and a starting point. Each instrument is priced by its own value() method, so swaptions and caps/floors both work. A spec for HWTree is included. Closes FinancePy-Org#25 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #25
Following the structure suggested in #25 (comment), this adds a generic calibration under
market/volatilityand implements it for Hull-White.What changed
New module
financepy/market/volatility/rates_model_calibration.py:ModelCalibrationSpecdescribes a model family: the model class, which constructor arguments are fitted, their bounds and a default starting point.RatesModelCalibration(value_dt, discount_curve, instruments, market_prices, model, ...)takes a set of instruments with their market prices and fits the model. LikeFXVolSurface, the fit runs when the object is created. The result is in.model(for example anHWTree),.params,.model_pricesand.price_errors.check_calibration()prints market and model prices.modelis a model class with an entry inCALIBRATION_SPECS(HWTreefor now) or aModelCalibrationSpec. BK and BDT can be added with one entry each.fixed_params(for example a fixed mean reversion{"a": 0.05}),initial_params,weights, andmodel_kwargsfor constructor arguments that are not fitted.Method
The free parameters$p$ minimise $\sum_i \left(w_i \left(V_i(p) - V_i^{mkt}\right)\right)^2$ with $w_i = 1/V_i^{mkt}$ , so each residual is a relative price error and cheap and expensive options count equally.
scipy.optimize.least_squares(trust region reflective, with bounds). By defaultEach instrument is priced by its own
value(value_dt, discount_curve, model), so the calibration uses exactly the prices a user gets from the product, and any product whosevalueaccepts the model works. ForHWTree,IborSwaptionuses Jamshidian's decomposition andIborCapFlooruses zero-coupon bond options, both analytic. A fit to 12 swaptions takes about 0.2 s.The spec follows$\sigma \ge 0$ , $a \ge 0$ . Only constant $\sigma$ and $a$ are supported, as in
HWTree's constraints:HWTree.Tests
tests/unit/test_rates_model_calibration.py:HWTree(0.012, 0.08)(payers and receivers, 1–7y expiries into 3–10y swaps): the fit recovers both parameters tortol=1e-8.HWTree(0.009, 0.03): the fit recovers both parameters.a: onlysigmais fitted and recovered.ModelCalibrationSpecforBachelierthat is not in the registry recovers the normal vol, which checks that the structure is not tied to HW.FinError.pytest tests/unit: 2152 passed.There is also an example,
examples/scripts/rates/example_rates_model_calibration.py, which fits HW to co-terminal swaptions quoted in Black vols, with and without a fixed mean reversion.I didn't add to the CHANGELOG or the generated docs.
AI disclosure
This was written with Claude Code (Claude Opus 5.5), which wrote the code, tests and example and ran the tests above. I read through the code before submitting.
🤖 Generated with Claude Code