cds2.montecarlo
Seeded Monte Carlo estimation on numpy.random.
Seeded Monte Carlo estimation routines built on numpy.random.
MCMCResult
dataclass
Markov-chain sample set plus diagnostics.
Source code in src/cds2/montecarlo.py
27 28 29 30 31 32 | |
pi_estimate(n=100000, seed=None)
Estimate pi by uniform sampling inside the unit square.
Source code in src/cds2/montecarlo.py
35 36 37 38 39 40 41 42 43 | |
mc_integrate(func, a, b, n=100000, seed=None)
Monte Carlo estimate of the oriented integral of func from a to b.
Source code in src/cds2/montecarlo.py
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 | |
mc_expectation(func, sampler, n=100000, seed=None)
Monte Carlo expectation E[func(X)] where sampler(rng, n) draws X.
Source code in src/cds2/montecarlo.py
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 | |
hit_or_miss(func, a, b, y_max, n=100000, seed=None)
Hit-or-miss area estimate for 0 <= func(x) <= y_max on [a, b].
Source code in src/cds2/montecarlo.py
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 | |
metropolis_hastings(log_prob, initial, n_samples=10000, burn_in=1000, proposal_scale=1.0, thin=1, seed=None)
Metropolis-Hastings sampler for an unnormalized log density.
Source code in src/cds2/montecarlo.py
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 | |
parallel_mc_integrate(func, a, b, n_total=4000000, workers=None, seed=None)
Chunked Monte Carlo integration using exactly n_total samples.
The sample budget is split into a fixed number of chunks independent of
workers, and each chunk is seeded by its global index. With a fixed
seed the estimate is therefore identical for every worker count and on
every CPU topology; workers only controls how many run in parallel.
Source code in src/cds2/montecarlo.py
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 | |
split_rhat(chains)
Split-R-hat convergence diagnostic (Gelman et al., BDA3, ch. 11).
Each chain is split in half (an odd final draw is dropped), then the
potential scale reduction factor sqrt(var_plus / W) is computed from
the between-half-chain variance B and the mean within-half-chain
variance W. Values near 1 indicate the chains mix to the same
distribution; values above ~1.01 flag non-convergence. Splitting also
detects non-stationarity within a single chain (e.g. a drifting mean).
Args:
chains: (m, n) array-like of draws, or a sequence of m
equal-length 1-D chains. At least two chains of four draws each.
Raises: ValueError: fewer than two chains, invalid shape, non-finite draws, or zero within-chain variance (constant chains).
Source code in src/cds2/montecarlo.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
effective_sample_size(chains)
Bulk effective sample size (ESS) of MCMC draws (Gelman et al., BDA3).
Autocorrelation inflates the variance of MCMC averages: n correlated
draws carry the information of fewer independent ones. This estimates
m * n / tau with the integrated autocorrelation time tau built
from the initial monotone positive sequence of paired autocorrelations
(Geyer's truncation), using the combined within/between-chain variance
so disagreeing chains are penalised. The result is capped at the total
draw count m * n (negatively autocorrelated chains can otherwise
exceed it).
Args:
chains: (m, n) array-like of draws, a sequence of equal-length
1-D chains, or a single 1-D chain. Each chain needs four draws.
Raises: ValueError: invalid shape, non-finite draws, or constant chains (zero within-chain variance makes autocorrelation undefined).
Source code in src/cds2/montecarlo.py
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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |