Skip to content

Commit

Permalink
Remove zero count
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Jan 28, 2021
1 parent 089be6b commit 14815be
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
36 changes: 11 additions & 25 deletions src/ddsketch/DDSketch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ interface BaseSketchConfig {
mapping: Mapping;
/** Storage for values */
store: DenseStore;
/** The number of zeroes added to the sketch */
zeroCount: number;
}

/** Base class for DDSketch*/
Expand All @@ -25,8 +23,6 @@ class BaseDDSketch {
mapping: Mapping;
/** Storage for values */
store: DenseStore;
/** The count of zero values */
zeroCount: number;
/** The minimum value seen by the sketch */
min: number;
/** The maximum value seen by the sketch */
Expand All @@ -36,13 +32,11 @@ class BaseDDSketch {
/** The sum of the values seen by the sketch */
sum: number;

constructor({ mapping, store, zeroCount }: BaseSketchConfig) {
constructor({ mapping, store }: BaseSketchConfig) {
this.mapping = mapping;
this.store = store;

this.zeroCount = zeroCount;

this.count = this.zeroCount + this.store.count;
this.count = this.store.count;
this.min = Infinity;
this.max = -Infinity;
this.sum = 0;
Expand All @@ -57,7 +51,10 @@ class BaseDDSketch {
* @throws Error if `weight` is 0 or negative
*/
accept(value: number, weight = 1): void {
if (value < 0 || value > this.mapping.maxPossible) {
if (
value < this.mapping.minPossible ||
value > this.mapping.maxPossible
) {
throw new Error(
'Input value is outside the range that is tracked by the sketch'
);
Expand All @@ -67,12 +64,8 @@ class BaseDDSketch {
throw new Error('Weight must be a positive number');
}

if (value > this.mapping.minPossible) {
const key = this.mapping.key(value);
this.store.add(key, weight);
} else {
this.zeroCount += weight;
}
const key = this.mapping.key(value);
this.store.add(key, weight);

/* Keep track of summary stats */
this.count += weight;
Expand All @@ -96,14 +89,8 @@ class BaseDDSketch {
}

const rank = quantile * (this.count - 1);

let quantileValue = 0;
if (rank < this.zeroCount) {
return 0;
} else {
const key = this.store.keyAtRank(rank - this.zeroCount);
quantileValue = this.mapping.value(key);
}
const key = this.store.keyAtRank(rank);
const quantileValue = this.mapping.value(key);

return quantileValue;
}
Expand Down Expand Up @@ -160,7 +147,6 @@ class BaseDDSketch {
*/
_copy(sketch: DDSketch): void {
this.store.copy(sketch.store);
this.zeroCount = sketch.zeroCount;
this.min = sketch.min;
this.max = sketch.max;
this.count = sketch.count;
Expand Down Expand Up @@ -192,6 +178,6 @@ export class DDSketch extends BaseDDSketch {
const mapping = new LogarithmicMapping(relativeAccuracy);
const store = new DenseStore();

super({ mapping, store, zeroCount: 0 });
super({ mapping, store });
}
}
19 changes: 19 additions & 0 deletions test/test.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
CubicallyInterpolatedMapping,
LinearlyInterpolatedMapping,
LogarithmicMapping
} from '../src/ddsketch/mapping';
import { MAX_INT_16, MIN_INT_16 } from '../src/ddsketch/mapping/KeyMapping';

it('is within bounds', () => {
const mapping = new CubicallyInterpolatedMapping(0.01);

const minIndex = mapping.key(mapping.minPossible);
const maxIndex = mapping.key(mapping.maxPossible);

console.log(minIndex);
console.log(maxIndex);

expect(minIndex).toBeGreaterThan(MIN_INT_16);
expect(maxIndex).toBeLessThan(MAX_INT_16);
});

0 comments on commit 14815be

Please sign in to comment.