-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #905 from null-a/ais
Add AIS
- Loading branch information
Showing
14 changed files
with
217 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// This closely follows the AIS implementation developed for WebPPL as | ||
// part of "Measuring the reliability of MCMC inference with | ||
// bidirectional Monte Carlo" (Grosse et al). | ||
|
||
// https://arxiv.org/abs/1606.02275 | ||
// https://github.com/siddancha/webppl/tree/b607efe714d78c44f763ffd36324c0b67de96f56 | ||
|
||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var assert = require('assert'); | ||
var util = require('../util'); | ||
var numeric = require('../math/numeric'); | ||
|
||
module.exports = function(env){ | ||
|
||
var Initialize = require('./initialize')(env); | ||
var kernels = require('./kernels')(env); | ||
|
||
function AIS(s, k, a, wpplFn, options) { | ||
options = util.mergeDefaults(options, { | ||
steps: 20, | ||
samples: 1 | ||
}); | ||
|
||
var weights = []; | ||
|
||
var singleSample = function(k) { | ||
|
||
var initialize, run, finish; | ||
|
||
initialize = function() { | ||
return Initialize(run, wpplFn, s, env.exit, a, {}); | ||
}; | ||
|
||
run = function(initialTrace) { | ||
|
||
var curStep = 0; | ||
var increment = 1 / options.steps; | ||
var weight = 0; | ||
|
||
var MHKernel = kernels.parseOptions('MH'); | ||
|
||
var mhStepKernel = function(k, trace) { | ||
weight += increment * trace.scoreAllFactors(); | ||
curStep += 1; | ||
return MHKernel(k, trace, { | ||
factorCoeff: curStep * increment, | ||
allowHardFactors: false | ||
}); | ||
}; | ||
|
||
var mhChainKernel = kernels.repeat(options.steps, mhStepKernel); | ||
|
||
return mhChainKernel(function(trace) { | ||
return k(weight); | ||
}, initialTrace); | ||
}; | ||
|
||
return initialize(); | ||
}; | ||
|
||
return util.cpsLoop(options.samples, function(i, next) { | ||
return singleSample(function(weight) { | ||
weights.push(weight); | ||
return next(); | ||
}); | ||
}, function() { | ||
var avgWeight = numeric._sum(weights) / options.samples; | ||
return k(s, avgWeight); | ||
}); | ||
} | ||
|
||
return {AIS: AIS}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"logZ": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"logZ": -0.6539264674066638 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"logZ": -2.266 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var model = function() { | ||
return flip(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var model = function() { | ||
var x = flip(0.2); | ||
var y = flip(0.8); | ||
observe(Bernoulli({p : x && y ? 0.1 : 0.6}), true); | ||
return {x, y}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// The exact normalization constant for this model is: | ||
// exp(-1) / (2 * sqrt(pi)) | ||
var model = function() { | ||
var mu = gaussian(0, 1); | ||
observe(Gaussian({mu, sigma: 1}), 2); | ||
return mu; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters