Skip to content

Commit

Permalink
Merge pull request #267 from null-a/cauchy
Browse files Browse the repository at this point in the history
Add Cauchy ERP.
  • Loading branch information
stuhlmueller committed Nov 11, 2015
2 parents f9d8ea5 + c15f4af commit 11cd4ec
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/erp.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var _ = require('underscore');
var util = require('./util');
var assert = require('assert');

var LOG_PI = 1.1447298858494002;
var LOG_2PI = 1.8378770664093453;

function ERP(obj) {
Expand Down Expand Up @@ -227,6 +228,20 @@ var multivariateGaussianERP = new ERP({
score: multivariateGaussianScore
});

var cauchyERP = new ERP({
sample: function(params) {
var location = params[0];
var scale = params[1];
var u = util.random();
return location + scale * Math.tan(180 * (u - 0.5));
},
score: function(params, x) {
var location = params[0];
var scale = params[1];
return -LOG_PI - Math.log(scale) - Math.log(1 + Math.pow((x - location) / scale, 2));
}
});

var discreteERP = new ERP({
sample: function(params) {
return multinomialSample(params[0]);
Expand Down Expand Up @@ -695,6 +710,7 @@ module.exports = setErpNames({
gaussianERP: gaussianERP,
multinomialSample: multinomialSample,
multivariateGaussianERP: multivariateGaussianERP,
cauchyERP: cauchyERP,
poissonERP: poissonERP,
randomIntegerERP: randomIntegerERP,
uniformERP: uniformERP,
Expand Down
4 changes: 4 additions & 0 deletions src/header.wppl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ var multivariateGaussian = function(mu, cov) {
return sample(multivariateGaussianERP, [mu, cov]);
};

var cauchy = function(location, scale) {
return sample(cauchyERP, [location, scale]);
};

var uniform = function(a, b) {
return sample(uniformERP, [a, b]);
};
Expand Down
6 changes: 6 additions & 0 deletions tests/test-data/stochastic/expected/cauchy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"hist": {
"true": 0.3743,
"false": 0.6257
}
}
4 changes: 4 additions & 0 deletions tests/test-data/stochastic/models/cauchy.wppl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var model = function() {
var x = cauchy(2, 1.5);
return 1 < x && x <= 3;
};
3 changes: 2 additions & 1 deletion tests/test-inference.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var tests = [
beta: true,
exponential: true,
binomial: true,
poisson: true
poisson: true,
cauchy: true
}
},
{
Expand Down

0 comments on commit 11cd4ec

Please sign in to comment.