/
normal.go
65 lines (49 loc) · 1.13 KB
/
normal.go
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
package kmath
import (
"math"
)
var k float64 = math.Sqrt(math.Pi * 2.0)
// NormalArea calculates the area below the normal curve.
//
// den = 2^n*n!*(2*n+1)
//
// x - x(3,5,7...) / den
//
func NormalArea_(x float64) float64 {
a := x
// xx = x*x
n := 1.0
f := 1.0
sign := true
for {
f = float64(Factorial(int64(n)))*math.Pow(2,n)*(2*n+1)
n +=2
println(n,f,a)
if sign {
a -= math.Pow(x,n) / f
sign = false
} else {
a += math.Pow(x,n) / f
sign = true
}
if n>10 {
break
}
}
println("a",a)
println("/",math.Exp(-x*x/2.0) / k)
return a * math.Exp(-x*x/2.0) / k
}
func NormalArea(x float64) float64 {
area := 0.0
inc := 0.0001
for i:=0.0;i<100.0;i+=inc {
area += Normal(i) * inc
}
return area
}
// Normal returns the y value at the given x of the standard unit normal
// distribution (mean = 0 and standard deviation = 1).
func Normal(x float64) float64 {
return math.Exp( -x*x/2.0 ) / k
}