Skip to content

Commit

Permalink
Recover performance of ParticleFilter in SMC.
Browse files Browse the repository at this point in the history
  • Loading branch information
null-a committed Aug 20, 2015
1 parent fca95dc commit f92c692
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/inference/particlefilter2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var _ = require('underscore');
var util = require('../util.js');
var erp = require('../erp.js');
var Trace = require('../trace');
var Particle = require('../particle');
var assert = require('assert');

module.exports = function(env) {
Expand All @@ -22,10 +23,9 @@ module.exports = function(env) {
return wpplFn(s, env.exit, a);
};

// Create initial particles.
// Particles are partial traces.
// Create initial particles/traces.
for (var i = 0; i < this.numParticles; i++) {
var p = new Trace();
var p = new (this.rejuvSteps === 0 ? Particle : Trace)();
p.saveContinuation(exitK, _.clone(s));
this.particles.push(p);
}
Expand Down
40 changes: 40 additions & 0 deletions src/particle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

var _ = require('underscore');
var assert = require('assert');

// Minimal Trace-like structure used to avoid unnecessary overhead in SMC
// without rejuvenation.

var Particle = function() {
this.score = 0;
this.logWeight = 0; // Importance weight.
};

Particle.prototype.saveContinuation = function(continuation, store) {
this.k = continuation;
this.store = store;
};

Particle.prototype.addChoice = function(erp, params, val, address, store, continuation) {
this.score += erp.score(params, val);
};

Particle.prototype.complete = function(value) {
// Called at coroutine exit.
assert(this.value === undefined);
this.value = value;
// Ensure any attempt to continue a completed Particle fails in an obvious way.
this.k = this.store = undefined;
};

Particle.prototype.copy = function() {
var t = new Particle();
t.score = this.score;
t.k = this.k;
t.store = _.clone(this.store);
t.value = this.value;
return t;
};

module.exports = Particle;

0 comments on commit f92c692

Please sign in to comment.