shit on stats
This commit is contained in:
1 parent
2add8284e6
commit
37e09a0f99
2 files changed
+102
-3
No files matched your search
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = fmt.Print
|
var _ = fmt.Print
|
||||||
@@ -9,12 +10,18 @@ var _ = fmt.Print
|
|||||||
const initialLength int = 1024
|
const initialLength int = 1024
|
||||||
const verySmall float64 = -100000
|
const verySmall float64 = -100000
|
||||||
const veryBig float64 = 100000
|
const veryBig float64 = 100000
|
||||||
|
const epsilon = 0.00000002
|
||||||
|
|
||||||
type StatAggregator struct {
|
type StatAggregator struct {
|
||||||
sum float64
|
sum float64
|
||||||
sumOfTheSquares float64
|
sumOfTheSquares float64
|
||||||
n int
|
n int
|
||||||
|
// histogram is a list of point between [min max] each point in the
|
||||||
|
//histogram represents the beginning of the histogram bars.
|
||||||
histogram []float64
|
histogram []float64
|
||||||
|
histogramValues []int
|
||||||
|
histogramBarWidth float64
|
||||||
|
numberOfHistogramBar int
|
||||||
min float64
|
min float64
|
||||||
max float64
|
max float64
|
||||||
}
|
}
|
||||||
@@ -22,7 +29,15 @@ type StatAggregator struct {
|
|||||||
type Stats struct{}
|
type Stats struct{}
|
||||||
|
|
||||||
func NewStatAggregator() *StatAggregator {
|
func NewStatAggregator() *StatAggregator {
|
||||||
return &StatAggregator{0, 0, 0, make([]float64, initialLength), veryBig, verySmall}
|
return &StatAggregator{0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
make([]float64, 0, initialLength),
|
||||||
|
make([]int, 0, initialLength),
|
||||||
|
0,
|
||||||
|
initialLength,
|
||||||
|
veryBig,
|
||||||
|
verySmall}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *StatAggregator) AddStats(dataChannel chan float64, output chan *Stats) {
|
func (this *StatAggregator) AddStats(dataChannel chan float64, output chan *Stats) {
|
||||||
@@ -39,3 +54,37 @@ func (this *StatAggregator) AddStats(dataChannel chan float64, output chan *Stat
|
|||||||
}
|
}
|
||||||
output <- nil
|
output <- nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// always assume the min and max includes the value!
|
||||||
|
func (this *StatAggregator) AddDataToHistogram(value float64) {
|
||||||
|
if len(this.histogramValues) < this.numberOfHistogramBar {
|
||||||
|
this.AddDataToHistogramWhenLessDataThanBarAreOnHistograms(value)
|
||||||
|
} else {
|
||||||
|
this.AddDataToHistogramWithMaximalNumberOfBars(value)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StatAggregator) AddDataToHistogramWithMaximalNumberOfBars(value float64) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StatAggregator) AddDataToHistogramWhenLessDataThanBarAreOnHistograms(value float64) {
|
||||||
|
for i, v := range this.histogram {
|
||||||
|
if math.Abs(value-v) < epsilon {
|
||||||
|
this.histogramValues[i]++
|
||||||
|
return
|
||||||
|
} else if value < v {
|
||||||
|
this.histogram = append(this.histogram, 0)
|
||||||
|
copy(this.histogram[i+1:], this.histogram[i:])
|
||||||
|
this.histogram[i] = value
|
||||||
|
|
||||||
|
this.histogramValues = append(this.histogramValues, 0)
|
||||||
|
copy(this.histogramValues[i+1:], this.histogramValues[i:])
|
||||||
|
this.histogramValues[i] = 1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.histogramValues = append(this.histogramValues, 1)
|
||||||
|
this.histogram = append(this.histogram, value)
|
||||||
|
}
|
||||||
+52
-2
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -9,9 +10,11 @@ func TestNewStats(t *testing.T) {
|
|||||||
if s.n != 0 ||
|
if s.n != 0 ||
|
||||||
s.sum != 0 ||
|
s.sum != 0 ||
|
||||||
s.sumOfTheSquares != 0 ||
|
s.sumOfTheSquares != 0 ||
|
||||||
len(s.histogram) != initialLength ||
|
s.numberOfHistogramBar != initialLength ||
|
||||||
s.min != veryBig ||
|
s.min != veryBig ||
|
||||||
s.max != verySmall {
|
s.max != verySmall ||
|
||||||
|
len(s.histogram) != 0 ||
|
||||||
|
len(s.histogramValues) != 0 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -31,5 +34,52 @@ func TestAddStatsBasic(t *testing.T) {
|
|||||||
if s.n != 4. || s.min != -1. || s.max != 20. || s.sum != 23. || s.sumOfTheSquares != 411. {
|
if s.n != 4. || s.min != -1. || s.max != 20. || s.sum != 23. || s.sumOfTheSquares != 411. {
|
||||||
t.Error(">>>", s.max, s.min)
|
t.Error(">>>", s.max, s.min)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddDataToHistogramWhenLessDataThanBarAreOnHistograms(t *testing.T) {
|
||||||
|
s := NewStatAggregator()
|
||||||
|
s.AddDataToHistogramWhenLessDataThanBarAreOnHistograms(17)
|
||||||
|
if !reflect.DeepEqual([]float64{17}, s.histogram) {
|
||||||
|
t.Error("bad histogram", s.histogram)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual([]int{1}, s.histogramValues) {
|
||||||
|
t.Error("bad histogramValues:", s.histogramValues)
|
||||||
|
}
|
||||||
|
s.histogram = []float64{-1, 1, 2, 3}
|
||||||
|
s.histogramValues = []int{1, 1, 1, 1}
|
||||||
|
s.AddDataToHistogramWhenLessDataThanBarAreOnHistograms(2)
|
||||||
|
if !reflect.DeepEqual([]int{1, 1, 2, 1}, s.histogramValues) {
|
||||||
|
t.Error("bad histogramValues", s.histogramValues)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.AddDataToHistogramWhenLessDataThanBarAreOnHistograms(2.5)
|
||||||
|
if !reflect.DeepEqual([]float64{-1, 1, 2, 2.5, 3}, s.histogram) {
|
||||||
|
t.Error("bad histogram", s.histogram)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual([]int{1, 1, 2, 1, 1}, s.histogramValues) {
|
||||||
|
t.Error("bad histogramValues:", s.histogramValues)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.AddDataToHistogramWhenLessDataThanBarAreOnHistograms(22.5)
|
||||||
|
if !reflect.DeepEqual([]float64{-1, 1, 2, 2.5, 3, 22.5}, s.histogram) {
|
||||||
|
t.Error("bad histogram", s.histogram)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual([]int{1, 1, 2, 1, 1, 1}, s.histogramValues) {
|
||||||
|
t.Error("bad histogramValues:", s.histogramValues)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAddDataToHistogramWithMaximalNumberOfBars(t *testing.T) {
|
||||||
|
s := NewStatAggregator()
|
||||||
|
s.numberOfHistogramBar = 5
|
||||||
|
s.histogram = []float64{1, 2, 3, 4, 5}
|
||||||
|
s.histogramValues = []int{1, 1, 2, 3, 4}
|
||||||
|
s.AddDataToHistogramWithMaximalNumberOfBars(3.5)
|
||||||
|
if !reflect.DeepEqual(s.histogram, []float64{1, 2, 3, 4, 5}) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(s.histogramValues, []int{1, 1, 2, 3, 4}) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user