Reparameterization¶
Reparameterization rewrites a sample site in other variables with the same distribution. A location and scale transform, for instance, writes a Normal draw as a location plus a scale times a standard Normal draw (Papaspiliopoulos et al., 2007). Keeping the target density requires the transformed prior, the observation scores, and any Jacobian correction. Neural transport learns a change of coordinates meant to simplify the posterior geometry before Markov chain Monte Carlo (MCMC) (Hoffman et al., 2019).
Reparameterization strategies for individual sample sites.
reparam
¶
Reparameterisation strategies.
Reparam is the abstract strategy interface; ReparamOrchestrator
(returned by the reparam(strategies) factory) is the effect handler
that dispatches each covered site to the matching strategy. The concrete
strategies are LocScaleReparam (non-centred Normal), TransformReparam
(fixed bijector), NeuTraReparam (warp geometry through a trained
autoguide), and ConjugateReparam (analytic marginalisation for
conjugate pairs).
See the individual module docstrings for citations to the original literature: Betancourt and Girolami 2015 for the non-centred rewrite, Hoffman et al. 2019 for NeuTra, and Bernardo and Smith 1994 for the conjugate pairs.
Reparam
¶
Bases: ABC
Base class for site-level reparameterisation strategies.
A strategy is a policy, not a handler. The ReparamOrchestrator
handler dispatches each covered site to the matching strategy and
installs the strategy's value and density on the site. Subclasses
override :meth:apply.
begin_run
¶
Start a run of the program.
The ReparamOrchestrator calls this once per run, before the
run's first site, for every strategy it holds. A strategy whose
sites share one draw within a run (NeuTraReparam) discards the
previous run's draw here; a strategy without per-run state has
nothing to do, which this default states.
Source code in src/quivers/effects/reparam/base.py
apply
abstractmethod
¶
apply(site: SiteRequest) -> tuple[Tensor, Tensor]
Rewrite one site.
| PARAMETER | DESCRIPTION |
|---|---|
site
|
The site, with a value fixed when one is given.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
The value the site takes, in the site's original space so downstream sites see the intended value, and the density a downstream sampler should score the site with. |
Source code in src/quivers/effects/reparam/base.py
ReparamOrchestrator
¶
ReparamOrchestrator(strategies: dict[str, Reparam])
Bases: EffectHandler
Dispatch sample sites to per-site reparam strategies.
| PARAMETER | DESCRIPTION |
|---|---|
strategies
|
Site name to strategy.
TYPE:
|
Source code in src/quivers/effects/reparam/base.py
install
¶
install(run: RunContext) -> tuple[Installation, ...]
Install the dispatching handler of the random instance.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
The handler, which answers covered sites and forwards the rest. |
Source code in src/quivers/effects/reparam/base.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
SiteRequest
didactic-model
¶
SiteRequest(*, name: str, sampleable: TorchSampleable = field(opaque=True, extras=OPAQUE_BY_VALUE), given: Tensor | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY), seen: Mapping[str, TorchSampleable] = field(default_factory=dict, opaque=True, extras=OPAQUE_BY_IDENTITY), observation: Tensor | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY))
Bases: ValueModel
What a strategy is given for one sample site.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The site's name.
TYPE:
|
sampleable
|
The site's distribution at its input.
TYPE:
|
given
|
A value the site is to take, when an enclosing strategy or a
caller fixed one;
TYPE:
|
seen
|
The sampleables of the sites the orchestrator saw before this one, by name, so a strategy may read a parent's parameters.
TYPE:
|
observation
|
The run's observation for the site, if any.
TYPE:
|
with_value
¶
with_value(value: Tensor) -> SiteRequest
The same request with a value fixed.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value the site is to take.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SiteRequest
|
The request with |
Source code in src/quivers/effects/reparam/base.py
ConjugateReparam
¶
Bases: Reparam
Score a child site under the conjugate marginal of its parent.
Applied to the child site of a conjugate pair, the strategy answers
the site with the run's observation for it, or a draw from the
child's own sampleable when there is none, and scores it under the
closed-form marginal that integrates the parent out, read from the
parent's parameters as the orchestrator saw them. The parent's own
density is left in the joint; collapse the parent with
quivers.effects.collapse when the marginal joint is wanted.
| PARAMETER | DESCRIPTION |
|---|---|
parent
|
The parent site's name.
TYPE:
|
parent_family
|
The parent's family.
TYPE:
|
child_family
|
The child's family.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the pair has no registered marginal. |
Source code in src/quivers/effects/reparam/conjugate.py
apply
¶
apply(site: SiteRequest) -> tuple[Tensor, Tensor]
Rewrite the child site.
| PARAMETER | DESCRIPTION |
|---|---|
site
|
The child site.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
The value and its marginal log density. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the orchestrator has not seen the parent site. |
ValueError
|
If the parent or the child is not of the declared family. |
Source code in src/quivers/effects/reparam/conjugate.py
LocScaleReparam
¶
Bases: Reparam
Non-centred rewrite for location-scale sample sites.
The site's morphism must expose a _get_params(x) method returning
(loc, scale) tensors of the same shape, the convention every
ConditionalNormal-shaped family in
quivers.continuous.families follows.
| PARAMETER | DESCRIPTION |
|---|---|
centered
|
Interpolation between fully centred (
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/effects/reparam/loc_scale.py
apply
¶
apply(site: SiteRequest) -> tuple[Tensor, Tensor]
Rewrite one site.
| PARAMETER | DESCRIPTION |
|---|---|
site
|
The site.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
The value and its log density under |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the site's morphism exposes no |
Source code in src/quivers/effects/reparam/loc_scale.py
NeuTraDraw
didactic-model
¶
NeuTraDraw(*, base: Tensor = field(opaque=True, extras=OPAQUE_BY_IDENTITY), sites: dict[str, Tensor] = field(opaque=True, extras=OPAQUE_BY_IDENTITY), log_abs_det: Tensor = field(opaque=True, extras=OPAQUE_BY_IDENTITY))
Bases: ValueModel
One run's draw of a flow guide's sites, with its change of variables.
| PARAMETER | DESCRIPTION |
|---|---|
base
|
The base point :math:
TYPE:
|
sites
|
Each covered site's constrained value at
TYPE:
|
log_abs_det
|
:math:
TYPE:
|
log_density
¶
log_density() -> Tensor
The guide's log density of the draw, by the change of variables.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
:math: |
Source code in src/quivers/effects/reparam/neutra.py
NeuTraReparam
¶
NeuTraReparam(guide: AutoNormalizingFlow)
Bases: Reparam
Reparameterise sites through a trained normalizing-flow guide.
One strategy object serves every site of the guide it is installed
on, as in reparam({"a": neutra, "b": neutra}): within a run the
sites share one draw (NeuTraDraw), taken at the first covered site
the run reaches and discarded when the next run begins
(Reparam.begin_run). A site the guide does not cover, or a site
whose value an enclosing handler fixed, is drawn or taken as given
and scored under its own density.
| PARAMETER | DESCRIPTION |
|---|---|
guide
|
A trained flow guide (
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
Source code in src/quivers/effects/reparam/neutra.py
current_draw
property
¶
current_draw: NeuTraDraw | None
The draw the current run's covered sites take.
| RETURNS | DESCRIPTION |
|---|---|
NeuTraDraw | None
|
The draw, or |
begin_run
¶
push
¶
push(base: Tensor) -> NeuTraDraw
Map a base point to the guide's sites.
| PARAMETER | DESCRIPTION |
|---|---|
base
|
The base point :math:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NeuTraDraw
|
The sites' values :math: |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/effects/reparam/neutra.py
draw
¶
draw() -> NeuTraDraw
Draw a base point from the standard Normal and push it.
| RETURNS | DESCRIPTION |
|---|---|
NeuTraDraw
|
The draw. |
Source code in src/quivers/effects/reparam/neutra.py
apply
¶
apply(site: SiteRequest) -> tuple[Tensor, Tensor]
Rewrite one site.
| PARAMETER | DESCRIPTION |
|---|---|
site
|
The site.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
The site's value, from the run's draw for a covered site whose value is not given, shaped as the site's own draws; and its log density under the site's sampleable. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a covered site's input has more than one row, since one run draws it once. |
Source code in src/quivers/effects/reparam/neutra.py
TransformReparam
¶
TransformReparam(bijector: Bijector)
Bases: Reparam
Reparameterise a sample site through a fixed bijector.
The reparameterised base sample lives in the bijector's domain and the site's value in its codomain. The joint density remains that of the original distribution: the strategy scores the value under the site's own sampleable, and the bijector reshapes the sampling geometry a downstream sampler works in.
| PARAMETER | DESCRIPTION |
|---|---|
bijector
|
The measurable bijection
TYPE:
|
Source code in src/quivers/effects/reparam/transform.py
apply
¶
apply(site: SiteRequest) -> tuple[Tensor, Tensor]
Rewrite one site.
| PARAMETER | DESCRIPTION |
|---|---|
site
|
The site.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
The value, drawn from the site's sampleable unless given, and its log density under the sampleable. |
Source code in src/quivers/effects/reparam/transform.py
reparam
¶
reparam(strategies: dict[str, Reparam]) -> ReparamOrchestrator
Return a ReparamOrchestrator for the given per-site strategies.
| PARAMETER | DESCRIPTION |
|---|---|
strategies
|
Site name to strategy.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ReparamOrchestrator
|
The handler. |
Source code in src/quivers/effects/reparam/base.py
Cited sources¶
Omiros Papaspiliopoulos, Gareth O. Roberts, and Martin Sköld. A General Framework for the Parametrization of Hierarchical Models. Statistical Science, 22(1):59–73, 2007. URL: https://projecteuclid.org/journals/statistical-science/volume-22/issue-1/A-General-Framework-for-the-Parametrization-of-Hierarchical-Models/10.1214/088342307000000014.pdf, doi:10.1214/088342307000000014.
Matthew Hoffman, Pavel Sountsov, Joshua V. Dillon, Ian Langmore, Dustin Tran, and Srinivas Vasudevan. NeuTra-lizing Bad Geometry in Hamiltonian Monte Carlo Using Neural Transport. arXiv preprint arXiv:1903.03704, 2019. URL: https://arxiv.org/abs/1903.03704, doi:10.48550/arXiv.1903.03704.