Skip to content

markov

Markov sequences and Markov processes.

MarkovSeq ¤

Bases: NamedTuple

Markov sequence.

Source code in probdiffeq/solvers/markov.py
 9
10
11
12
13
class MarkovSeq(containers.NamedTuple):
    """Markov sequence."""

    init: Any
    conditional: Any

marginals(markov_seq: MarkovSeq, *, reverse) ¤

Extract the (time-)marginals from a Markov sequence.

Source code in probdiffeq/solvers/markov.py
88
89
90
91
92
93
94
95
96
97
98
def marginals(markov_seq: MarkovSeq, *, reverse):
    """Extract the (time-)marginals from a Markov sequence."""
    _assert_filtering_solution_removed(markov_seq)

    def step(x, cond):
        extrapolated = impl.conditional.marginalise(x, cond)
        return extrapolated, extrapolated

    init, xs = markov_seq.init, markov_seq.conditional
    _, marg = control_flow.scan(step, init=init, xs=xs, reverse=reverse)
    return marg

rescale_cholesky(markov_seq: MarkovSeq, factor) -> MarkovSeq ¤

Rescale the Cholesky factor of the covariance of a Markov sequence.

Source code in probdiffeq/solvers/markov.py
66
67
68
69
70
def rescale_cholesky(markov_seq: MarkovSeq, factor) -> MarkovSeq:
    """Rescale the Cholesky factor of the covariance of a Markov sequence."""
    init = impl.variable.rescale_cholesky(markov_seq.init, factor)
    cond = _rescale_cholesky_conditional(markov_seq.conditional, factor)
    return MarkovSeq(init=init, conditional=cond)

sample(key, markov_seq: MarkovSeq, *, shape, reverse) ¤

Sample from a Markov sequence.

Source code in probdiffeq/solvers/markov.py
16
17
18
19
20
21
22
23
24
25
def sample(key, markov_seq: MarkovSeq, *, shape, reverse):
    """Sample from a Markov sequence."""
    _assert_filtering_solution_removed(markov_seq)
    # A smoother samples on the grid by sampling i.i.d values
    # from the terminal RV x_N and the backward noises z_(1:N)
    # and then combining them backwards as
    # x_(n-1) = l_n @ x_n + z_n, for n=1,...,N.
    markov_seq_shape = _sample_shape(markov_seq)
    base_samples = random.normal(key, shape=shape + markov_seq_shape)
    return _transform_unit_sample(markov_seq, base_samples, reverse=reverse)

select_terminal(markov_seq: MarkovSeq) -> MarkovSeq ¤

Discard all intermediate filtering solutions from a Markov sequence.

This function is useful to convert a smoothing-solution into a Markov sequence that is compatible with sampling or marginalisation.

Source code in probdiffeq/solvers/markov.py
78
79
80
81
82
83
84
85
def select_terminal(markov_seq: MarkovSeq) -> MarkovSeq:
    """Discard all intermediate filtering solutions from a Markov sequence.

    This function is useful to convert a smoothing-solution into a Markov sequence
    that is compatible with sampling or marginalisation.
    """
    init = tree_util.tree_map(lambda x: x[-1, ...], markov_seq.init)
    return MarkovSeq(init, markov_seq.conditional)