Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sumOfSq and std (standard deviation) - adding files
- Loading branch information
Showing
4 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var reductio_std = { | ||
add: function (prior) { | ||
return function (p, v) { | ||
if(prior) prior(p, v); | ||
if(p.count > 0) { | ||
p.std = 0.0; | ||
var n = p.sumOfSq - p.sum*p.sum/p.count; | ||
if (n>0.0) p.std = Math.sqrt(n/(p.count-1)); | ||
} else { | ||
p.std = 0.0; | ||
} | ||
return p; | ||
}; | ||
}, | ||
remove: function (prior) { | ||
return function (p, v) { | ||
if(prior) prior(p, v); | ||
if(p.count > 0) { | ||
p.std = 0.0; | ||
var n = p.sumOfSq - p.sum*p.sum/p.count; | ||
if (n>0.0) p.std = Math.sqrt(n/(p.count-1)); | ||
} else { | ||
p.std = 0; | ||
} | ||
return p; | ||
}; | ||
}, | ||
initial: function (prior) { | ||
return function (p) { | ||
p = prior(p); | ||
p.std = 0; | ||
return p; | ||
}; | ||
} | ||
}; | ||
|
||
module.exports = reductio_std; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var reductio_sum_of_sq = { | ||
add: function (a, prior) { | ||
return function (p, v) { | ||
if(prior) prior(p, v); | ||
p.sumOfSq = p.sumOfSq + a(v)*a(v); | ||
return p; | ||
}; | ||
}, | ||
remove: function (a, prior) { | ||
return function (p, v) { | ||
if(prior) prior(p, v); | ||
p.sumOfSq = p.sumOfSq - a(v)*a(v); | ||
return p; | ||
}; | ||
}, | ||
initial: function (prior) { | ||
return function (p) { | ||
p = prior(p); | ||
p.sumOfSq = 0; | ||
return p; | ||
}; | ||
} | ||
}; | ||
|
||
module.exports = reductio_sum_of_sq; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Counting tests | ||
describe('Reductio std', function () { | ||
|
||
var std, noSumOfSq, accStd; | ||
|
||
beforeEach(function () { | ||
var data = crossfilter([ | ||
{ foo: 'one', bar: 1 }, | ||
{ foo: 'two', bar: 2 }, | ||
{ foo: 'three', bar: 3 }, | ||
{ foo: 'one', bar: 4 }, | ||
{ foo: 'one', bar: 5 }, | ||
{ foo: 'two', bar: 6 }, | ||
]); | ||
|
||
var dim = data.dimension(function(d) { return d.foo; }); | ||
var group = dim.group(); | ||
var groupNoSumOfSq = dim.group(); | ||
var groupAccStd = dim.group(); | ||
|
||
var reducer = reductio() | ||
.std(true) | ||
.count(true); | ||
|
||
// This doesn't work because no .sumOfSq(accessor) has been defined. | ||
// The resulting group only tracks counts. | ||
reducer(groupNoSumOfSq); | ||
|
||
reducer.count(false).sumOfSq(false); | ||
reducer.std(function(d) { return d.bar; }); | ||
|
||
reducer(groupAccStd); | ||
|
||
reducer.sumOfSq(false); | ||
reducer.std(false); | ||
|
||
reducer.sumOfSq(function(d) { return d.bar; }) | ||
.std(true); | ||
|
||
// Now it should track count, sumOfSq, and std. | ||
reducer(group); | ||
|
||
std = group; | ||
noSumOfSq = groupNoSumOfSq; | ||
accStd = groupAccStd; | ||
}); | ||
|
||
it('has three groups', function (topic) { | ||
expect(std.top(Infinity).length).toEqual(3); | ||
}); | ||
|
||
it('grouping have the right averages', function (topic) { | ||
var values = {}; | ||
std.top(Infinity).forEach(function (d) { | ||
values[d.key] = d.value; | ||
}); | ||
|
||
expect(Math.round(values['one'].std)).toEqual(Math.round(2.08167)); | ||
expect(Math.round(values['two'].std)).toEqual(Math.round(2.82843)); | ||
expect(Math.round(values['three'].std)).toEqual(Math.round(0)); | ||
}); | ||
|
||
it('grouping with .std() but no .sumOfSq() doesn\'t work', function (topic) { | ||
var values = {}; | ||
noSumOfSq.top(Infinity).forEach(function (d) { | ||
values[d.key] = d.value; | ||
}); | ||
|
||
// It has a count, as defined. | ||
expect(values['one'].count).toEqual(3); | ||
|
||
// But no sumOfSq. | ||
expect(values['one'].sumOfSq).toBeUndefined(); | ||
|
||
// And no std. | ||
expect(values['one'].std).toBeUndefined(); | ||
|
||
// Also throws an error on the console, but that's more difficult to test. | ||
}); | ||
|
||
it('grouping with .std(accessor) works', function (topic) { | ||
var values = {}; | ||
accStd.top(Infinity).forEach(function (d) { | ||
values[d.key] = d.value; | ||
}); | ||
|
||
expect(Math.round(values['one'].std)).toEqual(Math.round(2.08167)); | ||
expect(Math.round(values['two'].std)).toEqual(Math.round(2.82843)); | ||
expect(Math.round(values['three'].std)).toEqual(Math.round(0)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Counting tests | ||
describe('Reductio sumOfSq', function () { | ||
var group; | ||
|
||
beforeEach(function () { | ||
var data = crossfilter([ | ||
{ foo: 'one', bar: 1 }, | ||
{ foo: 'two', bar: 2 }, | ||
{ foo: 'three', bar: 3 }, | ||
{ foo: 'one', bar: 4 }, | ||
{ foo: 'one', bar: 5 }, | ||
{ foo: 'two', bar: 6 }, | ||
]); | ||
|
||
var dim = data.dimension(function(d) { return d.foo; }); | ||
group = dim.group(); | ||
|
||
var reducer = reductio() | ||
.count(true) | ||
.sumOfSq(function(d) { return d.bar; }); | ||
|
||
reducer(group); | ||
}); | ||
|
||
it('has three groups', function () { | ||
expect(group.top(Infinity).length).toEqual(3); | ||
}); | ||
|
||
it('grouping have the right sum of squares', function () { | ||
var values = {}; | ||
group.top(Infinity).forEach(function (d) { | ||
values[d.key] = d.value; | ||
}); | ||
|
||
expect(values['one'].sumOfSq).toEqual(42); | ||
expect(values['two'].sumOfSq).toEqual(40); | ||
expect(values['three'].sumOfSq).toEqual(9); | ||
}); | ||
|
||
it('grouping plays nicely with count', function () { | ||
var values = {}; | ||
group.top(Infinity).forEach(function (d) { | ||
values[d.key] = d.value; | ||
}); | ||
|
||
expect(values['one'].count).toEqual(3); | ||
expect(values['two'].count).toEqual(2); | ||
expect(values['three'].count).toEqual(1); | ||
}); | ||
}); |
699b995
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should address #2