-
Notifications
You must be signed in to change notification settings - Fork 33
/
black-scholes.js
107 lines (102 loc) · 3.12 KB
/
black-scholes.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Black-Scholes option pricing formula and supporting statistical functions.
* @module black-scholes
* @author Matt Loppatto <mattloppatto@gmail.com>
* @copyright 2014 Matt Loppatto
*/
/**
* Standard normal cumulative distribution function. The probability is estimated
* by expanding the CDF into a series using the first 100 terms.
* See {@link http://en.wikipedia.org/wiki/Normal_distribution#Cumulative_distribution_function|Wikipedia page}.
*
* @param {Number} x The upper bound to integrate over. This is P{Z <= x} where Z is a standard normal random variable.
* @returns {Number} The probability that a standard normal random variable will be less than or equal to x
*/
function stdNormCDF(x)
{
var probability = 0;
// avoid divergence in the series which happens around +/-8 when summing the
// first 100 terms
if(x >= 8)
{
probability = 1;
}
else if(x <= -8)
{
probability = 0;
}
else
{
for(var i = 0; i < 100; i++)
{
probability += (Math.pow(x, 2*i+1)/_doubleFactorial(2*i+1));
}
probability *= Math.pow(Math.E, -0.5*Math.pow(x, 2));
probability /= Math.sqrt(2*Math.PI);
probability += 0.5;
}
return probability;
}
/**
* Double factorial. See {@link http://en.wikipedia.org/wiki/Double_factorial|Wikipedia page}.
* @private
*
* @param {Number} n The number to calculate the double factorial of
* @returns {Number} The double factorial of n
*/
function _doubleFactorial(n)
{
var val = 1;
for(var i = n; i > 1; i-=2)
{
val *= i;
}
return val;
}
/**
* Black-Scholes option pricing formula.
* See {@link http://en.wikipedia.org/wiki/Black%E2%80%93Scholes_model#Black-Scholes_formula|Wikipedia page}
* for pricing puts in addition to calls.
*
* @param {Number} s Current price of the underlying
* @param {Number} k Strike price
* @param {Number} t Time to experiation in years
* @param {Number} v Volatility as a decimal
* @param {Number} r Anual risk-free interest rate as a decimal
* @param {String} callPut The type of option to be priced - "call" or "put"
* @returns {Number} Price of the option
*/
function blackScholes(s, k, t, v, r, callPut)
{
var price = null;
var w = (r * t + Math.pow(v, 2) * t / 2 - Math.log(k / s)) / (v * Math.sqrt(t));
if(callPut === "call")
{
price = s * stdNormCDF(w) - k * Math.pow(Math.E, -1 * r * t) * stdNormCDF(w - v * Math.sqrt(t));
}
else // put
{
price = k * Math.pow(Math.E, -1 * r * t) * stdNormCDF(v * Math.sqrt(t) - w) - s * stdNormCDF(-w);
}
return price;
}
/**
* Calcuate omega as defined in the Black-Scholes formula.
*
* @param {Number} s Current price of the underlying
* @param {Number} k Strike price
* @param {Number} t Time to experiation in years
* @param {Number} v Volatility as a decimal
* @param {Number} r Anual risk-free interest rate as a decimal
* @returns {Number} The value of omega
*/
function getW(s, k, t, v, r)
{
var w = (r * t + Math.pow(v, 2) * t / 2 - Math.log(k / s)) / (v * Math.sqrt(t));
return w;
}
module.exports = {
blackScholes: blackScholes,
stdNormCDF: stdNormCDF,
getW: getW
};