cml

package module
v0.0.0-...-8f4a18c Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: MIT Imports: 6 Imported by: 3 modules, 3 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

Count-Min-Log

GoDoc

Count-Min-Log sketch: Approximately counting with approximate counters - Guillaume Pitel & Geoffroy Fouquier

TL;DR: Count-Min-Log Sketch for improved Average Relative Error on low frequency events

Count-Min Sketch is a widely adopted algorithm for approximate event counting in large scale processing. However, the original version of the Count-Min-Sketch (CMS) suffers of some deficiences, especially if one is interested in the low-frequency items, such as in text- mining related tasks. Several variants of CMS have been proposed to compensate for the high relative error for low-frequency events, but the proposed solutions tend to correct the errors instead of preventing them. In this paper, we propose the Count-Min-Log sketch, which uses logarithm-based, approximate counters instead of linear counters to improve the average relative error of CMS at constant memory footprint.

Example Usage

import cml

...
sk, err := cml.NewDefaultSketch()
sk.IncreaseCount([]byte("scott pilgrim"))
...

sk.Frequency([]byte("scott pilgrim")) // ==> 1

Documentation

Overview

Package cml implments the Count-Min-Log datastructure.

It is based on Count-Min-Log sketch: Approximately counting with approximate counters - Guillaume Pitel & Geoffroy Fouquier

http://iswag-symposium.org/2015/pdfs/shortpaper1.pdf

TL;DR: Count-Min-Log Sketch for improved Average Relative Error on low frequency events

Count-Min Sketch is a widely adopted algorithm for approximate event counting in large scale processing. However, the original version of the Count-Min-Sketch (CMS) suffers of some deficiences, especially if one is interested in the low-frequency items, such as in text- mining related tasks. Several variants of CMS have been proposed to compensate for the high relative error for low-frequency events, but the proposed solutions tend to correct the errors instead of preventing them. In this paper, we propose the Count-Min-Log sketch, which uses logarithm-based, approximate counters instead of linear counters to improve the average relative error of CMS at constant memory footprint.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Register

type Register interface {
	uint8 | uint16 | uint32
}

Register is the type of the register in the sketch.

type Sketch

type Sketch[T Register] struct {
	// contains filtered or unexported fields
}

Sketch is a Count-Min-Log Sketch with generic register size. Not safe for concurrent use.

func NewForCapacity

func NewForCapacity[T Register](capacity uint64, e float64) (*Sketch[T], error)

NewForCapacity creates a new Count-Min-Log Sketch optimized for a given capacity and error rate.

Parameters:

  • capacity: The expected maximum number of unique items to be counted. The sketch will be sized to efficiently handle up to this many items.
  • e: The desired error rate (between 0.001 and 1.0) that determines the sketch's accuracy. A smaller error rate provides more accurate counts but requires more memory. For example, e=0.01 means counts will be within ±1% of true values with high probability.

The sketch dimensions are automatically optimized based on these parameters to minimize memory usage while maintaining the desired error bounds. The base for logarithmic counting is tuned based on the register size T to optimize the space-accuracy tradeoff.

Returns a pointer to the initialized Sketch and an error if the error rate is invalid (must be between 0.001 and 1.0).

func NewSketch

func NewSketch[T Register](w, d uint, exp float64) (*Sketch[T], error)

NewSketch returns a new Count-Min-Log Sketch with specified register size.

Parameters:

  • w: Width of the sketch matrix. Controls the size of each row in the sketch. Larger values reduce collisions but increase memory usage.
  • d: Depth of the sketch matrix (number of hash functions). Controls the number of independent estimators. Higher values improve accuracy at the cost of more memory and computation.
  • exp: Base for logarithmic counting (must be > 1.0). Controls the compression rate of counters. Higher values save memory but reduce precision for large counts. Typical values range from 1.1 to 2.0.

Returns a pointer to the initialized Sketch and any error encountered during creation.

func NewSketchForEpsilonDelta

func NewSketchForEpsilonDelta[T Register](epsilon, delta float64) (*Sketch[T], error)

NewSketchForEpsilonDelta creates a new Count-Min-Log Sketch optimized for given error bounds.

Parameters:

  • epsilon: The maximum relative error rate (ε) for frequency estimates. A smaller epsilon gives more accurate counts but requires more memory. Typical values range from 0.01 to 0.1.
  • delta: The probability (δ) that the relative error exceeds epsilon. A smaller delta provides higher confidence but requires more hash functions. Typical values range from 0.01 to 0.1.

The width of the sketch is set to ⌈e/ε⌉ and depth to ⌈ln(1/δ)⌉, where e is Euler's number. The base for logarithmic counting (exp) is automatically tuned based on the register size T.

Returns a pointer to the initialized Sketch and any error encountered during creation.

func (*Sketch[T]) BulkUpdate

func (cml *Sketch[T]) BulkUpdate(e []byte, freq uint) bool

BulkUpdate increases the count of `s` by the given frequency, optimized to reduce redundant work. It computes the current minimum among the d registers for the item, then for each frequency, if the random decision passes, it increments all registers that equal the minimum.

func (*Sketch[T]) MarshalBinary

func (cml *Sketch[T]) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface. Returns a binary representation of the sketch.

func (*Sketch[T]) Merge

func (cml *Sketch[T]) Merge(other *Sketch[T]) error

Merge combines two sketches by taking the maximum value at each position. Returns an error if the sketches have different dimensions.

func (*Sketch[T]) Query

func (cml *Sketch[T]) Query(e []byte) float64

Query returns the count of `e`

func (*Sketch[T]) UnmarshalBinary

func (cml *Sketch[T]) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. Reconstructs a sketch from its binary representation.

func (*Sketch[T]) Update

func (cml *Sketch[T]) Update(e []byte) bool

Update increases the count of `s` by one, return true if added and the current count of `s`

Jump to

Keyboard shortcuts

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