Skip to content

Add generic rates model calibration, implemented for Hull-White - #500

Open
jjohnnystwsss wants to merge 1 commit into
FinancePy-Org:masterfrom
jjohnnystwsss:feat/25-model-calibration
Open

jjohnnystwsss wants to merge 1 commit into
FinancePy-Org:masterfrom
jjohnnystwsss:feat/25-model-calibration

Conversation

@jjohnnystwsss

Copy link
Copy Markdown

Closes #25

Following the structure suggested in #25 (comment), this adds a generic calibration under market/volatility and implements it for Hull-White.

What changed

New module financepy/market/volatility/rates_model_calibration.py:

  • ModelCalibrationSpec describes 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. Like FXVolSurface, the fit runs when the object is created. The result is in .model (for example an HWTree), .params, .model_prices and .price_errors. check_calibration() prints market and model prices.
  • model is a model class with an entry in CALIBRATION_SPECS (HWTree for now) or a ModelCalibrationSpec. BK and BDT can be added with one entry each.
  • Options: fixed_params (for example a fixed mean reversion {"a": 0.05}), initial_params, weights, and model_kwargs for 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 scipy.optimize.least_squares (trust region reflective, with bounds). By default $w_i = 1/V_i^{mkt}$, so each residual is a relative price error and cheap and expensive options count equally.

Each 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 whose value accepts the model works. For HWTree, IborSwaption uses Jamshidian's decomposition and IborCapFloor uses zero-coupon bond options, both analytic. A fit to 12 swaptions takes about 0.2 s.

The spec follows HWTree's constraints: $\sigma \ge 0$, $a \ge 0$. Only constant $\sigma$ and $a$ are supported, as in HWTree.

Tests

tests/unit/test_rates_model_calibration.py:

  • Swaption prices from HWTree(0.012, 0.08) (payers and receivers, 1–7y expiries into 3–10y swaps): the fit recovers both parameters to rtol=1e-8.
  • Cap and floor prices from HWTree(0.009, 0.03): the fit recovers both parameters.
  • Fixed a: only sigma is fitted and recovered.
  • A different starting point and equal weights reach the same parameters.
  • Prices from a normal-vol term structure that HW can't match exactly: the fitted point is a minimum of the weighted squared error (moving either parameter increases it).
  • A ModelCalibrationSpec for Bachelier that is not in the registry recovers the normal vol, which checks that the structure is not tied to HW.
  • Bad inputs raise 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

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hull-White Model Swaption Calibration

1 participant