adaptive

package module
v0.0.0-...-79e903d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2017 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

adaptive GoDoc

A probabilistic datastructure to estimate How many times did i see item "x" within the timerange "T", using a set Adaptive Count-Min Sketches.

The Adaptive Count-Min Sketch algorithm (Ada-CMS), is just CMS but with the update and query mechanisms adapted to use the pre-emphasis and de-emphasis mechanism.

For more information read the post by Adrian Colyer Time Adaptive Sketches (Ada-Sketches) for Summarizing Data Streams or the official paper with the same title by Anshumali Shrivastava, Arnd Christian König, Mikhail Bilenko)

Usage

duration := time.Duration(720 * time.Hour) // 720 hours range
unit := time.Hour

// Create sketch queryable with
// duation = 720 hours range
// unit = 1 hour
// width per sketch = 2^9
// depth per sketch = 8
// alpha = 1.004 (used for emphasizing and de-emphasizing)
sks := NewSketches(duration, unit, 9, 7, 1.004)

item := []byte("foo")
t1 := time.Date(2017, 06, 03, 0, 0, 0, 0, time.UTC)
t2 := t1.Add(time.Hour)
t3 := t2.Add(time.Hour)
t4 := t3.Add(time.Hour)
count1 := uint64(1337)
count2 := uint64(100000)

// Update item for given timestamps
sks.Insert(item, t1, count1)
sks.Insert(item, t3, count2)

// Estimate count of item within time range [t1, t2]
got, _ := sks.Estimate(item, t1, t2)
fmt.Printf("Expected count for \"%s\" in timerange [%v, %v] to be %d, got %d\n",
    string(item), t1.Format(time.Kitchen), t2.Format(time.Kitchen), count1, got)

// Estimate count of item within time range [t1, t3]
got, _ = sks.Estimate(item, t1, t3)
fmt.Printf("Expected count for \"%s\" in timerange [%v, %v] to be %d, got %d\n",
    string(item), t1.Format(time.Kitchen), t3.Format(time.Kitchen), count1+count2, got)

// Estimate count of item within time range [t1, t3]
got, _ = sks.Estimate(item, t3, t4)
fmt.Printf("Expected count for \"%s\" in timerange [%v, %v] to be %d, got %d\n",
    string(item), t3.Format(time.Kitchen), t4.Format(time.Kitchen), count2, got)

// Output:
// Expected count for "foo" in timerange [12:00AM, 1:00AM] to be 1337, got 1337
// Expected count for "foo" in timerange [12:00AM, 2:00AM] to be 101337, got 101337
// Expected count for "foo" in timerange [2:00AM, 3:00AM] to be 100000, got 100000

Documentation

Overview

Example
duration := time.Duration(720 * time.Hour) // 720 hours range
unit := time.Hour

// Create sketch queryable with
// duation = 720 hours range
// unit = 1 hour
// width per sketch = 2^9
// depth per sketch = 8
// alpha = 1.004 (used for emphasizing and de-emphasizing)
sks := NewSketches(duration, unit, 9, 7, 1.004)

item := []byte("foo")
t1 := time.Date(2017, 06, 03, 0, 0, 0, 0, time.UTC)
t2 := t1.Add(time.Hour)
t3 := t2.Add(time.Hour)
t4 := t3.Add(time.Hour)
count1 := uint64(1337)
count2 := uint64(100000)

// Update item for given timestamps
sks.Insert(item, t1, count1)
sks.Insert(item, t3, count2)

// Estimate count of item within time range [t1, t2]
got, _ := sks.Estimate(item, t1, t2)
fmt.Printf("Expected count for \"%s\" in timerange [%v, %v] to be %d, got %d\n",
	string(item), t1.Format(time.Kitchen), t2.Format(time.Kitchen), count1, got)

// Estimate count of item within time range [t1, t3]
got, _ = sks.Estimate(item, t1, t3)
fmt.Printf("Expected count for \"%s\" in timerange [%v, %v] to be %d, got %d\n",
	string(item), t1.Format(time.Kitchen), t3.Format(time.Kitchen), count1+count2, got)

// Estimate count of item within time range [t1, t3]
got, _ = sks.Estimate(item, t3, t4)
fmt.Printf("Expected count for \"%s\" in timerange [%v, %v] to be %d, got %d\n",
	string(item), t3.Format(time.Kitchen), t4.Format(time.Kitchen), count2, got)
Output:
Expected count for "foo" in timerange [12:00AM, 1:00AM] to be 1337, got 1337
Expected count for "foo" in timerange [12:00AM, 2:00AM] to be 101337, got 101337
Expected count for "foo" in timerange [2:00AM, 3:00AM] to be 100000, got 100000

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ACMS

type ACMS struct {
	// contains filtered or unexported fields
}

ACMS is a simple count-min-sketch imeplementation with a exp based pre-emphasis and de-emphasis of values

func NewACMS

func NewACMS(w, d uint64, alpha float64) *ACMS

NewACMS returns a Sketch of width 2^w and depth d for a given alpha the alpha is ussed to pre-emphasis and de-emphasis the counters using alpha^timestamp

func (*ACMS) Estimate

func (acms *ACMS) Estimate(item []byte, timestamp uint64) uint64

Estimate how many times item appeared at given timestamp

func (*ACMS) Insert

func (acms *ACMS) Insert(item []byte, timestamp, count uint64)

Insert item at given timestamp n times (n = count)

type Sketches

type Sketches struct {
	// contains filtered or unexported fields
}

Sketches represents an set of Adaptive Count-Min Sketch algorithm (Ada-CMS), which is just CMS but with the update and query mechanisms adapted to use the pre-emphasis and de-emphasis mechanism.

func NewSketches

func NewSketches(maxDuration, timeUnit time.Duration, w, d uint64, alpha float64) *Sketches

NewSketches returns an adaptive Sketch (representing multiple Adaptive Count-Min Sketches) with: maxDuration: specifies the max queriable range timeUnit: degines the unit of measurment, ns, ms, s, etc... w: width of CMS is 2^w d: depth of CMS alpha: ussed to pre-emphasis and de-emphasis the counters using alpha^timestamp

func (*Sketches) Estimate

func (sks *Sketches) Estimate(item []byte, start, end time.Time) (uint64, error)

Estimate returns the count of an item within a given timerange [start, end], if timerange > maxDuration return error

func (*Sketches) Insert

func (sks *Sketches) Insert(item []byte, timestamp time.Time, count uint64) error

Insert an item with a new count with at a given timestamp

func (*Sketches) MultiEstimate

func (sks *Sketches) MultiEstimate(item []byte, start, end time.Time) uint64

MultiEstimate returns the count of an item within a given timerange [start, end], where range can be > maxDuration Complexity however becomes O(n^3)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址