Skip to content

Add European option portfolio adjoint example - #23

Open
tylrcc wants to merge 1 commit into
auto-differentiation:mainfrom
tylrcc:example/european-option-adjoint
Open

tylrcc wants to merge 1 commit into
auto-differentiation:mainfrom
tylrcc:example/european-option-adjoint

Conversation

@tylrcc

@tylrcc tylrcc commented Nov 12, 2026 •

Copy link
Copy Markdown

Closes #15.

Python port of the AdjointEuropeanEquityOption C++ example, following the pattern of swap-adjoint.py:

  • Prices a portfolio of European put options (underlyings 15-60, strikes 10-79) over the same zero curve and black variance curve as the C++ original (evaluation date 15 May 1998, maturity 17 May 1999, dividend yield 0.01).
  • Registers all 98 market inputs (9 zero rates, 8 volatilities, 70 strikes, 10 underlyings, dividend yield) and gets every sensitivity in one adjoint pass; the portfolio is priced inside the recording, as in the C++ code.
  • The AAD computation is in an aad_sensitivities() function returning the portfolio value and a dict of sensitivities keyed by input name. The example cross-checks the adjoints against central differences (h = 1e-5) over all 98 inputs at the end.

Testing: run on QuantLibAAD 1.44.0 (Python 3.12): portfolio value 9481.90744, largest relative difference over all 98 inputs: 6.6e-08 (strike 33).

Note: this repo requires a signed CLA before merge (per CONTRIBUTING) — I understand it will be handled through the CLA flow.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@auto-differentiation-dev

Copy link
Copy Markdown
Contributor

Thanks a lot for these, Tyler! Could you sign the CLA (link above)? Then we can run CI and review all four.

@auto-differentiation-dev auto-differentiation-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up #15, and for porting the full portfolio rather than a single option: that's a nice first example for new users. A few things before we can merge:

  1. Run it on QuantLibAAD. QuantLibAAD 1.44.0 is now on PyPI (pip install QuantLibAAD, Python 3.10–3.14), so the example can now be run against the package it's for.
  2. Rebase onto current main. Your branch is based on the state before 1.44.0, so the diff currently shows the whole release. After the rebase it should only add Python/examples/european-option-adjoint.py and one CHANGELOG line.
  3. Central differences for the check. Bump each input up and down, (V(x+h) - V(x-h)) / (2h) with h = 1e-5, so the comparison agrees to about 1e-8 relative instead of 1e-5. Print the largest relative difference over all 98 inputs, not just the largest sensitivities.
  4. Put the AAD computation in a function, as the newer examples do: see aad_sensitivities() in Python/examples/trade-risk.py. Here it would return the portfolio value and a dict of sensitivities keyed by input name (e.g. "rate 1999-04-27", "strike 40", "underlying 36", "dividend yield"), and the script calls it and prints the results as now. That keeps the examples consistent and lets readers reuse the calculation from their own code or a notebook.
  5. CHANGELOG: add a line under ## [Unreleased] / ### Added, e.g. "Example european-option-adjoint.py: sensitivities of a European option portfolio to all market inputs in one adjoint pass".
  6. CLA: please sign it (link in the CLA check on this PR); we can't merge before it's signed.

The same points apply to #24, #25 and #26 (run on QuantLibAAD 1.44.0, rebase, central-difference check over all inputs, an aad_sensitivities() function, a CHANGELOG line each), so please work through them there too, rather than us repeating them. Two things specific to those:

  • #24 (American option): QuantLibAAD 1.44.0 fixes the zero dividend rho with finite-difference engines that your notes describe, so on 1.44.0 the adjoint dividend rho should match the bumped one. Please remove that caveat from the example and include the dividend yield in the check.
  • #26 (Heston): please run the calibration with COSHestonEngine as written, rather than the analytic engine you used locally, and include all sensitivities in the check, not only the eight largest.

Once these are in, we'll run our checks and review all four again.

Rebase onto current main and address review: run against QuantLibAAD
1.44.0, move the AAD computation into aad_sensitivities() returning the
portfolio value and a dict of sensitivities keyed by input name, replace
the forward-difference check with central differences over all 98 inputs,
and add a CHANGELOG entry.

Run on QuantLibAAD 1.44.0: portfolio value 9481.90744; largest relative
difference over all 98 inputs 6.6e-08 (strike 33).
@tylrcc
tylrcc force-pushed the example/european-option-adjoint branch from 613e782 to 7249324 Compare November 14, 2026 18:28
@tylrcc

tylrcc commented Nov 14, 2026

Copy link
Copy Markdown
Author

Review points addressed:

  1. Runs on QuantLibAAD 1.44.0 from PyPI (Python 3.12).
  2. Rebased onto current main — the diff is now just the example plus one CHANGELOG line.
  3. Central-difference check over all inputs: (V(x+h) - V(x-h)) / (2h) with h = 1e-5, over all 98 inputs. Actual run output:
    Largest relative difference over all 98 inputs: 6.6e-08 (strike 33)
    Each difference is relative to its own sensitivity, with a floor at 0.01% of the largest sensitivity where rounding in the central differences dominates (same approach as trade-risk.py).
  4. aad_sensitivities(): the AAD computation now lives in a function returning the portfolio value and a dict of sensitivities keyed by input name (e.g. "rate 1999-04-27", "strike 40", "underlying 36", "dividend yield"); the script calls it and prints the results as before.
  5. CHANGELOG: line added under ## [Unreleased] / ### Added.

The CLA is still unsigned on my side; that is separate from these changes.

@auto-differentiation-dev

Copy link
Copy Markdown
Contributor

Thanks for the quick turnaround, Tyler, and for running this on QuantLibAAD 1.44.0.

We can't review or merge contributions without a signed CLA, so that's the one thing holding this PR up. It takes a minute: the link is in the bot comment at the top. If you've signed and the check still shows pending, the "recheck" link next to it refreshes it. If something about the CLA is a concern, tell us and we'll sort it out.

Once it's signed, we'll go through this one straight away. One small thing meanwhile: the PR description still mentions the 1.33.3 run, so please update it to match your latest comment.

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.

Python example: European option sensitivities with AAD

3 participants