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

Uploaded CPython 3.13Windows x86-64

lets_be_precise-0.4.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (476.8 kB view details)

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

lets_be_precise-0.4.1-cp313-cp313-macosx_11_0_arm64.whl (97.9 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

lets_be_precise-0.4.1-cp312-cp312-win_amd64.whl (80.4 kB view details)

Uploaded CPython 3.12Windows x86-64

lets_be_precise-0.4.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (473.7 kB view details)

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

lets_be_precise-0.4.1-cp312-cp312-macosx_11_0_arm64.whl (98.0 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

lets_be_precise-0.4.1-cp311-cp311-win_amd64.whl (79.3 kB view details)

Uploaded CPython 3.11Windows x86-64

lets_be_precise-0.4.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (456.4 kB view details)

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

lets_be_precise-0.4.1-cp311-cp311-macosx_11_0_arm64.whl (97.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

lets_be_precise-0.4.1-cp310-cp310-win_amd64.whl (79.1 kB view details)

Uploaded CPython 3.10Windows x86-64

lets_be_precise-0.4.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (442.9 kB view details)

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

lets_be_precise-0.4.1-cp310-cp310-macosx_11_0_arm64.whl (98.0 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b2b9e717dd9feaea875d74b0ff9fabc1844d8a2b1828b9dba67d6e188dec911a
MD5 e738c1bdbcd618c73384acc1e83a93d9
BLAKE2b-256 0575753f4f15c3655342294e5c5a9714a27c852a051b733be61780619dd10289

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 309b2e9a655eecea1aec3759e12ccc1a0a87d4628a60f82e12106b3bc879d2f9
MD5 7c4447bf25c8556072a8a41cdd3de647
BLAKE2b-256 719915e60905d0a7403747a006f23e44c532fd909e43dd52e349fc4b34f87478

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 52cba550b77f830c1e7014f0390fcf196a9145b33d132f27863929c455690a2a
MD5 ffa5fb87b2bdfd0668aaef6c0381f453
BLAKE2b-256 5c5f223f632cde1fa39fdd99f1e68bbe867fceb06e0148523d8480f72635a18e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 3d05a34c8ef2d166854facdc8580b41a78eb443abcea2c78ccfbe93f33a12433
MD5 6d0171b046eae231f1ee70d54b631dcd
BLAKE2b-256 a35bb0009e5210bc518329689589284c2e19e6d1178ac4094c8263f4cc4254ec

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3c0b7c16a8593dc69a1838b4b80dda5e3e02ac2cfc5a1938be7e5880a4c4cebe
MD5 2c41550ea149710379446a186043ba83
BLAKE2b-256 8bb3812d1482dde993c1d450e35206113519fbcc881e5344504c623849c05fcd

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 61d85c4e41e00eafa3364384b178f00c31214296b46f51097cc477623bd6b86e
MD5 616f08584cf24029adeaa9fabee266c6
BLAKE2b-256 2a64928bf917c46cbe2a43a4434061aa6b26c3bba993110e48dcf7cfdf879bd4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3a09d8261bf468b55432426ce607de3ec4eb9d98a6e4e39af8721c869357ccb7
MD5 3adc3332564258bb8b40321a368745bf
BLAKE2b-256 14c993b73ee32f17cfa13c0447430c14d0145cd4ca609431eb914c9dab9c927e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a53b8185bdcb20acafc7d0151e3e161384df295643c7047826ec50cd440c1f2f
MD5 c3c9afa246168a01d68c536f64f388dc
BLAKE2b-256 345725d9dcd9f0fcb93ca7c721c1ceb99eed0451f0d6300189810261966a9975

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 043c2e9d28e57ebe218eed098aa71509c430dea85c701d87958fa9fae7f13d06
MD5 32fa5171f9bd3a194adc681d161e873d
BLAKE2b-256 3de465cc072c0f9aa3eda7c78d775e33c127223d5466213d59609c2bc5fbbcad

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 fb73ff4d429e0509275485dfe81b6c1f2bb930806d05c7fd30d83f8d86bb627c
MD5 bbaa37540d9316eb67691cd950477df7
BLAKE2b-256 040ba9c53355672af2da124262795218dadef4e28800155cf898e647d360cb47

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3be8ef57867e9e12ffc597d38c4effac124cbdca047e050f491bb6a6dfb48c38
MD5 0d1944062de25e0d4dbe3a4934b02184
BLAKE2b-256 30226077f73fdc9b1917e7f609c6b7220da956e5bd47778e4ea4fda42214984c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cfc58dcd513ef8bd01239371025293ecaa818e8b1e3459c7761e370f0dd09faf
MD5 1ee64777c75577fe06dc131e92ce8bf4
BLAKE2b-256 deb2d831c02449de7f8560261d0fe37fa6b96f7f7b117d19365a1a8edd39fc2b

See more details on using hashes here.

Provenance

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