/
bin1d.go
135 lines (110 loc) · 3.17 KB
/
bin1d.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
// Copyright 2015 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 "sort"
// Bin1D models a bin in a 1-dim space.
type Bin1D struct {
Range Range
Dist Dist1D
}
// Rank returns the number of dimensions for this bin.
func (Bin1D) Rank() int { return 1 }
func (b *Bin1D) scaleW(f float64) {
b.Dist.scaleW(f)
}
func (b *Bin1D) fill(x, w float64) {
b.Dist.fill(x, w)
}
// Entries returns the number of entries in this bin.
func (b *Bin1D) Entries() int64 {
return b.Dist.Entries()
}
// EffEntries returns the effective number of entries \f$ = (\sum w)^2 / \sum w^2 \f$
func (b *Bin1D) EffEntries() float64 {
return b.Dist.EffEntries()
}
// SumW returns the sum of weights in this bin.
func (b *Bin1D) SumW() float64 {
return b.Dist.SumW()
}
// SumW2 returns the sum of squared weights in this bin.
func (b *Bin1D) SumW2() float64 {
return b.Dist.SumW2()
}
// ErrW returns the absolute error on SumW()
func (b *Bin1D) ErrW() float64 {
return b.Dist.errW()
}
// XEdges returns the [low,high] edges of this bin.
func (b *Bin1D) XEdges() Range {
return b.Range
}
// XMin returns the lower limit of the bin (inclusive).
func (b *Bin1D) XMin() float64 {
return b.Range.Min
}
// XMax returns the upper limit of the bin (exclusive).
func (b *Bin1D) XMax() float64 {
return b.Range.Max
}
// XMid returns the geometric center of the bin.
// i.e.: 0.5*(high+low)
func (b *Bin1D) XMid() float64 {
return 0.5 * (b.Range.Min + b.Range.Max)
}
// XWidth returns the (signed) width of the bin
func (b *Bin1D) XWidth() float64 {
return b.Range.Max - b.Range.Min
}
// XFocus returns the mean position in the bin, or the midpoint (if the
// sum of weights for this bin is 0).
func (b *Bin1D) XFocus() float64 {
if b.SumW() == 0 {
return b.XMid()
}
return b.XMean()
}
// XMean returns the mean X.
func (b *Bin1D) XMean() float64 {
return b.Dist.mean()
}
// XVariance returns the variance in X.
func (b *Bin1D) XVariance() float64 {
return b.Dist.variance()
}
// XStdDev returns the standard deviation in X.
func (b *Bin1D) XStdDev() float64 {
return b.Dist.stdDev()
}
// XStdErr returns the standard error in X.
func (b *Bin1D) XStdErr() float64 {
return b.Dist.stdErr()
}
// XRMS returns the RMS in X.
func (b *Bin1D) XRMS() float64 {
return b.Dist.rms()
}
// Bin1Ds is a sorted slice of Bin1D implementing sort.Interface.
type Bin1Ds []Bin1D
func (p Bin1Ds) Len() int { return len(p) }
func (p Bin1Ds) Less(i, j int) bool { return p[i].Range.Min < p[j].Range.Min }
func (p Bin1Ds) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// IndexOf returns the index of the Bin1D containing the value v.
// It returns UndeflowBin if v is smaller than the smallest bin value.
// It returns OverflowBin if v is greater than the greatest bin value.
// It returns len(bins) if v falls within a bins gap.
func (p Bin1Ds) IndexOf(v float64) int {
i := sort.Search(len(p), func(i int) bool { return v < p[i].Range.Max })
if i == len(p) {
return OverflowBin1D
}
rng := p[i].Range
if i == 0 && v < rng.Min {
return UnderflowBin1D
}
if rng.Min <= v && v < rng.Max {
return i
}
return len(p)
}