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 ¶
- type Register
- type Sketch
- func (cml *Sketch[T]) BulkUpdate(e []byte, freq uint) bool
- func (cml *Sketch[T]) MarshalBinary() ([]byte, error)
- func (cml *Sketch[T]) Merge(other *Sketch[T]) error
- func (cml *Sketch[T]) Query(e []byte) float64
- func (cml *Sketch[T]) UnmarshalBinary(data []byte) error
- func (cml *Sketch[T]) Update(e []byte) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
MarshalBinary implements the encoding.BinaryMarshaler interface. Returns a binary representation of the sketch.
func (*Sketch[T]) Merge ¶
Merge combines two sketches by taking the maximum value at each position. Returns an error if the sketches have different dimensions.
func (*Sketch[T]) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. Reconstructs a sketch from its binary representation.