|
| 1 | +import numpy as np |
| 2 | +import numpy.typing as npt |
| 3 | +import bayes_kit.autocorr as autocorr |
| 4 | + |
| 5 | +FloatType = np.float64 |
| 6 | +IntType = np.int64 |
| 7 | +VectorType = npt.NDArray[FloatType] |
| 8 | + |
| 9 | + |
| 10 | +def _end_pos_pairs(acor: VectorType) -> IntType: |
| 11 | + """ |
| 12 | + Return the index 1 past the last positive pair of autocorrelations |
| 13 | + starting on an even index. The sequence `acor` should contain |
| 14 | + autocorrelations from a Markov chain with values at the lag given by |
| 15 | + the index (i.e., `acor[0]` is autocorrelation at lag 0 and `acor[5]` |
| 16 | + is autocorrelation at lag 5). |
| 17 | +
|
| 18 | + The even index pairs are (0, 1), (2, 3), (4, 5), ... This function |
| 19 | + scans the pairs in order, and returns 1 plus the second index of the |
| 20 | + last such pair that has a positive sum. |
| 21 | +
|
| 22 | + Examples: |
| 23 | + ```python |
| 24 | + _end_pos_pairs([]) = 0 |
| 25 | + _end_pos_pairs([1]) = 0 |
| 26 | + _end_pos_pairs([1, 0.4]) = 2 |
| 27 | + _end_pos_pairs([1, -0.4]) = 2 |
| 28 | + _end_pos_pairs([1, -0.5, 0.25, -0.3]) == 2 |
| 29 | + _end_pos_pairs([1, -0.5, 0.25, -0.1]) == 4 |
| 30 | + _end_pos_pairs([1, -0.5, 0.25, -0.3, 0.05]) == 2 |
| 31 | + _end_pos_pairs([1, -0.5, 0.25, -0.1, 0.05]) == 4 |
| 32 | + ``` |
| 33 | +
|
| 34 | + Parameters: |
| 35 | + acor (VectorType): Input sequence of autocorrelations at lag given by index. |
| 36 | +
|
| 37 | + Returns: |
| 38 | + The index 1 past the last positive pair of values starting on an even index. |
| 39 | + """ |
| 40 | + N = len(acor) |
| 41 | + n = 0 |
| 42 | + while n + 1 < N: |
| 43 | + if acor[n] + acor[n + 1] < 0: |
| 44 | + return n |
| 45 | + n += 2 |
| 46 | + return n |
| 47 | + |
| 48 | + |
| 49 | +def iat_ipse(chain: VectorType) -> FloatType: |
| 50 | + """ |
| 51 | + Return an estimate of the integrated autocorrelation time (IAT) |
| 52 | + of the specified Markov chain using the initial positive sequence |
| 53 | + estimator (IPSE). |
| 54 | +
|
| 55 | + The integrated autocorrelation time of a chain is defined to be |
| 56 | + the sum of the autocorrelations at every lag (positive and negative). |
| 57 | + If `autocorr[n]` is the autocorrelation at lag `n`, then |
| 58 | +
|
| 59 | + ``` |
| 60 | + IAT = SUM_{n in Z} autocorr[n], |
| 61 | + ``` |
| 62 | +
|
| 63 | + where `Z = {..., -2, -1, 0, 1, 2, ...}` is the set of integers. |
| 64 | +
|
| 65 | + Because the autocorrelations are symmetric, `autocorr[n] == autocorr[-n]` and |
| 66 | + `autocorr[0] = 1`, if we double count the non-negative entries, we will have |
| 67 | + counted `autocorr[0]`, which is 1, twice, so we subtract 1, to get |
| 68 | +
|
| 69 | + ``` |
| 70 | + IAT = -1 + 2 * SUM_{n in Nat} autocorr[n], |
| 71 | + ``` |
| 72 | +
|
| 73 | + where `Nat = {0, 1, 2, ...}` is the set of natural numbers. |
| 74 | +
|
| 75 | + References: |
| 76 | + Geyer, Charles J. 2011. “Introduction to Markov Chain Monte Carlo.” |
| 77 | + In Handbook of Markov Chain Monte Carlo, edited by Steve Brooks, |
| 78 | + Andrew Gelman, Galin L. Jones, and Xiao-Li Meng, 3–48. Chapman; |
| 79 | + Hall/CRC. |
| 80 | +
|
| 81 | + Parameters: |
| 82 | + chain: A Markov chain. |
| 83 | +
|
| 84 | + Return: |
| 85 | + An estimate of the integrated autocorrelation time (IAT) for the specified chain. |
| 86 | +
|
| 87 | + Raises: |
| 88 | + ValueError: if there are fewer than 4 elements in the chain |
| 89 | + """ |
| 90 | + if len(chain) < 4: |
| 91 | + raise ValueError(f"ess requires len(chains) >= 4, but {len(chain)=}") |
| 92 | + acor = autocorr(chain) |
| 93 | + n = _end_pos_pairs(acor) |
| 94 | + return 2 * acor[0:n].sum() - 1 |
| 95 | + |
| 96 | + |
| 97 | +def iat_imse(chain: VectorType) -> FloatType: |
| 98 | + """ |
| 99 | + Return an estimate of the integrated autocorrelation time (IAT) |
| 100 | + of the specified Markov chain using the initial monotone sequence |
| 101 | + estimator (IMSE). |
| 102 | +
|
| 103 | + The IMSE imposes a monotonic downward condition on the sum of pairs, |
| 104 | + replacing each sum with the minimum of the sum and the minimum of |
| 105 | + the previous sums. |
| 106 | +
|
| 107 | + References: |
| 108 | + Geyer, C.J., 1992. Practical Markov chain Monte Carlo. Statistical Science |
| 109 | + 7(4):473--483. |
| 110 | +
|
| 111 | + Geyer, Charles J. 2011. “Introduction to Markov Chain Monte Carlo.” |
| 112 | + In Handbook of Markov Chain Monte Carlo, edited by Steve Brooks, |
| 113 | + Andrew Gelman, Galin L. Jones, and Xiao-Li Meng, 3–48. Chapman; |
| 114 | + Hall/CRC. |
| 115 | +
|
| 116 | + Parameters: |
| 117 | + chain: A Markov chain. |
| 118 | +
|
| 119 | + Return: |
| 120 | + An estimate of integrated autocorrelation time (IAT) for the specified chain. |
| 121 | +
|
| 122 | + Throws: |
| 123 | + ValueError: If there are fewer than 4 elements in the chain. |
| 124 | + """ |
| 125 | + if len(chain) < 4: |
| 126 | + raise ValueError(f"iat requires len(chains) >=4, but {len(chain) = }") |
| 127 | + acor = autocorr(chain) |
| 128 | + n = _end_pos_pairs(acor) |
| 129 | + prev_min = acor[0] + acor[1] |
| 130 | + acor_sum = prev_min |
| 131 | + i = 2 |
| 132 | + while i + 1 < n: |
| 133 | + # enforce monotone downward condition (slow loop) |
| 134 | + prev_min = min(prev_min, acor[i] + acor[i + 1]) |
| 135 | + acor_sum += prev_min |
| 136 | + i += 2 |
| 137 | + return 2 * acor_sum - 1 |
| 138 | + |
| 139 | + |
| 140 | +def iat(chain: VectorType) -> FloatType: |
| 141 | + """ |
| 142 | + Return an estimate of the integrated autocorrelation time (IAT) |
| 143 | + of the specified Markov chain. Evaluated by delegating to the |
| 144 | + initial monotone sequence estimator, `iat_imse(chain)`. |
| 145 | +
|
| 146 | + The IAT can be less than one in cases where the Markov chain is |
| 147 | + anti-correlated. |
| 148 | +
|
| 149 | + Parameters: |
| 150 | + chain: A Markov chain. |
| 151 | +
|
| 152 | + Return: |
| 153 | + The integrated autocorrelation time (IAT) for the specified chain. |
| 154 | +
|
| 155 | + Throws: |
| 156 | + ValueError: If there are fewer than 4 elements in the chain. |
| 157 | + """ |
| 158 | + return iat_imse(chain) |
0 commit comments