Skip to main content

Numerically precise Black-Scholes pricing and implied-volatility primitives.

Project description

lets-be-precise

Numerically precise Black-Scholes pricing and implied-volatility primitives.

lbp.lbp is a compact compiled kernel whose forward (price) and inverse (implied volatility) paths target the lower edge of double-precision error across the whole (sigma, log(K/F)) plane -- including the deep-OTM and small-t corners where the standard Jaeckel and Cody-erfc paths shed several digits. The wheel also ships lbp.lbr, a thin Cython binding for Peter Jaeckel's "Let's Be Rational" 2013-2023 reference, exposed for honest side-by-side speed and accuracy comparison.

Install

pip install lets-be-precise

Optional extras:

  • lets-be-precise[demo] -- pulls in mpmath so python -m lbp.demos.* comparison modules can run their high-precision truth side.
  • lets-be-precise[dev] -- adds pytest and scipy for the (separately hosted) test suite.

Quick start

import math
import lbp                                # compiled BSM kernel
import lbp.lbr                            # Jaeckel LBR reference (same flags)

sigma, logk = 0.20, 0.15                  # logk = ln(K/F)
mlogk = -logk                             # lbp's k_or_mlogk on the mlogk branch
                                          # (matches LBR's x = ln(F/K))

C    = lbp.black(sigma, mlogk)            # call price under our BSM kernel
sig1 = lbp.iv(C, mlogk)                   # implied vol, lbp's Householder solver

# LBR uses Jaeckel's normalised price b = C / sqrt(F K) and takes the
# reflected moneyness x = ln(F/K) directly (same numeric value as mlogk).
beta = C * math.exp(-logk / 2.0)          # C -> Jaeckel's normalised price
sig2 = lbp.lbr.iv(beta, mlogk)            # same answer to ~eps

assert abs(sig1 - sig2) < 1e-13

# Diagnostic variant: returns (sigma, n_iter) so callers can verify the
# iter exited via the floor check (n_iter <= max_iter) rather than via
# max_iter exhaustion (n_iter == max_iter + 1).  Production code can stay
# on the plain lbp.iv; iv_with_status is for tests, benchmarks, or any
# caller that wants explicit convergence confirmation.
sig, n_iter = lbp.iv_with_status(C, mlogk)

The two implied-vol entry points are deliberately the same shape so the algorithmic differences are timing-clean for benchmarks; see src/lbp/lbr.pyx for the conversion table.

Numerical reliability

The compiled BSM kernel is built on top of mathpf's Mills-ratio primitives, including the cancellation-free symmetric divided difference R_DD(x, dx, +1) = (R(x - dx) - R(x + dx)) / (2 dx). This is the algorithmic counterpart to Jaeckel's two single-purpose expansions inside normalised_black_call_over_vega. They satisfy

b/vega = R(-h-t) - R(-h+t) = 2 t * R_DD(-h, t, +1)

so each Jaeckel expansion matches one R_DD branch. The headline difference:

region what Jaeckel uses R_DD branch accuracy
h < -10, h+t < -9.79 (deep OTM) 17th-order asymptotic series in q CF asymp ladder (n=2/4/6/8) or Taylor both ~eps; AEXP a touch cleaner at the Taylor corner
-10 <= h <= 0, t < 0.21 (small t) 12th-order Taylor in t 5-term Taylor seeded by R'''(x) mathpf wins by up to ~7 bits near h = -10

The small-t gap comes from Jaeckel's coefficient a := 1 + h * Y(h) (with Y(h) := Phi(h)/phi(h)), which suffers cancellation because Y(h) -> -1/h as h -> -infinity. At h = -10, |a| ~ 1/h^2 ~ 0.01 -- two decimal digits are killed before the series even runs. mathpf's R_DD Taylor branch is seeded from R'''(x) and descends to R'(x) via the cancellation-free (r_d3 + 1) / (x^2 + 3) relation, so the offending coefficient never appears.

Worst single cell, inside Jaeckel's actual STEXP dispatch region:

(h, t) = (-9.4, 0.17)            mpmath truth (80 dps)  =  3.72524275689953624e-03
LBR small_t_expansion_of_normalised_black_call_over_vega = 3.72524275689937664e-03
mathpf 2 t * R_DD(-h, t, +1)                             = 3.72524275689953624e-03

LBR    relerr : 4.28e-14   (~192 eps, ~7 bits lost)
mathpf relerr : 0          (bit-exact)

Reproduce on your machine:

pip install lets-be-precise[demo]
python -m lbp.demos.lbr_vs_mathpf

The demo prints both the AEXP-in-gate and STEXP-in-gate tables and explains the dispatch geometry. lbp.lbr also exposes Jaeckel's two file-scope expansions directly as lbp.lbr.aexp_over_vega(h, t) and lbp.lbr.stexp_over_vega(h, t) so users can probe further cells of their own choosing.

License

Proprietary -- see LICENSE. The package is distributed as binary wheels only via PyPI; the underlying Cython / C++ source is not included in the wheel.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

lets_be_precise-0.3.0-cp313-cp313-win_amd64.whl (71.9 kB view details)

Uploaded CPython 3.13Windows x86-64

lets_be_precise-0.3.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (410.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

lets_be_precise-0.3.0-cp313-cp313-macosx_11_0_arm64.whl (79.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

lets_be_precise-0.3.0-cp312-cp312-win_amd64.whl (71.9 kB view details)

Uploaded CPython 3.12Windows x86-64

lets_be_precise-0.3.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (407.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

lets_be_precise-0.3.0-cp312-cp312-macosx_11_0_arm64.whl (79.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

lets_be_precise-0.3.0-cp311-cp311-win_amd64.whl (70.7 kB view details)

Uploaded CPython 3.11Windows x86-64

lets_be_precise-0.3.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (391.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

lets_be_precise-0.3.0-cp311-cp311-macosx_11_0_arm64.whl (79.5 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

lets_be_precise-0.3.0-cp310-cp310-win_amd64.whl (70.5 kB view details)

Uploaded CPython 3.10Windows x86-64

lets_be_precise-0.3.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (378.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

lets_be_precise-0.3.0-cp310-cp310-macosx_11_0_arm64.whl (79.6 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file lets_be_precise-0.3.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.3.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3395f4de3e5d90aa8414f3e0aff03732ec3f74013f92c4c2f70558428a4e0f21
MD5 63d281d6341506daa4854d0cab88f0a7
BLAKE2b-256 8d4ba5b4aa7c42fb175f9999618dca35661e7d24c5795ab6ea7334de58380dff

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.3.0-cp313-cp313-win_amd64.whl:

Publisher: build_wheels.yml on quants-net/lets-be-precise

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lets_be_precise-0.3.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.3.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2329d24ed49d04dfc48e9cbe22a74817cb73a069fa9c30452598804a8c5fdad0
MD5 d1f1b56d4afa2b6b8d34b0790a17c6bb
BLAKE2b-256 d5c0ed5e525e7524ede46baa7e622f20a5bfba4b0ab08441852054698e93145c

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.3.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build_wheels.yml on quants-net/lets-be-precise

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lets_be_precise-0.3.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.3.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ebb664814f0e8fc97a16728a8751c071a652d0565f696e6a8839b3f0e4418772
MD5 0f907ed7791ca138107eb8c18b388bfb
BLAKE2b-256 f0d423e16ed6ff21e6022dcac55e63d8e772408b1536aef3827d4649de2539a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.3.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on quants-net/lets-be-precise

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lets_be_precise-0.3.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.3.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 702104f18809241d4e654c0bb6bd20757368abda7c08fe3547ca412cee9be891
MD5 d6f061f7f95bcf74e18fd2dd7405e9ee
BLAKE2b-256 4940ed24dee03e90aba218d05c1cdd6e7167b7c6e77982cd75dee99af8f367c6

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.3.0-cp312-cp312-win_amd64.whl:

Publisher: build_wheels.yml on quants-net/lets-be-precise

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lets_be_precise-0.3.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.3.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8c8119637c24692deabdeda04b0b523c1aef35dbcc0645b86f2dfb87349c1cfa
MD5 8508f2dcab231fbae65ed521aba42df3
BLAKE2b-256 a38db3a2cbc8d2a601766d8fee14924b45e1cd35a23a0c62090a6fd64fae677f

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.3.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build_wheels.yml on quants-net/lets-be-precise

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lets_be_precise-0.3.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.3.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3be2bf141873a6486e5cf700cc20c08ee4f9fe08c13983a51a318e0532360e29
MD5 cc0a8065aea9ea600473fe5ed24e2f5b
BLAKE2b-256 014e71bc9bbae2fcc1e4ed8dbdab9518d07cb9e7b2980c64ea115bc060fc7463

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.3.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on quants-net/lets-be-precise

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lets_be_precise-0.3.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.3.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c910cac36bfc436a94b37f3b3ead384851e6b7b842d04e7bff339f0193080278
MD5 9665ef2e7c2ae8849de2d979459882ec
BLAKE2b-256 22749499206f5b7b7a480950e4fbdfe844b03a0ac814af9f127a706ce7533f8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.3.0-cp311-cp311-win_amd64.whl:

Publisher: build_wheels.yml on quants-net/lets-be-precise

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lets_be_precise-0.3.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.3.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 be63f7b5ca8f2fa057c8173b8afc766f8b11d6a3824d7299b55bed704724dd30
MD5 6a93a3554648c4c11c1f02372621fdd5
BLAKE2b-256 47d4edd902f9575c528248f36293d0d966c5875534bab0789c24f02db2439c79

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.3.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build_wheels.yml on quants-net/lets-be-precise

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lets_be_precise-0.3.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.3.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4913b78fa4718149036c7a79c51a24ce8b34422cb28f2730a5e96162ab335317
MD5 768af6c9f6ecedf4be376be01eea7e88
BLAKE2b-256 814367fdde4d36e3b840c66ce66806039f15f5daec7261a25bfeb5a2e6b16397

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.3.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on quants-net/lets-be-precise

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lets_be_precise-0.3.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.3.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ca7dfe797867bd8ade2a3b02cbce9fc096375d6985bbda7200bff55254468d38
MD5 c83e0052a7d979feae41e5a5bc4fe73a
BLAKE2b-256 63992bf402a205297e6792568d61ab0f3f3d743e2534a618b57ac5155a7140d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.3.0-cp310-cp310-win_amd64.whl:

Publisher: build_wheels.yml on quants-net/lets-be-precise

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lets_be_precise-0.3.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.3.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5eaee07564c4dca96d262d120409a99d3e805478cad2997c7fec4676b47e3945
MD5 459ea0bdc1ce1747c2e58945b228d425
BLAKE2b-256 a1f76dea4f15925bb8fcf4cdc97d8a3aa5ad92d0d77976481fcf560e3aaa823b

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.3.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build_wheels.yml on quants-net/lets-be-precise

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lets_be_precise-0.3.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.3.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 738a186d331f98f415a4ddfb11bcdf2c076dae49c9a8115396d9fc8205ca1b10
MD5 659e3e9e5cb057907e490f3c71daf8a9
BLAKE2b-256 bc9ffc7e3cc71f3deba6a0bcfbc3d077e88a8b94848fa0f71fda8bae46bed210

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.3.0-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on quants-net/lets-be-precise

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page