Skip to content

Commit

Permalink
Merge pull request #827 from null-a/multimodality-test
Browse files Browse the repository at this point in the history
Handle multimodality in sampler tests
  • Loading branch information
longouyang committed Apr 20, 2017
2 parents 77160ab + 377698b commit 5e91397
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/math/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var _ = require('lodash');
var assert = require('assert');
var util = require('../util');

var abs = Math.abs,
pow = Math.pow,
Expand Down Expand Up @@ -78,7 +79,14 @@ function kde(samps, kernel) {
function mode(samps) {
// tally values and sort
var tallied = _.sortBy(_.toPairs(_.countBy(samps)), '1');
return _.last(tallied)[0];
var last = _.last(tallied);
if (tallied.length > 1) {
var penultimate = tallied.slice(-2)[0];
if (penultimate[1] === last[1]) {
util.warn('Samples have more than one mode.');
}
}
return last[0];
}

// estimate the mode of a continuous distribution from some
Expand Down
6 changes: 6 additions & 0 deletions tests/test-data/sampler/binomial.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ module.exports = {
mode: function(params) {
var p = params[0];
var n = params[1];
// When (n+1)p is an integer and p != 0 or 1, then there are two
// modes: (n+1)p and (n+1)p - 1
if (Number.isInteger((n + 1) * p) &&
p !== 0 && p !== 1) {
throw new Error("Don't know how to test multimodal distributions.");
}
return Math.floor((n + 1) * p)
},
variance: function(params) {
Expand Down
8 changes: 4 additions & 4 deletions tests/test-samplers.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ var generateSettingTest = function(seed, distMetadata, settings) {
var n = settings.n;

// only test the stats that aren't blacklisted
var populationStatisticFunctions = _.pick(distMetadata.populationStatisticFunctions,
function(v, k) {
return !_.includes(settings.skip, k)
});
var populationStatisticFunctions = _.pickBy(distMetadata.populationStatisticFunctions,
function(v, k) {
return !_.includes(settings.skip, k)
});
var group = {};

var moment = distMetadata.moment;
Expand Down

0 comments on commit 5e91397

Please sign in to comment.