-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblackscholes.py
76 lines (52 loc) · 1.99 KB
/
blackscholes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
# BLACK-SCHOLES FRAMEWORK
# ------------------------------------------------------------------------------
# IMPORTS
import numpy as np
from scipy.stats import norm
# ------------------------------------------------------------------------------
# DEFINITIONS
def pricer(flag, spot_price, strike, time_to_maturity, vol, risk_free_rate):
"""
Computes the Black-Scholes price of a European option with possibly
vectorized inputs.
:param flag: either "c" for calls or "p" for puts
:param spot_price: spot price of the underlying
:param strike: strike price
:param time_to_maturity: time to maturity in years
:param vol: annualized volatility assumed constant until expiration
:param risk_free_rate: risk-free interest rate
:type flag: string
:type spot_price: float / numpy array
:type strike: float / numpy array
:type time_to_maturity: float / numpy array
:type vol: float / numpy array
:type risk_free_rate: float
:return: Black-Scholes price
:rtype: float / numpy array
# Example taking vectors as inputs
>>> spot = np.array([3.9,4.1])
>>> strike = 4
>>> time_to_maturity = 1
>>> vol = np.array([0.1, 0.2])
>>> rate = 0
>>> p = BlackScholes.pricer('c', spot, strike, time_to_maturity, vol, rate)
>>> expected_price = np.array([0.1125, 0.3751])
>>> abs(expected_price - p) < 0.0001
array([ True, True], dtype=bool)
"""
# Rename variables for convenience.
S = spot_price
K = strike
T = time_to_maturity
r = risk_free_rate
# Compute option price.
d1 = 1/(vol * np.sqrt(T)) * (np.log(S) - np.log(K) + (r+0.5 * vol**2) * T)
d2 = d1 - vol * np.sqrt(T)
if flag == 'c':
price = S * norm.cdf(d1) - K * np.exp(-r * T) * norm.cdf(d2)
elif flag == 'p':
price = K * np.exp(-r * T) * norm.cdf(-d2) - S * norm.cdf(-d1)
return price