Skip to content

Commit

Permalink
Add log-collapsing sketches
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Belanger committed Aug 23, 2022
1 parent ae7356d commit 1956970
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
55 changes: 54 additions & 1 deletion src/ddsketch/DDSketch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
* Copyright 2020 Datadog, Inc.
*/

import { DenseStore } from './store';
import {
DenseStore,
CollapsingLowestDenseStore,
CollapsingHighestDenseStore
} from './store';
import { Mapping, KeyMapping, LogarithmicMapping } from './mapping';

const DEFAULT_RELATIVE_ACCURACY = 0.01;
const DEFAULT_BIN_LIMIT = 2048;

interface BaseSketchConfig {
/** The mapping between values and indicies for the sketch */
Expand Down Expand Up @@ -243,3 +248,51 @@ export class DDSketch extends BaseDDSketch {
super({ mapping, store, negativeStore, zeroCount: 0 });
}
}

interface LogCollapsingSketchConfig {
/** The accuracy guarantee of the sketch, between 0-1 (default 0.01) */
relativeAccuracy?: number;
binLimit?: number;
}

export class LogCollapsingLowestDenseDDSketch extends BaseDDSketch {
/**
* Initialize a new LogCollapsingLowestDenseDDSketch
*
* @param relativeAccuracy The accuracy guarantee of the sketch (default 0.01)
* @param binLimit Number of bins before lowest indices are collapsed (default 2048)
*/
constructor(
{
relativeAccuracy = DEFAULT_RELATIVE_ACCURACY,
binLimit = DEFAULT_BIN_LIMIT
} = defaultConfig as LogCollapsingSketchConfig
) {
const mapping = new LogarithmicMapping(relativeAccuracy);
const store = new CollapsingLowestDenseStore(binLimit);
const negativeStore = new CollapsingLowestDenseStore(binLimit);

super({ mapping, store, negativeStore, zeroCount: 0 });
}
}

export class LogCollapsingHighestDenseDDSketch extends BaseDDSketch {
/**
* Initialize a new LogCollapsingHighestDenseDDSketch
*
* @param relativeAccuracy The accuracy guarantee of the sketch (default 0.01)
* @param binLimit Number of bins before highest indices are collapsed (default 2048)
*/
constructor(
{
relativeAccuracy = DEFAULT_RELATIVE_ACCURACY,
binLimit = DEFAULT_BIN_LIMIT
} = defaultConfig as LogCollapsingSketchConfig
) {
const mapping = new LogarithmicMapping(relativeAccuracy);
const store = new CollapsingHighestDenseStore(binLimit);
const negativeStore = new CollapsingHighestDenseStore(binLimit);

super({ mapping, store, negativeStore, zeroCount: 0 });
}
}
2 changes: 1 addition & 1 deletion src/ddsketch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* Copyright 2020 Datadog, Inc.
*/

export { DDSketch } from './DDSketch';
export * from './DDSketch';
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* Copyright 2020 Datadog, Inc.
*/

export { DDSketch } from './ddsketch';
export * from './ddsketch';
20 changes: 18 additions & 2 deletions test/ddsketch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
* Copyright 2020 Datadog, Inc.
*/

import { DDSketch } from '../src/ddsketch';
import {
DDSketch,
LogCollapsingHighestDenseDDSketch,
LogCollapsingLowestDenseDDSketch
} from '../src/ddsketch';
import {
generateDecreasing,
generateIncreasing,
Expand Down Expand Up @@ -37,7 +41,7 @@ const getQuantile = (data: number[], quantile: number) => {
return sortedIncreasingData[rank];
};

describe('DDSketch', () => {
function test(DDSketch: any) {
const evaluateSketchAccuracy = (sketch: DDSketch, data: number[]) => {
for (const quantile of testQuantiles) {
const sketchQ = sketch.getValueAtQuantile(quantile);
Expand Down Expand Up @@ -226,4 +230,16 @@ describe('DDSketch', () => {
evaluateSketchAccuracy(sketch2, [...data1, ...data2]);
});
});
}

describe('DDSketch', () => {
test(DDSketch)
});

describe('LogCollapsingHighestDenseDDSketch', () => {
test(LogCollapsingHighestDenseDDSketch)
});

describe('LogCollapsingLowestDenseDDSketch', () => {
test(LogCollapsingLowestDenseDDSketch)
});

0 comments on commit 1956970

Please sign in to comment.