pybaw 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pybaw-1.0.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Martial Ren
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ include pybaw/*.pyx
pybaw-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,78 @@
1
+ Metadata-Version: 2.4
2
+ Name: pybaw
3
+ Version: 1.0.0
4
+ Summary: Barone-Adesi-Whaley (1987) American option pricing and implied volatility (Cython)
5
+ Author: Martial Ren
6
+ License-Expression: MIT
7
+ Requires-Python: >=3.11
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Requires-Dist: numpy
11
+ Requires-Dist: scipy
12
+ Provides-Extra: test
13
+ Requires-Dist: pytest; extra == "test"
14
+ Dynamic: license-file
15
+
16
+ # pybaw
17
+
18
+ Barone-Adesi–Whaley (1987) American option pricing and implied volatility as a Cython
19
+ extension: `nogil` scalar loops with scipy's Cython `brentq` doing the root solves.
20
+
21
+ ## Conventions
22
+
23
+ - `sigma` is the Black **total** vol (total standard deviation, `sigma_annual * sqrt(T)`).
24
+ - The pricer works in `(D, F, S, sigma)` terms — `rT = -ln(D)`, `bT = ln(F/S)` — so no
25
+ explicit `T` appears (T=1 convention).
26
+ - Dividends never appear explicitly: the cost of carry is implied via `carry = F/S`.
27
+ - `baw_implied_vol` takes the **raw premium** (not undiscounted); its intrinsic is the
28
+ American `max(S-K, 0)` / `max(K-S, 0)`. Sentinels: `0.0` at/below intrinsic, `NaN` on
29
+ failure.
30
+
31
+ ## Install
32
+
33
+ ```
34
+ pip install pybaw
35
+ ```
36
+
37
+ Prebuilt wheels cover CPython 3.11+ on Linux (x86_64, aarch64), Windows (x86_64),
38
+ and macOS (arm64, x86_64). On anything else pip falls back to the sdist, which
39
+ needs a C compiler — the compiled extension is required, there is no pure-Python
40
+ fallback. Note scipy is needed at **build** time too, not just runtime: the
41
+ extension cimports `scipy.optimize.cython_optimize`.
42
+
43
+ To work on the package itself, either install from a checkout:
44
+
45
+ ```
46
+ pip install .
47
+ ```
48
+
49
+ or build in place and put the repo root on `PYTHONPATH`:
50
+
51
+ ```
52
+ python setup.py build_ext --inplace
53
+ ```
54
+
55
+ ## API
56
+
57
+ | Function | Purpose |
58
+ |---|---|
59
+ | `baw_price(S, K, D, F, sigma, flag)` | American price; `flag` `'c'`/`'p'` |
60
+ | `baw_implied_vol(price, S, K, D, F, flag)` | invert a raw premium to total vol |
61
+ | `vectorized_baw_implied_vol(prices, S[], K[], D[], F[], flags[])` | batch inversion |
62
+ | `find_forward_baw(calls, puts, strikes, spot, discount)` | forward where call IV ≈ put IV near ATM |
63
+ | `bsm_price(K, D, F, sigma, flag)` | pure European reference (used by tests/bounds) |
64
+
65
+ `find_forward_baw` exists because European put-call parity is biased for American
66
+ options by the early-exercise premium; solving for the forward that equates call and
67
+ put implied vols near the money removes that bias.
68
+
69
+ ## Tests
70
+
71
+ ```
72
+ pip install -e .[test]
73
+ pytest
74
+ ```
75
+
76
+ ## License
77
+
78
+ MIT — see [LICENSE](LICENSE).
pybaw-1.0.0/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # pybaw
2
+
3
+ Barone-Adesi–Whaley (1987) American option pricing and implied volatility as a Cython
4
+ extension: `nogil` scalar loops with scipy's Cython `brentq` doing the root solves.
5
+
6
+ ## Conventions
7
+
8
+ - `sigma` is the Black **total** vol (total standard deviation, `sigma_annual * sqrt(T)`).
9
+ - The pricer works in `(D, F, S, sigma)` terms — `rT = -ln(D)`, `bT = ln(F/S)` — so no
10
+ explicit `T` appears (T=1 convention).
11
+ - Dividends never appear explicitly: the cost of carry is implied via `carry = F/S`.
12
+ - `baw_implied_vol` takes the **raw premium** (not undiscounted); its intrinsic is the
13
+ American `max(S-K, 0)` / `max(K-S, 0)`. Sentinels: `0.0` at/below intrinsic, `NaN` on
14
+ failure.
15
+
16
+ ## Install
17
+
18
+ ```
19
+ pip install pybaw
20
+ ```
21
+
22
+ Prebuilt wheels cover CPython 3.11+ on Linux (x86_64, aarch64), Windows (x86_64),
23
+ and macOS (arm64, x86_64). On anything else pip falls back to the sdist, which
24
+ needs a C compiler — the compiled extension is required, there is no pure-Python
25
+ fallback. Note scipy is needed at **build** time too, not just runtime: the
26
+ extension cimports `scipy.optimize.cython_optimize`.
27
+
28
+ To work on the package itself, either install from a checkout:
29
+
30
+ ```
31
+ pip install .
32
+ ```
33
+
34
+ or build in place and put the repo root on `PYTHONPATH`:
35
+
36
+ ```
37
+ python setup.py build_ext --inplace
38
+ ```
39
+
40
+ ## API
41
+
42
+ | Function | Purpose |
43
+ |---|---|
44
+ | `baw_price(S, K, D, F, sigma, flag)` | American price; `flag` `'c'`/`'p'` |
45
+ | `baw_implied_vol(price, S, K, D, F, flag)` | invert a raw premium to total vol |
46
+ | `vectorized_baw_implied_vol(prices, S[], K[], D[], F[], flags[])` | batch inversion |
47
+ | `find_forward_baw(calls, puts, strikes, spot, discount)` | forward where call IV ≈ put IV near ATM |
48
+ | `bsm_price(K, D, F, sigma, flag)` | pure European reference (used by tests/bounds) |
49
+
50
+ `find_forward_baw` exists because European put-call parity is biased for American
51
+ options by the early-exercise premium; solving for the forward that equates call and
52
+ put implied vols near the money removes that bias.
53
+
54
+ ## Tests
55
+
56
+ ```
57
+ pip install -e .[test]
58
+ pytest
59
+ ```
60
+
61
+ ## License
62
+
63
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,17 @@
1
+ """pybaw: Barone-Adesi-Whaley American option pricing and implied vol (Cython)."""
2
+
3
+ from .baw import (
4
+ baw_implied_vol,
5
+ baw_price,
6
+ bsm_price,
7
+ find_forward_baw,
8
+ vectorized_baw_implied_vol,
9
+ )
10
+
11
+ __all__ = [
12
+ "baw_implied_vol",
13
+ "baw_price",
14
+ "bsm_price",
15
+ "find_forward_baw",
16
+ "vectorized_baw_implied_vol",
17
+ ]
@@ -0,0 +1,74 @@
1
+ """Barone-Adesi-Whaley (1987) American option pricing + implied vol.
2
+
3
+ The hot functions (`baw_price`, `baw_implied_vol`, `vectorized_baw_implied_vol`,
4
+ `find_forward_baw`) come from the compiled `baw_core` extension — a nogil scalar loop
5
+ with scipy's Cython brentq inside. The extension is required; build it with
6
+ `python setup.py build_ext --inplace` or `pip install .`.
7
+
8
+ Conventions: `sigma` is the Black *total* vol (total standard deviation,
9
+ = sigma_annual * sqrt(T)). The pricer works entirely in (D, F, S, sigma) terms —
10
+ rT = -ln(D), bT = ln(F/S) — so no explicit T is needed (T=1 convention).
11
+
12
+ Dividends never appear: the cost-of-carry b is implied from the spot and forward via
13
+ carry = F/S = e^{bT}. Feeding a measured spot is what keeps this dividend-free.
14
+
15
+ `baw_implied_vol` takes the RAW option premium (not the undiscounted price a European
16
+ solver takes): its intrinsic is the American max(S-K, 0) / max(K-S, 0). Sentinels:
17
+ 0.0 at/below intrinsic, NaN on failure / outside no-arbitrage bounds.
18
+ """
19
+
20
+ import math
21
+
22
+ import numpy as np
23
+
24
+ from .baw_core import (
25
+ baw_implied_vol,
26
+ baw_price,
27
+ vectorized_baw_implied_vol,
28
+ )
29
+ from .baw_core import find_forward_baw as _core_find_forward_baw
30
+
31
+ __all__ = [
32
+ "baw_implied_vol",
33
+ "baw_price",
34
+ "bsm_price",
35
+ "find_forward_baw",
36
+ "vectorized_baw_implied_vol",
37
+ ]
38
+
39
+ _SQRT2 = math.sqrt(2.0)
40
+
41
+
42
+ def _norm_cdf(x: float) -> float:
43
+ return 0.5 * (1.0 + math.erf(x / _SQRT2))
44
+
45
+
46
+ def bsm_price(strike: float, discount: float, forward: float, sigma: float, flag: str) -> float:
47
+ """European (Black) price, kept as a pure reference for tests and premium bounds.
48
+
49
+ sigma = total std dev; flag 'c'/'p'.
50
+ """
51
+ if sigma <= 0:
52
+ if flag == "c":
53
+ return max(discount * (forward - strike), 0.0)
54
+ return max(discount * (strike - forward), 0.0)
55
+ d1 = math.log(forward / strike) / sigma + 0.5 * sigma
56
+ d2 = d1 - sigma
57
+ if flag == "c":
58
+ return discount * (forward * _norm_cdf(d1) - strike * _norm_cdf(d2))
59
+ return discount * (strike * _norm_cdf(-d2) - forward * _norm_cdf(-d1))
60
+
61
+
62
+ def find_forward_baw(
63
+ call_prices: np.ndarray, put_prices: np.ndarray, strikes: np.ndarray,
64
+ spot: float, discount: float, strike_pct: float = 0.01,
65
+ ) -> float:
66
+ """Find the forward F where the BAW call IV ≈ put IV across near-ATM strikes.
67
+
68
+ European put-call parity (F = K + (C−P)/D) is biased for American options by the
69
+ early-exercise premium, so instead solve for the F that makes the American call and
70
+ put imply the SAME vol near the money — the BAW-consistent forward, using the
71
+ measured spot. call_prices / put_prices are mid premiums, aligned with strikes.
72
+ """
73
+ # the core names these params S/D positionally; bridge the keyword names here
74
+ return float(_core_find_forward_baw(call_prices, put_prices, strikes, spot, discount, strike_pct))