Guide 02 Three independent choices
Methods, backends and arrays
A calibration combines three choices that the package keeps independent: how a smile is priced, how derivatives are taken, and where the arrays live. Each one is an argument, and changing one does not change the others.
How a smile is priced
Fourier for Lévy models and Heston, a closed form where one exists, Monte Carlo for everything else.
Default pick CosMethod for Black–Scholes, Merton and Heston; LewisMethod with a wide range for Kou, Variance Gamma and NIG.
How derivatives are taken
Outside the calibration any backend; inside, four keywords, each an ADTypes backend.
Default pick ForwardDiff everywhere; a reverse backend outside with many quotes.
03 · ArraysWhere the numbers live
Float64 or Float32, on the host or on a CUDA device, derivatives included.
Default pick the CPU; Float32 from about a thousand quotes.
Pricing methods
The second argument of calibrate is any pricing method of FinancialMonteCarlo's pricer interface, so the kind of calibration is the choice of that argument:
| Method | Package | Models |
|---|---|---|
LewisMethod, CosMethod, CarrMadanLewisMethod, CarrMadanMethod | FinancialFFT.jl | every model with a characteristic exponent: Black–Scholes, Merton, Kou, Variance Gamma, NIG, Heston |
AnalyticMethod | FinancialCalibrator.jl | closed forms: BlackScholesProcess, LogNormalMixture, ShiftedProcess of either, see Closed forms |
MonteCarloConfiguration | FinancialMonteCarlo.jl | every model that it simulates, with the same seed at every evaluation |
Vibrato, VibratoSaltando, PureLRM | VibratoMonteCarlo.jl | Monte Carlo with smooth estimators of the derivatives |
calibrate(model, und, CosMethod(2^10), mkt) # Fourier
calibrate(model, und, LewisMethod(400.0, 40001), mkt) # Fourier, another quadrature
calibrate(slnm, und, AnalyticMethod(), mkt) # closed form
calibrate(model, und, MonteCarloConfiguration(10^5, 1), mkt) # simulationThe same Heston calibration of the DAX surface with three Fourier methods (measured by docs/benchmarks.jl):
| Heston, DAX, pricing engine | one loss | calibration | RMS error |
|---|---|---|---|
CosMethod(512) | 1.35 ms | 312 ms | 68.9 bp |
LewisMethod(200, 4001) | 4.63 ms | 1.07 s | 68.9 bp |
CarrMadanLewisMethod(400, 14) | 35.74 ms | 9.85 s | 68.9 bp |
Same model, same optimum: the engine sets the cost. CarrMadanLewisMethod(400, 14) runs an FFT of 2¹⁴ points per maturity, far more than nine smiles of twelve strikes need, and still stops near 3e-7 in volatility.
Choosing the size
A larger method costs more and stops being more accurate once its error is below the rounding of the implied volatilities. What it must be accurate for is the whole calibration, not only its starting point: on the DAX surface, at the starting point of each model of the quick start and the tests, and at the optimum its calibration reaches:
| method | Black–Scholes | Merton | Kou | Variance Gamma | NIG | Heston |
|---|---|---|---|---|---|---|
CosMethod(128) | < 1e-12 / < 1e-12 0.19 ms | 3e-10 / 2e-07 0.24 ms | 5e-09 / fails 0.26 ms | 3e-04 / fails 0.29 ms | 4e-05 / fails 0.24 ms | 1e-06 / 2e-03 0.44 ms |
CosMethod(256) | < 1e-12 / < 1e-12 0.44 ms | < 1e-12 / < 1e-12 0.46 ms | < 1e-12 / fails 0.44 ms | 3e-05 / 2e-03 0.49 ms | 3e-08 / 7e-04 0.39 ms | 3e-12 / 8e-07 0.73 ms |
CosMethod(512) | < 1e-12 / < 1e-12 0.94 ms | < 1e-12 / < 1e-12 1.19 ms | < 1e-12 / 4e-03 0.89 ms | 3e-06 / 7e-05 0.87 ms | < 1e-12 / 1e-06 0.81 ms | < 1e-12 / 8e-12 1.36 ms |
CosMethod(1024) | < 1e-12 / < 1e-12 1.62 ms | < 1e-12 / < 1e-12 1.99 ms | < 1e-12 / 6e-06 1.98 ms | 3e-07 / 2e-05 1.63 ms | < 1e-12 / 3e-12 1.61 ms | < 1e-12 / < 1e-12 2.92 ms |
CosMethod(2048) | < 1e-12 / < 1e-12 3.05 ms | < 1e-12 / < 1e-12 2.73 ms | < 1e-12 / < 1e-12 4.18 ms | 4e-09 / 2e-06 4.00 ms | < 1e-12 / < 1e-12 3.86 ms | < 1e-12 / < 1e-12 4.99 ms |
LewisMethod(100, 1001) | 8e-05 / 7e-05 0.75 ms | 1e-05 / 4e-04 0.82 ms | 3e-05 / 3e-04 0.72 ms | 2e-04 / 7e-03 0.78 ms | 2e-05 / 8e-04 0.56 ms | 1e-04 / 3e-04 1.42 ms |
LewisMethod(100, 2001) | 1e-11 / 1e-11 1.44 ms | 2e-12 / 6e-08 1.65 ms | 4e-12 / 5e-07 1.89 ms | 2e-04 / 7e-03 1.90 ms | 3e-06 / 7e-04 1.11 ms | 1e-07 / 5e-05 2.27 ms |
LewisMethod(100, 4001) | < 1e-12 / < 1e-12 2.79 ms | < 1e-12 / 6e-08 2.35 ms | < 1e-12 / 6e-07 3.55 ms | 2e-04 / 7e-03 3.74 ms | 3e-06 / 7e-04 2.77 ms | 1e-07 / 5e-05 4.56 ms |
LewisMethod(200, 4001) | 1e-11 / 1e-11 3.22 ms | 2e-12 / 6e-11 2.53 ms | 4e-12 / 4e-11 3.97 ms | 2e-05 / 6e-04 3.83 ms | 2e-10 / 1e-06 2.04 ms | 2e-11 / 2e-08 4.32 ms |
LewisMethod(400, 8001) | 1e-11 / 1e-11 6.51 ms | 2e-12 / 6e-11 5.17 ms | 4e-12 / 4e-11 7.69 ms | 2e-06 / 8e-05 5.48 ms | 3e-12 / 6e-11 5.40 ms | 2e-11 / 3e-11 8.79 ms |
LewisMethod(800, 16001) | 1e-11 / 1e-11 11.37 ms | 2e-12 / 6e-11 10.14 ms | 4e-12 / 4e-11 14.38 ms | 2e-07 / 4e-06 15.04 ms | 3e-12 / 6e-11 10.11 ms | 2e-11 / 3e-11 19.73 ms |
CosMethod(0.0001) | 1e-07 / 1e-07 2.75 ms | 4e-08 / 1e-06 3.31 ms | 4e-08 / 1e-06 3.54 ms | 2e-08 / 9e-08 4.41 ms | 3e-08 / 4e-07 3.39 ms | 4e-07 / 5e-07 5.69 ms |
CosMethod(1.0e-6) | 2e-10 / 2e-10 2.90 ms | 3e-10 / 8e-09 4.04 ms | 1e-09 / 1e-08 4.09 ms | 3e-09 / 4e-08 6.98 ms | 4e-10 / 4e-09 3.50 ms | 1e-09 / 3e-09 6.52 ms |
LewisMethod(0.0001) | 1e-07 / 2e-07 1.48 ms | 9e-08 / 2e-06 2.29 ms | 9e-08 / 4e-06 2.53 ms | 1e-07 / 2e-06 5.07 ms | 2e-07 / 1e-06 2.09 ms | 1e-06 / 1e-06 5.76 ms |
LewisMethod(1.0e-6) | 1e-08 / 1e-08 1.59 ms | 1e-09 / 5e-08 2.24 ms | 4e-09 / 4e-08 2.61 ms | — N > N_max | 1e-09 / 4e-08 2.25 ms | 2e-08 / 2e-08 5.83 ms |
CarrMadanLewisMethod(400, 14) | 4e-07 / 4e-07 11.20 ms | 1e-07 / 1e-05 14.21 ms | 1e-07 / 2e-05 17.50 ms | 1e-05 / 1e-04 18.01 ms | 7e-07 / 4e-06 12.73 ms | 1e-06 / 5e-06 32.12 ms |
CarrMadanMethod(400, 14) | 7e-07 / 6e-07 10.49 ms | 1e-07 / 8e-06 15.30 ms | 2e-07 / fails 18.65 ms | 1e-05 / 9e-05 18.12 ms | 8e-07 / 4e-06 14.69 ms | 7e-07 / 4e-06 36.11 ms |
Largest implied volatility error over the 99 DAX quotes of the loss, against LewisMethod(3200, 512001), at the starting point of each model / at the optimum of its calibration; under it, one loss at the starting point. In bold the recommended setting: Black–Scholes: CosMethod(256), Merton: CosMethod(256), Kou: LewisMethod(200, 4001), Variance Gamma: LewisMethod(800, 16001), NIG: LewisMethod(400, 8001), Heston: CosMethod(512). Measured 2026-11-27 on Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz.
CosMethodfor Black–Scholes, Merton and Heston:CosMethod(256)is exact to the rounding for the first two, at a fraction of a millisecond per loss; Heston needsCosMethod(512)at its optimum (a volatility of variance above 1).LewisMethodfor Kou. The DAX optimum of Kou has almost no up jumps (p → 0) andλ₊ → 1. The truncation range ofCosMethodcomes from a Chernoff bound,L / λ₊for the upper tail however rare the jumps are, and grows to[-4, 32]:CosMethod(512)is 43 bp off there, and its derivatives (range held constant, FinancialFFT.jl#64) are not those of its prices. LBFGS then takes its 1000 iterations and 50000 evaluations of the loss, 128 s, against 1 s withCosMethod(1024)or 1.5 s withLewisMethod(100, 4001). Lewis has no truncation range to grow:LewisMethod(200, 4001)is accurate to 1e-10 at both points.- Variance Gamma and NIG need a wide Lewis range. Their characteristic functions decay slowly at short maturities (as a power of the frequency for Variance Gamma), so
LewisMethod(100, ·)stops at 1e-4 to 1e-2 in volatility whatever its number of nodes: on DAX it moved the Variance Gamma optimum by 1.5 bp of RMS error.LewisMethod(800, 16001)(VG) andLewisMethod(400, 8001)(NIG), with the spacing2A/Nof 0.1, reach 4e-6 and 1e-10.CosMethodconverges for them too, but its derivatives are not those of its prices there: a derivative error of 4e-5 for Variance Gamma withCosMethod(1024), which the polish andcalibrated_parameterswould inherit. - From a tolerance, no tuning.
CosMethod(ε)andLewisMethod(ε)(FinancialFFT 0.9, 0.10) choose the size of each maturity from a toleranceεon the prices, in their units: on DAX (spot 10514)ε = 1e-6gives at most 4e-8 in volatility at both points for every model,ε = 1e-4at most 4e-6 (the volatility error is aboutε/ vega). The sizes are chosen again at every loss, which costs 2 to 7 ms on this surface, more than the whole loss of a fixedCosMethod(256): the fixed settings above stay the fast choice where they are known (FinancialCalibrator#47 would choose the sizes once per calibration). The exception is Variance Gamma, whose characteristic function decays as a power: at its optimumCosMethod(1e-4)errs by 9e-8, 40 times less thanLewisMethod(800, 16001), at about the same cost (15 ms per loss there, 4 ms at the start), with derivatives within 6e-7 of those of the reference;CosMethod(1e-6)takes 90 ms there andLewisMethod(1e-6)more than itsN_maxnodes. - Carr–Madan stops near 1e-7 to 1e-5 whatever the size of its FFT, at several times the cost of a quadrature: use it only for very large smiles, where one FFT prices all the strikes.
Automatic differentiation
From outside
calibrated_parameters is a function of the quoted volatilities that every backend can differentiate, since its rules come from the implicit function theorem and not from tracing the optimizer:
using ForwardDiff, Zygote
f(v) = calibrated_parameters(model, und, method, mkt, v)
ForwardDiff.jacobian(f, mkt.Volatility) # ∂p*/∂v, forward mode
Zygote.gradient(v -> w' * f(v), mkt.Volatility) # wᵀ ∂p*/∂v, reverse mode (ChainRules)Mooncake has a rule of its own, in the package extension loaded with Mooncake (ImplicitDifferentiation's ChainRules rule needs a RuleConfig, which Mooncake.@from_rrule does not pass):
using Mooncake, ADTypes
import DifferentiationInterface as DI
DI.gradient(v -> w' * f(v), AutoMooncake(; config = nothing), mkt.Volatility)Support
Every derivative of the calibration runs through the implicit function theorem, so the outer backend never traces the optimizer: it only meets the rule of the implicit function, and the inner backends meet the loss. The backends can be mixed freely, forward over reverse or reverse over forward. The table shows the gradient of $w^\top p^*(v)$ for Variance Gamma with LewisMethod(100.0, 4001) on 15 quotes, relative to the ForwardDiff gradient on the host. Every case ran in a fresh Julia process (bench/bench.jl vg15, 2026-12-01, FinancialCalibrator 0.4.6). The first call is timed after one calibration in that process, so it is the compilation of the derivatives only; the device is a laptop GPU (NVIDIA MX150):
| Outer | Inner x / y | Host: first call | Host: next calls | Device: next calls | Error |
|---|---|---|---|---|---|
| ForwardDiff¹ | ForwardDiff / ForwardDiff | 0.95 s | 0.15 s | 0.32 s² | 0 (host), 5e-13 (device) |
| Zygote | ForwardDiff / ForwardDiff | 26 s | 0.14 s | 0.20 s | 1e-14, 2e-12 |
| Mooncake | ForwardDiff / ForwardDiff | 41 s | 0.19 s | 0.80 s | 1e-14, 2e-12 |
| Zygote | Mooncake / ForwardDiff | 92 s | 0.11 s | ✗³ | 1e-14 |
| Mooncake | Mooncake / ForwardDiff | 76 s | 0.21 s | ✗³ | 1e-14 |
| Enzyme, reverse⁴ | ForwardDiff / ForwardDiff | 127 s | 0.11 s | 0.22 s | 1e-14, 2e-12 |
| Enzyme, forward⁴ | ForwardDiff / ForwardDiff | 120 s | 0.10 s | ✗⁵ | 1e-15 |
One run per case: on this laptop the times of two runs differ by up to a third. The device column is 2026-12-01 too, but for ForwardDiff (² measured 2026-11-21; the device keeps the conditions of 0.4.5, see below).
¹ With one chunk for all the quotes, ForwardDiff.gradient(f, v, ForwardDiff.GradientConfig(f, v, ForwardDiff.Chunk{length(v)}())). Or build the function once with calibrated_parameters_function, whose solver remembers its last quotes: any chunk then runs one calibration (#48; at 45 quotes the default chunk without it was 2.9x slower). ForwardDiff's default chunk (8) splits 15 quotes in two, and each chunk runs the whole calibration again. ² One direction: ForwardDiff cannot seed a CuArray, so on the device it gives one derivative at a time (ForwardDiff.derivative along a bump of one quote). ³ Mooncake differentiating the inner ForwardDiff gradient of the conditions meets a broadcast of dual numbers over a CuArray, which it does not support. ⁴ Enzyme works through rules of the extension of FinancialCalibrator for EnzymeCore: they build the implicit function outside of the differentiated code, then apply ImplicitDifferentiation's Enzyme rules to it as a constant (Enzyme cannot trace the construction, and failed on the GPU when it did, #21). Those rules need the solver to return an inferred type, which it does. On the device the first call compiles for 4 to 7 minutes here, 18 on Black–Scholes with CosMethod(1024). ⁵ Enzyme's forward mode seeds the gradient with one-hot arrays that it fills by scalar indexing, which a CuArray refuses (EnzymeAD/Enzyme.jl#4048); the reverse mode works on the device.
What the numbers say:
- Once compiled, a gradient costs about one calibration, whatever the backend: here the solve takes about 0.1 s (NelderMead, then the Levenberg–Marquardt polish). On the host the quotes enter the optimality conditions linearly (#49): the derivatives in the quotes are one matrix-vector product with the Jacobian of the errors at the solution, which the solver computes once, so no pricing is differentiated with respect to the quotes. On these 15 quotes that precomputation costs about what it saves (Zygote outside: 0.14 s against 0.12-0.14 s with 0.4.5); from about a hundred quotes it is 2 to 30 times faster (below).
- The first call is compilation: Zygote and Mooncake compile their rules for the whole pricing method.
- A reverse backend for
xis no longer reverse over forward on the host: the conditions are linear in the quotes, so Mooncake differentiates a matrix-vector product, not the pricer (twice as fast as 0.4.5 here, and it compiles in half the time). On the device the conditions keep the gradient at the current quotes, and Mooncake forxmeets the ForwardDiff gradient through the pricer (³).
The test suite checks ForwardDiff, Zygote, Mooncake and Enzyme (reverse, and forward on Black–Scholes) against the formula $\partial p^*/\partial v = -H^{-1} \partial_v \nabla_p L$, and Zygote and Mooncake on the GPU.
Inside
Four derivatives are taken inside, each by an ADTypes.jl backend, through DifferentiationInterface.jl:
| Keyword | Of | Default |
|---|---|---|
autodiff | the gradient of the loss, for a gradient based Optim optimizer (LBFGS(), BFGS(), ...) | AutoForwardDiff() |
polish_backend | the Jacobian $J$ of the volatility errors, in the Levenberg–Marquardt polish | AutoForwardDiff() |
conditions_backend | the gradient $g = \partial_x L$, the optimality conditions | AutoForwardDiff() |
implicit_backends = (x = ..., y = ...) | the derivatives of $g$ with respect to the quotes (x) and to the variables (y) | AutoForwardDiff() for both |
The last row differentiates the gradient of the row before it: a second order derivative, nested. ForwardDiff everywhere suits a calibration, whose parameters are few: the Jacobian $J$ has as many columns as parameters, and the Hessian $\partial_x g$ is small. The one place where reverse mode can pay is x with a reverse outer backend and many quotes: the reverse rule needs $(\partial_v g)^\top w$, which forward mode gets one quote at a time.
using ADTypes, Mooncake
calibrated_parameters(model, und, method, mkt, v; implicit_backends = (x = AutoMooncake(; config = nothing), y = AutoForwardDiff()))The table above times these combinations. Zygote for x does not work: it cannot differentiate the inner ForwardDiff gradient of the conditions.
What each backend differentiates
The inner backends differentiate two functions: FinancialCalibrator.loss (for autodiff) and vol_errors (for polish_backend and the conditions). loss returns $\infty$ where the parameters cannot be priced, by catching the exception. Zygote and Mooncake cannot differentiate a catch, so FinancialCalibrator gives them a rule for loss (a ChainRules rrule for Zygote, a Mooncake rule in the extension) that runs the try itself around the reverse pass of the loss without it, with a zero gradient where the loss is $\infty$, as ForwardDiff and Enzyme give (FinancialCalibrator.jl#40). Gradients on the DAX surface, against ForwardDiff (shifted lognormal mixture with AnalyticMethod, Variance Gamma with LewisMethod(100.0, 2001)):
| Backend | vol_errors | loss |
|---|---|---|
| ForwardDiff | ✓ | ✓ |
| Zygote | ✓ | ✓ (a rule, FinancialCalibrator.jl#40) |
| Mooncake | ✓ | ✓ (a rule, FinancialCalibrator.jl#40) |
| Enzyme, reverse | ✓ | ✓ |
Enzyme needs no runtime activity with FinancialMonteCarlo 0.9 or later, whose curves are isbits (FinancialMonteCarlo.jl#79); with 0.8, a model rebuilt from active parameters on the dividend curve that adapter returns needs AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Reverse)). When calling Enzyme.autodiff directly, pass the model, the market data and the cache of loss_cache as Const arguments: captured in a closure annotated Const, they make Enzyme ask for runtime activity on any underlying. Every backend works as the autodiff of a gradient optimizer such as LBFGS() on the mixture and on Variance Gamma, whose line search on DAX reaches parameters that cannot be priced. On Heston with CosMethod, Enzyme does not (EnzymeNoDerivativeError, FinancialCalibrator.jl#41).
Zygote is correct but slow here: a gradient of vol_errors costs 1.6 ms on the mixture (ForwardDiff 0.11 ms) and 16 ms with Lewis (ForwardDiff 2.7 ms), with FinancialMonteCarlo 0.9 (laptop, i7-8550U). With 0.8 the mixture took 7.7 ms, most of it Zygote tracing the model setup (rebuilding the model and integrating the dividend curve element by element): FinancialMonteCarlo 0.9 gives set_parameters! and the integral of a curve reverse rules (FinancialMonteCarlo.jl#80). The inner backends have few parameters to differentiate, which is what ForwardDiff is for; Zygote belongs outside, on calibrated_parameters, where it meets only the rule of the implicit function.
Every backend differentiates the pricing code itself, which is written for it: no branches, validations outside the derivatives, and for the closed forms a kernel whose Zygote gradient is inferred (see Other closed forms).
Which backend
bench/ times every combination of backends, each in a fresh Julia process, on three models with 99 quotes of the DAX surface (synthetic volatilities of a known model, so the optimum is well conditioned): the shifted lognormal mixture with AnalyticMethod, Variance Gamma with LewisMethod(100.0, 4001) and Heston with CosMethod(1024). The times are run times once compiled (laptop: i7-8550U, NVIDIA MX150; Enzyme 0.13.221, CUDA 6.5.1, 2026-11-20); the first call compiles for 1 to 3 minutes with Enzyme, Zygote and Mooncake, against 10 ms with ForwardDiff. ✗ marks a combination that fails; FinancialCalibrator.jl#41 explains each one.
Inside: ForwardDiff or Enzyme forward. The derivatives in the parameters (host, Float64):
| Backend | loss gradient: mixture | VG | Heston | vol_errors Jacobian: mixture | VG | Heston |
|---|---|---|---|---|---|---|
| ForwardDiff | 97 μs | 6.5 ms | 9.0 ms | 90 μs | 6.0 ms | 8.5 ms |
| Enzyme, forward | 85 μs | 4.3 ms | 7.5 ms | 89 μs | 4.6 ms | 6.9 ms |
| Enzyme, reverse | 128 μs | 9.2 ms | 10 ms | 1.5 ms | 0.74 s | 0.54 s |
| Zygote | 1.5 ms | 20 ms | 26 ms | 72 ms | 1.1 s | 1.0 s |
| Mooncake | 0.40 ms | 69 ms | 58 ms | 12 ms | 1.8 s | 2.8 s |
A gradient in 3 to 5 parameters is what forward mode is for. Reverse mode builds the Jacobian of vol_errors one quote at a time, 100 to 300 times slower. In calibrate (LBFGS()):
autodiff / polish_backend | Mixture | VG | Heston |
|---|---|---|---|
| ForwardDiff / ForwardDiff (the default) | 4.6 ms | 0.34 s | 0.43 s |
| Enzyme forward / ForwardDiff | 4.8 ms | 0.21 s | ✗ |
| Enzyme forward / Enzyme forward | 5.0 ms | 0.26 s | ✗ |
| ForwardDiff / Enzyme forward | 5.1 ms | 0.24 s | 0.38 s |
| Enzyme reverse / ForwardDiff | 6.2 ms | 0.32 s | ✗ |
| Zygote / ForwardDiff | 0.16 s | 0.53 s | 0.61 s |
| Mooncake / ForwardDiff | 33 ms | 2.7 s | 3.0 s |
| ForwardDiff / Enzyme reverse, Zygote, Mooncake | 19 ms to 0.81 s | 7.6 to 20 s | 4.8 to 13 s |
NelderMead(), ForwardDiff polish | 6.9 ms | 0.71 s | 0.68 s |
Enzyme forward saves up to a third on the Fourier methods, but the default stays ForwardDiff: it compiles in milliseconds and works on every model.
Outside: any reverse backend, or ForwardDiff. The gradient of $w^\top p^*(v)$ with the default inner backends, and the full Jacobian $\partial p^*/\partial v$:
| Outer backend | Gradient: mixture | VG | Heston | Jacobian: mixture | VG | Heston |
|---|---|---|---|---|---|---|
| ForwardDiff | 8.1 ms | 0.42 s | 0.60 s | 8.0 ms | 0.43 s | 0.66 s |
| Zygote | 8.6 ms | 0.45 s | 0.63 s | 9.6 ms | 0.46 s | 0.74 s |
| Enzyme, reverse | 8.0 ms | 0.44 s | 0.63 s | 23 ms | 1.8 s | 2.0 s |
| Enzyme, forward | 54 ms | 3.1 s | 4.4 s | 82 ms⁶ | 4.3 s⁶ | 5.9 s⁶ |
| Mooncake | 17 ms | 0.86 s | 1.2 s | 113 ms⁶ | 5.4 s⁶ | 11 s⁶ |
Zygote, inner x Mooncake | 12 ms | 0.46 s | 0.68 s |
bench/bench.jl on the DAX scenarios (99 quotes in the loss), 2026-12-01, FinancialCalibrator 0.4.6. Against 0.4.5, measured case by case in alternating fresh processes, the quotes entering the conditions linearly (#49) made the gradients 2 to 3 times faster (VG with ForwardDiff outside 1.06 → 0.42 s, Heston 1.74 → 0.60 s) and the Jacobians 2.5 to 8.5 times faster (Heston with Zygote outside 6.31 → 0.74 s); Mooncake for x 1.8 → 0.46 s (VG), 2.3 → 0.68 s (Heston). ⁶ Not rerun with 0.4.6 (2026-11-20, 0.4.x before #49).
The outer backend meets only the rule of the implicit function, so the inner derivatives set the cost, and the reverse backends tie with ForwardDiff on 99 quotes. Enzyme as an inner backend of calibrated_parameters fails in this benchmark (#41). For the full Jacobian, ForwardDiff or Zygote.
Arrays: Float64 on the host for a hundred quotes, Float32 on the host from about a thousand. Heston with CosMethod(1024) on 99 DAX quotes and on a large surface (4 maturities × 500 strikes, 1929 quotes in the loss), 2026-12-02, FinancialFFT 0.10.4:
Host Float64 | Host Float32 | Device Float64 | Device Float32 | |
|---|---|---|---|---|
loss gradient, ForwardDiff, 99 quotes | 6.1 ms | 6.3 ms | 6.6 ms | 6.1 ms |
calibrate, NelderMead(), 99 quotes | 0.57 s | 0.45 s | 1.2 s | 1.3 s |
calibrate, LBFGS(), 99 quotes | 0.30 s | 0.49 s | 0.37 s | 1.1 s |
| Gradient of $w^\top p^*$, Zygote, 99 quotes | 0.63 s | 0.55 s | 3.2 s | 2.5 s |
loss gradient, ForwardDiff, 1929 quotes | 28 ms | 6.3 ms | 16 ms | 7.9 ms |
calibrate, NelderMead(), 1929 quotes | 1.4 s | 0.61 s | 1.3 s | 0.69 s |
| Gradient of $w^\top p^*$, Zygote, 1929 quotes | 1.7 s | 0.71 s | 40 s | 21 s |
Since FinancialFFT 0.10.4, which keeps the Float32 prices free of subnormal numbers (FinancialFFT#96), Float32 on the host is 2 to 4 times faster than Float64 on the large surface (on 2026-12-01 it was 2 to 4 times slower), and faster than the device in either type on this laptop (NVIDIA MX150). On 99 quotes the two types cost about the same. LBFGS() is the exception: in Float32 its line search takes 2.5 times the evaluations of the loss on Heston (77 against 31, the same 29 iterations), so it is slower than in Float64 (#52); NelderMead() is not affected.
The gradient of the calibrated parameters on 1929 quotes went from 57 s to 1.9 s on the host (161 → 6.6 s in Float32, 0.71 s since FinancialFFT 0.10.4): the conditions linear in the quotes (#49) apply on the host only, and the device keeps the gradient at the current quotes, differentiated through the pricer once per quote.
Mooncake differentiates the Fourier methods on the device too (Variance Gamma and Heston, the loss gradient matches ForwardDiff to 4e-15; the concatenation of the maturities through a rule of the extension, faster on the host than Mooncake's own of 0.5.74, chalk-lab/Mooncake.jl#1484), and the closed forms (Black–Scholes and the mixtures, 1e-14): their device broadcasts refuse a callable with state, so a rule of the CUDA and Mooncake extension differentiates the same prices with the numbers of the kernel passed as broadcast arguments (#41 group 3c). The Mooncake Jacobian of vol_errors on the device works since Mooncake 0.5.74, which accepts a CuArray output (#41 group 3d, chalk-lab/Mooncake.jl#1485).
Float32 loses digits: the calibrated parameters agree with the model that generated the quotes to 1e-5 to 2e-4 (1e-13 in Float64), at a final loss of about 1e-10 (3e-6 in volatility), and the derivatives of the calibrated parameters agree with Float64 to 4e-5 (shifted lognormal mixture) to 4e-3 (Variance Gamma, 15 quotes): enough for a calibration, not for derivatives that need more digits. On 1929 quotes the gradient of $w^\top p^*$ is dominated by the derivative of the conditions in the quotes (implicit_backends.x, forward mode: one direction per quote); a reverse backend for x needs one pullback per parameter instead, see #41. Its full Jacobian with Zygote takes 5.5 minutes on the host and did not end in 50 minutes in host Float32 (FinancialFFT 0.10.3, before FinancialFFT#96; not measured since).
Arrays
Floating point types
MarketData and CalibratedData are parametric in the floating point type. adapter(raw, Float32) reads a surface in Float32, and the losses are computed in it.
GPU
The strikes and the volatilities of MarketData can live on the GPU. With CUDA.jl loaded, every smile is then priced on the device (the extension moves the method to CudaMode), the implied volatilities are a broadcast on the device, and so are the derivatives: the dual numbers of ForwardDiff and the cotangents of Zygote are CuArrays too. The small linear systems (the Levenberg–Marquardt step, the implicit function theorem) come back to the CPU.
using CUDA, Adapt
gmkt = adapt(CuArray, mkt) # strikes and volatilities on the device
calibrate(model, und, CosMethod(1024), gmkt)
calibrated_parameters(model, und, CosMethod(1024), gmkt)
Zygote.gradient(V -> sum(calibrated_parameters(model, und, CosMethod(1024), gmkt, V)), gmkt.Volatility)[1] # a CuMatrixMeasured on a laptop GPU (NVIDIA MX150) with the DAX surface of 108 quotes, once compiled:
| Host | Device | Agreement | |
|---|---|---|---|
calibrate, Black–Scholes, CosMethod(1024) | 0.06 s | 0.41 s | 4e-15 |
calibrate, Variance Gamma, LewisMethod(100.0, 4001) | 0.36 s | 0.71 s | 3e-8 |
one loss, Black–Scholes, CosMethod(1024) | 1.1 ms | 8.5 ms | |
one loss, Variance Gamma, LewisMethod(100.0, 4001) | 2.7 ms | 7.2 ms | |
| Zygote gradient, Black–Scholes | 4e-14 | ||
| Zygote gradient, Variance Gamma | 8e-8 |
How the cost of one loss grows with the strikes of a smile, on the same laptop (Black–Scholes, CosMethod(4096), measured by docs/benchmarks.jl):
A smile of 12 strikes is far too small to occupy a GPU, and these numbers time kernel launches: a loss costs 7 to 9 ms on the device whatever the model and the method. The device pays off with many strikes per smile, or many quotes. The point here is that nothing in the calibration, nor in its derivatives, needs to leave the device.
Float32
Consumer GPUs compute in Float64 at a fraction of their Float32 rate (1/32 on the MX150), and adapter(raw, Float32) gives a surface in Float32. On the DAX surface this changes nothing on the device, since the launches dominate: a loss costs 9.1 ms (Black–Scholes, Cos) and 7.6 ms (Variance Gamma, Lewis), and a calibration takes about as long (Black–Scholes 0.46 s, Variance Gamma 0.75 s, parameters within 2e-4 of the host in Float32). On a smile large enough to occupy the device, Float32 pays: 2000 strikes with CosMethod(4096):
| Host | Device | |
|---|---|---|
Float64 | 17.7 ms | 14.3 ms |
Float32 | 17.6 ms | 6.7 ms |
Reactant (XLA)
The closed-form loss of AnalyticMethod (Black–Scholes, the lognormal mixtures, shifted or not) compiles with Reactant.jl, and so does its Enzyme gradient. The parameters are traced; the market data and the cache are constants of the compiled program. The implied volatilities go through FinancialToolbox's traced Let's Be Rational, with one traced call/put flag per quote (out of the money).
using Reactant, Enzyme
cache = FinancialCalibrator.loss_cache(model, und, AnalyticMethod(), mkt)
L(q) = FinancialCalibrator.loss(q, model, AnalyticMethod(), mkt, cache)
p = Reactant.to_rarray(FinancialMonteCarlo.get_parameters(model))
@jit L(p)
@jit Enzyme.gradient(Reverse, L, p)The values agree with plain Julia to 1e-15 (the errors per quote), the gradient with ForwardDiff to 5e-15 (test/reactant, Linux CPU and CUDA). It is correct, not fast: on the DAX surface (108 quotes, Linux, Reactant 0.2.309, once compiled)
| Compile (loss, gradient) | Loss | Gradient | Host loss | Host ForwardDiff | |
|---|---|---|---|---|---|
| Black–Scholes, CPU | 200 s, 32 s | 548 μs | 678 μs | 48 μs | 64 μs |
| Shifted mixture, CPU | 32 s, 35 s | 634 μs | 951 μs | 62 μs | 99 μs |
| Black–Scholes, CUDA (MX150) | 283 s, 53 s | 24.5 ms | 24.5 ms | 51 μs | 132 μs |
| Shifted mixture, CUDA (MX150) | 75 s, 58 s | 24.1 ms | 24.9 ms | 64 μs | 110 μs |
The cost is the traced implied volatility: about 0.4 ms per compiled call on the CPU whatever the size, and the branches of the solver are XLA conditionals, which the MX150 runs at about 0.3 ms per quote (FinancialToolbox's gpu.md, decision D-010 there, measures the alternatives). The calibration itself, the Fourier methods and the Monte Carlo methods are not traced. On Windows the XLA CPU compiler cannot link the implied volatility (EnzymeAD/Reactant.jl#3848).