uqtils
Assorted utilities for uncertainty quantification and scientific computing.
- Author - Joshua Eckels (eckelsjd.@umich.edu)
- License - GPL-3.0
Includes:
- MCMC - A standard DRAM MCMC sampler.
- Gradients - Vectorized finite-difference implementation of Jacobian and Hessians.
- Plotting - Some plotting utilities for
matplotlib
. - Sobol' - Sobol' global, variance-based sensitivity analysis.
approx_hess(func, theta, pert=0.01)
Approximate Hessian of func
at a specified theta
location using finite difference approximation.
PARAMETER | DESCRIPTION |
---|---|
func
|
expects to be called as
|
theta
|
TYPE:
|
pert
|
perturbation percent for approximate partial derivatives
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|
Source code in src/uqtils/gradient.py
approx_jac(func, theta, pert=0.01)
Approximate Jacobian of func
at a specified theta
location using finite difference approximation.
PARAMETER | DESCRIPTION |
---|---|
func
|
expects to be called as
|
theta
|
TYPE:
|
pert
|
perturbation percent for approximate partial derivatives
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|
Source code in src/uqtils/gradient.py
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
ax_default(ax, xlabel='', ylabel='', legend=None, cmap='tab10')
Nice default plt formatting for plotting X-Y data.
PARAMETER | DESCRIPTION |
---|---|
ax
|
the axes to apply these settings to
TYPE:
|
xlabel
|
the xlabel to set for
DEFAULT:
|
ylabel
|
the ylabel to set for
DEFAULT:
|
legend
|
will display a legend if bool(legend) is truthy, can pass a dict of legend kwargs here (optional)
DEFAULT:
|
cmap
|
colormap to use for cycling
DEFAULT:
|
Source code in src/uqtils/plots.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 |
|
format_input(x, ndim)
Helper function to make sure input x
is an ndarray
of shape (..., ndim)
.
PARAMETER | DESCRIPTION |
---|---|
x
|
if 1d-like as
TYPE:
|
ndim
|
the dimension of the inputs
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[bool, ndarray]
|
|
Source code in src/uqtils/uq_types.py
is_positive_definite(A)
ishigami(x, a=7.0, b=0.1)
For testing Sobol indices: Ishigami function
ndscatter(samples, labels=None, tick_fmts=None, plot1d=None, plot2d='scatter', cmap='viridis', bins=20, cmin=0, z=None, cb_label=None, cb_norm='linear', subplot_size=3, cov_overlay=None)
Triangle scatter plots of n-dimensional samples.
Warning
Best for dim < 10
. You can shrink the subplot_size
to assist graphics loading time.
PARAMETER | DESCRIPTION |
---|---|
samples
|
TYPE:
|
labels
|
list of axis labels of length
TYPE:
|
tick_fmts
|
list of str.format() specifiers for ticks, e.g
TYPE:
|
plot1d
|
'hist' or 'kde' for 1d marginals, defaults to plot2d if None
TYPE:
|
plot2d
|
'hist' for 2d hist plot, 'kde' for kernel density estimation, 'hex', or 'scatter' (default)
TYPE:
|
cmap
|
the matplotlib string specifier of a colormap
DEFAULT:
|
bins
|
number of bins in each dimension for histogram marginals
DEFAULT:
|
cmin
|
the minimum bin count below which the bins are not displayed
DEFAULT:
|
z
|
TYPE:
|
cb_label
|
label for color bar (if
DEFAULT:
|
cb_norm
|
DEFAULT:
|
subplot_size
|
size in inches of a single 2d marginal subplot
DEFAULT:
|
cov_overlay
|
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
the |
Source code in src/uqtils/plots.py
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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
|
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
|
|
Source code in src/uqtils/mcmc.py
plot_slice(funs, bds, x0=None, x_idx=None, y_idx=None, N=50, random_walk=False, xlabels=None, ylabels=None, cmap='viridis', fun_labels=None)
Helper function to plot 1d slices of a function(s) over inputs.
PARAMETER | DESCRIPTION |
---|---|
funs
|
function callable as
|
bds
|
list of tuples of
TYPE:
|
x0
|
the default values for all inputs; defaults to middle of
TYPE:
|
x_idx
|
list of input indices to take 1d slices of
TYPE:
|
y_idx
|
list of output indices to plot 1d slices of
TYPE:
|
N
|
the number of points to take in each 1d slice
TYPE:
|
random_walk
|
whether to slice in a random d-dimensional direction or hold all params const while slicing
TYPE:
|
xlabels
|
list of labels for the inputs
TYPE:
|
ylabels
|
list of labels for the outputs
TYPE:
|
cmap
|
the name of the matplotlib colormap to use
DEFAULT:
|
fun_labels
|
the legend labels if plotting multiple functions on each plot
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
|
Source code in src/uqtils/plots.py
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 |
|
sobol_sa(model, sampler, num_samples, qoi_idx=None, qoi_labels=None, param_labels=None, plot=False, verbose=True, cmap='viridis', compute_s2=False)
Perform a global Sobol' sensitivity analysis.
PARAMETER | DESCRIPTION |
---|---|
model
|
callable as
|
sampler
|
callable as
|
num_samples
|
number of samples
TYPE:
|
qoi_idx
|
list of indices of model output to report results for
TYPE:
|
qoi_labels
|
list of labels for plotting QoIs
TYPE:
|
param_labels
|
list of labels for plotting input parameters
TYPE:
|
plot
|
whether to plot bar/pie charts
TYPE:
|
verbose
|
whether to print
TYPE:
|
cmap
|
TYPE:
|
compute_s2
|
whether to compute the second order indices
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
|
Source code in src/uqtils/sobol.py
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|