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

Uploaded CPython 3.13Windows x86-64

lets_be_precise-0.4.7-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (476.6 kB view details)

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

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

Uploaded CPython 3.13macOS 11.0+ ARM64

lets_be_precise-0.4.7-cp312-cp312-win_amd64.whl (79.7 kB view details)

Uploaded CPython 3.12Windows x86-64

lets_be_precise-0.4.7-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.7-cp312-cp312-macosx_11_0_arm64.whl (97.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

lets_be_precise-0.4.7-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.7-cp311-cp311-macosx_11_0_arm64.whl (98.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

lets_be_precise-0.4.7-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (443.7 kB view details)

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

lets_be_precise-0.4.7-cp310-cp310-macosx_11_0_arm64.whl (98.1 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.7-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 32d0aa2c99d1fb939d61aac9ce067d32ccfb4a7248ee28eae87b58f451e9634c
MD5 5653dbef0155e0b5f99b504b37256ac6
BLAKE2b-256 d217bb99eda83a221f6511eb787b96c4f820361eeedef4c0aac6464217830dfa

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.7-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8e3629576560dec423b336e06231aca8a8a5c86af62d56f928e18f0c0b2316e5
MD5 3b98ca252fe5363d13139c8b0966fad4
BLAKE2b-256 c5bebe611a40cf3a874dcc607033a7e9aeed1dfca6a2d65ea107eb415c2e737f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.7-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3df422af0e36fec415a13ed3e505beb010e78335173d8aa95d4b73b23cca343d
MD5 cd177e89275f76fe6cbc108534054bda
BLAKE2b-256 019a8de33eaec2a726cd0719d079777001c71faeeddf1960b264ac908307029f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 af3590d00a12fc713b85a28da65ad65868f5ad1e06a8e64e8355a2185ea93118
MD5 33a14af773287819af7174970e41fa5b
BLAKE2b-256 0afcf63073f99f03a2e81f6f222ebdaed017cb73ae7844bdb50460eafc239252

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e51f367dbb9a20cee8915fad8ce68a1f2c25f17aafa15868768bed6247d45458
MD5 eaff86edda97756ac6c5a593666aa6c1
BLAKE2b-256 b4aa2a3546608274ee0b0ae025dfecefb9dce08af8aee36510ca96d481b23a54

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 469aa6e3b698cfb49112a6c1fddbd9ddd918a3fa515dfc711708d26a28e3f8e8
MD5 36b10f2452034210633c6d2ec39e36a3
BLAKE2b-256 e5d5539913346e4e32489b14c6679604443763946413f13ac58730e15a089438

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 265ed689e15f3ec026802c90d930cba9cc6567b97549cc8a920f2195b573f489
MD5 32444be79881337f8e04aad04b963645
BLAKE2b-256 8b10cc12924f2820c7e3a86b17bf25bbb3d81169279f737c546edcea561638c4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0dc7e8e092f01404bb3c03076453118db617ca13ce1463a4298cfe023b6669e2
MD5 1989801ed7bc99f46a3ba82b3bdb5b59
BLAKE2b-256 fbe59192d23b2b8ffcf61780201828c70dfb8730b384c35ee616de9268f2e95e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.7-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 acf5a84c477cd627b247c87519f4f2f452009479a93ba50297c65992b4901cd6
MD5 6d771e04d8780de9132c54992409fd96
BLAKE2b-256 fe86b86f098fec4a45de0f96efd791ce863d58d4795a8d0e906f5d5729a9d4ac

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 625c7e03665ee713c03f55b57c330983c1e74f5db9b71ee27c6994c36ee4c002
MD5 7ede899479a5796a395ed4af529787b5
BLAKE2b-256 122976298a0f34c8fa754fea14729b111194a56cb7dc323b8a0a594f1b259470

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.7-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b27638cd5e9ed95771238c1f4590ac350dbc619a7a1109f92b3d363feac67210
MD5 e21ba280ece2dc36a013376980db9d35
BLAKE2b-256 21ab5a37701d6132ff926088c0cc64517cf43fc6301d7fdc51da5572b093d723

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.4.7-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 97448c742e50945736691bbd131918677b819e6e6021900b3a987d2ae7b3fe6e
MD5 84d10f9ba72d85ddfa028fa8b8c88329
BLAKE2b-256 c1ff5510b78cffe9aeaed0e97fecd299a144930d9b5810ea19770d6384ffc2f1

See more details on using hashes here.

Provenance

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