-
Notifications
You must be signed in to change notification settings - Fork 12
/
mv_normal.go
54 lines (48 loc) · 1.12 KB
/
mv_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
package stat
import (
. "github.com/skelterjohn/go.matrix"
)
func MVNormal_PDF(μ *DenseMatrix, Σ *DenseMatrix) func(x *DenseMatrix) float64 {
p := μ.Rows()
backμ := μ.DenseMatrix()
backμ.Scale(-1)
Σdet := Σ.Det()
ΣdetRt := sqrt(Σdet)
Σinv, _ := Σ.Inverse()
normalization := pow(2*π, -float64(p)/2) / ΣdetRt
return func(x *DenseMatrix) float64 {
δ, _ := x.PlusDense(backμ)
tmp := δ.Transpose()
tmp, _ = tmp.TimesDense(Σinv)
tmp, _ = tmp.TimesDense(δ)
f := tmp.Get(0, 0)
return normalization * exp(-f/2)
}
}
func NextMVNormal(μ *DenseMatrix, Σ *DenseMatrix) *DenseMatrix {
n := μ.Rows()
x := Zeros(n, 1)
for i := 0; i < n; i++ {
x.Set(i, 0, NextNormal(0, 1))
}
C, err := Σ.Cholesky()
Cx, err := C.TimesDense(x)
μCx, err := μ.PlusDense(Cx)
if err != nil {
panic(err)
}
return μCx
}
func MVNormal(μ *DenseMatrix, Σ *DenseMatrix) func() *DenseMatrix {
C, _ := Σ.Cholesky()
n := μ.Rows()
return func() *DenseMatrix {
x := Zeros(n, 1)
for i := 0; i < n; i++ {
x.Set(i, 0, NextNormal(0, 1))
}
Cx, _ := C.TimesDense(x)
MCx, _ := μ.PlusDense(Cx)
return MCx
}
}