black-scholes-model
Version:
Complete plug and play Black-Scholes-Merton model for option pricing with features including implied volatility and Greeks.
29 lines (21 loc) • 569 B
text/typescript
import { erf } from 'mathjs';
type CDFReturnType = number;
interface ICdfInputs {
x: number;
mean?: number;
vol?: number;
}
export const cdf = ({ x, mean, vol }: ICdfInputs): CDFReturnType => {
if (!x) {
throw new Error('Random variable cannot be empty');
}
const u: number = mean || 0;
const sig: number = vol || 1;
if (sig === 0) {
return x < u ? 0.0 : 1.0;
}
const denom: number = sig * Math.sqrt(2);
const vc: number = x - u;
const result: number = 0.5 * (1 + erf(vc / denom));
return result;
};