Files
go-opt-pricer/utils.go
2017-09-17 16:58:11 -04:00

36 lines
760 B
Go

package main
import "math"
// NormCDF computes the Cumulative Density Function of a Standard Normal
// Distribution.
// Code adapted from https://www.johndcook.com/blog/cpp_phi/
func NormCDF(z float64) float64 {
// constants
var a1, a2, a3, a4, a5, p, t, y, sign float64
a1 = 0.254829592
a2 = -0.284496736
a3 = 1.421413741
a4 = -1.453152027
a5 = 1.061405429
p = 0.3275911
sign = 1
if z < 0 {
sign = -1
}
x := math.Abs(z) / math.Sqrt2
t = 1.0 / (1.0 + p*x)
y = 1.0 - (((((a5*t+a4)*t)+a3)*t+a2)*t+a1)*t*math.Exp(-x*x)
return 0.5 * (1.0 + sign*y)
}
// NormPDF computes the Probability Density Function of a Standard Normal
// Distribution.
func NormPDF(z float64) float64 {
return 1 / math.Sqrt(2*math.Pi) * math.Exp(-math.Pow(z, 2)/2)
}