Skip to content

Commit 04554f3

Browse files
authored
Merge pull request #12 from PyFE/dev-0.1.4
Wrapped around np.array()
2 parents 86890ad + f5969b1 commit 04554f3

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ array([15.71361973, 9.69250803, 5.52948546, 2.94558338, 1.48139131])
4040
```python
4141
sigma = np.array([[0.2], [0.5]])
4242
m = pf.Bsm(sigma, intr=0.05, divr=0.1) # sigma in axis=0
43-
m.price(strike=np.array([90, 95, 100]), spot=100, texp=1.2, cp=np.array([-1,1,1]))
43+
m.price(strike=[90, 95, 100], spot=100, texp=1.2, cp=[-1,1,1])
4444
```
4545
`Out [2]:`
4646
```

pyfeng/bsm.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ def price_formula(strike, spot, sigma, texp, cp=1, intr=0.0, divr=0.0, is_fwd=Fa
4646
Vanilla option price
4747
"""
4848
disc_fac = np.exp(-texp * intr)
49-
fwd = spot * (1.0 if is_fwd else np.exp(-texp * divr) / disc_fac)
49+
fwd = np.array(spot) * (1.0 if is_fwd else np.exp(-texp * divr) / disc_fac)
5050

51-
sigma_std = np.maximum(sigma * np.sqrt(texp), np.finfo(float).eps)
51+
sigma_std = np.maximum(np.array(sigma) * np.sqrt(texp), np.finfo(float).eps)
5252

5353
# don't directly compute d1 just in case sigma_std is infty
5454
d1 = np.log(fwd / strike) / sigma_std
5555
d2 = d1 - 0.5*sigma_std
5656
d1 += 0.5*sigma_std
5757

58+
cp = np.array(cp)
5859
price = fwd * spst.norm.cdf(cp * d1) - strike * spst.norm.cdf(cp * d2)
5960
price *= cp * disc_fac
6061
return price

pyfeng/norm.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ def price_formula(strike, spot, sigma, texp, cp=1, intr=0.0, divr=0.0, is_fwd=Fa
6363
Returns:
6464
Vanilla option price
6565
"""
66+
df = np.exp(-texp * intr)
67+
fwd = np.array(spot) * (1.0 if is_fwd else np.exp(-texp * divr) / df)
6668

67-
disc_fac = np.exp(-texp * intr)
68-
fwd = spot * (1.0 if is_fwd else np.exp(-texp * divr) / disc_fac)
69-
70-
sigma_std = np.maximum(sigma * np.sqrt(texp), np.finfo(float).eps)
69+
sigma_std = np.maximum(np.array(sigma) * np.sqrt(texp), np.finfo(float).eps)
7170
d = (fwd - strike) / sigma_std
7271

73-
price = disc_fac * (cp*(fwd - strike)*spst.norm.cdf(cp * d) + sigma_std * spst.norm.pdf(d))
72+
cp = np.array(cp)
73+
price = df * (cp*(fwd - strike)*spst.norm.cdf(cp * d) + sigma_std * spst.norm.pdf(d))
7474
return price
7575

7676
def _impvol_Choi2009(self, price, strike, spot, texp, cp=1, setval=False):
@@ -94,8 +94,8 @@ def _impvol_Choi2009(self, price, strike, spot, texp, cp=1, setval=False):
9494
implied volatility
9595
"""
9696
fwd, df, _ = self._fwd_factor(spot, texp)
97-
price_fwd = price / df
98-
strike_std = cp*(fwd - strike)
97+
price_fwd = np.array(price) / df
98+
strike_std = np.array(cp)*(fwd - strike)
9999

100100
time_val = price_fwd - np.maximum(0, strike_std) # option time value
101101
strd = 2*price_fwd - strike_std # straddle value (=call + put)

0 commit comments

Comments
 (0)