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

Uploaded CPython 3.13Windows x86-64

lets_be_precise-0.4.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (473.0 kB view details)

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

lets_be_precise-0.4.3-cp313-cp313-macosx_11_0_arm64.whl (95.9 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

lets_be_precise-0.4.3-cp312-cp312-win_amd64.whl (79.1 kB view details)

Uploaded CPython 3.12Windows x86-64

lets_be_precise-0.4.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (469.8 kB view details)

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

lets_be_precise-0.4.3-cp312-cp312-macosx_11_0_arm64.whl (95.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

lets_be_precise-0.4.3-cp311-cp311-win_amd64.whl (78.0 kB view details)

Uploaded CPython 3.11Windows x86-64

lets_be_precise-0.4.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (453.3 kB view details)

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

lets_be_precise-0.4.3-cp311-cp311-macosx_11_0_arm64.whl (95.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

lets_be_precise-0.4.3-cp310-cp310-win_amd64.whl (77.8 kB view details)

Uploaded CPython 3.10Windows x86-64

lets_be_precise-0.4.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (439.3 kB view details)

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

lets_be_precise-0.4.3-cp310-cp310-macosx_11_0_arm64.whl (96.0 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 2080b4c70efa150e66b04a4fe5972c47f86696ef6651be4f1431daa46163a1de
MD5 0f161ae7caeffd30019de86d55737228
BLAKE2b-256 d284d372f9d0a1773c06e6fe0ac7eb0ce46a63c79f10be05785e7954f818a10f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ac2c93a602827ecb81e49c9925ac56cca8d258098c0077c1cdbf6e1d169e54cf
MD5 536588b65eda3dc5fb4e75dc4c66ec55
BLAKE2b-256 5fd5244e6dfe76df7f80c86a75caaf9eba07b055f8da82ae4e452c97dcbdc5b1

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f743b2a24c5506a3dacbb689824042f0b560a1c195102c23b58e223d84878562
MD5 c58174876f9514df7f55058780eaf3fa
BLAKE2b-256 86025ee6551cbcaa863f5b1d713bc1abb4f8f7f0e39a461a41068fac27dbd9f6

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0dc477e5c14d49676e825c478aafad5256ded4d42bbe40fca188bf754548eba1
MD5 6524da2abf866553d32d66248bdceaa9
BLAKE2b-256 d57179bbfb662c39ebe950724f6f100449560b7c316b0d2ef69af018e3326758

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d4e2e1623cf042694c74b70732fe351820f3b2a5df8efd5f422ad15057a98442
MD5 134ccf97eaef4648a409aa73b0c13219
BLAKE2b-256 ab5c76b53a7ffdf54e9ed650d1c3e001d5a2d9a5a8a40ae7f6e5e767e3204f6d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f05cbea8675c1a2607ef145d594bc65e0fcde179bcf4d094c401b011f8d33b83
MD5 03293dac7e04bc0b7a8af3b68b77cca5
BLAKE2b-256 f742b489e7d7918ff9b7a62dbed98cce076527d8cf162546f432aa515fa13518

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e341bdb4b6de9361d45bec14860bb2e08c84dfe58857fe3eeacb8e55134ccdae
MD5 649816e977e1c188d1dd48612908e869
BLAKE2b-256 40723edf484f49dcb28c4bb99f7b8f1e741805cf41a43846953aff125d6661d0

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e39ffb9a4fed69f29825292aa8c1217dea57ee33585903dfa979972e4abd5cfb
MD5 2c96a5e0bf2bc4f56328e84425d6f142
BLAKE2b-256 b9f5801b0100b60ae97c9d46f76a6180ee57e06557f7e55ee703d7de739801db

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a4b03c5c7b23b337203f440a4f6d547db62dfdcf2709a1e0b2490974ba131d2f
MD5 1f02a4877eb1c6eb67f1baeee32efe7c
BLAKE2b-256 01ff84daf1c3cd6a3cc7036acf73d683a638fc19927ad42bf7b46e9ff9ee3cd3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3ae66105dfe13411f2bfba7c2e0b2ec36ee9ba9b235f6abc2304c9865b93360d
MD5 2976a83a5863484fa6286d3048d9aa64
BLAKE2b-256 c03e1372e05af33b59746cf13b68266624a4592c0fe95268b39483303268fb7e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9a862afc55ac55c467a8c1ad07b5a9362c68291f558f205719ba4e3db439377d
MD5 3cd3d64eeb1f3e4e08c45fc5ab5ed541
BLAKE2b-256 b6582a720ba344b8ac60695eb078a7a3636805aa3f98356b6f08416a1227703b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 19a10048920ceb799868771ec5009abdb78be4ca0cc559e29b1f78d756855bcd
MD5 be8b8a2c62c0c54a45a8e91869eb419d
BLAKE2b-256 bbbb57a047cb9fc2ca112362ce098e0f95692021763730e9b9936c2b9c79cbd2

See more details on using hashes here.

Provenance

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