uqtils.mcmc
Module for Markov-Chain Monte Carlo routines.
Includes:
normal_pdf
- vectorized Gaussian pdf evaluationnormal_sample
- vectorized Gaussian samplingis_positive_definite
- whether a matrix is positive semi-definitenearest_positive_definite
- finds the nearest PSD matrixdram
- Delayed rejection adaptive Metropolis-Hastings MCMCautocorrelation
- computes the autocorrelation of a set of samples
autocorrelation(samples, maxlag=100, step=1)
Compute the auto-correlation of a set of samples.
PARAMETER | DESCRIPTION |
---|---|
samples
|
|
maxlag
|
maximum distance to compute the correlation for
DEFAULT:
|
step
|
step between distances from 0 to
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
lags, autos, tau, ess - the lag times, auto-correlations, integrated auto-correlation, and effective sample sizes |
Source code in src/uqtils/mcmc.py
dram(logpdf, x0, niter, cov0=None, gamma=0.5, eps=1e-06, adapt_after=100, adapt_interval=10, delayed=True, progress=True, filename=None)
Delayed adaptive metropolis-hastings MCMC with a Gaussian proposal.
PARAMETER | DESCRIPTION |
---|---|
logpdf
|
log PDF function of target distribution
|
x0
|
|
cov0
|
DEFAULT:
|
niter
|
number of iterations
|
gamma
|
scale factor for the covariance matrix for delayed rejection step
DEFAULT:
|
eps
|
small constant for making sure covariance is well-conditioned
DEFAULT:
|
adapt_after
|
the number of iterations before covariance adaptation begins (ignored if <=0)
DEFAULT:
|
adapt_interval
|
the number of iterations between each covariance adaptation (ignored if
DEFAULT:
|
delayed
|
whether to try to sample again after first rejection
DEFAULT:
|
progress
|
whether to display progress of the sampler
DEFAULT:
|
filename
|
if specified, an hdf5 file to save results to. If the file already has dram results, the new samples will be appended. Follows the same format as the
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
|
Source code in src/uqtils/mcmc.py
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 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 |
|
is_positive_definite(A)
nearest_positive_definite(A)
Find the nearest positive-definite matrix to input.
A Python port of John D'Errico's nearestSPD
MATLAB code [1], which credits [2].
[1] https://www.mathworks.com/matlabcentral/fileexchange/42885-nearestspd
[2] N.J. Higham, "Computing a nearest symmetric positive semidefinite matrix", 1988
Source code in src/uqtils/mcmc.py
normal_pdf(x, mean, cov, logpdf=False)
Compute the Gaussian pdf at each x
location (pretty much however you want).
PARAMETER | DESCRIPTION |
---|---|
x
|
TYPE:
|
mean
|
TYPE:
|
cov
|
TYPE:
|
logpdf
|
whether to return the logpdf instead
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|
Source code in src/uqtils/mcmc.py
normal_sample(mean, cov, size=(), sqrt=False)
Generic batch sample multivariate normal distributions (pretty much however you want).
Note
The provided mean
and cov
should match along the last dimension, that is the dimension of the random
variables to sample. If you want to sample a 1d Gaussian, then you can specify both the mean and covariance
as scalars. However, as long as the mean and covariance are broadcastable in size, then you can use this
function however you want, (i.e. sample many multivariate distributions at once, all with different means
and covariances, etc., just get creative)
PARAMETER | DESCRIPTION |
---|---|
mean
|
TYPE:
|
cov
|
TYPE:
|
size
|
shape of additional samples
TYPE:
|
sqrt
|
whether
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|