UNPKG

@volare.finance/volare.js

Version:
97 lines 3.83 kB
"use strict"; /** * @file black-scholes.ts * @description Black-Scholes option pricing formula and supporting statistical functions. * @author astra <astra@volare.finance> * @date 2022 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getW = exports.blackScholes = exports.stdNormCDF = exports.stdNormDensity = void 0; /** * Standard normal density function. * @description See {@link http://en.wikipedia.org/wiki/Normal_distribution#Cumulative_distribution_function|Wikipedia page}. * @param {Number} x The value to calculate the standard normal density of * @returns {Number} The value of the standard normal density function at x */ function stdNormDensity(x) { return Math.pow(Math.E, (-1 * Math.pow(x, 2)) / 2) / Math.sqrt(2 * Math.PI); } exports.stdNormDensity = stdNormDensity; /** * 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) { let 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 (let 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; } exports.stdNormCDF = stdNormCDF; /** * 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 expatriation in years * @param {Number} v Volatility as a decimal * @param {Number} r Annual risk-free interest rate as a decimal * @param {Boolean} isPut The type of option to be priced * @returns {Number} Price of the option */ function blackScholes(s, k, t, v, r, isPut) { const w = (r * t + (Math.pow(v, 2) * t) / 2 - Math.log(k / s)) / (v * Math.sqrt(t)); return isPut ? k * Math.pow(Math.E, -1 * r * t) * stdNormCDF(v * Math.sqrt(t) - w) - s * stdNormCDF(-w) : s * stdNormCDF(w) - k * Math.pow(Math.E, -1 * r * t) * stdNormCDF(w - v * Math.sqrt(t)); } exports.blackScholes = blackScholes; /** * Calculate 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 expatriation in years * @param {Number} v Volatility as a decimal * @param {Number} r Annual risk-free interest rate as a decimal * @returns {Number} The value of omega */ function getW(s, k, t, v, r) { return (r * t + (Math.pow(v, 2) * t) / 2 - Math.log(k / s)) / (v * Math.sqrt(t)); } exports.getW = getW; /** * 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) { let val = 1; for (let i = n; i > 1; i -= 2) { val *= i; } return val; } //# sourceMappingURL=black-scholes.js.map