Skip to content

Commit

Permalink
Merge pull request #736 from null-a/clarify-fwd-sampling-docs
Browse files Browse the repository at this point in the history
Warn when forward sampling hits factor/clarify docs.
  • Loading branch information
stuhlmueller committed Dec 19, 2016
2 parents 4e9e5b0 + 8814edd commit ebab602
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
21 changes: 11 additions & 10 deletions docs/inference/methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,16 @@ Forward Sampling
.. js:function:: Infer({model: ..., method: 'forward'[, ...]})

This method builds a histogram of return values obtained by
repeatedly executing either the target or :ref:`guide <guides>`
program given by ``model``.
repeatedly executing the program given by ``model``, ignoring any
``factor`` statements encountered while doing so. Since
``condition`` and ``observe`` are written in terms of ``factor``,
they are also effectively ignored.

While the :ref:`guide <guides>` does not include ``factor``
statements by definition, those in the target are ignored by this
method.

When executing the target, this method often corresponds to
sampling from the prior of a model.
This means that unlike all other methods described here, forward
sampling does not perform marginal inference. However, sampling
from a model without any factors etc. taken into account is often
useful in practice, and this method is provided as a convenient way
to achieve that.

The following options are supported:

Expand All @@ -341,8 +342,8 @@ Forward Sampling

.. describe:: guide

When ``true``, execute the guide using the current global
parameters. Otherwise, execute the target.
When ``true``, sample random choices from the guide using the
current global parameters. Otherwise, sample from the model.

Default: ``false``

Expand Down
8 changes: 8 additions & 0 deletions src/inference/forwardSample.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module.exports = function(env) {
this.k = k;
this.a = a;

this.factorWarningIssued = false;

this.coroutine = env.coroutine;
env.coroutine = this;
}
Expand Down Expand Up @@ -68,6 +70,12 @@ module.exports = function(env) {
},

factor: function(s, k, a, score) {
if (!this.opts.guide && !this.factorWarningIssued) {
this.factorWarningIssued = true;
var msg = 'Note that factor, condition and observe statements are ' +
'ignored when forward sampling from a model.';
util.warn(msg);
}
this.logWeight += ad.value(score);
return k(s);
},
Expand Down

0 comments on commit ebab602

Please sign in to comment.