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.4.6-cp313-cp313-win_amd64.whl (79.7 kB view details)

Uploaded CPython 3.13Windows x86-64

lets_be_precise-0.4.6-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (476.4 kB view details)

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

lets_be_precise-0.4.6-cp313-cp313-macosx_11_0_arm64.whl (96.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

lets_be_precise-0.4.6-cp312-cp312-win_amd64.whl (79.6 kB view details)

Uploaded CPython 3.12Windows x86-64

lets_be_precise-0.4.6-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (473.1 kB view details)

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

lets_be_precise-0.4.6-cp312-cp312-macosx_11_0_arm64.whl (96.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

lets_be_precise-0.4.6-cp311-cp311-win_amd64.whl (78.6 kB view details)

Uploaded CPython 3.11Windows x86-64

lets_be_precise-0.4.6-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (457.0 kB view details)

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

lets_be_precise-0.4.6-cp311-cp311-macosx_11_0_arm64.whl (96.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

lets_be_precise-0.4.6-cp310-cp310-win_amd64.whl (78.4 kB view details)

Uploaded CPython 3.10Windows x86-64

lets_be_precise-0.4.6-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (443.5 kB view details)

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

lets_be_precise-0.4.6-cp310-cp310-macosx_11_0_arm64.whl (97.1 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 7f1ad2a97d15719da3c57a059b66bac0e4b94b50c7c700d95c307498a3378a37
MD5 b5a6465e8c95a3d3951c44b10cb0844e
BLAKE2b-256 b0b41f486238e09c376946005c4d3a02cb4f6761d6f5d64d9ca1f20ad00df46a

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.4.6-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.4.6-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.4.6-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a0085e940e703ca9c7ebcc2147955f603bcab81f78f49691aa6622ab023e5e7d
MD5 86228b5b3422301dcb4d63fbc651ae23
BLAKE2b-256 4079bff6b69df71c0fb1847a5992baccd8d13e6dd30b12daee21012889f2b7f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.4.6-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.4.6-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.4.6-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b5616e3056965c83dafbd5c20f91a2c32f6e61b37d5f6de91436625d358fcd41
MD5 af9d7c95475243079d4835e93fb9352c
BLAKE2b-256 122715f6fdb6a86da6325b4f4b33165db5cea459e042d32c90c9c4523e72d36b

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.4.6-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.4.6-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.4.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 fdccf9717809846e8cc974c2d2e2f4dfec25bb4288c12fdf3885db2b82cf8963
MD5 db709977f7ee07e55c22d5bc6b65720c
BLAKE2b-256 61261cbe8bfaa85e0ed2bcfca49a8192757df50e42113348c5c7e55b4b415744

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.4.6-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.4.6-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.4.6-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cc00ba989d026cf7a45c0c7d67494889a524eaf11a7e9eec27348ea9ea7db12b
MD5 1a4a1f3e9a5a3e482bb147c689b8b7eb
BLAKE2b-256 e42b1d7073feae74982b7f69781f0febd9988d5e45ecbaaa3cd9a4efad986b1b

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.4.6-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.4.6-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.4.6-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f079a95cc44d440e245b4112857f32665a0dc0e4d6d81ec83986a90f452daedd
MD5 d7e9b2da0d5de838d61d186894c5b17c
BLAKE2b-256 99b70a4a115f3b79e32964364ca89c09f43a103e76138a9faf21a6b89b035eb7

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.4.6-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.4.6-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.4.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 45f084d4259017cf6d734be58d7db2672743f2e3e87ec22c92937bed99f56393
MD5 ae8611da215a266fe099dca5a99df327
BLAKE2b-256 6fcfb05d4e14cb3dca8f69b4ee9613abc2757dbd0126b4e5b258b3a856071704

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.4.6-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.4.6-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.4.6-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4510e068174f4f19553b9dbbd875c6d29fc2076bbc5386f66aa4a18b38710c31
MD5 a314721ee601524eab075837f8f2e59a
BLAKE2b-256 f6a636d2ab2161f7ddd2fa4fb2970e765b7b8149ffe722f4ca35c205cf06ecdb

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.4.6-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.4.6-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.4.6-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c5505016cc4c880978b9ae29892bc1adfa15e3b908b2808d39ee41564945720c
MD5 af36cd35e803fbd792f65f1019f20a6a
BLAKE2b-256 47de085e9fcf13b8ca0c8f2b64fc95ea36004fb9fb107d6adfce78483bced1cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.4.6-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.4.6-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.4.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 06b65c45e13b7e51306589adad2cb6a735c1bd1c55f934bf5ba3a90118e3586c
MD5 0b11e295684d9e06116fcc769f2bb24e
BLAKE2b-256 042cb56c8ecc63738ed0e195adf0fd423f4b659164484002ca4ea55c892dff1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.4.6-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.4.6-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.4.6-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5c343cb84978e9e4366d8776f38baf18756b3a7738db2e9d2002aa9a1624e0bd
MD5 bb1b1b81fb11550a8ab6481ec0de8815
BLAKE2b-256 a08c6557192c8cc9fdae71a6da46dd349f19f608885b2ea504b8d5e5e5846b53

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.4.6-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.4.6-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lets_be_precise-0.4.6-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fe6744be039504e17ace38bae004c29de63edf46e43667245a28d918985a6180
MD5 5f97e5d449ec51fee23fcc7227067fb6
BLAKE2b-256 bcd7b85705e6177019fa95232b07b1d1c15f8cf86a4b3885004e4d289c680726

See more details on using hashes here.

Provenance

The following attestation bundles were made for lets_be_precise-0.4.6-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