Skip to content

Commit fe984d4

Browse files
committed
Black source
1 parent 5da19de commit fe984d4

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Moved `create_price_samples` from support.py to a new module price_array.py and renamed it to `create_price_array`
1111
- Commented a code snippet in engine.py where terminal stock prices are created using `create_price_samples`, to be removed in a next version
1212
- Allowed a dictionary as input for `create_price_array` in price_array.py
13+
- Allowed a dictionary as input for `get_pop` in support.py
1314

1415
## 1.3.5 (2024-12-28)
1516

optionlab/price_array.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from optionlab.models import BlackScholesModelInputs, LaplaceInputs
99

1010

11-
@lru_cache
1211
def create_price_array(
1312
inputs_data: BlackScholesModelInputs | LaplaceInputs | dict,
1413
n: int = 100_000,
@@ -19,7 +18,7 @@ def create_price_array(
1918
2019
Parameters
2120
----------
22-
inputs_data : BlackScholesModelInputs | LaplaceInputs
21+
inputs_data : BlackScholesModelInputs | LaplaceInputs | dict
2322
Input data used to generate the terminal stock prices. See the documentation
2423
for `BlackScholesModelInputs` and `LaplaceInputs` for more details.
2524
n : int, optional
@@ -32,37 +31,40 @@ def create_price_array(
3231
numpy.ndarray
3332
Array of terminal prices.
3433
"""
35-
34+
3635
if isinstance(inputs_data, dict):
3736
input_type = inputs_data["model"]
38-
elif isinstance(inputs_data, BlackScholesModelInputs):
39-
input_type = "black-scholes"
40-
elif isinstance(inputs_data, LaplaceInputs):
41-
input_type = "laplace"
37+
38+
if input_type in ("black-scholes", "normal"):
39+
inputs = BlackScholesModelInputs.model_validate(inputs_data)
40+
elif input_type == "laplace":
41+
inputs = LaplaceInputs.model_validate(inputs_data)
42+
else:
43+
raise ValueError("Inputs are not valid!")
4244
else:
43-
raise ValueError("Inputs are not valid!")
44-
45+
inputs = inputs_data
46+
47+
if isinstance(inputs, BlackScholesModelInputs):
48+
input_type = "black-scholes"
49+
elif isinstance(inputs, LaplaceInputs):
50+
input_type = "laplace"
51+
else:
52+
raise ValueError("Inputs are not valid!")
53+
4554
np_seed_number(seed)
4655

4756
if input_type in ("black-scholes", "normal"):
48-
arr = _get_array_price_from_BS(inputs_data, n)
57+
arr = _get_array_price_from_BS(inputs, n)
4958
elif input_type == "laplace":
50-
arr = _get_array_price_from_laplace(inputs_data, n)
59+
arr = _get_array_price_from_laplace(inputs, n)
5160

5261
np_seed_number(None)
5362

5463
return arr
5564

5665

57-
def _get_array_price_from_BS(
58-
inputs_data: BlackScholesModelInputs | dict, n: int
59-
) -> np.ndarray:
60-
inputs = (
61-
BlackScholesModelInputs.model_validate(inputs_data)
62-
if isinstance(inputs_data, dict)
63-
else inputs_data
64-
)
65-
66+
@lru_cache
67+
def _get_array_price_from_BS(inputs: BlackScholesModelInputs, n: int) -> np.ndarray:
6668
return exp(
6769
normal(
6870
(
@@ -80,15 +82,8 @@ def _get_array_price_from_BS(
8082
)
8183

8284

83-
def _get_array_price_from_laplace(
84-
inputs_data: LaplaceInputs | dict, n: int
85-
) -> np.ndarray:
86-
inputs = (
87-
LaplaceInputs.model_validate(inputs_data)
88-
if isinstance(inputs_data, dict)
89-
else inputs_data
90-
)
91-
85+
@lru_cache
86+
def _get_array_price_from_laplace(inputs: LaplaceInputs, n: int) -> np.ndarray:
9287
return exp(
9388
laplace(
9489
(log(inputs.stock_price) + inputs.mu * inputs.years_to_target_date),

optionlab/support.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def get_profit_range(
215215
216216
Returns
217217
-------
218-
list
218+
list[Range]
219219
List of stock price pairs.
220220
"""
221221

@@ -279,10 +279,10 @@ def get_pop(
279279

280280
if len(profit_ranges) == 0:
281281
return pop
282-
282+
283283
if isinstance(inputs_data, dict):
284284
input_type = inputs_data["model"]
285-
285+
286286
if input_type in ("black-scholes", "normal"):
287287
inputs = BlackScholesModelInputs.model_validate(inputs_data)
288288
elif input_type == "laplace":
@@ -293,7 +293,7 @@ def get_pop(
293293
raise ValueError("Inputs are not valid!")
294294
else:
295295
inputs = inputs_data
296-
296+
297297
if isinstance(inputs, BlackScholesModelInputs):
298298
input_type = "black-scholes"
299299
elif isinstance(inputs, LaplaceInputs):

tests/test_misc.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
import pytest
55

6-
from optionlab.models import BlackScholesModelInputs, LaplaceInputs
7-
from optionlab.price_array import create_price_array
6+
from optionlab.models import BlackScholesModelInputs
7+
from optionlab.price_array import create_price_array, _get_array_price_from_BS
88
from optionlab.utils import get_nonbusiness_days
99

1010

@@ -55,7 +55,7 @@ def test_benchmark_holidays(benchmark):
5555

5656

5757
def test_cache_price_samples():
58-
create_price_array.cache_clear()
58+
_get_array_price_from_BS.cache_clear()
5959

6060
stock_price = 168.99
6161
volatility = 0.483
@@ -97,12 +97,13 @@ def test_cache_price_samples():
9797
stock_price = 167.0
9898

9999
sample3 = create_price_array(
100-
inputs_data=BlackScholesModelInputs(
101-
stock_price=stock_price,
102-
volatility=volatility,
103-
interest_rate=interest_rate,
104-
years_to_target_date=years_to_target,
105-
),
100+
inputs_data={
101+
"model": "normal",
102+
"stock_price": stock_price,
103+
"volatility": volatility,
104+
"interest_rate": interest_rate,
105+
"years_to_target_date": years_to_target,
106+
},
106107
seed=0,
107108
)
108109

@@ -113,9 +114,13 @@ def test_cache_price_samples():
113114
assert sample3.sum() == pytest.approx(16752035.781465728, rel=0.01)
114115

115116
sample4 = create_price_array(
116-
inputs_data=LaplaceInputs(
117-
stock_price=168.99, volatility=0.483, mu=0.05, years_to_target_date=24 / 365
118-
),
117+
inputs_data={
118+
"model": "laplace",
119+
"stock_price": 168.99,
120+
"volatility": 0.483,
121+
"mu": 0.05,
122+
"years_to_target_date": 24 / 365,
123+
},
119124
seed=0,
120125
)
121126

0 commit comments

Comments
 (0)