Skip to content

Add barrier option replication adjoint example - #25

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

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

Conversation

@tylrcc

@tylrcc tylrcc commented Nov 12, 2026 •

Copy link
Copy Markdown

Closes #20.

Python port of the AdjointReplication C++ example (Joshi's static replication of a down-and-out barrier option with European options), following the pattern of swap-adjoint.py:

  • Same setup as the C++ original: evaluation date 29 May 2006, down-and-out put, strike 101, barrier 70, spot 100, vol 0.20, flat 4% zero curve on the same node dates, kill loop over 26 fortnightly dates.
  • The portfolio construction (including the kill notionals, which are computed from recorded portfolio values, and the evaluation-date changes at each kill date) runs inside the recording, so one adjoint pass yields rhos, strike sensitivity, vega, delta, and barrier sensitivity.
  • 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 every adjoint against central differences (h = 1e-5) over all 13 inputs at the end.

Testing: run on QuantLibAAD 1.44.0 (Python 3.12): barrier option value 4.652838435318750, replicating portfolio 4.154324163665421, largest relative difference over all 13 inputs: 3.4e-06 (rate February 9th, 2007). The worst input is a small sensitivity (2.7e-3) at the central-difference noise floor through the kill loop; the large sensitivities agree to ~1e-8 (volatility 3.9e-08). Noted in the example.

One deliberate difference from the C++ code, also noted in the example: the C++ registers a single variable for the flat 4% rate shared by all nine curve nodes, so its rho is the sum over nodes. This port registers one input per node (all set to 4%) and reports the rhos per node; the per-node values sum to the C++-style sensitivity. Shout if you would prefer the single-variable form instead.

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.

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 13 inputs,
and add a CHANGELOG entry.

Run on QuantLibAAD 1.44.0: barrier option value 4.652838435318750,
replicating portfolio value 4.154324163665421; largest relative difference
over all 13 inputs 3.4e-06 (rate February 9th, 2007), on a small
sensitivity at the central-difference noise floor through the kill loop.
@tylrcc
tylrcc force-pushed the example/replication-adjoint branch from 019bd89 to a3a9dc1 Compare November 14, 2026 18:33
@tylrcc

tylrcc commented Nov 14, 2026

Copy link
Copy Markdown
Author

Review points addressed (same as #23):

  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 13 inputs. Actual run output:
    Largest relative difference over all 13 inputs: 3.4e-06 (rate February 9th, 2007)
    One honest caveat: the largest difference is on a small rate sensitivity (2.7e-3), where the central differences are at their noise floor through the 26-step kill loop (the difference does not shrink with smaller h); the large sensitivities agree to about 1e-8 (volatility 3.9e-08). The example notes this.
  4. aad_sensitivities(): the AAD computation now lives in a function returning the portfolio value and a dict of sensitivities keyed by input name; the script calls it and prints the results as before. The reference barrier option value is priced separately without the tape.
  5. CHANGELOG: line added under ## [Unreleased] / ### Added.

Run on QuantLibAAD 1.44.0: barrier option value 4.652838435318750, replicating portfolio value 4.154324163665421, both unchanged from the 1.33.3 run.

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

@auto-differentiation-dev

Copy link
Copy Markdown
Contributor

Thanks, the rebase and the 1.44.0 run are what we asked for. Before we can take the review further, the CLA needs signing for this PR as well: see the bot comment at the top for the link, or use its "recheck" link if you've already signed. Tell us if anything about the CLA is unclear.

The description above still describes the 1.33.3 run; please bring it in line with your latest comment. We'll review once the CLA check passes.

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.

Barrier option replication example (AAD)

3 participants