feat(greeks): Black-76 Greeks (delta/gamma/vega/theta/rho)#402
Merged
Conversation
Closed-form Greeks for the Black-76 (Black 1976) model, complementing the pricing kernel landed in #398. Black-76 prices options on futures, forwards, swaptions, caps/floors, and commodity futures — anywhere the underlying is a forward price `F` with the cost of carry already baked in. The Greeks share the same `e^(-rT)` discount factor across both legs and ignore `dividend_yield` (since `F` is carry-adjusted). Public surface (`src/greeks/black_76.rs`): - `delta_b76`, `gamma_b76`, `vega_b76`, `theta_b76`, `rho_b76` - `Black76Greeks` trait mirroring the `Black76` pricing trait — implementors provide `get_option(&self)` and inherit default delegations to the free functions above. Units mirror the BSM module: vega per 1 % vol, theta per calendar day (annual ÷ 365), rho per 1 % rate (annual ÷ 100). Quantity scales linearly; `Side::Short` flips the delta sign only (consistent with `greeks::delta`). Promoted `calculate_d_values_black_76` from `pub(crate)` to `pub` so external crates and the new module can share the helper. Formulas use the complete Hull (10th ed., Ch. 18) expressions. Note that the issue's formula for theta and rho omitted the `r·F·e^(-rT)·N(d1)` and F-leg discount terms; this implementation follows Hull rigorously and the relationship `ρ = -T · price` is verified analytically and by test. Tests (21, all passing): - Delta range: call ∈ (0,1), put ∈ (-1,0) - Identity `Δ_call − Δ_put = e^(-rT)` to 1e-9 - Gamma > 0, Vega > 0 across multiple strikes - Gamma and Vega are call/put symmetric - Theta < 0 for long ATM call/put (decay) - `ρ = -T · price / 100` analytic identity - BSM cross-check via `S = F·e^(-rT)`, `q = 0`: - `Δ_b76 = e^(-rT) · Δ_bsm` to 1e-9 - `ν_b76 = ν_bsm` to 1e-9 - `Γ_b76 = e^(-2rT) · Γ_bsm` to 1e-9 - Hull ATM-call reference (F=K=20, r=0.09, T≈1/3, σ=0.25) → Δ ≈ 0.5132 - Zero volatility → error - American / Bermuda / exotic → `GreeksError::Pricing(UnsupportedOptionType)` - Trait impl, side negation, quantity scaling Example: `examples/examples_pricing/src/bin/black_76_greeks.rs` walks ATM / ITM / OTM calls and puts on a 6-month CL futures contract, prints all Greeks, demonstrates the call-minus-put identity and trait usage. Closes #400.
Codecov Report❌ Patch coverage is
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
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.
Summary
Closed-form Greeks for the Black-76 (Black 1976) pricing model, completing the futures/forwards story started in #398. Closes #400.
Black-76 prices options on futures, forwards, swaptions, caps/floors and commodity futures — anywhere the underlying is a forward price
Fwith the cost of carry already baked in. The Greeks share a singlee^(-rT)discount factor across both legs and ignoredividend_yield(becauseFis carry-adjusted).Public surface
New module
src/greeks/black_76.rs:pub fn delta_b76(option: &Options) -> Result<Decimal, GreeksError>pub fn gamma_b76(option: &Options) -> Result<Decimal, GreeksError>pub fn vega_b76(option: &Options) -> Result<Decimal, GreeksError>— per 1 % volpub fn theta_b76(option: &Options) -> Result<Decimal, GreeksError>— per calendar daypub fn rho_b76(option: &Options) -> Result<Decimal, GreeksError>— per 1 % ratepub trait Black76Greeksmirroring the existingBlack76pricing traitcalculate_d_values_black_76was promoted frompub(crate)topubso callers and the new module can share the helper.Conventions match the BSM Greeks module:
Decimalend-to-end viad_mul/d_sub/d_div.Side::Shortflips the delta sign only (gamma/vega/theta/rho stay sign-agnostic, same asgreeks::gamma/vega/theta/rho).tracing::instrumenton every entry point.Formulas
The implementation follows Hull, Options, Futures and Other Derivatives (10th ed., Ch. 18) rigorously. The issue's formula for theta omitted the
+ r·F·e^(-rT)·N(d1)term and the formula for rho was the BSM-style two-leg expression rather than the simpler Black-76 form. The complete Hull forms are:The
ρ = -T · priceidentity is asserted analytically by tests for both call and put.Tests (21, all passing)
#[cfg(test)] mod testsinsrc/greeks/black_76.rs:Δ_call ∈ (0,1),Δ_put ∈ (-1,0)across moneynessΔ_call − Δ_put = e^(-rT)to 1e-9Γ > 0,ν > 0for both call and put across strikesΓ_call = Γ_put,ν_call = ν_put(Black-76 invariant)Θ < 0)ρ = -T · price / 100for both call and putS = F·e^(-rT),q = 0transform (1e-9):Δ_b76 = e^(-rT) · Δ_bsmν_b76 = ν_bsmΓ_b76 = e^(-2rT) · Γ_bsmGreeksErrorGreeksError::Pricing(UnsupportedOptionType)Black76Greeksround-trips against the free functionsSide::Shortnegates deltaExample
examples/examples_pricing/src/bin/black_76_greeks.rs— 6-month crude-oil futures (σ=30 %, r=4.5 %), walks ATM / ITM / OTM calls and puts, prints all Greeks, demonstrates theΔ_call − Δ_put = e^(-rT)identity (zero residual) and trait usage on a wrapper type.Acceptance criteria (from #400)
Δ_call − Δ_put = e^(-rT)S = F·e^(-rT),q = 0to 1e-9 (post-transform relationships)UnsupportedOptionTypecargo clippy --all-targets --all-features --workspace -- -D warningscleancargo fmt --all --checkcleancargo testandcargo test --features plotlyclean (lib: 3826 / 3822 passed). Thestatic_exportpass is pre-existing red onorigin/maindue to missing chromium for headless render — unrelated to this PR.cargo build --releasecleanpubitems documentedTest plan
cargo test --lib --all-features— 3822 passedcargo clippy --all-targets --all-features --workspace -- -D warnings— cleancargo fmt --all --check— cleancargo build --release— cleancargo run --bin black_76_greeks -p examples_pricing— identity holds to zero