Codebase list golang-github-beorn7-perks / 6684ad6
no longer expose Invariant type Blake Mizerany 11 years ago
4 changed file(s) with 18 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
55
66 func BenchmarkInsert(b *testing.B) {
77 b.StopTimer()
8 s := New(Targeted(0.01, 0.5, 0.9, 0.99))
8 s := NewTargeted(0.01, 0.5, 0.9, 0.99)
99 b.StartTimer()
1010 for i := float64(0); i < float64(b.N); i++ {
1111 s.Insert(i)
1414 go readFloats(ch)
1515
1616 // Compute the 50th, 90th, and 99th percentile for a stream within the set error epsilon of 0.01.
17 q := quantile.New(quantile.Targeted(0.01, 0.50, 0.90, 0.99))
17 q := quantile.NewTargeted(0.01, 0.50, 0.90, 0.99)
1818 for v := range ch {
1919 q.Insert(v)
2020 }
4040 a[i], a[j] = a[j], a[i]
4141 }
4242
43 type Invariant func(s *stream, r float64) float64
44
45 // Biased returns an Invariant for high-biased (>50th) quantiles not known a
43 type invariant func(s *stream, r float64) float64
44
45 // Biased returns an Stream for high-biased (>50th) quantiles not known a
4646 // priori with associated error bounds e (usually 0.01).
4747 // See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error properties.
48 func Biased(e float64) Invariant {
49 return func(s *stream, r float64) float64 {
48 func NewBiased(e float64) *Stream {
49 f := func(s *stream, r float64) float64 {
5050 return 2 * e * r
5151 }
52 }
53
54 // Targeted returns an Invariant that is only concerned with a set
52 return newStream(f)
53 }
54
55 // Targeted returns an Stream that is only concerned with a set
5556 // of quantile values with associated error bounds e (usually 0.01) that are supplied a priori.
5657 // See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error properties.
57 func Targeted(e float64, quantiles ...float64) Invariant {
58 return func(s *stream, r float64) float64 {
58 func NewTargeted(e float64, quantiles ...float64) *Stream {
59 f := func(s *stream, r float64) float64 {
5960 var m float64 = math.MaxFloat64
6061 var f float64
6162 for _, q := range quantiles {
6869 }
6970 return m
7071 }
72 return newStream(f)
7173 }
7274
7375 // Stream calculates quantiles for a stream of float64s.
7678 b Samples
7779 }
7880
79 // New returns an initialized Stream with the Invariant ƒ.
80 func New(ƒ Invariant) *Stream {
81 func newStream(ƒ invariant) *Stream {
8182 x := &stream{ƒ: ƒ, l: list.New()}
8283 return &Stream{x, make(Samples, 0, 500)}
8384 }
141142 type stream struct {
142143 n float64
143144 l *list.List
144 ƒ Invariant
145 ƒ invariant
145146 }
146147
147148 func (s *stream) reset() {
77 )
88
99 func TestQuantRandQuery(t *testing.T) {
10 s := New(Targeted(0.01, 0.5, 0.90, 0.99))
10 s := NewTargeted(0.01, 0.5, 0.90, 0.99)
1111 a := make([]float64, 0, 1e5)
1212 rand.Seed(42)
1313 for i := 0; i < cap(a); i++ {
3939 done := make(chan *Stream)
4040 for i := 0; i < 2; i++ {
4141 go func() {
42 s := New(Targeted(0.01, 0.5, 0.90, 0.99))
42 s := NewTargeted(0.01, 0.5, 0.90, 0.99)
4343 for v := range ch {
4444 s.Insert(v)
4545 }