PosteriorPrior¶
Convert a previous posterior into a prior for the next sequential update step.
Works with all samplers — the object implements the same callable(theta) -> float
interface as any log_prior function.
Quick start¶
# Step 1: initial run with a flat prior
result1 = mc.DRAM(n_samples=20_000, initial_cov=np.eye(2)).run(problem1, x0=[10.0, 8.0])
# Step 2: use the posterior as prior — Gaussian approximation
prior2 = result1.as_prior(method='gaussian', discard=2000)
problem2 = mc.Problem(prior=prior2, likelihood=ll_new, param_names=["k1", "k2"])
result2 = mc.DRAM(n_samples=20_000, initial_cov=prior2.cov).run(
problem2, x0=prior2.mean
)
# Step 3: repeat as new data arrives
prior3 = result2.as_prior(method='gaussian', discard=2000)
Constructing from samples directly¶
Methods¶
| Method | When to use |
|---|---|
'gaussian' |
Fast, any dimension. Works well when posterior is unimodal and roughly elliptical — common in structural model updating. |
'kde' |
Non-parametric. Handles multimodal / skewed posteriors. Expensive above ~8 parameters. |
Using with TMCMC¶
TMCMC also needs samples from the prior (not just density evaluations).
Use prior.sample(n):
prior2 = result1.as_prior(method='gaussian', discard=2000)
result2_tmcmc = mc.TMCMC(n_particles=1000).run(
problem2,
prior_samples=prior2.sample(1000),
)
Initial covariance hint¶
prior.cov returns the sample covariance, which is a good starting value for
adaptive samplers:
PosteriorPrior ¶
Represent a previous posterior as a prior for sequential Bayesian updating.
Wraps a set of posterior samples and exposes a callable log_prior
interface compatible with all mcmckit samplers. Also provides a
sample(n) method for use with TMCMC (which requires explicit prior
samples).
Two density estimation methods are supported:
Gaussian (method='gaussian')
Fits a multivariate normal :math:\mathcal{N}(\mu, \Sigma) to the
samples. Fast and exact in any dimension. Works well when the
posterior is approximately unimodal and elliptical — common in
structural model updating.
.. math::
\ln p(\theta) = -\tfrac{1}{2}(\theta-\mu)^T\Sigma^{-1}(\theta-\mu)
- \tfrac{d}{2}\ln(2\pi) - \tfrac{1}{2}\ln|\Sigma|
KDE (method='kde')
Fits a non-parametric kernel density estimate using
scipy.stats.gaussian_kde with Scott's bandwidth rule. More
general: handles multimodal, skewed, or otherwise non-Gaussian
posteriors. Evaluation cost scales with the number of samples and
is expensive above ~8 parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
(ndarray, shape(n_samples, n_params))
|
Posterior samples (already discarded / thinned as desired). |
required |
method
|
('gaussian', 'kde')
|
Density estimation method. No default is provided — you must choose explicitly. |
'gaussian'
|
regularise
|
float
|
Small diagonal jitter added to the sample covariance before inversion (Gaussian mode only). Prevents singular covariance for near-deterministic parameters. Default 1e-8. |
1e-08
|
Examples:
Gaussian approximation (any dimension)::
prior2 = mc.PosteriorPrior(result1.discard(1000).samples, method='gaussian')
problem2 = mc.Problem(prior=prior2, likelihood=ll_new)
result2 = mc.DRAM(n_samples=20_000, initial_cov=prior2.cov).run(
problem2, x0=prior2.mean
)
KDE (recommended for ≤ 6–8 parameters)::
prior2 = mc.PosteriorPrior(result1.discard(1000).samples, method='kde')
Use with TMCMC (requires samples from the prior)::
result2 = mc.TMCMC(n_particles=1000).run(
problem2, prior_samples=prior2.sample(1000)
)
Convenience shorthand via Result::
prior2 = result1.as_prior(method='gaussian', discard=1000)
Source code in mcmckit/core/sequential.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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 | |
Attributes¶
cov
property
¶
Sample covariance matrix, shape (n_params, n_params).
For the Gaussian method this is the regularised covariance used
internally. Useful as initial_cov for the next sampler.
Methods:¶
__call__ ¶
Evaluate the log-prior at theta.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
(array - like, shape(n_params))
|
|
required |
Returns:
| Type | Description |
|---|---|
float
|
|
Source code in mcmckit/core/sequential.py
sample ¶
Draw n samples from the approximate prior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of samples to draw. |
required |
rng
|
Generator or None
|
Optional random generator for reproducibility. |
None
|
Returns:
| Type | Description |
|---|---|
(ndarray, shape(n, n_params))
|
|