A digital (cash-or-nothing / asset-or-nothing) option that is additionally knocked in or knocked out by a single barrier H (Rubinstein & Reiner, Risk 4(9) (1991); Haug, The Complete Guide to Option Pricing Formulas, 2nd ed. (2007), ch. 4, "Binary Barrier Options"). Unlike the two-asset barriers in HeynenKatTwoAssetBarrier.lean, this is single-underlying, single-variable Black–Scholes machinery — no bivariate normal CDF needed.
Transcribed from QuantLib's ql/pricingengines/barrier/analyticbinarybarrierengine.cpp (AnalyticBinaryBarrierEngine_helper::payoffAtExpiry, upstream commit 6efc1ed, 2026-09-23). QuantLib computes a drift mu once (mu = log(dividendDiscount/discount)/variance − 1/2, which for constant cost-of-carry b = r − q is (b − σ²/2)/σ², i.e. the same hkMu-style drift divided by σ²) and shares one formula shape between the cash-or-nothing payoff (K = the fixed cash amount) and the asset-or-nothing payoff (mu shifted by +1, K = the forward price S·e^{bT}, QuantLib's spot·dividendDiscount/discount). We keep mu an explicit parameter rather than hard-wiring either case, exactly mirroring QuantLib's shared code path — the docstrings below name which mu/K substitution each payoff kind uses, but the lemmas hold for the shared formula shape regardless of which substitution is made.
With stdDev = σ√T, η = 1 for Down{In,Out} / η = −1 for Up{In,Out}, φ = 1 for a call / φ = −1 for a put:
x₁ = φ(log(S/X)/stdDev + μ·stdDev), x₂ = φ(log(S/H)/stdDev + μ·stdDev), y₁ = η(log(H²/(SX))/stdDev + μ·stdDev), y₂ = η(log(H/S)/stdDev + μ·stdDev),
and, with (H/S)^{2μ} written as exp(2μ·log(H/S)) (algebraically identical for S, H > 0; QuantLib's own overflow guard evaluates the product (H/S)^{2μ}·Φ(y) as a whole in logs, exp(2μ·log(H/S) + log Φ(y)), rather than this factor alone),
B₁ = Φ(x₁), B₂ = Φ(x₂), B₃ = (H/S)^{2μ}·Φ(y₁), B₄ = (H/S)^{2μ}·Φ(y₂).
QuantLib's 16-branch switch (barrier type × call/put × strike-vs-barrier) selects one of B₁, B₂, B₃, B₄ and their ± combinations per branch; the final price is discount·K·α. The 16 branches are transcribed verbatim below as bboDownInAlpha/bboDownOutAlpha (both share η = 1) and bboUpInAlpha/bboUpOutAlpha (both share η = −1).
Main definitions
bboPhiOf: the sign φ, equal to 1 for a call and -1 for a put.bboX1, bboX2, bboY1, bboY2: the exponents of the four terms, struck at X (x₁), at the barrier H (x₂), at the reflected log log (H²/(S X)) (y₁) and at log (H/S) (y₂).bboHS2mu: the factor (H/S)^{2μ}, written as exp (2μ · log (H/S)).bboB1, bboB2, bboB3, bboB4: the four building blocks B₁ to B₄ of the Rubinstein and Reiner binary-barrier formulas.bboDownInAlpha, bboDownOutAlpha, bboUpInAlpha, bboUpOutAlpha: the knock-in and knock-out α-factors for a down or up barrier (before the discount and payoff scale), each selected from B₁ to B₄ by isCall and the strike-barrier comparison xGeH.
Main results
bboDownIn_add_DownOut: knock-in + knock-out, same direction/type/regime, collapses to the vanilla digital α-factor B₁ — the barrier-hit event and its complement partition the payoff (the closed-form counterpart of BarrierParity.lean's knockIn_add_knockOut_eq_vanilla, checked on the literal 16-branch table; not derived from it here). Checked for every (isCall, xGeH) combination by decide-free case split.bboUpIn_add_UpOut: the same parity for the up-barrier family (η = −1).bboB1_eq_Phi_bsd2: at the risk-neutral drift μ = (r − σ²/2)/σ² the vanilla exponent x₁ is Black–Scholes' d₂ (bsd2 S X r σ T) — B₁ is the standard-normal factor of the cash-or-nothing digital.value_digitalCall_eq_bboB1: under BSCallHyp, the Black–Scholes value of the digital call contract Contracts.digitalCall K T is the parity's vanilla leg e^{−rT}·B₁ (call side), through Contracts.BlackScholes.value_digitalCall.
Scope, disclosed honestly: this is a transcription of the QuantLib closed form, not a from-first-principles reflection-principle derivation of the barrier density. The in–out identity here is the closed-form counterpart of the measure-theoretic parity knockIn_add_knockOut_eq_vanilla (BarrierParity.lean): the barrier-hit event and its complement partition the payoff, so knock-in + knock-out must equal the vanilla leg B₁ — and the 16-branch table satisfies it branch-by-branch (a misplaced +/− sign on any one of the 14 sign-bearing QuantLib branches breaks it; the other two branches are the constant 0), though by pure term cancellation once the table is fixed. The vanilla leg alone is grounded beyond transcription: value_digitalCall_eq_bboB1 proves that, on the call side and at the risk-neutral drift, e^{−rT}·B₁ is the Black–Scholes value of the digital call contract. The knock-in and knock-out terms are not proved to be prices. The variance = 0 and (H/S)^{2μ} overflow edge cases QuantLib special-cases numerically, and the already-knocked-in/knocked-out spot guards AnalyticBinaryBarrierEngine::calculate() applies before calling payoffAtExpiry (a DownOut/UpOut priced at 0 once the spot has already crossed the barrier, a DownIn/UpIn re-priced as a plain vanilla digital via AnalyticEuropeanEngine once it has), are all out of scope — this file formalizes exactly payoffAtExpiry's own closed form, for a spot strictly on the live side of the barrier.
References
Tags
digital barrier option, Rubinstein–Reiner, cash-or-nothing, knock-in knock-out, exotic option, closed-form pricing, MSC 91G20, JEL G13