File: state.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 (1171 lines) | stat: -rw-r--r-- 43,315 bytes parent folder | download
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
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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
"""
Sampling history for MCMC.

MCMC keeps track of a number of things during sampling.

The results may be queried as follows::

    draws, generation, thinning
    sample(condition) returns draws, points, logp
    logp()            returns draws, logp
    acceptance_rate() returns draws, AR
    chains()          returns draws, chains, logp
    CR_weight()       returns draws, CR_weight
    best()            returns best_x, best_logp
    outliers()        returns outliers
    show()/save(file)/load(file)

Data is stored in circular arrays, which keeps the last N generations and
throws the rest away.

draws is the total number of draws from the sampler.

generation is the total number of generations.

thinning is the number of generations per stored sample.

draws[i] is the number of draws including those required to produce the
information in the corresponding return vector.  Note that draw numbers
need not be linearly spaced, since techniques like delayed rejection
will result in a varying number of samples per generation.

logp[i] is the set of log likelihoods, one for each member of the population.
The logp() method returns the complete set, and the sample() method returns
a thinned set, with on element of logp[i] for each vector point[i, :].

AR[i] is the acceptance rate at generation i, showing the proportion of
proposed points which are accepted into the population.

chains[i, :, :] is the set of points in the differential evolution population
at thinned generation i.  Ideally, the thinning rate of the MCMC process
is chosen so that thinned generations i and i+1 are independent samples
from the posterior distribution, though there is a chance that this may
not be the case, and indeed, some points in generation i+1 may be identical
to those in generation i.  Actual generation number is i*thinning.

points[i, :] is the ith point in a returned sample.  The i is just a place
holder; there is no inherent ordering to the sample once they have been
extracted from the chains.  Note that the sample may be from a marginal
distribution.

R[i] is the Gelman R statistic measuring convergence of the Markov chain.

CR_weight[i] is the set of weights used for selecting between the crossover
ratios available to the candidate generation process of differential
evolution.  These will be fixed early in the sampling, even when adaptive
differential evolution is selected.

outliers[i] is a vector containing the thinned generation number at which
an outlier chain was removed, the id of the chain that was removed and
the id of the chain that replaced it.  We leave it to the reader to decide
if the cloned samples, point[:generation, :, removed_id], should be included
in further analysis.

best_logp is the highest log likelihood observed during the analysis and
best_x is the corresponding point at which it was observed.

generation is the last generation number
"""

# TODO: state should be collected in files as we go

__all__ = ["MCMCDraw", "load_state", "save_state"]

import os.path
import re
import gzip

import numpy as np
from numpy import empty, sum, asarray, inf, argmax, hstack, dstack
from numpy import savetxt, reshape

from .convergence import burn_point
from .outliers import identify_outliers
from .util import draw, rng
from .gelman import gelman

EXT = ".mc.gz"
CREATE = gzip.open
# EXT = ".mc"
# CREATE = open

# CRUFT: python 2 uses bytes rather than unicode for strings
try:
    # python 2.x
    unicode

    def write(fid, s):
        fid.write(s)
except NameError:
    # python 3.x
    def write(fid, s):
        fid.write(s.encode("utf-8") if isinstance(s, str) else s)


class NoTrace:
    def write(self, data):
        pass

    def flush(self):
        pass

    def close(self):
        pass


def save_state(state, filename):
    trace = NoTrace()
    # trace = open(filename+"-trace.mc", "w")

    write(trace, "starting trace\n")
    # Build 2-D data structures
    write(trace, "extracting draws, logp\n")
    draws, logp = state.logp(full=True)
    write(trace, "extracting acceptance rate\n")
    _, AR = state.acceptance_rate()
    write(trace, "building chain from draws, AR and logp\n")
    chain = hstack((draws[:, None], AR[:, None], logp))

    write(trace, "extracting point, logp\n")
    _, point, logp = state.chains()
    Nthin, Npop, Nvar = point.shape
    write(trace, "shape is %d,%d,%d\n" % (Nthin, Npop, Nvar))
    write(trace, "adding logp to point\n")
    point = dstack((logp[:, :, None], point))
    write(trace, "collapsing to draws x point\n")
    point = reshape(point, (point.shape[0] * point.shape[1], point.shape[2]))

    write(trace, "extracting CR_weight\n")
    draws, CR_weight = state.CR_weight()
    Nupdate, Ncr = CR_weight.shape
    write(trace, "building stats\n")
    stats = hstack((draws[:, None], CR_weight))

    # TODO: missing _outliers from save_state

    # Write convergence info
    write(trace, "writing chain\n")
    fid = CREATE(filename + "-chain" + EXT, "wb")
    write(fid, "# draws acceptance_rate %d*logp\n" % Npop)
    savetxt(fid, chain)
    fid.close()

    # Write point info
    write(trace, "writing point\n")
    fid = CREATE(filename + "-point" + EXT, "wb")
    write(fid, "# logp point (Nthin x Npop x Nvar = [%d,%d,%d])\n" % (Nthin, Npop, Nvar))
    savetxt(fid, point)
    fid.close()

    # Write stats
    write(trace, "writing stats\n")
    fid = CREATE(filename + "-stats" + EXT, "wb")
    write(fid, "# draws %d*CR_weight\n" % Ncr)
    savetxt(fid, stats)
    fid.close()
    write(trace, "done state save\n")
    trace.close()


IND_PAT = re.compile("-1#IND")
INF_PAT = re.compile("1#INF")


def loadtxt(file, report=0):
    """
    Like numpy loadtxt, but adapted for windows non-finite numbers.
    """
    if not hasattr(file, "readline"):
        if file.endswith(".gz"):
            # print("opening with gzip")
            fh = gzip.open(file, "rt")
        else:
            fh = open(file, "rt")
    else:
        fh = file
    res = []
    section = 0
    lineno = 0
    for line in fh:
        lineno += 1
        if report and lineno % report == 0:
            print("read", section * report)
            section += 1
        IND_PAT.sub("nan", line)
        INF_PAT.sub("inf", line)
        line = line.split("#")[0].strip()
        values = line.split()
        if len(values) > 0:
            try:
                res.append([float(v) for v in values])
            except ValueError:
                print("Parse error:", values)
    if fh != file:
        fh.close()
    return asarray(res)


def path_contains_saved_state(filename):
    chain_file = filename + "-chain" + EXT
    return os.path.exists(chain_file)


def openmc(filename):
    if filename.endswith(".gz"):
        if os.path.exists(filename):
            # print("opening with gzip")
            fh = gzip.open(filename, "rt")
        elif os.path.exists(filename[:-3]):
            fh = open(filename[:-3], "rt")
        else:
            raise RuntimeError("file %s does not exist" % filename)
    else:
        if os.path.exists(filename):
            fh = open(filename, "rt")
        elif os.path.exists(filename + ".gz"):
            # print("opening with gzip")
            fh = gzip.open(filename + ".gz", "rt")
        else:
            raise RuntimeError("file %s does not exist" % filename)
    return fh


def load_state(filename, skip=0, report=0, derived_vars=0):
    # Read chain file
    with openmc(filename + "-chain" + EXT) as fid:
        chain = loadtxt(fid)

    # Read point file
    with openmc(filename + "-point" + EXT) as fid:
        line = fid.readline()
        point_dims = line[line.find("[") + 1 : line.find("]")]
        Nthin, Npop, Nvar = eval(point_dims)
        for _ in range(skip * Npop):
            fid.readline()
        point = loadtxt(fid, report=report * Npop)

    # Read stats file
    with openmc(filename + "-stats" + EXT) as fd:
        stats_header = fd.readline()
        stats = loadtxt(fd)

    # Determine number of R-stat stored in the stats file
    if "R-stat" in stats_header:
        # Old header looks like:
        #     # draws {Nvar}*R-stat {Ncr}*CR_weight
        # however, number of R-stat stored in stats file is the number of
        # variables stored each generation, not including the derived variables
        # calculated after the MCMC has completed.
        num_r = int(stats_header.split("*")[0].split()[-1]) - derived_vars
    else:
        num_r = 0

    # Guess dimensions
    Ngen = chain.shape[0]
    thinning = 1
    Nthin -= skip
    Nupdate = stats.shape[0]

    # Create empty draw and fill it with loaded data
    state = MCMCDraw(0, 0, 0, 0, 0, 0, thinning)
    # print("gen, var, pop", Ngen, Nvar, Npop)
    state.draws = Ngen * Npop
    state.generation = Ngen
    state._gen_index = 0
    state._gen_draws = chain[:, 0]
    state._gen_acceptance_rate = chain[:, 1]
    state._gen_logp = chain[:, 2:]
    state.thinning = thinning
    state._thin_count = Ngen // thinning
    state._thin_index = 0
    state._thin_draws = state._gen_draws[(skip + 1) * thinning - 1 :: thinning]
    state._thin_logp = point[:, 0].reshape((Nthin, Npop))
    state._thin_point = reshape(point[:, 1 : Nvar + 1 - derived_vars], (Nthin, Npop, -1))
    state._gen_current = state._thin_point[-1].copy()
    state._update_count = Nupdate
    state._update_index = 0
    state._update_draws = stats[:, 0]
    state._update_CR_weight = stats[:, 1 + num_r :]
    state._outliers = []

    bestidx = np.argmax(point[:, 0])
    state._best_logp = point[bestidx, 0]
    state._best_x = point[bestidx, 1 : Nvar + 1 - derived_vars]
    state._best_gen = 0

    return state


class MCMCDraw(object):
    """ """

    _labels = None
    _integer_vars = None  # boolean array of integer variables, or None
    title = None

    def __init__(self, Ngen, Nthin, Nupdate, Nvar, Npop, Ncr, thinning):
        # Total number of draws so far
        self.draws = 0

        # Maximum observed likelihood
        self._best_x = None
        self._best_logp = -inf
        self._best_gen = 0

        # Per generation iteration
        self.generation = 0
        self._gen_index = 0
        self._gen_draws = empty(Ngen, "i")
        self._gen_logp = empty((Ngen, Npop))
        self._gen_acceptance_rate = empty(Ngen)

        # If we are thinning, we need to keep the current generation
        # separately. [Note: don't remember why we need both the _gen_*
        # and _thin_*]  [Note: the caller x vector is assigned to
        # _gen_current; this may lead to unexpected behaviour if x is
        # changed by the caller.
        self._gen_current = None

        # Per thinned generation iteration
        self.thinning = thinning
        self._thin_index = 0
        self._thin_count = 0
        self._thin_timer = 0
        self._thin_draws = empty(Nthin, "i")
        self._thin_point = empty((Nthin, Npop, Nvar))
        self._thin_logp = empty((Nthin, Npop))

        # Per update iteration
        self._update_index = 0
        self._update_count = 0
        self._update_draws = empty(Nupdate, "i")
        self._update_CR_weight = empty((Nupdate, Ncr))

        self._outliers = []

        # Query functions will not return outlier chains; initially, all
        # chains are marked as good.  Call mark_outliers to remove
        # outlier chains from the set.
        self._good_chains = slice(None, None)

    @property
    def Ngen(self):
        return self._gen_draws.shape[0]

    @property
    def Nsamples(self):
        return self._gen_logp.size

    @property
    def Nthin(self):
        return self._thin_draws.shape[0]

    @property
    def Nupdate(self):
        return self._update_draws.shape[0]

    @property
    def Nvar(self):
        """Number of parameters in the fit"""
        return self._thin_point.shape[2]

    @property
    def Npop(self):
        return self._gen_logp.shape[1]

    @property
    def Ncr(self):
        return self._update_CR_weight.shape[1]

    def resize(self, Ngen, Nthin, Nupdate, Nvar, Npop, Ncr, thinning):
        if self.Nvar != Nvar or self.Npop != Npop or self.Ncr != Ncr:
            raise ValueError("Cannot change Nvar, Npop or Ncr on resize")

        # For now, only handle the case where the we have one complete
        # frame of data, such as on reloading the state vector
        assert self._gen_index == 0 and self._update_index == 0 and self._thin_index == 0
        assert self.generation == self.Ngen and self._update_count == self.Nupdate and self._thin_count == self.Nthin

        self.thinning = thinning

        if Ngen > self.Ngen:
            self._gen_index = self.Ngen  # must happen before resize!!
            self._gen_draws = np.resize(self._gen_draws, Ngen)
            self._gen_logp = np.resize(self._gen_logp, (Ngen, Npop))
            self._gen_acceptance_rate = np.resize(self._gen_acceptance_rate, Ngen)
        elif Ngen < self.Ngen:
            self._gen_draws = self._gen_draws[-Ngen:].copy()
            self._gen_logp = self._gen_logp[-Ngen:, :].copy()
            self._gen_acceptance_rate = self._gen_acceptance_rate[-Ngen:].copy()

        if Nthin > self.Nthin:
            self._thin_index = self.Nthin  # must happen before resize!!
            self._thin_draws = np.resize(self._thin_draws, Nthin)
            self._thin_point = np.resize(self._thin_point, (Nthin, Npop, Nvar))
            self._thin_logp = np.resize(self._thin_logp, (Nthin, Npop))
        elif Nthin < self.Nthin:
            self._thin_draws = self._thin_draws[-Nthin:].copy()
            self._thin_point = self._thin_point[-Nthin:, :, :].copy()
            self._thin_logp = self._thin_logp[-Nthin:, :].copy()

        if Nupdate > self.Nupdate:
            self._update_count = self.Nupdate  # must happen before resize!!
            self._update_draws = np.resize(self._update_draws, Nupdate)
            self._update_CR_weight = np.resize(self._update_CR_weight, (Nupdate, Ncr))
        elif Nupdate < self.Nupdate:
            self._update_draws = self._update_draws[-Nupdate:].copy()
            self._update_CR_weight = self._update_CR_weight[-Nupdate:, :].copy()

    def save(self, filename):
        save_state(self, filename)

    def trim_portion(self):
        index = burn_point(self)
        portion = 1 - (index / self.Ngen) if index >= 0 else 0.5
        return portion

    def show(self, portion=1.0, figfile=None):
        from .views import plot_all

        plot_all(self, portion=portion, figfile=figfile)

    def _last_gen(self):
        """
        Returns x, logp for most recent generation to dream.py.
        """
        # Note: if generation number has wrapped and _gen_index is 0
        # (the usual case when this function is called to resume an
        # existing chain), then this returns the last row in the array.
        return (self._thin_point[self._thin_index - 1], self._thin_logp[self._thin_index - 1])

    def _generation(self, new_draws, x, logp, accept, force_keep=False):
        """
        Called from dream.py after each generation is completed with
        a set of accepted points and their values.
        """
        # Keep track of the total number of draws
        # Note: this is first so that we tag the record with the number of
        # draws taken so far, including the current draw.
        self.draws += new_draws
        self.generation += 1

        # Record if this is the best so far
        maxid = argmax(logp)
        if logp[maxid] > self._best_logp:
            self._best_logp = logp[maxid]
            self._best_x = x[maxid, :] + 0  # Force a copy
            self._best_gen = self.generation
            # print("new best", logp[maxid], self.generation)

        # Record acceptance rate and cost
        i = self._gen_index
        # print("generation", i, self.draws, "\n x", x, "\n logp", logp, "\n accept", accept)
        self._gen_draws[i] = self.draws
        self._gen_acceptance_rate[i] = 100 * sum(accept) / new_draws
        self._gen_logp[i] = logp
        i = i + 1
        if i == len(self._gen_draws):
            i = 0
        self._gen_index = i

        # Keep every nth iteration
        self._thin_timer += 1
        if self._thin_timer == self.thinning or force_keep:
            self._thin_timer = 0
            self._thin_count += 1
            i = self._thin_index
            self._thin_draws[i] = self.draws
            self._thin_point[i] = x
            self._thin_logp[i] = logp
            i = i + 1
            if i == len(self._thin_draws):
                i = 0
            self._thin_index = i
            self._gen_current = x + 0  # force a copy
        else:
            self._gen_current = x + 0  # force a copy

    def _update(self, CR_weight):
        """
        Called from dream.py when a series of DE steps is completed and
        summary statistics/adaptations are ready to be stored.
        """
        self._update_count += 1
        i = self._update_index
        # print("update", i, self.draws, "\n CR weight", CR_weight)
        self._update_draws[i] = self.draws
        self._update_CR_weight[i] = CR_weight
        i = i + 1
        if i == len(self._update_draws):
            i = 0
        self._update_index = i

    @property
    def labels(self):
        if self._labels is None:
            return ["P%d" % i for i in range(self._thin_point.shape[2])]
        else:
            return self._labels

    @labels.setter
    def labels(self, v):
        self._labels = v

    def _draw_pop(self):
        """
        Return the current population.
        """
        return self._gen_current

    def _draw_large_pop(self, Npop):
        _, chains, _ = self.chains()
        Ngen, Nchain, Nvar = chains.shape
        points = reshape(chains, (Ngen * Nchain, Nvar))

        # There are two complications with the history buffer:
        # (1) due to thinning, not every generation is stored
        # (2) because it is circular, the cursor may be in the middle
        # If the current generation isn't in the buffer (but is instead
        # stored separately as _gen_current), then the entire buffer
        # becomes the history pool.
        # otherwise we need to exclude the current generation from
        # the pool.  If (2) happens, we need to increment everything
        # above the cursor by the number of chains.
        if self._gen_current is not None:
            pool_size = Ngen * Nchain
            cursor = pool_size  # infinite
        else:
            pool_size = (Ngen - 1) * Nchain
            k = len(self._thin_draws)
            cursor = Nchain * ((k + self._thin_index - 1) % k)

        # Make a return population and fill it with the current generation
        pop = empty((Npop, Nvar), "d")
        if self._gen_current is not None:
            pop[:Nchain] = self._gen_current
        else:
            # print(pop.shape, points.shape, chains.shape)
            pop[:Nchain] = points[cursor : cursor + Nchain]

        if Npop > Nchain:
            # Find the remainder with unique ancestors.
            # Again, because this is a circular buffer, their may be random
            # numbers generated at or above the cursor.  All of these must
            # be shifted by Nchains to avoid the cursor.
            perm = draw(Npop - Nchain, pool_size)
            perm[perm >= cursor] += Nchain
            # print("perm", perm; raw_input('wait'))
            pop[Nchain:] = points[perm]

        return pop

    def _unroll(self):
        """
        Unroll the circular queue so that data access can be done inplace.

        Call this when done stepping, and before plotting.  Calls to
        logp, sample, etc. assume the data is already unrolled.
        """
        if self.generation > self._gen_index > 0:
            self._gen_draws[:] = np.roll(self._gen_draws, -self._gen_index, axis=0)
            self._gen_logp[:] = np.roll(self._gen_logp, -self._gen_index, axis=0)
            self._gen_acceptance_rate[:] = np.roll(self._gen_acceptance_rate, -self._gen_index, axis=0)
            self._gen_index = 0

        if self._thin_count > self._thin_index > 0:
            self._thin_draws[:] = np.roll(self._thin_draws, -self._thin_index, axis=0)
            self._thin_point[:] = np.roll(self._thin_point, -self._thin_index, axis=0)
            self._thin_logp[:] = np.roll(self._thin_logp, -self._thin_index, axis=0)
            self._thin_index = 0

        if self._update_count > self._update_index > 0:
            self._update_draws[:] = np.roll(self._update_draws, -self._update_index, axis=0)
            self._update_CR_weight[:] = np.roll(self._update_CR_weight, -self._update_index, axis=0)
            self._update_index = 0

    def remove_outliers(self, x, logp, test="IQR"):
        """
        Replace outlier chains with clones of good ones.  This should happen
        early in the sampling processes so the clones have an opportunity
        to evolve their own identity.  Only the head of the chain is modified.

        *state* contains the chains, with log likelihood for each point.

        *x*, *logp* are the current population and the corresponding
        log likelihoods; these are updated with cloned chain values.

        *test* is the name of the test to use (one of IQR, Grubbs, Mahal
        or none). See :func:`.outliers.identify_outliers` for details.

        Updates *state*, *x* and *logp* to reflect the changes.

        Returns a list of the outliers that were removed.
        """
        # Grab the last part of the chain histories
        _, chains = self.logp()
        chain_len, Nchains = chains.shape
        outliers = identify_outliers(test, chains, x)
        # if len(outliers): print("old llf", logp[outliers])

        # Loop over each outlier chain, replacing each with another
        for old in outliers:
            # Draw another chain at random, with replacement
            # TODO: consider using relative likelihood as a weight factor
            while True:
                new = rng.randint(Nchains)
                if new not in outliers:
                    break
            # Update the saved state and current population
            self._replace_outlier(old=old, new=new)
            x[old, :] = x[new, :]
            logp[old] = logp[new]

        # if len(outliers): print("new llf", logp[outliers])
        return outliers

    def _replace_outlier(self, old, new):
        """
        Called from outliers.py when a chain is replaced by the
        clone of another.
        """
        self._outliers.append((self._thin_index, old, new))

        # 2017-10-06 [PAK] only replace the head, not the full chain
        index = self._gen_index
        self._gen_current[old] = self._gen_current[new]
        self._gen_logp[index, old] = self._gen_logp[index, new]
        self._thin_logp[index, old] = self._thin_logp[index, new]
        self._thin_point[index, old, :] = self._thin_point[index, new, :]

    def mark_outliers(self, test="IQR", portion=1.0):
        """
        Mark some chains as outliers but don't remove them.  This can happen
        after drawing is complete, so that chains that did not converge are
        not included in the statistics.

        *test* is 'IQR', 'Mahol' or 'none'.

        *portion* indicates what portion of the samples should be included
        in the outlier test.  The default is to include all of them.
        """
        _, chains, logp = self.chains()

        if test == "none":
            self._good_chains = slice(None, None)
        else:
            Ngen = chains.shape[0]
            start = int(Ngen * (1 - portion)) if portion else 0
            outliers = identify_outliers(test, logp[start:], chains[-1])
            # print("outliers", outliers)
            # print(logp.shape, chains.shape)
            if len(outliers) > 0:
                self._good_chains = np.array([i for i in range(logp.shape[1]) if i not in outliers])
            else:
                self._good_chains = slice(None, None)
            # print(self._good_chains)

    def logp(self, full=False):
        """
        Return the iteration number and the log likelihood for each point in
        the individual sequences in that iteration.

        For example, to plot the convergence of each sequence::

            draw, logp = state.logp()
            plot(draw, logp)

        Note that draw[i] represents the total number of samples taken,
        including those for the samples in logp[i].

        If full is True, then return all chains, not just good chains.
        """
        # self._unroll()
        # draws, logp = self._gen_draws, self._gen_logp
        # if self.generation == self._gen_index:
        #    draws, logp = [v[:self.generation] for v in (draws, logp)]

        # Don't do a full unroll here
        if self.generation == self._gen_index:
            draws = self._gen_draws[: self.generation]
            logp = self._gen_logp[: self.generation]
        elif self._gen_index > 0:
            draws = np.roll(self._gen_draws, -self._gen_index, axis=0)
            logp = np.roll(self._gen_logp, -self._gen_index, axis=0)
        else:
            draws = self._gen_draws
            logp = self._gen_logp

        # TODO: just return logp, not logp and draws
        return draws, (logp if full else logp[:, self._good_chains])

    def logp_slice(self, n):
        """
        Return a slice of the logp chains, either the first n if n > 0
        or the last n if n < 0.  Avoids unrolling the circular buffer if
        possible.
        """
        if n < 0:  # tail
            if self._gen_index >= -n:
                return self._gen_logp[self._gen_index + n : self._gen_index]
            elif self._gen_index == 0:
                return self._gen_logp[n:]
            else:  # unroll across boundary
                return np.vstack((self._gen_logp[n + self._gen_index :], self._gen_logp[: self._gen_index]))
        else:  # head
            if self.generation < self.Ngen:
                return self._gen_logp[:n]
            elif self._gen_index + n <= self.Ngen:
                return self._gen_logp[self._gen_index : self._gen_index + n]
            else:
                return np.vstack((self._gen_logp[self._gen_index :], self._gen_logp[-n + self._gen_index :]))

    def min_slice(self, n):
        """
        Return the minimum logp for n slices, from the head if positive
        or the tail if negative.

        This is a specialized function so it can be fast.  Convergence
        can be quickly rejected if the min in a short head is smaller
        than the min in a long tail.  Unfortunately, if the data is
        wrapped, then the max function will cost extra.
        """
        # Copy the logic of slice
        if n < 0:  # tail
            if self._gen_index >= -n:
                return np.min(self._gen_logp[self._gen_index + n : self._gen_index])
            elif self._gen_index == 0:
                return np.min(self._gen_logp[n:])
            else:  # max across boundary
                return min(np.min(self._gen_logp[n + self._gen_index :]), np.min(self._gen_logp[: self._gen_index]))
        else:  # head
            if self.generation < self.Ngen:
                return np.min(self._gen_logp[:n])
            elif self._gen_index + n <= self.Ngen:
                return np.min(self._gen_logp[self._gen_index : self._gen_index + n])
            else:
                return min(np.min(self._gen_logp[self._gen_index :]), np.min(self._gen_logp[-n + self._gen_index :]))

    def acceptance_rate(self):
        """
        Return the iteration number and the acceptance rate for that iteration.

        For example, to plot the acceptance rate over time::

            draw, AR = state.acceptance_rate()
            plot(draw, AR)

        """
        retval = self._gen_draws, self._gen_acceptance_rate
        if self.generation == self._gen_index:
            retval = [v[: self.generation] for v in retval]
        elif self._gen_index > 0:
            retval = [np.roll(v, -self._gen_index, axis=0) for v in retval]
        return retval

    def chains(self):
        """
        Returns the observed Markov chains and the corresponding likelihoods.

        The return value is a tuple (*draws*, *chains*, *logp*).

        *draws* is the number of samples taken up to and including the samples
        for the current generation.

        *chains* is a three dimensional array of generations X chains X vars
        giving the set of points observed for each chain in every generation.
        Only the thinned samples are returned.

        *logp* is a two dimensional array of generation X population giving
        the log likelihood of observing the set of variable values given in
        chains.
        """
        self._unroll()
        retval = self._thin_draws, self._thin_point, self._thin_logp
        if self._thin_count == self._thin_index:
            retval = [v[: self._thin_count] for v in retval]
        return retval

    def gelman(self):
        """
        Compute the R-statistic for the current frame
        """
        # Calculate Gelman and Rubin convergence diagnostic
        if self.generation < self.Ngen:
            return gelman(self._thin_point[: self.generation], portion=1.0)
        else:
            return gelman(self._thin_point, portion=1.0)

    def CR_weight(self):
        """
        Return the crossover ratio weights to be used in the next generation.

        For example, to see if the adaptive CR is stable use::

            draw, weight = state.CR_weight()
            plot(draw, weight)

        See :mod:`.crossover` for details.
        """
        self._unroll()
        retval = self._update_draws, self._update_CR_weight
        if self._update_count == self._update_index:
            retval = [v[: self._update_count] for v in retval]
        return retval

    def outliers(self):
        """
        Return a list of outlier removal operations.

        Each outlier operation is a tuple giving the thinned generation
        in which it occurred, the old chain id and the new chain id.

        The chains themselves have already been updated to reflect the
        removal.

        Curiously, it is possible for the maximum likelihood seen so far
        to be removed by this operation.
        """
        return asarray(self._outliers, "i")

    def best(self):
        """
        Return the best point seen and its log likelihood.
        """
        return self._best_x, self._best_logp

    def stable_best(self):
        """
        Return the best point seen and its log likelihood.
        """
        return self._best_gen + self.Ngen <= self.generation

    def keep_best(self):
        """
        Place the best point at the end of the last good chain.

        Good chains are defined by mark_outliers.

        Because the Markov chain is designed to wander the parameter
        space, the best individual seen during the random walk may have
        been observed during the burn-in period, and may no longer be
        present in the chain.  If this is the case, replace the final
        point with the best, otherwise swap the positions of the final
        and the best.
        """

        # Get state as a 1D array
        _, chains, logp = self.chains()
        Ngen, Npop, Nvar = chains.shape
        points = reshape(chains, (Ngen * Npop, Nvar))
        logp = reshape(logp, Ngen * Npop)

        # Set the final position to the end of the last good chain.  If
        # mark_outliers has not been called, then _good_chains will
        # just be slice(None, None)
        if isinstance(self._good_chains, slice):
            final = -1
        else:
            final = self._good_chains[-1] - Npop

        # Find the location of the best point if it exists and swap with
        # the final position
        idx = np.where(logp == self._best_logp)[0]
        if len(idx) == 0:
            logp[final] = self._best_logp
            points[final, :] = self._best_x
        else:
            idx = idx[0]
            logp[final], logp[idx] = logp[idx], logp[final]
            points[final, :], points[idx, :] = points[idx, :], points[final, :]
        # For multiple minima, arbitrarily choose one of them
        # TODO: this will lead to possible confusion when the best value
        # spontaneously changes when the fit is complete.
        self._best_p = points[final]
        self._best_logp = logp[final]

    def sample(self, **kw):
        """
        Return a sample from the posterior distribution.

        **Deprecated** use :meth:`draw` instead.
        """
        drawn = self.draw(**kw)
        return drawn.points, drawn.logp

    def entropy(self, vars=None, portion=1.0, selection=None, n_est=10000, thin=None, method=None):
        r"""
        Return entropy estimate and uncertainty from an MCMC draw.

        *portion* is the portion of each chain to use

        *vars* is the set of variables to marginalize over.  It is None for
        the visible variables, or a list of variables.

        *vars* is the list of variables to use for marginalization.

        *selection* sets the range each parameter in the returned distribution,
        using {variable: (low, high)}. Missing variables use the full range.

        *n_est* is the number of points to use from the draw when estimating
        the entropy (default=10000).

        *thin* is the amount of thinning to use when selecting points from the
        draw.

        *method* determines which entropy calculation to use:

        * gmm: fit sample to a gaussian mixture model (GMM) with $5 \sqrt{d}$
          components where $d$ is the number fitted parameters and estimate
          entropy by sampling from the GMM.

        * llf: estimates likelihood scale factor from ratio of density
          estimate to model likelihood, then computes Monte Carlo entropy
          from sample; this does not work for marginal likelihood estimates.
          DOI:10.1109/CCA.2010.5611198

        * mvn: fit sample to a multi-variate Gaussian and return the entropy
          of the best fit gaussian; uses bootstrap to estimate uncertainty.

        * wnn: estimate entropy from nearest-neighbor distances in sample.
          DOI:10.1214/18-AOS1688
        """
        from . import entropy

        # Get the sample from the state.
        # set default thinning to max((steps * samples/step) // n_est, 1)
        if thin is None:
            Nsteps = min(self.Nthin, self._thin_count)
            thin = max(Nsteps * self.Npop // n_est, 1)
            # print("thin", thin, Nsteps, self.Npop, self.Nthin, self._thin_count)
        drawn = self.draw(portion=portion, vars=vars, selection=selection, thin=thin)

        # TODO: don't print within a library function!
        M = entropy.MVNEntropy(drawn.points)
        print("Entropy from MVN: %s" % str(M))

        if method is None:
            # TODO: change default to gmm
            method = "llf"

        if method == "llf":
            S, Serr = entropy.entropy(drawn.points, drawn.logp, N_entropy=n_est)
            # print("Entropy from llf (Kramer): %s"%str(S))
        elif method == "gmm":
            # Try pure gmm ... pretty good
            S, Serr = entropy.gmm_entropy(drawn.points, n_est=n_est)
            # print("Entropy from gmm: %g +/- %g"% (S, Serr))
        elif method == "wnn":
            # Try pure wnn ... no good
            S, Serr = entropy.wnn_entropy(drawn.points, n_est=n_est)
            # print("Entropy from wnn: %s"%str(S))
        elif method == "mvn":
            S, Serr = entropy.mvn_entropy_bootstrap(drawn.points)
            # print("Entropy from mvn: %s"%str(S))
        else:
            raise ValueError("unknown method %r" % method)

        # Always return entropy estimate from draw, even if it is normal
        return S, Serr

    def draw(self, portion=1.0, vars=None, selection=None, thin=1):
        """
        Return a sample from the posterior distribution.

        *portion* is the portion of each chain to use

        *vars* is a list of variables to return for each point

        *selection* sets the range each parameter in the returned distribution,
        using {variable: (low, high)}. Missing variables use the full range.

        *thin* takes every nth item.

        To plot the distribution for parameter p1::

            draw = state.draw()
            hist(draw.points[:, 0])

        To plot the interdependence of p1 and p2::

            draw = state.sample()
            plot(draw.points[:, 0], draw.points[:, 1], '.')
        """
        vars = vars if vars is not None else getattr(self, "_shown", None)
        return Draw(self, portion=portion, vars=vars, selection=selection, thin=thin)

    def set_visible_vars(self, labels):
        self._shown = [self.labels.index(v) for v in labels]
        # print("\n".join(str(pair) for pair in enumerate(self.labels)))
        # print(labels)
        # print(self._shown)

    def set_integer_vars(self, labels):
        """
        Indicate tha variables should be considered integer variables when
        computing statistics.
        """
        self._integer_vars = np.array([var in labels for var in self.labels])

    def derive_vars(self, fn, labels=None):
        """
        Generate derived variables from the current sample, adding columns
        for the derived variables to each sample of every chain.

        The new columns are treated as part of the sample.

        *fn* is a function taking points p[:, k] for k in 0 ... samples and
        returning a set of derived variables pj[k] for each sample k.  The
        variables can be returned as any kind of sequence including an
        array or a tuple with one entry per variable.  The caller uses
        asarray to convert the returned variables into a vars X samples array.
        For convenience, a single variable can be returned by itself.

        *labels* are the labels to use for the derived variables.

        The following example adds the new variable x+y = P[0] + P[1]::

            state.derive_vars(lambda p: p[0]+p[1], labels=["x+y"])
        """
        # Grab all samples as a set of points
        _, chains, _ = self.chains()
        Ngen, Npop, Nvar = chains.shape
        points = reshape(chains, (Ngen * Npop, Nvar))

        # Compute new variables from the points
        newvars = asarray(fn(points.T)).T
        Nnew = newvars.shape[1] if len(newvars.shape) == 2 else 1
        newvars.reshape((Ngen, Npop, Nnew))

        # Extend new variables to be the same length as the stored selection
        Nthin = self._thin_point.shape[0]
        newvars = np.resize(newvars, (Nthin, Npop, Nnew))

        # Add new variables to the points
        self._thin_point = dstack((self._thin_point, newvars))

        # Add labels for the new variables, if available.
        if labels is not None:
            self.labels = self.labels + labels
        elif self._labels is not None:
            labels = ["P%d" % i for i in range(Nvar, Nvar + Nnew)]
            self.labels = self.labels + labels
        else:  # no labels specified, old or new
            pass


class Draw(object):
    def __init__(self, state, vars=None, portion=None, selection=None, thin=1):
        self.state = state
        self.vars = vars
        self.portion = portion
        self.selection = selection
        self.points, self.logp = _sample(state, portion=portion, vars=vars, selection=selection, thin=thin)
        self.labels = state.labels if vars is None else [state.labels[v] for v in vars]
        self._stats = None
        self.weights = None
        self.num_vars = len(self.labels)
        if state._integer_vars is not None:
            self.integers = state._integer_vars[vars] if vars else None
        else:
            self.integers = None
        self._argsort_indices = {}

    # cache the argsort indices for each variable
    def get_argsort_indices(self, var: int):
        if var not in self._argsort_indices:
            self._argsort_indices[var] = np.argsort(self.points[:, var].flatten())
        return self._argsort_indices[var]


def _sample(state, portion, vars, selection, thin):
    """
    Return a sample from a set of chains.
    """
    draw, chains, logp = state.chains()
    start = int((1 - portion) * len(draw)) if portion else 0

    # Collect the subset we are interested in
    chains = chains[start::thin, state._good_chains, :]
    logp = logp[start::thin, state._good_chains]

    Ngen, Npop, Nvar = chains.shape
    points = reshape(chains, (-1, Nvar))
    logp = reshape(logp, (-1))
    if selection not in [None, {}]:
        idx = True
        for v, r in selection.items():
            if v == "logp":
                idx = idx & (logp >= r[0]) & (logp <= r[1])
            else:
                idx = idx & (points[:, v] >= r[0]) & (points[:, v] <= r[1])
        points = points[idx, :]
        logp = logp[idx]
    if vars is not None:
        points = points[:, vars]
    return points, logp


def test():
    from numpy.linalg import norm
    from numpy.random import rand
    from numpy import arange

    # Make some fake data
    Nupdate, Nstep = 3, 5
    Ngen = Nupdate * Nstep
    Nvar, Npop, Ncr = 3, 6, 2
    xin = rand(Ngen, Npop, Nvar)
    pin = rand(Ngen, Npop)
    accept = rand(Ngen, Npop) < 0.8
    CRin = rand(Nupdate, Ncr)
    # thinning = 2
    # Nthin = int(Ngen/thinning)

    # Put it into a state
    thinning = 2
    Nthin = int(Ngen / thinning)
    state = MCMCDraw(Ngen=Ngen, Nthin=Nthin, Nupdate=Nupdate, Nvar=Nvar, Npop=Npop, Ncr=Ncr, thinning=thinning)
    for i in range(Nupdate):
        state._update(CR_weight=CRin[i])
        for j in range(Nstep):
            gen = i * Nstep + j
            state._generation(new_draws=Npop, x=xin[gen], logp=pin[gen], accept=accept[gen])

    # Check that it got there
    draws, logp = state.logp()
    assert norm(draws - Npop * arange(1, Ngen + 1)) == 0
    assert norm(logp - pin) == 0
    draws, AR = state.acceptance_rate()
    assert norm(draws - Npop * arange(1, Ngen + 1)) == 0
    assert norm(AR - 100 * sum(accept, axis=1) / Npop) == 0
    draws, logp = state.sample()
    # assert norm(draws - thinning*Npop*arange(1, Nthin+1)) == 0
    # assert norm(sample - xin[thinning-1::thinning]) == 0
    # assert norm(logp - pin[thinning-1::thinning]) == 0
    draws, CR = state.CR_weight()
    assert norm(draws - Npop * Nstep * arange(Nupdate)) == 0
    assert norm(CR - CRin) == 0
    x, p = state.best()
    bestid = argmax(pin)
    i, j = bestid // Npop, bestid % Npop
    assert pin[i, j] == p
    assert norm(xin[i, j, :] - x) == 0

    # Check that outlier updates properly
    state._replace_outlier(1, 2)
    outliers = state.outliers()
    draws, logp = state.sample()
    assert norm(outliers - asarray([[state._thin_index, 1, 2]])) == 0
    # assert norm(sample[:, 1, :] - xin[thinning-1::thinning, 2, :]) == 0
    # assert norm(sample[:, 2, :] - xin[thinning-1::thinning, 2, :]) == 0
    # assert norm(logp[:, 1] - pin[thinning-1::thinning, 2]) == 0
    # assert norm(logp[:, 2] - pin[thinning-1::thinning, 2]) == 0

    from .stats import var_stats, format_vars

    vstats = var_stats(state.draw())
    print(format_vars(vstats))


if __name__ == "__main__":
    test()