/
dist.go
313 lines (258 loc) · 7.13 KB
/
dist.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright ©2016 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hbook
import "math"
// Dist0D is a 0-dim distribution.
type Dist0D struct {
N int64 // number of entries
SumW float64 // sum of weights
SumW2 float64 // sum of squared weights
}
func (d Dist0D) clone() Dist0D {
return d
}
// Rank returns the number of dimensions of the distribution.
func (*Dist0D) Rank() int {
return 1
}
// Entries returns the number of entries in the distribution.
func (d *Dist0D) Entries() int64 {
return d.N
}
// EffEntries returns the number of weighted entries, such as:
// (\sum w)^2 / \sum w^2
func (d *Dist0D) EffEntries() float64 {
if d.SumW2 == 0 {
return 0
}
return d.SumW * d.SumW / d.SumW2
}
// errW returns the absolute error on sumW()
func (d *Dist0D) errW() float64 {
return math.Sqrt(d.SumW2)
}
// // relErrW returns the relative error on sumW()
// func (d *Dist0D) relErrW() float64 {
// // FIXME(sbinet) check for low stats ?
// return d.errW() / d.SumW
// }
func (d *Dist0D) fill(w float64) {
d.N++
d.SumW += w
d.SumW2 += w * w
}
func (d *Dist0D) addScaled(a, a2 float64, o Dist0D) {
d.N += o.N
d.SumW += a * o.SumW
d.SumW2 += a2 * o.SumW2
}
func (d *Dist0D) scaleW(f float64) {
d.SumW *= f
d.SumW2 *= f * f
}
// Dist1D is a 1-dim distribution.
type Dist1D struct {
Dist Dist0D // weight moments
Stats struct {
SumWX float64 // 1st order weighted x moment
SumWX2 float64 // 2nd order weighted x moment
}
}
func (d Dist1D) clone() Dist1D {
return Dist1D{
Dist: d.Dist.clone(),
Stats: d.Stats,
}
}
// Rank returns the number of dimensions of the distribution.
func (*Dist1D) Rank() int {
return 1
}
// Entries returns the number of entries in the distribution.
func (d *Dist1D) Entries() int64 {
return d.Dist.Entries()
}
// EffEntries returns the effective number of entries in the distribution.
func (d *Dist1D) EffEntries() float64 {
return d.Dist.EffEntries()
}
// SumW returns the sum of weights of the distribution.
func (d *Dist1D) SumW() float64 {
return d.Dist.SumW
}
// SumW2 returns the sum of squared weights of the distribution.
func (d *Dist1D) SumW2() float64 {
return d.Dist.SumW2
}
// SumWX returns the 1st order weighted x moment.
func (d *Dist1D) SumWX() float64 {
return d.Stats.SumWX
}
// SumWX2 returns the 2nd order weighted x moment.
func (d *Dist1D) SumWX2() float64 {
return d.Stats.SumWX2
}
// errW returns the absolute error on sumW()
func (d *Dist1D) errW() float64 {
return d.Dist.errW()
}
// // relErrW returns the relative error on sumW()
// func (d *Dist1D) relErrW() float64 {
// return d.Dist.relErrW()
// }
// mean returns the weighted mean of the distribution
func (d *Dist1D) mean() float64 {
// FIXME(sbinet): check for low stats?
return d.SumWX() / d.SumW()
}
// variance returns the weighted variance of the distribution, defined as:
// sig2 = ( \sum(wx^2) * \sum(w) - \sum(wx)^2 ) / ( \sum(w)^2 - \sum(w^2) )
// see: https://en.wikipedia.org/wiki/Weighted_arithmetic_mean
func (d *Dist1D) variance() float64 {
// FIXME(sbinet): check for low stats?
sumw := d.SumW()
num := d.SumWX2()*sumw - d.SumWX()*d.SumWX()
den := sumw*sumw - d.SumW2()
v := num / den
return math.Abs(v)
}
// stdDev returns the weighted standard deviation of the distribution
func (d *Dist1D) stdDev() float64 {
return math.Sqrt(d.variance())
}
// stdErr returns the weighted standard error of the distribution
func (d *Dist1D) stdErr() float64 {
// FIXME(sbinet): check for low stats?
// TODO(sbinet): unbiased should check that Neff>1 and divide by N-1?
return math.Sqrt(d.variance() / d.EffEntries())
}
// rms returns the weighted RMS of the distribution, defined as:
// rms = \sqrt{\sum{w . x^2} / \sum{w}}
func (d *Dist1D) rms() float64 {
// FIXME(sbinet): check for low stats?
meansq := d.SumWX2() / d.SumW()
return math.Sqrt(meansq)
}
func (d *Dist1D) fill(x, w float64) {
d.Dist.fill(w)
d.Stats.SumWX += w * x
d.Stats.SumWX2 += w * x * x
}
func (d *Dist1D) addScaled(a, a2 float64, o Dist1D) {
d.Dist.addScaled(a, a2, o.Dist)
d.Stats.SumWX += a * o.Stats.SumWX
d.Stats.SumWX2 += a * o.Stats.SumWX2
}
func (d *Dist1D) scaleW(f float64) {
d.Dist.scaleW(f)
d.Stats.SumWX *= f
d.Stats.SumWX2 *= f
}
// Dist2D is a 2-dim distribution.
type Dist2D struct {
X Dist1D // x moments
Y Dist1D // y moments
Stats struct {
SumWXY float64 // 2nd-order cross-term
}
}
// Rank returns the number of dimensions of the distribution.
func (*Dist2D) Rank() int {
return 2
}
// Entries returns the number of entries in the distribution.
func (d *Dist2D) Entries() int64 {
return d.X.Entries()
}
// EffEntries returns the effective number of entries in the distribution.
func (d *Dist2D) EffEntries() float64 {
return d.X.EffEntries()
}
// SumW returns the sum of weights of the distribution.
func (d *Dist2D) SumW() float64 {
return d.X.SumW()
}
// SumW2 returns the sum of squared weights of the distribution.
func (d *Dist2D) SumW2() float64 {
return d.X.SumW2()
}
// SumWX returns the 1st order weighted x moment
func (d *Dist2D) SumWX() float64 {
return d.X.SumWX()
}
// SumWX2 returns the 2nd order weighted x moment
func (d *Dist2D) SumWX2() float64 {
return d.X.SumWX2()
}
// SumWY returns the 1st order weighted y moment
func (d *Dist2D) SumWY() float64 {
return d.Y.SumWX()
}
// SumWY2 returns the 2nd order weighted y moment
func (d *Dist2D) SumWY2() float64 {
return d.Y.SumWX2()
}
// SumWXY returns the 2nd-order cross-term.
func (d *Dist2D) SumWXY() float64 {
return d.Stats.SumWXY
}
// // errW returns the absolute error on sumW()
// func (d *Dist2D) errW() float64 {
// return d.X.errW()
// }
//
// // relErrW returns the relative error on sumW()
// func (d *Dist2D) relErrW() float64 {
// return d.X.relErrW()
// }
// xMean returns the weighted mean of the distribution
func (d *Dist2D) xMean() float64 {
return d.X.mean()
}
// yMean returns the weighted mean of the distribution
func (d *Dist2D) yMean() float64 {
return d.Y.mean()
}
// xVariance returns the weighted variance of the distribution
func (d *Dist2D) xVariance() float64 {
return d.X.variance()
}
// yVariance returns the weighted variance of the distribution
func (d *Dist2D) yVariance() float64 {
return d.Y.variance()
}
// xStdDev returns the weighted standard deviation of the distribution
func (d *Dist2D) xStdDev() float64 {
return d.X.stdDev()
}
// yStdDev returns the weighted standard deviation of the distribution
func (d *Dist2D) yStdDev() float64 {
return d.Y.stdDev()
}
// xStdErr returns the weighted standard error of the distribution
func (d *Dist2D) xStdErr() float64 {
return d.X.stdErr()
}
// yStdErr returns the weighted standard error of the distribution
func (d *Dist2D) yStdErr() float64 {
return d.Y.stdErr()
}
// xRMS returns the weighted RMS of the distribution
func (d *Dist2D) xRMS() float64 {
return d.X.rms()
}
// yRMS returns the weighted RMS of the distribution
func (d *Dist2D) yRMS() float64 {
return d.Y.rms()
}
func (d *Dist2D) fill(x, y, w float64) {
d.X.fill(x, w)
d.Y.fill(y, w)
d.Stats.SumWXY += w * x * y
}
func (d *Dist2D) scaleW(f float64) {
d.X.scaleW(f)
d.Y.scaleW(f)
d.Stats.SumWXY *= f
}