Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
stuhlmueller committed Oct 1, 2015
2 parents d13f5a5 + 37cb6fe commit acbfc03
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 87 deletions.
7 changes: 5 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sys
import os
import shlex
import json

on_rtd = os.environ.get('READTHEDOCS', None) == 'True'

Expand Down Expand Up @@ -61,10 +62,12 @@
# |version| and |release|, also used in various other places throughout the
# built documents.
#
with open('../package.json') as f:
pkg = json.load(f)
# The short X.Y version.
version = '0.1.1'
version = pkg['version']
# The full version, including alpha/beta/rc tags.
release = '0.1.1'
release = pkg['version']

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
1 change: 1 addition & 0 deletions docs/development/npm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Updating the npm package
1. Update version in dev::

git checkout dev
git pull
npm version patch // or minor, or major; prints new version number

2. Merge into master::
Expand Down
12 changes: 12 additions & 0 deletions docs/development/workflow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ To only run the tests, do::

npm test

To reproduce intermittent test failures run the inference tests with
the random seed displayed in the test output. For example::

RANDOM_SEED=2344512342 nodeunit tests/test-inference.js

nodeunit can also run individual tests or test groups. For example::

nodeunit tests/test-inference.js -t Enumerate

See the `nodeunit documentation`_ for details.

To only run the linter::

grunt gjslint
Expand Down Expand Up @@ -39,3 +50,4 @@ Multiple ``--require`` arguments can be used to include multiple
packages.

.. _continuous integration tests: https://travis-ci.org/probmods/webppl
.. _nodeunit documentation: https://github.com/caolan/nodeunit#command-line-options
4 changes: 4 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Running webppl programs::

webppl examples/geometric.wppl

Seeding the random number generator::

RANDOM_SEED=2344512342 webppl examples/lda.wppl

Compiling webppl programs to Javascript::

webppl examples/geometric.wppl --compile --out geometric.js
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "webppl",
"description": "Probabilistic programming for the web",
"version": "0.2.0",
"version": "0.2.1",
"author": "webppl contributors",
"main": "src/main.js",
"repository": {
Expand Down Expand Up @@ -36,6 +36,7 @@
"grunt-contrib-nodeunit": "^0.4.1",
"grunt-gjslint": "^0.1.6",
"nodeunit": "^0.9.1",
"seedrandom": "^2.4.2",
"through2": "^2.0.0"
},
"scripts": {
Expand Down
46 changes: 24 additions & 22 deletions src/erp.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ var deserializeERP = function(JSONString) {

var uniformERP = new ERP({
sample: function(params) {
var u = Math.random();
var u = util.random();
return (1 - u) * params[0] + u * params[1];
},
score: function(params, val) {
Expand All @@ -142,7 +142,7 @@ var uniformERP = new ERP({
var bernoulliERP = new ERP({
sample: function(params) {
var weight = params[0];
var val = Math.random() < weight;
var val = util.random() < weight;
return val;
},
score: function(params, val) {
Expand All @@ -166,7 +166,7 @@ var bernoulliERP = new ERP({

var randomIntegerERP = new ERP({
sample: function(params) {
return Math.floor(Math.random() * params[0]);
return Math.floor(util.random() * params[0]);
},
score: function(params, val) {
var stop = params[0];
Expand All @@ -183,8 +183,8 @@ function gaussianSample(params) {
var sigma = params[1];
var u, v, x, y, q;
do {
u = 1 - Math.random();
v = 1.7156 * (Math.random() - 0.5);
u = 1 - util.random();
v = 1.7156 * (util.random() - 0.5);
x = u - 0.449871;
y = Math.abs(v) + 0.386595;
q = x * x + y * (0.196 * y - 0.25472 * x);
Expand Down Expand Up @@ -266,7 +266,7 @@ function gammaSample(params) {
var a = params[0];
var b = params[1];
if (a < 1) {
return gammaSample([1 + a, b]) * Math.pow(Math.random(), 1 / a);
return gammaSample([1 + a, b]) * Math.pow(util.random(), 1 / a);
}
var x, v, u;
var d = a - 1 / 3;
Expand All @@ -277,7 +277,7 @@ function gammaSample(params) {
v = 1 + c * x;
} while (v <= 0);
v = v * v * v;
u = Math.random();
u = util.random();
if ((u < 1 - 0.331 * x * x * x * x) || (Math.log(u) < 0.5 * x * x + d * (1 - v + Math.log(v)))) {
return b * d * v;
}
Expand All @@ -298,7 +298,7 @@ var gammaERP = new ERP({
var exponentialERP = new ERP({
sample: function(params) {
var a = params[0];
var u = Math.random();
var u = util.random();
return Math.log(u) / (-1 * a);
},
score: function(params, val) {
Expand Down Expand Up @@ -363,7 +363,7 @@ function binomialSample(params) {
}
var u;
for (var i = 0; i < n; i++) {
u = Math.random();
u = util.random();
if (u < p) {
k++;
}
Expand Down Expand Up @@ -452,7 +452,7 @@ var poissonERP = new ERP({
var emu = Math.exp(-mu);
var p = 1;
do {
p *= Math.random();
p *= util.random();
k++;
} while (p > emu);
return (k - 1) || 0;
Expand Down Expand Up @@ -499,16 +499,16 @@ var dirichletERP = new ERP({ sample: dirichletSample, score: dirichletScore });

function multinomialSample(theta) {
var thetaSum = util.sum(theta);
var x = Math.random() * thetaSum;
var x = util.random() * thetaSum;
var k = theta.length;
var probAccum = 0;
for (var i = 0; i < k; i++) {
probAccum += theta[i];
if (probAccum >= x) {
if (x < probAccum) {
return i;
} //FIXME: if x=0 returns i=0, but this isn't right if theta[0]==0...
}
}
return k;
return k - 1;
}

// Make a discrete ERP from a {val: prob, etc.} object (unormalized).
Expand All @@ -535,14 +535,16 @@ function makeMarginalERP(marginal) {
// Make an ERP from marginal:
var dist = new ERP({
sample: function(params) {
var x = Math.random();
var x = util.random();
var probAccum = 0;
for (var i in marginal) {if (marginal.hasOwnProperty(i)) {
probAccum += marginal[i].prob;
// FIXME: if x=0 returns i=0, but this isn't right if theta[0]==0...
if (probAccum >= x)
return marginal[i].val;
}}
for (var i in marginal) {
if (marginal.hasOwnProperty(i)) {
probAccum += marginal[i].prob;
if (x < probAccum) {
return marginal[i].val;
}
}
}
return marginal[i].val;
},
score: function(params, val) {
Expand Down Expand Up @@ -620,7 +622,7 @@ function gaussianProposalParams(params, prevVal) {
}

function dirichletProposalParams(params, prevVal) {
var concentration = 0.1; // TODO: choose the right parameters.
var concentration = 0.1;
var driftParams = params.map(function(x) {return concentration * x});
return driftParams;
}
Expand Down
4 changes: 2 additions & 2 deletions src/hashtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,9 @@ function Hashtable() {
// console.log("SIZE: " + size);
var L = biggestBucket.entries.length;
while (true) {
var bi = Math.floor(Math.random() * buckets.length);
var bi = Math.floor(util.random() * buckets.length);
var bucket = buckets[bi];
var p = Math.floor(Math.random() * L);
var p = Math.floor(util.random() * L);
if (p < bucket.entries.length)
return bucket.entries[p][1];
}
Expand Down
2 changes: 1 addition & 1 deletion src/headerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = function(env) {
var args = Array.prototype.slice.call(arguments, 3);
var stringedArgs = JSON.stringify(args);
var foundInCache = stringedArgs in c;
var recomp = Math.random() < recompProb;
var recomp = util.random() < recompProb;
if (foundInCache && !recomp) { // return stored value
return k(s, c[stringedArgs]);
} else { // recompute
Expand Down
4 changes: 2 additions & 2 deletions src/inference/asyncpf.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module.exports = function(env) {

// launch a new particle OR continue an existing one
var p, launchP;
var i = Math.floor((this.buffer.length + 1) * Math.random());
var i = Math.floor((this.buffer.length + 1) * util.random());
if (i == this.buffer.length) { // generate new particle
p = initParticle(this.store, this.exitK);
} else { // launch particle in queue
Expand Down Expand Up @@ -122,7 +122,7 @@ module.exports = function(env) {

// compute number of children and their weights
if (logRatio < 0) {
numChildrenAndWeight = Math.log(Math.random()) < logRatio ?
numChildrenAndWeight = Math.log(util.random()) < logRatio ?
[1, wbar] :
[0, -Infinity];
} else {
Expand Down
17 changes: 10 additions & 7 deletions src/inference/incrementalmh.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ module.exports = function(env) {
};

ArrayERPMasterList.prototype.getRandom = function() {
var idx = Math.floor(Math.random() * this.erpNodes.length);
var idx = Math.floor(util.random() * this.erpNodes.length);
return this.erpNodes[idx];
};

Expand Down Expand Up @@ -862,7 +862,7 @@ module.exports = function(env) {
this.rvsPropLP, this.fwdPropLP);
debuglog(1, 'num vars:', this.erpMasterList.size(), 'old num vars:', this.erpMasterList.oldSize());
debuglog(1, 'acceptance prob:', acceptance);
if (Math.random() >= acceptance) {
if (util.random() >= acceptance) {
debuglog(1, 'REJECT');
this.score = this.oldScore;
var n = this.touchedNodes.length;
Expand Down Expand Up @@ -934,11 +934,14 @@ module.exports = function(env) {
}
}
} else {
var dist;
if (this.returnHist)
dist = erp.makeMarginalERP(util.logHist(this.returnHist));
else
dist = erp.makeMarginalERP({});
var hist;
if (this.returnSamps || this.onlyMAP) {
hist = {};
hist[JSON.stringify(this.MAP.value)] = { prob: 1, val: this.MAP.value };
} else {
hist = this.returnHist;
}
var dist = erp.makeMarginalERP(util.logHist(hist));
if (this.returnSamps) {
if (this.onlyMAP)
this.returnSamps.push(this.MAP);
Expand Down
4 changes: 2 additions & 2 deletions src/inference/mhkernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = function(env) {
}
// Make a new proposal.
env.query.clear();
this.regenFrom = this.proposalBoundary + Math.floor(Math.random() * numERP);
this.regenFrom = this.proposalBoundary + Math.floor(util.random() * numERP);
this.trace = this.oldTrace.upto(this.regenFrom);
var regen = this.oldTrace.choiceAtIndex(this.regenFrom);
return this.sample(_.clone(regen.store), regen.k, regen.address, regen.erp, regen.params, true);
Expand Down Expand Up @@ -90,7 +90,7 @@ module.exports = function(env) {
assert(!this.trace.isComplete(), 'Particle missed exit address during rejuvenation.');
}
var prob = acceptProb(this.trace, this.oldTrace, this.regenFrom, this.reused, this.proposalBoundary);
var accept = Math.random() < prob;
var accept = util.random() < prob;
return this.cont(accept ? this.trace : this.oldTrace, accept);
};

Expand Down
2 changes: 1 addition & 1 deletion src/inference/pmcmc.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module.exports = function(env) {

PMCMC.prototype.activeContinuationWithStore = function() {
var k = last(this.activeParticle().continuations);
var s = _.clone(last(this.activeParticle().stores)); // FIXME: why is cloning here necessary?
var s = _.clone(last(this.activeParticle().stores));
return function() {
return k(s);
};
Expand Down
2 changes: 1 addition & 1 deletion src/inference/rejection.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = function(env) {

Rejection.prototype.run = function() {
this.scoreSoFar = 0;
this.threshold = this.maxScore + Math.log(Math.random());
this.threshold = this.maxScore + Math.log(util.random());
return this.wpplFn(_.clone(this.s), env.exit, this.a);
}

Expand Down
2 changes: 0 additions & 2 deletions src/transforms/caching.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ var build = require('ast-types').builders;
var types = require('ast-types').types;
var isPrimitive = require('../syntax').isPrimitive;


// TODO: Auto-extract this list, somehow?
var cacheExempt = [
'flip',
'randomInteger',
Expand Down

0 comments on commit acbfc03

Please sign in to comment.