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

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

Uploaded CPython 3.13Windows x86-64

lets_be_precise-0.2.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (410.1 kB view details)

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

lets_be_precise-0.2.0-cp313-cp313-macosx_11_0_arm64.whl (79.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

lets_be_precise-0.2.0-cp312-cp312-win_amd64.whl (71.7 kB view details)

Uploaded CPython 3.12Windows x86-64

lets_be_precise-0.2.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (407.2 kB view details)

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

lets_be_precise-0.2.0-cp312-cp312-macosx_11_0_arm64.whl (79.5 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

lets_be_precise-0.2.0-cp311-cp311-win_amd64.whl (70.5 kB view details)

Uploaded CPython 3.11Windows x86-64

lets_be_precise-0.2.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (391.2 kB view details)

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

lets_be_precise-0.2.0-cp311-cp311-macosx_11_0_arm64.whl (79.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

lets_be_precise-0.2.0-cp310-cp310-win_amd64.whl (70.3 kB view details)

Uploaded CPython 3.10Windows x86-64

lets_be_precise-0.2.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (377.6 kB view details)

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

lets_be_precise-0.2.0-cp310-cp310-macosx_11_0_arm64.whl (79.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

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

File metadata

File hashes

Hashes for lets_be_precise-0.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 05a03bee3f89a816d9b2dd9f5bcb7f90cfccc698b774e5e55d962c71006146c7
MD5 8a7dc476c3ed56946646ea33e78b3269
BLAKE2b-256 a6cc0cfbdca536de2d08a37e3e0c1787a242303533f167bd636078ebfed6fa02

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.2.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b566046cb1cef5e8a408c8fec33fd9a972cbad14f75650448bc4044776c61235
MD5 5cb3bf54eda2a171fb9d88954c1c745d
BLAKE2b-256 e133e7574febd4313a744b6abd1ea257b2d98c3ba6faddeaa150cc79d93714d2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 312f93bf1184f7acc102dd3c6bf2acbc4f1c0cf20ccb496f0442f3ffce7e7447
MD5 4b8021f04353db3afcc0feb008c57548
BLAKE2b-256 d41de03f6314f09c9f96bf729747ab54a8ac67077a56ea9c77e25efa582c9ed8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0960ca02b712a4e5fcfeb665d9999113a54bab0b4cb13f74c3a2eafb721071f4
MD5 b0a35992dcd7142998069cbe5006a186
BLAKE2b-256 1fee631f2910532acca22a20efb57f4f16157504b202e5bd5d6ea6c21b7c1526

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.2.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 37c58fa2fb3e7fc3fa5493df1c528d12d0ec3a6b4219c2429a7ec6cb4678f781
MD5 e86c410516d9e7c5e8b54347ec3ed807
BLAKE2b-256 1018a960d4b0c993cf4d2fda97ff92b9c1a1a08ff5f5fc39376007bf871e91c9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7db0d5b5ae61d1de2f4da2b2d959fdbd9709842ffb838a83d0097eb5d75cac89
MD5 2e0abb0d39c9999545bc18fff20e0115
BLAKE2b-256 10a9e86e39db319d7f0ca3a2ad5d8227774a69603dc156706cfa7fd4f2a7b21c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 721279326deda14efc635e2ad76cd6474869d5479f12bf050bd149219b791a40
MD5 45861cd42b36fdb6979b9d900bb96455
BLAKE2b-256 9c9e9f1c3ef9033a96244e85f378be9a5372a2a3ab5c126061794c37234f0998

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.2.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8ad3f84a69f522062f19b12e593185772423ab84571d721773395d6f82e6b2b2
MD5 2ad8ae59b7c564b8c5ab05a0f75acaf7
BLAKE2b-256 7593aad16f728696ec90e8c6e8a09166bee611b2589e85ac7ec5c38fd1119d6d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.2.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 654c9afdddb2a56c23dd0f435a51f65092181974db0318b611fa304769cc001f
MD5 2fb93595758b14ec8a2229cbf4b0a955
BLAKE2b-256 a78bb0b4e52764c9d0d42bf62c0992e65b02ffb3de73c4a04ca388ccae6e9bc9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.2.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 fac4cf7b772d741dade23c1f25eac05829e1485856e21b136f5f980ee89ae3ca
MD5 a04831520d4da643db73ee6d69f83e55
BLAKE2b-256 e1e6ba8d8322dd1dd491effa01128483d32f069c4d1a303a4ffd4c34d94dde57

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.2.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7a36aa0caa0d09df4d1cc974a4fe278a00289291f7923f741198889b5c0a4747
MD5 d25440abf98f76e0e3aeae37f4b49027
BLAKE2b-256 1250f9a27e3ea1ddbba99c9ee9174c1e088516824c80bf63dc713f4ed41512bf

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for lets_be_precise-0.2.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 40c981b566c3d51da5123030a881e51daa572de68dc3eadd83d7f03e36d867db
MD5 85d85d49a5e3449ee42aaa8dc8eb6045
BLAKE2b-256 54590fa2149b0dd13c303df27fe0fbf5a05a72b3ff30d65c31607840ee4f2fca

See more details on using hashes here.

Provenance

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