Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/finance/option.pyx
8815 views
1
"""
2
Tools for working with financial options
3
4
AUTHORS:
5
- Brian Manion, 2013: initial version
6
"""
7
8
cdef extern from "math.h":
9
double erf(double)
10
double exp(double)
11
double log(double)
12
double sqrt(double)
13
14
def black_scholes(double spot_price, double strike_price, double time_to_maturity, double risk_free_rate, double vol, opt_type):
15
r"""
16
Calculates call/put price of European style options
17
using Black-Scholes formula. See [S]_ for one of many
18
standard references for this formula.
19
20
INPUT:
21
22
- ``spot_price`` -- The current underlying asset price
23
- ``strike_price`` -- The strike of the option
24
- ``time_to_maturity`` -- The # of years until expiration
25
- ``risk_free_rate`` -- The risk-free interest-rate
26
- ``vol`` -- The volatility
27
- ``opt_type`` -- string; The type of option, either ``'put'``
28
for put option or ``'call'`` for call option
29
30
OUTPUT:
31
32
The price of an option with the given parameters.
33
34
EXAMPLES::
35
36
sage: finance.black_scholes(42, 40, 0.5, 0.1, 0.2, 'call') # abs tol 1e-10
37
4.759422392871532
38
sage: finance.black_scholes(42, 40, 0.5, 0.1, 0.2, 'put') # abs tol 1e-10
39
0.8085993729000958
40
sage: finance.black_scholes(100, 95, 0.25, 0.1, 0.5, 'call') # abs tol 1e-10
41
13.695272738608132
42
sage: finance.black_scholes(100, 95, 0.25, 0.1, 0.5, 'put') # abs tol 1e-10
43
6.349714381299734
44
sage: finance.black_scholes(527.07, 520, 0.424563772, 0.0236734,0.15297,'whichever makes me more money')
45
Traceback (most recent call last):
46
...
47
ValueError: 'whichever makes me more money' is not a valid string
48
49
REFERENCES:
50
51
.. [S] Shreve, S. Stochastic Calculus for Finance II: Continuous-Time
52
Models. New York: Springer, 2004
53
"""
54
#First we calculate d1, d1=d11*d12, d12=d121+d122*d123
55
cdef double d11 = 1/(vol*sqrt(time_to_maturity))
56
57
cdef double d121 = log(spot_price/strike_price)
58
cdef double d122 = risk_free_rate + pow(vol,2)/2
59
cdef double d123 = time_to_maturity
60
cdef double d12 = d121+d122*d123
61
62
cdef double d1 = d11*d12
63
cdef double d2 = d1 - vol*sqrt(time_to_maturity)
64
65
cdef double call_value = _std_norm_cdf(d1)*spot_price - _std_norm_cdf(d2)*strike_price*exp(-1*risk_free_rate*time_to_maturity)
66
cdef double put_value = _std_norm_cdf(-1*d2)*strike_price*exp(-1*risk_free_rate*time_to_maturity) - _std_norm_cdf(-1*d1)*spot_price
67
68
if opt_type == 'call':
69
return call_value
70
elif opt_type == 'put':
71
return put_value
72
else:
73
raise ValueError("'%s' is not a valid string" % opt_type)
74
75
def _std_norm_cdf(double x):
76
r"""
77
Standard normal cumulative distribution function; Used internally.
78
79
INPUT:
80
81
- `x` -- The upper limit of integration for the standard normal cdf
82
83
OUTPUT:
84
85
The probability that a random variable X, which is standard normally
86
distributed, takes on a value less than or equal to x.
87
88
EXAMPLES::
89
90
sage: from sage.finance.option import _std_norm_cdf
91
sage: _std_norm_cdf(0) # abs tol 1e-10
92
0.5
93
sage: x = _std_norm_cdf(1.96); x # abs tol 1e-10
94
0.9750021048517795
95
sage: y = _std_norm_cdf(-1.96); y # abs tol 1e-10
96
0.02499789514822044
97
sage: x + y # abs tol 1e-10
98
1.0
99
"""
100
cdef double e1 = x/sqrt(2)
101
cdef double prob = 0.5*(1+erf(e1))
102
return prob
103
104