File: ksmirnov.py

package info (click to toggle)
python-bumps 1.0.0b2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,144 kB
  • sloc: python: 23,941; xml: 493; ansic: 373; makefile: 209; sh: 91; javascript: 90
file content (32 lines) | stat: -rw-r--r-- 1,139 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""
Kolmogorov-Smirnov test for MCMC convergence.

Use the K-S tests to compare the distribution of values at the front of
the chain to that at the end of the chain.  If the distributions are
significantly different, then the MCMC chain has not converged.
"""

__all__ = ["ksmirnov"]

from numpy import reshape, apply_along_axis
from scipy.stats import ks_2samp


def ksmirnov(seq, portion=0.25, filter_order=15):
    """
    Kolmogorov-Smirnov test of similarity between the empirical distribution
    at the start and at the end of the chain.  Apply a median
    filter (filter=15) on neighbouring K-S values to reduce variation in
    the test statistic value.
    """
    chlen, nchains, nvars = seq.shape
    count = portion * chlen * nchains
    n = filter_order
    ks, p = apply_along_axis(lambda chain: _ksm(chain, n, count), 0, reshape(seq, (chlen * nchains, nvars)))
    return ks, p


def _ksm(chain, n, count):
    # return ks_2samp(chain[:count], chain[-count:])
    ks, p = zip(*[ks_2samp(chain[i : count + i], chain[-count - n + i : -n + i]) for i in range(n)])
    return sorted(ks)[(n - 1) // 2], sorted(p)[(n - 1) // 2]