Exemplo n.º 1
0
  ParticleFilter.prototype.resampleParticles = function() {
    // Residual resampling following Liu 2008; p. 72, section 3.4.4
    var m = this.particles.length;
    var W = webpplUtil.logsumexp(_.map(this.particles, function(p) {
      return p.weight;
    }));
    var avgW = W - Math.log(m);

    if (avgW == -Infinity) {      // debugging: check if NaN
      if (this.strict) {
        throw 'Error! All particles -Infinity';
      }
    } else {
      // Compute list of retained particles
      var retainedParticles = [];
      var newExpWeights = [];
      _.each(
          this.particles,
          function(particle) {
            var w = Math.exp(particle.weight - avgW);
            var nRetained = Math.floor(w);
            newExpWeights.push(w - nRetained);
            for (var i = 0; i < nRetained; i++) {
              retainedParticles.push(copyParticle(particle));
            }
          });
      // Compute new particles
      var numNewParticles = m - retainedParticles.length;
      var newParticles = [];
      var j;
      for (var i = 0; i < numNewParticles; i++) {
        j = erp.multinomialSample(newExpWeights);
        newParticles.push(copyParticle(this.particles[j]));
      }

      // Particles after update: Retained + new particles
      this.particles = newParticles.concat(retainedParticles);
    }

    // Reset all weights
    _.each(this.particles, function(particle) {
      particle.weight = avgW;
    });
  };
Exemplo n.º 2
0
 ParticleFilter.prototype.retrainAis = function() {
   var self = this;
   var W = webpplUtil.logsumexp(_.map(this.particles, function(p) {
     return p.weight;
   }));
   for (var i = 0; i < this.aisRetTypes.length; ++i) {
     var retType = this.aisRetTypes[i];
     var oldParams = this.aisParams[i] || expfam.defaultParameters([[], retType]);
     var samps = this.aisTrainingSamples[i].map(function(s) {
       var whichParticle = s[0];
       var val = s[1];
       var weight = self.particles.length * Math.exp(self.particles[whichParticle].weight - W);
       return [weight, retType.sufStat(val)];
     });
     // var newParams = expfam.mle(retType, samps, oldParams);
     var newParams = {
       base: optNp(retType, samps, retType.defaultNatParam),
       weights: _.times(expfam.featuresDim(retType), function() { return []; })
     };
     console.log('newParams', newParams);
     this.aisParams[i] = newParams;
   }
 };