Liking cljdoc? Tell your friends :D

org.replikativ.foerster.trace

Probabilistic programs as traced computations.

sample, observe and factor publish savepoints (sites :inference/choose and :inference/factor), so a probabilistic program is run and replayed by spindel.trace like any other computation. This namespace adds what is specific to inference and nothing else: a policy that scores, pure functions over scored traces, and Metropolis-Hastings as replay plus an accept step.

Every entry's :note carries

:dist the site's distribution (nil for a factor) :log-prob log density of the entry's value under :dist, for EVERY site, sampled ones included (a factor's weight) :log-proposal log density under whatever drew the value; absent when the value was not drawn (observed, constrained, kept) :observed? :constrained? :kept? :symmetric? :factor?

A deterministic site is recorded with {:deterministic? true} only; it is not among entries, so it neither scores nor moves.

and the world accumulates the importance weight at [:inference :log-weight]: log p of what was observed, constrained or factored, and log p - log q of what a proposal drew. A policy writes it to the world it is about to resume, so the weight of a fork starts from the weight at its site.

Probabilistic programs as traced computations.

`sample`, `observe` and `factor` publish savepoints (sites
`:inference/choose` and `:inference/factor`), so a probabilistic program is
run and replayed by `spindel.trace` like any other computation. This
namespace adds what is specific to inference and nothing else: a policy that
scores, pure functions over scored traces, and Metropolis-Hastings as replay
plus an accept step.

Every entry's `:note` carries

  :dist          the site's distribution (nil for a factor)
  :log-prob      log density of the entry's value under :dist, for EVERY
                 site, sampled ones included (a factor's weight)
  :log-proposal  log density under whatever drew the value; absent when the
                 value was not drawn (observed, constrained, kept)
  :observed? :constrained? :kept? :symmetric? :factor?

A `deterministic` site is recorded with `{:deterministic? true}` only; it is
not among `entries`, so it neither scores nor moves.

and the world accumulates the importance weight at `[:inference
:log-weight]`: `log p` of what was observed, constrained or factored, and
`log p - log q` of what a proposal drew. A policy writes it to the world it
is about to resume, so the weight of a fork starts from the weight at its
site.
raw docstring

anchor?clj/s

(anchor? sp)

Whether a site is worth an anchor: only a site a move can start from. An observe or a factor is never replayed from, and an anchor is a forked world. Pass as :anchor? to trace/run and trace/replay.

Whether a site is worth an anchor: only a site a move can start from. An
observe or a factor is never replayed from, and an anchor is a forked
world. Pass as `:anchor?` to `trace/run` and `trace/replay`.
sourceraw docstring

barrier-site?clj/s

(barrier-site? sp)

Whether a savepoint is where SMC parks a particle: an observation, a stream site, or a factor marked :barrier.

Whether a savepoint is where SMC parks a particle: an observation, a
stream site, or a factor marked `:barrier`.
sourceraw docstring

choicesclj/s

(choices trace)

{address value} of the sample sites of trace.

{address value} of the sample sites of `trace`.
sourceraw docstring

choose-siteclj/s

source

deterministic-siteclj/s

source

entriesclj/s

(entries trace)

The inference entries of trace in program order, each with its :address.

The inference entries of `trace` in program order, each with its :address.
sourceraw docstring

factor-siteclj/s

source

latent-addressesclj/s

(latent-addresses trace)

Addresses of the sample sites of trace that inference may move.

Addresses of the sample sites of `trace` that inference may move.
sourceraw docstring

latent?clj/s

(latent? entry)

Whether a trace entry is a sample site inference may move: not observed, not constrained.

Whether a trace entry is a sample site inference may move: not observed,
not constrained.
sourceraw docstring

legacy-traceclj/s

(legacy-trace trace)

trace in the legacy shape of a particle's trace ([:inference :trace]): {address {:value :distribution :log-prob :observed?}}, and {address {:value :deterministic? true}} for deterministic sites.

`trace` in the legacy shape of a particle's trace (`[:inference :trace]`):
{address {:value :distribution :log-prob :observed?}}, and
{address {:value :deterministic? true}} for deterministic sites.
sourceraw docstring

log-jointclj/s

(log-joint trace)

log p(choices, observations) of trace: the sum of every entry's :log-prob.

log p(choices, observations) of `trace`: the sum of every entry's
:log-prob.
sourceraw docstring

log-weightclj/s

(log-weight trace)

The importance weight the world of trace accumulated.

The importance weight the world of `trace` accumulated.
sourceraw docstring

mh-chainclj/s

(mh-chain trace n)
(mh-chain trace
          n
          {:keys [on-step first-iteration]
           move :step
           :or {move mh-step first-iteration 0}
           :as opts})

n Metropolis-Hastings moves from trace; opts as for mh-step. Returns a CPS operation resolving {:trace final :moves m :accepted k}: of the m moves made, k were accepted. :on-step (fn [step-result]) sees every step. :step (fn [trace opts]) -> CPS resolving a step result replaces mh-step (e.g. foerster.hmc/within-gibbs); a step that makes several moves reports :moves and :accepted-moves, otherwise it is one move, accepted when :accepted?. Moves are numbered from :first-iteration (default 0): a move's randomness is keyed by its number, so the moves of one chain need distinct numbers.

`n` Metropolis-Hastings moves from `trace`; `opts` as for `mh-step`.
Returns a CPS operation resolving {:trace final :moves m :accepted k}: of
the m moves made, k were accepted. `:on-step` (fn [step-result]) sees every
step. `:step` (fn [trace opts]) -> CPS resolving a step result replaces
`mh-step` (e.g. `foerster.hmc/within-gibbs`); a step that makes several
moves reports `:moves` and `:accepted-moves`, otherwise it is one move,
accepted when `:accepted?`. Moves are numbered from `:first-iteration`
(default 0): a move's randomness is keyed by its number, so the moves of
one chain need distinct numbers.
sourceraw docstring

mh-log-ratioclj/s

(mh-log-ratio old new log-selection)
(mh-log-ratio old new log-selection from)
(mh-log-ratio old new log-selection from score)

Log acceptance ratio of moving from old to new, where new is old replayed from its earliest target with kept values elsewhere.

log a = [log p(new) - log p(old)] + [log q(old | new) - log q(new | old)] + [log s(targets | new) - log s(targets | old)]

q(new | old) is the density of everything new drew afresh; q(old | new) is the density of everything of old that new did not keep, which the reverse move would have to draw (from the prior, hence its :log-prob, or a block's :sample density). A symmetric move cancels on both sides. s is the probability of selecting the targets, log-selection; it differs between the traces when the move changed how many sites there are to select from.

A kept value that fell out of its site's support is drawn again (:redrawn?). That is reversible only if the reverse move would redraw there too, i.e. if the new value is outside the OLD support; otherwise the reverse keeps it, the old state cannot be reached back, and the move is impossible.

Entries upstream of the replayed address are the SAME entries in both traces and cancel everywhere. That relies on a fork sharing the notes of its source by reference, which holds for the in-process worlds a session forks and would not survive a serialized trace. With from, the replayed address, only the entries from it on are compared: the ratio is the same, and it costs the replayed suffix instead of the whole trace. score (fn [entry]) is an entry's log target (default its :log-prob), e.g. tempered-score.

Log acceptance ratio of moving from `old` to `new`, where `new` is `old`
replayed from its earliest target with kept values elsewhere.

  log a = [log p(new) - log p(old)]
          + [log q(old | new) - log q(new | old)]
          + [log s(targets | new) - log s(targets | old)]

q(new | old) is the density of everything `new` drew afresh; q(old | new) is
the density of everything of `old` that `new` did not keep, which the
reverse move would have to draw (from the prior, hence its :log-prob, or a
block's :sample density). A
symmetric move cancels on both sides. s is the probability of selecting the
targets, `log-selection`; it differs between the traces when the move
changed how many sites there are to select from.

A kept value that fell out of its site's support is drawn again
(`:redrawn?`). That is reversible only if the reverse move would redraw there
too, i.e. if the new value is outside the OLD support; otherwise the reverse
keeps it, the old state cannot be reached back, and the move is impossible.

Entries upstream of the replayed address are the SAME entries in both
traces and cancel everywhere. That relies on a fork sharing the notes of its
source by reference, which holds for the in-process worlds a session forks
and would not survive a serialized trace. With `from`, the replayed
address, only the entries from it on are compared: the ratio is the same,
and it costs the replayed suffix instead of the whole trace. `score`
(fn [entry]) is an entry's log target (default its :log-prob), e.g.
`tempered-score`.
sourceraw docstring

mh-stepclj/s

(mh-step trace)
(mh-step trace
         {:keys [select propose iteration constraints policy-options until
                 shared-anchors? temperature keep-old?]
          :or {select uniform-site propose prior-proposal iteration 0}
          anchor-pred :anchor?})

One Metropolis-Hastings move on trace.

Options: :select (fn [trace iteration]) -> {:targets #{address} :log-selection (fn [trace])}, the sites to move together and the log probability of selecting them in a given trace (default: uniform-site) :propose (fn [sp old-entry]) -> {:value v :log-proposal lq} or {:value v :symmetric? true}, for each target (default: prior-proposal). The reverse move is scored under the prior, so a proposal must be the prior or symmetric. :constraints as for policy; the conditioning of the chain, which every move must repeat :policy-options the options of the policy the trace was made with (interventions, …), which every move repeats; :keep?, :draw and :constraints are the move's own :iteration passed to :select :until, :anchor? passed to the replay (spindel.trace/run): a move of a partial trace stops where the trace did :temperature β: the move targets p(x)·L(x)^β, whatever temperature the trace was recorded at (see policy) :keep-old? an accepted move releases nothing of the old trace: its caller releases what no one refers to :shared-anchors? the trace's anchors may be shared with other traces (SMC particles of one ancestor): an accepted move releases the old trace's world only, and the caller its anchors

The computation is replayed from the earliest target; every other site keeps its value and is rescored under its distribution as it is now. The loser's worlds are released. Returns a CPS operation resolving {:trace t :accepted? boolean :log-ratio r}; a trace with nothing to move resolves unchanged.

One Metropolis-Hastings move on `trace`.

Options:
  :select    (fn [trace iteration]) -> {:targets #{address}
             :log-selection (fn [trace])}, the sites to move together and
             the log probability of selecting them in a given trace
             (default: `uniform-site`)
  :propose   (fn [sp old-entry]) -> {:value v :log-proposal lq} or
             {:value v :symmetric? true}, for each target (default:
             `prior-proposal`). The reverse move is scored under the prior,
             so a proposal must be the prior or symmetric.
  :constraints as for `policy`; the conditioning of the chain, which
             every move must repeat
  :policy-options the options of the policy the trace was made with
             (interventions, …), which every move repeats; `:keep?`,
             `:draw` and `:constraints` are the move's own
  :iteration passed to :select
  :until, :anchor? passed to the replay (`spindel.trace/run`): a move of
             a partial trace stops where the trace did
  :temperature β: the move targets p(x)·L(x)^β, whatever temperature the
             trace was recorded at (see `policy`)
  :keep-old? an accepted move releases nothing of the old trace: its
             caller releases what no one refers to
  :shared-anchors? the trace's anchors may be shared with other traces
             (SMC particles of one ancestor): an accepted move releases
             the old trace's world only, and the caller its anchors

The computation is replayed from the earliest target; every other site
keeps its value and is rescored under its distribution as it is now. The
loser's worlds are released. Returns a CPS operation resolving
{:trace t :accepted? boolean :log-ratio r}; a trace with nothing to move
resolves unchanged.
sourceraw docstring

policyclj/s

(policy)
(policy {fallback :else :as opts})

The scoring policy.

Options: :constraints {address value} fix those sample sites; their density enters the weight :keep? reuse the value a sample site had in the replayed trace, rescored under the site's distribution NOW :draw (fn [sp old-entry]) -> nil (not my site) or {:value v :log-proposal lq}, or {:value v :symmetric? true} for a symmetric move around the old value :interventions {selector transform}: a selected sample site takes {:do v} (that value, no score), {:policy (fn [choices])} (a value from the choices made so far), {:dist d} (a new mechanism) or {:shift δ} (the old one moved by δ). A key that is not a selector (spindel.select) is an address; the world's [:inference :interventions] (intervene!) apply too :noise {address u}: the site takes its mechanism's value for u (foerster.mechanism), observed sites included — a counterfactual world. Sites without noise are marked :unaligned? :simulate-observed? true: observed sites draw a fresh value from their distribution instead of scoring the data (predictive runs: with :constraints holding a posterior draw's latents) :temperature β in [0, 1]: observations and factors count β·log p — the target p(x)·L(x)^β of tempered SMC (foerster.tempering). Their notes keep :log-lik, the untempered log p :init? start a sample site at its :init option. Only for the first state of a Markov chain, which may be anything; an :init value is not a draw, so it has no place in a move or in a weighted sample. :else policy for every other site (default: its payload)

With no options every sample site is drawn from its prior: forward simulation, likelihood weighting.

Every choose entry's note records :barrier, its barrier index (see barrier-site?); policy-options recovers the options.

The scoring policy.

Options:
  :constraints {address value} fix those sample sites; their density enters
               the weight
  :keep?       reuse the value a sample site had in the replayed trace,
               rescored under the site's distribution NOW
  :draw        (fn [sp old-entry]) -> nil (not my site) or
               {:value v :log-proposal lq}, or {:value v :symmetric? true}
               for a symmetric move around the old value
  :interventions {selector transform}: a selected sample site takes
               `{:do v}` (that value, no score), `{:policy (fn [choices])}`
               (a value from the choices made so far), `{:dist d}` (a new
               mechanism) or `{:shift δ}` (the old one moved by δ). A key
               that is not a selector (`spindel.select`) is an address; the
               world's `[:inference :interventions]` (`intervene!`) apply too
  :noise       {address u}: the site takes its mechanism's value for u
               (`foerster.mechanism`), observed sites included — a
               counterfactual world. Sites without noise are marked
               `:unaligned?`
  :simulate-observed? true: observed sites draw a fresh value from their
               distribution instead of scoring the data (predictive runs:
               with `:constraints` holding a posterior draw's latents)
  :temperature β in [0, 1]: observations and factors count β·log p — the
               target p(x)·L(x)^β of tempered SMC (`foerster.tempering`).
               Their notes keep `:log-lik`, the untempered log p
  :init?       start a sample site at its `:init` option. Only for the
               first state of a Markov chain, which may be anything; an
               `:init` value is not a draw, so it has no place in a move or
               in a weighted sample.
  :else        policy for every other site (default: its payload)

With no options every sample site is drawn from its prior: forward
simulation, likelihood weighting.

Every choose entry's note records `:barrier`, its barrier index (see
`barrier-site?`); `policy-options` recovers the options.
sourceraw docstring

policy-optionsclj/s

(policy-options policy)

The options a policy was made from, or nil for another function.

The options a `policy` was made from, or nil for another function.
sourceraw docstring

prior-proposalclj/s

(prior-proposal sp _old-entry)

Propose a fresh draw from a target site's own distribution.

Propose a fresh draw from a target site's own distribution.
sourceraw docstring

random-walk-proposalclj/s

(random-walk-proposal step-size)

A symmetric Gaussian step of step-size around a real-valued target's old value. A target whose law is not continuous (a boolean, an integer count, a vector) has no such step; it gets a prior proposal.

A symmetric Gaussian step of `step-size` around a real-valued target's
old value. A target whose law is not continuous (a boolean, an integer
count, a vector) has no such step; it gets a prior proposal.
sourceraw docstring

tempered-scoreclj/s

(tempered-score beta)

The log target of a trace entry at temperature beta, for entries recorded under any temperature: observations and factors by their :log-lik.

The log target of a trace entry at temperature `beta`, for entries recorded
under any temperature: observations and factors by their `:log-lik`.
sourceraw docstring

uniform-siteclj/s

(uniform-site trace _iteration)

Select one latent site uniformly. A selection is {:targets #{address} :log-selection (fn [trace])}.

Select one latent site uniformly. A selection is
{:targets #{address} :log-selection (fn [trace])}.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close