GMRF
Reference: Rue, Held 2005 Gaussian Markov Random Fields
Helper functions for sampling and dealing with Multivariate normal distributions defined by precision matrices which avoid the need for direct inversion and efficiently reuse cholesky factorisations with sparse implementations
Notation: b: conditional mean Q: precision matrix L: lower triangle cholesky factorisation of a precision matrix Q
sample_normal(mu, Q=None, L=None, n=1)
Generate multivariate random variables from a precision matrix Q using lower cholesky factorisation to get L.
Note: sparse_linalg.spsolve_triangular compared to sparse_linalg.spsolve, and it appears to be much slower. Algorithm 2.4 from Rue, Held 2005 Gaussian Markov Random Fields Sampling x ~ N(mu , Q^-1) 1: Compute the lower Cholesky factorisation, Q = L @ L' 2: Sample z ~ N(0, I) 3: Solve L' v = z 4: Compute x = z + v 5: Return x Args: mu (np.array): p x 1 mean Q (np.array, optional): p x p for precision matrix. Defaults to None. L (np.array, optional): p x p for lower triangular cholesky factorisation of precision matrix. Defaults to None. n (int, optional): number of samples. Defaults to 1.
Returns:
Type | Description |
---|---|
array
|
p x n random normal values |
Source code in src/openmcmc/gmrf.py
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 |
|
sample_truncated_normal(mu, Q=None, L=None, lower=None, upper=None, n=1, method='Gibbs')
Sample from multivariate truncated normal using either rejection sampling or Gibbs sampling.
Gibbs sampling should be faster but is generated through a markov chain so samples may not be completely independent The Markov chain is set up for sampling from gibbs_canonical_truncated_normal which is thinned by every 10 observations to get more i.i.d. samples
Rejection sampling will work well for low dimensions and low amounts of truncated but will scale very poorly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu |
array
|
p x 1 mean |
required |
Q |
array
|
p x p for precision matrix. Defaults to None. |
None
|
L |
array
|
p x p for lower triangular cholesky factorisation of precision matrix. Defaults to None. |
None
|
lower |
array
|
lower bound |
None
|
upper |
array
|
upper bound |
None
|
n |
int
|
number of samples. Defaults to 1. |
1
|
method |
str
|
defines method to use for TN sampling Either 'Gibbs' or 'Rejection' Defaults to 'Gibbs'. |
'Gibbs'
|
Returns:
Type | Description |
---|---|
array
|
p x n random truncated normal values |
Source code in src/openmcmc/gmrf.py
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 |
|
sample_truncated_normal_rejection(mu, Q=None, L=None, lower=None, upper=None, n=1)
Sample from multivariate truncated normal using rejection sampling.
Rejection sampling will work well for low dimensions and low amounts of truncated but will scale very poorly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu |
array
|
p x 1 mean |
required |
Q |
array
|
p x p for precision matrix. Defaults to None. |
None
|
L |
array
|
p x p for lower triangular cholesky factorisation of precision matrix. Defaults to None. |
None
|
lower |
array
|
lower bound |
None
|
upper |
array
|
upper bound |
None
|
n |
int
|
number of samples. Defaults to 1. |
1
|
Returns:
Type | Description |
---|---|
array
|
p x n random truncated normal values |
Source code in src/openmcmc/gmrf.py
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 |
|
sample_normal_canonical(b, Q=None, L=None)
Generate multivariate random variables canonical representation precision matrix using cholesky factorisation.
Algorithm 2.5 from Rue, Held 2005 Gaussian Markov Random Fields: Sampling x ~ N( Q^-1 b, Q^-1) 1: Compute the Cholesky factorisation, Q = L @ L' 2: Solve L w = b 3: Solve L' mu = w 4: Sample z ~ N(0; I) 5: Solve L' v = z 6: Compute x = mu + v 7: Return x
Steps 2 and 3 are done in the function cho_solve and the output is thus mu. Steps 4, 5 and 6 are the algorithm 2.5 implemented in the function sample_normal
Parameters:
Name | Type | Description | Default |
---|---|---|---|
b |
ndarray
|
p x 1 conditional mean |
required |
Q |
ndarray
|
p x p for precision matrix. Defaults to None. |
None
|
L |
ndarray
|
p x p for lower triangular cholesky factorisation of precision matrix. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
p x 1 random normal values |
Source code in src/openmcmc/gmrf.py
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 |
|
gibbs_canonical_truncated_normal(b, Q, x, lower=-np.inf, upper=np.inf)
Generate truncated multivariate random variables from a precision matrix Q using lower cholesky factorisation to get L based on current state x using Gibbs sampling.
subject to linear inequality constraints lower < X < upper
Lemma 2.1 from Rue, Held 2005 Gaussian Markov Random Fields Sampling x ~ N_c( Q^-1 b , Q^-1) x_a | x_b ~ N_c( b_a - Q_ab x_b, Q_aa)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
b |
array
|
p x 1 mean |
required |
Q |
array
|
p x p for precision matrix. Defaults to None. |
required |
x |
array
|
p x 1 current state. |
required |
lower |
array
|
p x 1 lower bound for each dimension |
-inf
|
upper |
array
|
p x 1 upper bound for each dimension |
inf
|
Returns:
Type | Description |
---|---|
array
|
p x 1 random normal values |
Source code in src/openmcmc/gmrf.py
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 |
|
truncated_normal_rv(mean, scale, lower, upper, size=1)
Wrapper for scipy.stats.truncnorm.rvs handles cases a, b not standard form.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mean |
array
|
p x 1 mean for each dimension |
required |
scale |
array
|
p x 1 standard deviation for each dimension |
required |
lower |
array
|
p x 1 lower bound for each dimension |
required |
upper |
array
|
p x 1 upper bound for each dimension |
required |
size |
int
|
size of output array default = 1 |
1
|
Returns:
Type | Description |
---|---|
ndarray
|
size x 1 truncated normal samples |
Source code in src/openmcmc/gmrf.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
truncated_normal_log_pdf(x, mean, scale, lower, upper)
Wrapper for scipy.stats.truncnorm.logpdf handles cases a, b not standard form.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
ndarray
|
values |
required |
mean |
ndarray
|
mean |
required |
scale |
ndarray
|
standard deviation |
required |
lower |
ndarray
|
lower bound |
required |
upper |
ndarray
|
upper bound |
required |
Returns:
Type | Description |
---|---|
ndarray
|
truncated normal sample |
Source code in src/openmcmc/gmrf.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|
multivariate_normal_pdf(x, mu, Q, by_observation=False)
Compute diagonalized log-pdf of a multivariate Gaussian distribution in terms of the precision matrix, can take sparse precision matrix inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
ndarray
|
dim x n value for the distribution response. where dim is the number of dimensions and n is the number of observations |
required |
mu |
ndarray
|
dim x 1 distribution mean vector. |
required |
Q |
(ndarray, csc_matrix)
|
dim x dim distribution precision matrix can be sparse or np.array |
required |
by_observation |
bool
|
indicates whether we should sum over observations default= False |
False
|
Returns:
Type | Description |
---|---|
ndarray
|
log-pdf of the Gaussian distribution either: (1,) if by_observation = False or (n,) if by_observation = True |
Source code in src/openmcmc/gmrf.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
|
precision_temporal(time, unit_length=1.0, is_sparse=True)
Generate temporal difference penalty matrix.
Details can be found on pages 97-99 of 'Gaussian Markov Random Fields' [Rue, Held 2005], 'The first-order random walk for irregular locations'.
Converts time to number of seconds then call precision_irregular
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time |
DatetimeArray
|
vector of times |
required |
unit_length |
float
|
numbers seconds to define unit difference Defaults to 1 second |
1.0
|
is_sparse |
bool
|
Flag if generated as sparse. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
P |
Union[ndarray, csc_matrix]
|
un-scaled precision matrix |
Source code in src/openmcmc/gmrf.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
|
precision_irregular(s, is_sparse=True)
Generate penalty matrix from irregular observations using first order random walk.
Details can be found on pages 97-99 of 'Gaussian Markov Random Fields' [Rue, Held 2005], 'The first-order random walk for irregular locations'.
Diagonal and off-diagonal elements of the precision found as follows
1/del_{i-1} + 1/del_{i}, j = i,
Q_{ij} = -1/del_{i}, j = i+1, 0, else. where del = [t_{i+1} - t_{i}]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s |
ndarray
|
vector of locations. |
required |
is_sparse |
bool
|
Flag if generated as sparse. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
P |
Union[np.ndarray, sparse.csc_matrix]
|
un-scaled precision matrix |
Source code in src/openmcmc/gmrf.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
|
solve(a, b)
Solve a linear matrix equation, or system of linear scalar equations.
Computes the “exact” solution, x, of the well-determined, i.e., full rank, linear matrix equation ax = b.
If inputs are sparse calls scipy.linalg.spsolve else calls np.linalg.solve
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a |
Union[ndarray, csc_matrix]
|
description |
required |
b |
Union[ndarray, csc_matrix]
|
description |
required |
Returns Union(np.ndarray, sparse.csc_matrix) solution to the system in same format as input
Source code in src/openmcmc/gmrf.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
|
cho_solve(c_and_lower, b)
Solve the linear equations A x = b, given the Cholesky factorization of A.
If inputs are sparse calls sparse solvers otherwise uses scipy.linalg.cho_solve
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c_and_lower |
tuple(Union(np.ndarray, sparse.csc_matrix), bool
|
Cholesky factorization of A and flag for if it is a lower Cholesky |
required |
b |
Union(np.ndarray, sparse.csc_matrix
|
Right-hand side |
required |
Returns (Union(np.ndarray, sparse.csc_matrix)) The solution to the system A x = b
Source code in src/openmcmc/gmrf.py
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
|
cholesky(Q, lower=True)
Compute Cholesky factorization of input matrix.
If it is sparse will use gmf.sparse_cholesky otherwise will use linalg.cholesky
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Q |
Union[ndarray, csc_matrix]
|
precision matrix, for factorization |
required |
lower |
bool
|
flag for lower triangular matrix, default is true |
True
|
Returns (Union[np.ndarray, sparse.csc_matrix]: Cholesky factorization of the input in the same format as the input
Source code in src/openmcmc/gmrf.py
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
|
sparse_cholesky(Q)
Compute sparse Cholesky factorization of input matrix.
Uses the scipy.sparse functionality for LU decomposition, and converts to Cholesky factorization. Approach taken from: https://gist.github.com/omitakahiro/c49e5168d04438c5b20c921b928f1f5d
If the sparse matrix is identified as unsuitable for Cholesky factorization, the function attempts to compute the Chol of the dense matrix instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Q |
csc_matrix
|
sparse precision matrix, for factorization |
required |
Returns:
Type | Description |
---|---|
csc_matrix
|
Cholesky factorization of the input |
Source code in src/openmcmc/gmrf.py
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 |
|