-
Notifications
You must be signed in to change notification settings - Fork 5
/
gaussian.go
198 lines (166 loc) · 4.82 KB
/
gaussian.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package spn
import (
//"github.com/RenatoGeh/gospn/sys"
"bytes"
"fmt"
"gonum.org/v1/gonum/stat/distuv"
"math"
)
const (
// GaussMax is the maximum value of a standard gaussian, namely 1/sqrt(2*pi).
GaussMax = 0.398942280 // 1/sqrt(2*pi) := max value of a standard Gaussian
)
// Gaussian represents a gaussian distribution.
type Gaussian struct {
Node
// Variable ID
varid int
// GoNum's normal distribution
dist distuv.Normal
}
// NewGaussianParams constructs a new Gaussian from a mean and variance.
func NewGaussianParams(varid int, mu float64, sigma float64) *Gaussian {
return &Gaussian{Node{nil, []int{varid}}, varid, distuv.Normal{mu, sigma, nil}}
}
// NewGaussianRaw constructs a new Gaussian from a slice of values.
func NewGaussianRaw(varid int, vals []float64) *Gaussian {
var mean, sd float64
n := len(vals)
for i := 0; i < n; i++ {
mean += vals[i]
}
mean /= float64(n)
for i := 0; i < n; i++ {
d := vals[i] - mean
sd += d * d
}
sd = math.Sqrt(sd / float64(n))
return &Gaussian{Node{sc: []int{varid}}, varid, distuv.Normal{mean, sd, nil}}
}
// NewGaussian constructs a new Gaussian from a counting slice.
func NewGaussian(varid int, counts []int) *Gaussian {
var mean, sd float64
var N int
n := len(counts)
for i := range counts {
N += counts[i]
}
for i := 0; i < n; i++ {
mean += float64(counts[i]) / float64(N) * float64(i)
}
for i := 0; i < n; i++ {
d := float64(i) - mean
sd += (float64(counts[i]) / float64(N)) * d * d
}
sd = math.Sqrt(sd)
//sys.Printf("Created new gaussian with Mu: %f and StdDev: %f\n", mean, sd)
return &Gaussian{Node{sc: []int{varid}}, varid, distuv.Normal{mean, sd, nil}}
}
// NewGaussianMode constructs a new Gaussian centered on the Mode instead of the Mean.
func NewGaussianMode(varid int, counts []int) *Gaussian {
var mean, mode, sd float64
var N int
n := len(counts)
for i := range counts {
N += counts[i]
}
for i := 0; i < n; i++ {
mean += float64(counts[i]) / float64(N) * float64(i)
}
max := math.Inf(-1)
for i := 0; i < n; i++ {
c := float64(counts[i])
if c > max {
max = c
}
d := float64(i) - mean
sd += (c / float64(N)) * d * d
}
sd = math.Sqrt(sd)
//sys.Printf("Created new gaussian with Mu: %f and StdDev: %f\n", mean, sd)
return &Gaussian{Node{sc: []int{varid}}, varid, distuv.Normal{mode, sd, nil}}
}
// NewGaussianFit constructs a new Gaussian from GoNum's Fit function.
func NewGaussianFit(varid int, counts []float64) *Gaussian {
N := distuv.Normal{}
sample := make([]float64, len(counts))
for i := range sample {
sample[i] = float64(i)
}
N.Fit(sample, counts)
return &Gaussian{Node{sc: []int{varid}}, varid, N}
}
// Type returns the type of this node.
func (g *Gaussian) Type() string { return "leaf" }
func zeroSigma(v int, ok bool, mu float64) float64 {
if ok && v == int(mu) {
return 0
}
return math.Inf(-1)
}
// Value returns the probability of a certain valuation. That is Pr(X=val[varid]), where
// Pr is a probability function over a gaussian distribution.
func (g *Gaussian) Value(val VarSet) float64 {
v, ok := val[g.varid]
var l float64
if g.dist.Sigma == 0 {
return zeroSigma(v, ok, g.dist.Mu)
} else if ok {
l = g.dist.LogProb(float64(v))
} else {
l = 0.0 // ln(1.0) = 0.0
}
//sys.Printf("Gaussian value (mu=%f, sigma=%f) for value %d (pixel %d): %f = ln(%f)\n", g.dist.Mu, g.dist.Sigma, v, g.varid, l, math.Exp(l))
return l
}
// Max returns the MAP given a valuation.
func (g *Gaussian) Max(val VarSet) float64 {
v, ok := val[g.varid]
if g.dist.Sigma == 0 {
return zeroSigma(v, ok, g.dist.Mu)
} else if ok {
return g.dist.LogProb(float64(v))
}
return g.dist.LogProb(g.dist.Mu)
}
// ArgMax returns both the arguments and the value of the MAP state given a certain valuation.
func (g *Gaussian) ArgMax(val VarSet) (VarSet, float64) {
retval := make(VarSet)
v, ok := val[g.varid]
if g.dist.Sigma == 0 {
retval[g.varid] = int(g.dist.Mu)
return retval, 0.0
} else if ok {
retval[g.varid] = v
z := g.dist.LogProb(float64(v))
return retval, z
}
retval[g.varid] = int(math.Round(g.dist.Mu))
return retval, g.dist.LogProb(g.dist.Mu)
}
// Params returns mean and standard deviation.
func (g *Gaussian) Params() (float64, float64) {
return g.dist.Mu, g.dist.Sigma
}
// Sc returns the scope of this node.
func (g *Gaussian) Sc() []int {
if len(g.sc) == 0 {
g.sc = []int{g.varid}
}
return g.sc
}
// GobEncode serializes this gaussian node.
func (g *Gaussian) GobEncode() ([]byte, error) {
var b bytes.Buffer
fmt.Fprintln(&b, g.varid, g.dist.Mu, g.dist.Sigma)
return b.Bytes(), nil
}
// GobDecode unserializes this gaussian node.
func (g *Gaussian) GobDecode(data []byte) error {
b := bytes.NewBuffer(data)
_, err := fmt.Fscanln(b, &g.varid, &g.dist.Mu, &g.dist.Sigma)
g.sc = []int{g.varid}
return err
}
// SubType returns this leaf's subtype.
func (g *Gaussian) SubType() string { return "gaussian" }