This file is indexed.

/usr/lib/python3/dist-packages/csb/statistics/samplers/mc/multichain.py is in python3-csb 1.2.2+dfsg-2ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
"""
Implements several extended-ensemble Monte Carlo sampling algorithms.

Here is a short example which shows how to sample from a PDF using the replica
exchange with non-equilibrium switches (RENS) method. It draws 5000 samples from
a 1D normal distribution using the RENS algorithm working on three Markov chains
being generated by the HMC algorithm:


    >>> import numpy
    >>> from numpy import sqrt
    >>> from csb.io.plots import Chart
    >>> from csb.statistics.pdf import Normal
    >>> from csb.statistics.samplers import State
    >>> from csb.statistics.samplers.mc.multichain import ThermostattedMDRENSSwapParameterInfo
    >>> from csb.statistics.samplers.mc.multichain import ThermostattedMDRENS, AlternatingAdjacentSwapScheme
    >>> from csb.statistics.samplers.mc.singlechain import HMCSampler

    >>> # Pick some initial state for the different Markov chains:
    >>> initial_state = State(numpy.array([1.]))

    >>> # Set standard deviations:
    >>> std_devs = [1./sqrt(5), 1. / sqrt(3), 1.]

    >>> # Set HMC timesteps and trajectory length:
    >>> hmc_timesteps = [0.6, 0.7, 0.6]
    >>> hmc_trajectory_length = 20
    >>> hmc_gradients = [lambda q, t: 1 / (std_dev ** 2) * q for std_dev in std_devs]

    >>> # Set parameters for the thermostatted RENS algorithm:
    >>> rens_trajectory_length = 30
    >>> rens_timesteps = [0.3, 0.5]

    >>> # Set interpolation gradients as a function of the work parameter l:
    >>> rens_gradients = [lambda q, l, i=i: (l / (std_devs[i + 1] ** 2) + (1 - l) / (std_devs[i] ** 2)) * q 
                          for i in range(len(std_devs)-1)]

    >>> # Initialize HMC samplers:
    >>> samplers = [HMCSampler(Normal(sigma=std_devs[i]), initial_state, hmc_gradients[i], hmc_timesteps[i],
                    hmc_trajectory_length) for i in range(len(std_devs))]

    >>> # Create swap parameter objects:
    params = [ThermostattedMDRENSSwapParameterInfo(samplers[0], samplers[1], rens_timesteps[0],
              rens_trajectory_length, rens_gradients[0]),
              ThermostattedMDRENSSwapParameterInfo(samplers[1], samplers[2], rens_timesteps[1],
              rens_trajectory_length, rens_gradients[1])]

    >>> # Initialize thermostatted RENS algorithm:
    >>> algorithm = ThermostattedMDRENS(samplers, params)

    >>> # Initialize swapping scheme:
    >>> swapper = AlternatingAdjacentSwapScheme(algorithm)

    >>> # Initialize empty list which will store the samples:
    >>> states = []
    >>> for i in range(5000):
            if i % 5 == 0:
                swapper.swap_all()
            states.append(algorithm.sample())

    >>> # Print acceptance rates:
    >>> print('HMC acceptance rates:', [s.acceptance_rate for s in samplers])
    >>> print('swap acceptance rates:', algorithm.acceptance_rates)

    >>> # Create and plot histogram for first sampler and numpy.random.normal reference:
    >>> chart = Chart()
    >>> rawstates = [state[0].position[0] for state in states]
    >>> chart.plot.hist([numpy.random.normal(size=5000, scale=std_devs[0]), rawstates], bins=30, normed=True)
    >>> chart.plot.legend(['numpy.random.normal', 'RENS + HMC'])
    >>> chart.show()


For L{ReplicaExchangeMC} (RE), the procedure is easier because apart from the
two sampler instances the corresponding L{RESwapParameterInfo} objects take
no arguments.

Every replica exchange algorithm in this module (L{ReplicaExchangeMC}, L{MDRENS},
L{ThermostattedMDRENS}) is used in a similar way. A simulation is always
initialized with a list of samplers (instances of classes derived from
L{AbstractSingleChainMC}) and a list of L{AbstractSwapParameterInfo} objects
suited for the algorithm under consideration. Every L{AbstractSwapParameterInfo}
object holds all the information needed to perform a swap between two samplers.
The usual scheme is to swap only adjacent replicae in a scheme::

    1 <--> 2, 3 <--> 4, ...
    2 <--> 3, 4 <--> 5, ...
    1 <--> 2, 3 <--> 4, ...
    
This swapping scheme is implemented in the L{AlternatingAdjacentSwapScheme} class,
but different schemes can be easily implemented by deriving from L{AbstractSwapScheme}.
Then the simulation is run by looping over the number of samples to be drawn
and calling the L{AbstractExchangeMC.sample} method of the algorithm. By calling
the L{AbstractSwapScheme.swap_all} method of the specific L{AbstractSwapScheme}
implementation, all swaps defined in the list of L{AbstractSwapParameterInfo}
objects are performed according to the swapping scheme. The
L{AbstractSwapScheme.swap_all} method may be called for example after sampling
intervals of a fixed length or randomly.
"""

import numpy

import csb.numeric

from abc import ABCMeta, abstractmethod

from csb.statistics.samplers import EnsembleState
from csb.statistics.samplers.mc import AbstractMC, Trajectory, MCCollection, augment_state
from csb.statistics.samplers.mc.propagators import MDPropagator, ThermostattedMDPropagator
from csb.statistics.samplers.mc.neqsteppropagator import NonequilibriumStepPropagator
from csb.statistics.samplers.mc.neqsteppropagator import Protocol, Step, ReducedHamiltonian
from csb.statistics.samplers.mc.neqsteppropagator import ReducedHamiltonianPerturbation
from csb.statistics.samplers.mc.neqsteppropagator import HMCPropagation, HMCPropagationParam
from csb.statistics.samplers.mc.neqsteppropagator import HamiltonianSysInfo, NonequilibriumTrajectory
from csb.numeric.integrators import AbstractGradient, FastLeapFrog


class AbstractEnsembleMC(AbstractMC):
    """
    Abstract class for Monte Carlo sampling algorithms simulating several ensembles.

    @param samplers: samplers which sample from their respective equilibrium distributions
    @type samplers: list of L{AbstractSingleChainMC}    
    """

    __metaclass__ = ABCMeta

    def __init__(self, samplers):
        
        self._samplers = MCCollection(samplers)
        state = EnsembleState([x.state for x in self._samplers])
        
        super(AbstractEnsembleMC, self).__init__(state)        

    def sample(self):
        """
        Draw an ensemble sample.
        
        @rtype: L{EnsembleState}
        """
        
        sample = EnsembleState([sampler.sample() for sampler in self._samplers])
        self.state = sample

        return sample

    @property
    def energy(self):
        """
        Total ensemble energy.
        """ 
        return sum([x.energy for x in self._samplers])


class AbstractExchangeMC(AbstractEnsembleMC):
    """
    Abstract class for Monte Carlo sampling algorithms employing some replica exchange method.

    @param samplers: samplers which sample from their respective equilibrium distributions
    @type samplers: list of L{AbstractSingleChainMC}

    @param param_infos: list of ParameterInfo instances providing information needed
        for performing swaps
    @type param_infos: list of L{AbstractSwapParameterInfo}
    """

    __metaclass__ = ABCMeta

    def __init__(self, samplers, param_infos):
        super(AbstractExchangeMC, self).__init__(samplers)
        
        self._swaplist1 = []
        self._swaplist2 = []        
        self._currentswaplist = self._swaplist1
        
        self._param_infos = param_infos
        self._statistics = SwapStatistics(self._param_infos)
        
    def _checkstate(self, state):
        if not isinstance(state, EnsembleState):
            raise TypeError(state)        

    def swap(self, index):
        """
        Perform swap between sampler pair described by param_infos[index]
        and return outcome (true = accepted, false = rejected).

        @param index: index of swap pair in param_infos
        @type index: int

        @rtype: boolean
        """
        param_info = self._param_infos[index]
        swapcom = self._propose_swap(param_info)
        swapcom = self._calc_pacc_swap(swapcom)
        result = self._accept_swap(swapcom)
        
        self.state = EnsembleState([x.state for x in self._samplers])

        self.statistics.stats[index].update(result)
        
        return result

    @abstractmethod
    def _propose_swap(self, param_info):
        """
        Calculate proposal states for a swap between two samplers.
        
        @param param_info: ParameterInfo instance holding swap parameters
        @type param_info: L{AbstractSwapParameterInfo}
        
        @rtype: L{AbstractSwapCommunicator}
        """ 
        pass

    @abstractmethod
    def _calc_pacc_swap(self, swapcom):
        """
        Calculate probability to accept a swap given initial and proposal states.

        @param swapcom: SwapCommunicator instance holding information to be communicated
                        between distinct swap substeps
        @type swapcom: L{AbstractSwapCommunicator}

        @rtype: L{AbstractSwapCommunicator}
        """
        pass

    def _accept_swap(self, swapcom):
        """
        Accept / reject an exchange between two samplers given proposal states and
        the acceptance probability and returns the outcome (true = accepted, false = rejected).

        @param swapcom: SwapCommunicator instance holding information to be communicated
            between distinct swap substeps
        @type swapcom: L{AbstractSwapCommunicator}

        @rtype: boolean
        """

        if numpy.random.random() < swapcom.acceptance_probability:
            if swapcom.sampler1.state.momentum is None and swapcom.sampler2.state.momentum is None:
                swapcom.traj12.final.momentum = None
                swapcom.traj21.final.momentum = None
            swapcom.sampler1.state = swapcom.traj21.final
            swapcom.sampler2.state = swapcom.traj12.final
            return True
        else:
            return False

    @property
    def acceptance_rates(self):
        """
        Return swap acceptance rates.

        @rtype: list of floats
        """        
        return self.statistics.acceptance_rates

    @property
    def param_infos(self):
        """
        List of SwapParameterInfo instances holding all necessary parameters.

        @rtype: list of L{AbstractSwapParameterInfo}
        """
        return self._param_infos
    
    @property
    def statistics(self):
        return self._statistics

    def _update_statistics(self, index, accepted):
        """
        Update statistics of a given swap process.
        
        @param index: position of swap statistics to be updated
        @type index: int
        
        @param accepted: outcome of the swap
        @type accepted: boolean
        """
        
        self._stats[index][0] += 1
        self._stats[index][1] += int(accepted)


class AbstractSwapParameterInfo(object):
    """
    Subclass instances hold all parameters necessary for performing a swap
    between two given samplers.
    """

    __metaclass__ = ABCMeta

    def __init__(self, sampler1, sampler2):
        """
        @param sampler1: First sampler
        @type sampler1: L{AbstractSingleChainMC}

        @param sampler2: Second sampler
        @type sampler2: L{AbstractSingleChainMC}
        """

        self._sampler1 = sampler1
        self._sampler2 = sampler2

    @property
    def sampler1(self):
        return self._sampler1

    @property
    def sampler2(self):
        return self._sampler2


class AbstractSwapCommunicator(object):
    """
    Holds all the information which needs to be communicated between
    distinct swap substeps.

    @param param_info: ParameterInfo instance holding swap parameters
    @type param_info: L{AbstractSwapParameterInfo}

    @param traj12: Forward trajectory
    @type traj12: L{Trajectory}

    @param traj21: Reverse trajectory
    @type traj21: L{Trajectory}
    """

    __metaclass__ = ABCMeta
    
    def __init__(self, param_info, traj12, traj21):
        
        self._sampler1 = param_info.sampler1
        self._sampler2 = param_info.sampler2

        self._traj12 = traj12
        self._traj21 = traj21
        
        self._param_info = param_info
        
        self._acceptance_probability = None
        self._accepted = False
        
    @property
    def sampler1(self):
        return self._sampler1

    @property
    def sampler2(self):
        return self._sampler2
    
    @property
    def traj12(self):
        return self._traj12    

    @property
    def traj21(self):
        return self._traj21

    @property
    def acceptance_probability(self):
        return self._acceptance_probability
    @acceptance_probability.setter
    def acceptance_probability(self, value):
        self._acceptance_probability = value

    @property
    def accepted(self):
        return self._accepted
    @accepted.setter
    def accepted(self, value):
        self._accepted = value

    @property
    def param_info(self):
        return self._param_info


class ReplicaExchangeMC(AbstractExchangeMC):
    """
    Replica Exchange (RE, Swendsen & Yang 1986) implementation.
    """
        
    def _propose_swap(self, param_info):
        
        return RESwapCommunicator(param_info, Trajectory([param_info.sampler1.state,
                                                          param_info.sampler1.state]),
                                              Trajectory([param_info.sampler2.state,
                                                          param_info.sampler2.state]))
    
    def _calc_pacc_swap(self, swapcom):
        
        E1 = lambda x:-swapcom.sampler1._pdf.log_prob(x)
        E2 = lambda x:-swapcom.sampler2._pdf.log_prob(x)
            
        T1 = swapcom.sampler1.temperature
        T2 = swapcom.sampler2.temperature
        
        state1 = swapcom.traj12.initial
        state2 = swapcom.traj21.initial
        
        proposal1 = swapcom.traj21.final
        proposal2 = swapcom.traj12.final

        swapcom.acceptance_probability = csb.numeric.exp(-E1(proposal1.position) / T1 
                                                         + E1(state1.position) / T1 
                                                         - E2(proposal2.position) / T2 
                                                         + E2(state2.position) / T2)
                                                         
        return swapcom


class RESwapParameterInfo(AbstractSwapParameterInfo):
    """
    Holds parameters for a standard Replica Exchange swap.
    """
    pass


class RESwapCommunicator(AbstractSwapCommunicator):
    """
    Holds all the information which needs to be communicated between distinct
    RE swap substeps.

    See L{AbstractSwapCommunicator} for constructor signature.
    """
    pass


class AbstractRENS(AbstractExchangeMC):
    """
    Abstract Replica Exchange with Nonequilibrium Switches
    (RENS, Ballard & Jarzynski 2009) class.
    Subclasses implement various ways of generating trajectories
    (deterministic or stochastic).
    """

    __metaclass__ = ABCMeta

    def _propose_swap(self, param_info):

        init_state1 = param_info.sampler1.state
        init_state2 = param_info.sampler2.state
        
        trajinfo12 = RENSTrajInfo(param_info, init_state1, direction="fw")
        trajinfo21 = RENSTrajInfo(param_info, init_state2, direction="bw")
        
        traj12 = self._run_traj_generator(trajinfo12)
        traj21 = self._run_traj_generator(trajinfo21)

        return RENSSwapCommunicator(param_info, traj12, traj21)

    def _setup_protocol(self, traj_info):
        """
        Sets the protocol lambda(t) to either the forward or the reverse protocol.

        @param traj_info: TrajectoryInfo object holding information neccessary to
                          calculate the rens trajectories.
        @type traj_info: L{RENSTrajInfo}
        """

        if traj_info.direction == "fw":
            return traj_info.param_info.protocol
        else:
            return lambda t, tau: traj_info.param_info.protocol(tau - t, tau)
        
        return protocol

    def _get_init_temperature(self, traj_info):
        """
        Determine the initial temperature of a RENS trajectory.

        @param traj_info: TrajectoryInfo object holding information neccessary to
                          calculate the RENS trajectory.
        @type traj_info: L{RENSTrajInfo}
        """
        
        if traj_info.direction == "fw":
            return traj_info.param_info.sampler1.temperature
        else:
            return traj_info.param_info.sampler2.temperature

    @abstractmethod
    def _calc_works(self, swapcom):
        """
        Calculates the works expended during the nonequilibrium
        trajectories.

        @param swapcom: Swap communicator object holding all the
                        neccessary information.
        @type swapcom: L{RENSSwapCommunicator}

        @return: The expended during the forward and the backward
                 trajectory.
        @rtype: 2-tuple of floats
        """
        
        pass

    def _calc_pacc_swap(self, swapcom):

        work12, work21 = self._calc_works(swapcom)
        swapcom.acceptance_probability = csb.numeric.exp(-work12 - work21)

        return swapcom

    @abstractmethod
    def _propagator_factory(self, traj_info):
        """
        Factory method which produces the propagator object used to calculate
        the RENS trajectories.

        @param traj_info: TrajectoryInfo object holding information neccessary to
                          calculate the rens trajectories.
        @type traj_info: L{RENSTrajInfo}
        @rtype: L{AbstractPropagator}
        """
        pass

    def _run_traj_generator(self, traj_info):
        """
        Run the trajectory generator which generates a trajectory
        of a given length between the states of two samplers.

        @param traj_info: TrajectoryInfo instance holding information
                          needed to generate a nonequilibrium trajectory   
        @type traj_info: L{RENSTrajInfo}
        
        @rtype: L{Trajectory}
        """

        init_temperature = self._get_init_temperature(traj_info)
        
        init_state = traj_info.init_state.clone()

        if init_state.momentum is None:
            init_state = augment_state(init_state,
                                       init_temperature,
                                       traj_info.param_info.mass_matrix)
            
        gen = self._propagator_factory(traj_info)

        traj = gen.generate(init_state, int(traj_info.param_info.traj_length))
        
        return traj


class AbstractRENSSwapParameterInfo(RESwapParameterInfo):
    """
    Holds parameters for a RENS swap.
    """

    __metaclass__ = ABCMeta

    def __init__(self, sampler1, sampler2, protocol):

        super(AbstractRENSSwapParameterInfo, self).__init__(sampler1, sampler2)

        ## Can't pass the linear protocol as a default argument because of a reported bug
        ## in epydoc parsing which makes it fail building the docs.
        self._protocol = None
        if protocol is None:
            self._protocol = lambda t, tau: t / tau
        else:
            self._protocol = protocol

    @property
    def protocol(self):
        """
        Switching protocol determining the time dependence
        of the switching parameter.
        """
        return self._protocol
    @protocol.setter
    def protocol(self, value):
        self._protocol = value


class RENSSwapCommunicator(AbstractSwapCommunicator):
    """
    Holds all the information which needs to be communicated between distinct
    RENS swap substeps.

    See L{AbstractSwapCommunicator} for constructor signature.
    """
    
    pass


class RENSTrajInfo(object):
    """
    Holds information necessary for calculating a RENS trajectory.

    @param param_info: ParameterInfo instance holding swap parameters
    @type param_info: L{AbstractSwapParameterInfo}

    @param init_state: state from which the trajectory is supposed to start
    @type init_state: L{State}

    @param direction: Either "fw" or "bw", indicating a forward or backward
                      trajectory. This is neccessary to pick the protocol or
                      the reversed protocol, respectively.
    @type direction: string, either "fw" or "bw"
    """
    
    def __init__(self, param_info, init_state, direction):
        
        self._param_info = param_info
        self._init_state = init_state
        self._direction = direction
        
    @property
    def param_info(self):
        return self._param_info

    @property
    def init_state(self):
        return self._init_state

    @property
    def direction(self):
        return self._direction


class MDRENS(AbstractRENS):
    """
    Replica Exchange with Nonequilibrium Switches (RENS, Ballard & Jarzynski 2009)
    with Molecular Dynamics (MD) trajectories.

    @param samplers: Samplers which sample their
                         respective equilibrium distributions
    @type samplers: list of L{AbstractSingleChainMC}

    @param param_infos: ParameterInfo instance holding
                        information required to perform a MDRENS swap
    @type param_infos: list of L{MDRENSSwapParameterInfo}

    @param integrator: Subclass of L{AbstractIntegrator} to be used to
                       calculate the non-equilibrium trajectories
    @type integrator: type
    """

    def __init__(self, samplers, param_infos,
                 integrator=csb.numeric.integrators.FastLeapFrog):
        
        super(MDRENS, self).__init__(samplers, param_infos)
        
        self._integrator = integrator

    def _propagator_factory(self, traj_info):
        
        protocol = self._setup_protocol(traj_info)
        tau = traj_info.param_info.traj_length * traj_info.param_info.timestep
        factory = InterpolationFactory(protocol, tau)
        gen = MDPropagator(factory.build_gradient(traj_info.param_info.gradient),
                           traj_info.param_info.timestep,
                           mass_matrix=traj_info.param_info.mass_matrix,
                           integrator=self._integrator)

        return gen

    def _calc_works(self, swapcom):

        T1 = swapcom.param_info.sampler1.temperature
        T2 = swapcom.param_info.sampler2.temperature
        
        heat12 = swapcom.traj12.heat
        heat21 = swapcom.traj21.heat
        
        proposal1 = swapcom.traj21.final
        proposal2 = swapcom.traj12.final
        
        state1 = swapcom.traj12.initial
        state2 = swapcom.traj21.initial
        
        if swapcom.param_info.mass_matrix.is_unity_multiple:
            inverse_mass_matrix = 1.0 / swapcom.param_info.mass_matrix[0][0]
        else:
            inverse_mass_matrix = swapcom.param_info.mass_matrix.inverse
        
        E1 = lambda x:-swapcom.sampler1._pdf.log_prob(x)
        E2 = lambda x:-swapcom.sampler2._pdf.log_prob(x)
        K = lambda x: 0.5 * numpy.dot(x.T, numpy.dot(inverse_mass_matrix, x))

        w12 = (K(proposal2.momentum) + E2(proposal2.position)) / T2 - \
              (K(state1.momentum) + E1(state1.position)) / T1 - heat12 
        w21 = (K(proposal1.momentum) + E1(proposal1.position)) / T1 - \
              (K(state2.momentum) + E2(state2.position)) / T2 - heat21

        return w12, w21


class MDRENSSwapParameterInfo(RESwapParameterInfo):
    """
    Holds parameters for a MDRENS swap.

    @param sampler1: First sampler
    @type sampler1: L{AbstractSingleChainMC}

    @param sampler2: Second sampler
    @type sampler2: L{AbstractSingleChainMC}

    @param timestep: Integration timestep
    @type timestep: float

    @param traj_length: Trajectory length in number of timesteps
    @type traj_length: int

    @param gradient: Gradient which determines the dynamics during a trajectory
    @type gradient: L{AbstractGradient}
    
    @param protocol: Switching protocol determining the time dependence of the
                     switching parameter. It is a function M{f} taking the running
                     time t and the switching time tau to yield a value in M{[0, 1]}
                     with M{f(0, tau) = 0} and M{f(tau, tau) = 1}. Default is a linear
                     protocol, which is being set manually due to an epydoc bug
    @type protocol: callable
    
    @param mass_matrix: Mass matrix
    @type mass_matrix: n-dimensional matrix of type L{InvertibleMatrix} with n being the dimension
                               of the configuration space, that is, the dimension of
                               the position / momentum vectors
    """

    def __init__(self, sampler1, sampler2, timestep, traj_length, gradient,
                 protocol=None, mass_matrix=None):
        
        super(MDRENSSwapParameterInfo, self).__init__(sampler1, sampler2)

        self._mass_matrix = mass_matrix
        if self.mass_matrix is None:
            d = len(sampler1.state.position)
            self.mass_matrix = csb.numeric.InvertibleMatrix(numpy.eye(d), numpy.eye(d))

        self._traj_length = traj_length
        self._gradient = gradient
        self._timestep = timestep

        ## Can't pass the linear protocol as a default argument because of a reported bug
        ## in epydoc parsing which makes it fail building the docs.
        self._protocol = None
        if protocol is None:
            self._protocol = lambda t, tau: t / tau
        else:
            self._protocol = protocol
    
    @property
    def timestep(self):
        """
        Integration timestep.
        """
        return self._timestep
    @timestep.setter
    def timestep(self, value):
        self._timestep = float(value)

    @property
    def traj_length(self):
        """
        Trajectory length in number of integration steps.
        """
        return self._traj_length
    @traj_length.setter
    def traj_length(self, value):
        self._traj_length = int(value)

    @property
    def gradient(self):
        """
        Gradient which governs the equations of motion.
        """
        return self._gradient

    @property
    def mass_matrix(self):
        return self._mass_matrix
    @mass_matrix.setter
    def mass_matrix(self, value):
        self._mass_matrix = value

    @property
    def protocol(self):
        """
        Switching protocol determining the time dependence
        of the switching parameter.
        """
        return self._protocol
    @protocol.setter
    def protocol(self, value):
        self._protocol = value


class ThermostattedMDRENS(MDRENS):
    """
    Replica Exchange with Nonequilibrium Switches (RENS, Ballard & Jarzynski, 2009)
    with Andersen-thermostatted Molecular Dynamics (MD) trajectories.

    @param samplers: Samplers which sample their
                         respective equilibrium distributions
    @type samplers: list of L{AbstractSingleChainMC}

    @param param_infos: ParameterInfo instance holding
                        information required to perform a MDRENS swap
    @type param_infos: list of L{ThermostattedMDRENSSwapParameterInfo}

    @param integrator: Subclass of L{AbstractIntegrator} to be used to
                       calculate the non-equilibrium trajectories
    @type integrator: type
    """

    def __init__(self, samplers, param_infos, integrator=csb.numeric.integrators.LeapFrog):
        
        super(ThermostattedMDRENS, self).__init__(samplers, param_infos, integrator)

    def _propagator_factory(self, traj_info):

        protocol = self._setup_protocol(traj_info)
        tau = traj_info.param_info.traj_length * traj_info.param_info.timestep
        factory = InterpolationFactory(protocol, tau)
        
        grad = factory.build_gradient(traj_info.param_info.gradient)
        temp = factory.build_temperature(traj_info.param_info.temperature)

        gen = ThermostattedMDPropagator(grad,
                                        traj_info.param_info.timestep, temperature=temp, 
                                        collision_probability=traj_info.param_info.collision_probability,
                                        update_interval=traj_info.param_info.collision_interval,
                                        mass_matrix=traj_info.param_info.mass_matrix,
                                        integrator=self._integrator)

        return gen

class ThermostattedMDRENSSwapParameterInfo(MDRENSSwapParameterInfo):
    """
    @param sampler1: First sampler
    @type sampler1: subclass instance of L{AbstractSingleChainMC}

    @param sampler2: Second sampler
    @type sampler2: subclass instance of L{AbstractSingleChainMC}

    @param timestep: Integration timestep
    @type timestep: float

    @param traj_length: Trajectory length in number of timesteps
    @type traj_length: int

    @param gradient: Gradient which determines the dynamics during a trajectory
    @type gradient: subclass instance of L{AbstractGradient}

    @param mass_matrix: Mass matrix
    @type mass_matrix: n-dimensional L{InvertibleMatrix} with n being the dimension
                       of the configuration space, that is, the dimension of
                       the position / momentum vectors

    @param protocol: Switching protocol determining the time dependence of the
                     switching parameter. It is a function f taking the running
                     time t and the switching time tau to yield a value in [0, 1]
                     with f(0, tau) = 0 and f(tau, tau) = 1
    @type protocol: callable

    @param temperature: Temperature interpolation function.
    @type temperature: Real-valued function mapping from [0,1] to R.
        T(0) = temperature of the ensemble sampler1 samples from, T(1) = temperature
        of the ensemble sampler2 samples from

    @param collision_probability: Probability for a collision with the heatbath during one timestep
    @type collision_probability: float

    @param collision_interval: Interval during which collision may occur with probability
        collision_probability
    @type collision_interval: int
    """
        
    def __init__(self, sampler1, sampler2, timestep, traj_length, gradient, mass_matrix=None,
                 protocol=None, temperature=lambda l: 1.0,
                 collision_probability=0.1, collision_interval=1):
        
        super(ThermostattedMDRENSSwapParameterInfo, self).__init__(sampler1, sampler2, timestep,
                                                                   traj_length, gradient,
                                                                   mass_matrix=mass_matrix,
                                                                   protocol=protocol)
        
        self._collision_probability = None
        self._collision_interval = None
        self._temperature = temperature
        self.collision_probability = collision_probability
        self.collision_interval = collision_interval

    @property
    def collision_probability(self):
        """
        Probability for a collision with the heatbath during one timestep.
        """
        return self._collision_probability
    @collision_probability.setter
    def collision_probability(self, value):
        self._collision_probability = float(value)

    @property
    def collision_interval(self):
        """
        Interval during which collision may occur with probability
        C{collision_probability}.
        """
        return self._collision_interval
    @collision_interval.setter
    def collision_interval(self, value):
        self._collision_interval = int(value)

    @property
    def temperature(self):
        return self._temperature


class AbstractStepRENS(AbstractRENS):
    """
    Replica Exchange with Nonequilibrium Switches (RENS, Ballard & Jarzynski 2009)
    with stepwise trajectories as described in Nilmeier et al., "Nonequilibrium candidate
    Monte Carlo is an efficient tool for equilibrium simulation", PNAS 2011.
    The switching parameter dependence of the Hamiltonian is a linear interpolation
    between the PDFs of the sampler objects, 
    M{H(S{lambda}) = H_2 * S{lambda} + (1 - S{lambda}) * H_1}.
    The perturbation kernel is a thermodynamic perturbation and the propagation is subclass
    responsibility.
    Note that due to the linear interpolations between the two Hamiltonians, the
    log-probability has to be evaluated four times per perturbation step which can be
    costly. In this case it is advisable to define the intermediate log probabilities
    in _run_traj_generator differently.

    @param samplers: Samplers which sample their respective equilibrium distributions
    @type samplers: list of L{AbstractSingleChainMC}

    @param param_infos: ParameterInfo instances holding
                        information required to perform a HMCStepRENS swaps
    @type param_infos: list of L{AbstractSwapParameterInfo}
    """

    __metaclass__ = ABCMeta

    def __init__(self, samplers, param_infos):

        super(AbstractStepRENS, self).__init__(samplers, param_infos)

        self._evaluate_im_works = True

    @abstractmethod
    def _setup_propagations(self, im_sys_infos, param_info):
        """
        Set up the propagation steps using the information about the current system
        setup and parameters from the SwapParameterInfo object.

        @param im_sys_infos: Information about the intermediate system setups
        @type im_sys_infos: List of L{AbstractSystemInfo}

        @param param_info: SwapParameterInfo object containing parameters for the
                           propagations like timesteps, trajectory lengths etc.
        @type param_info: L{AbstractSwapParameterInfo}
        """
        
        pass

    @abstractmethod
    def _add_gradients(self, im_sys_infos, param_info, t_prot):
        """
        If needed, set im_sys_infos.hamiltonian.gradient.

        @param im_sys_infos: Information about the intermediate system setups
        @type im_sys_infos: List of L{AbstractSystemInfo}

        @param param_info: SwapParameterInfo object containing parameters for the
                           propagations like timesteps, trajectory lengths etc.
        @type param_info: L{AbstractSwapParameterInfo}

        @param t_prot: Switching protocol defining the time dependence of the switching
                       parameter.
        @type t_prot: callable
        """
        
        pass

    def _setup_stepwise_protocol(self, traj_info):
        """
        Sets up the stepwise protocol consisting of perturbation and relaxation steps.

        @param traj_info: TrajectoryInfo instance holding information
                          needed to generate a nonequilibrium trajectory   
        @type traj_info: L{RENSTrajInfo}
        
        @rtype: L{Protocol}
        """

        pdf1 = traj_info.param_info.sampler1._pdf
        pdf2 = traj_info.param_info.sampler2._pdf
        T1 = traj_info.param_info.sampler1.temperature
        T2 = traj_info.param_info.sampler2.temperature
        traj_length = traj_info.param_info.intermediate_steps
        prot = self._setup_protocol(traj_info)
        t_prot = lambda i: prot(float(i), float(traj_length))

        im_log_probs = [lambda x, i=i: pdf2.log_prob(x) * t_prot(i) + \
                                       (1 - t_prot(i)) * pdf1.log_prob(x)
                        for i in range(traj_length + 1)]
        
        im_temperatures = [T2 * t_prot(i) + (1 - t_prot(i)) * T1 
                           for i in range(traj_length + 1)]
        im_reduced_hamiltonians = [ReducedHamiltonian(im_log_probs[i],
                                                      temperature=im_temperatures[i]) 
                                   for i in range(traj_length + 1)]
        im_sys_infos = [HamiltonianSysInfo(im_reduced_hamiltonians[i])
                        for i in range(traj_length + 1)]
        perturbations = [ReducedHamiltonianPerturbation(im_sys_infos[i], im_sys_infos[i+1])
                        for i in range(traj_length)]
        if self._evaluate_im_works == False:
            for p in perturbations:
                p.evaluate_work = False
        im_sys_infos = self._add_gradients(im_sys_infos, traj_info.param_info, t_prot)
        propagations = self._setup_propagations(im_sys_infos, traj_info.param_info)
        
        steps = [Step(perturbations[i], propagations[i]) for i in range(traj_length)]

        return Protocol(steps)

    def _propagator_factory(self, traj_info):

        protocol = self._setup_stepwise_protocol(traj_info)
        gen = NonequilibriumStepPropagator(protocol)
        
        return gen

    def _run_traj_generator(self, traj_info):

        init_temperature = self._get_init_temperature(traj_info)

        gen = self._propagator_factory(traj_info)

        traj = gen.generate(traj_info.init_state)
        
        return NonequilibriumTrajectory([traj_info.init_state, traj.final], jacobian=1.0,
                                         heat=traj.heat, work=traj.work, deltaH=traj.deltaH)       


class HMCStepRENS(AbstractStepRENS):
    """
    Replica Exchange with Nonequilibrium Switches (RENS, Ballard & Jarzynski 2009)
    with stepwise trajectories as described in Nilmeier et al., "Nonequilibrium candidate
    Monte Carlo is an efficient tool for equilibrium simulation", PNAS 2011.
    The switching parameter dependence of the Hamiltonian is a linear interpolation
    between the PDFs of the sampler objects, 
    M{H(S{lambda}) = H_2 * S{lambda} + (1 - S{lambda}) * H_1}.
    The perturbation kernel is a thermodynamic perturbation and the propagation is done using HMC.

    Note that due to the linear interpolations between the two Hamiltonians, the
    log-probability and its gradient has to be evaluated four times per perturbation step which
    can be costly. In this case it is advisable to define the intermediate log probabilities
    in _run_traj_generator differently.

    @param samplers: Samplers which sample their respective equilibrium distributions
    @type samplers: list of L{AbstractSingleChainMC}

    @param param_infos: ParameterInfo instances holding
                        information required to perform a HMCStepRENS swaps
    @type param_infos: list of L{HMCStepRENSSwapParameterInfo}
    """

    def __init__(self, samplers, param_infos):

        super(HMCStepRENS, self).__init__(samplers, param_infos)

    def _add_gradients(self, im_sys_infos, param_info, t_prot):

        im_gradients = [lambda x, t, i=i: param_info.gradient(x, t_prot(i))
                        for i in range(param_info.intermediate_steps + 1)]

        for i, s in enumerate(im_sys_infos):
            s.hamiltonian.gradient = im_gradients[i]

        return im_sys_infos
            
    def _setup_propagations(self, im_sys_infos, param_info):
                        
        propagation_params = [HMCPropagationParam(param_info.timestep,
                                                  param_info.hmc_traj_length,
                                                  im_sys_infos[i+1].hamiltonian.gradient,
                                                  param_info.hmc_iterations,
                                                  mass_matrix=param_info.mass_matrix,
                                                  integrator=param_info.integrator)
                              for i in range(param_info.intermediate_steps)]

        propagations = [HMCPropagation(im_sys_infos[i+1], propagation_params[i], evaluate_heat=False)
                        for i in range(param_info.intermediate_steps)]

        return propagations        

    def _calc_works(self, swapcom):

        return swapcom.traj12.work, swapcom.traj21.work


class HMCStepRENSSwapParameterInfo(AbstractRENSSwapParameterInfo):
    """
    Holds all required information for performing HMCStepRENS swaps.

    @param sampler1: First sampler
    @type sampler1: subclass instance of L{AbstractSingleChainMC}

    @param sampler2: Second sampler
    @type sampler2: subclass instance of L{AbstractSingleChainMC}

    @param timestep: integration timestep
    @type timestep: float

    @param hmc_traj_length: HMC trajectory length
    @type hmc_traj_length: int

    @param hmc_iterations: number of HMC iterations in the propagation step
    @type hmc_iterations: int

    @param gradient: gradient governing the equations of motion, function of
                     position array and switching protocol
    @type gradient: callable
    
    @param intermediate_steps: number of steps in the protocol; this is a discrete version
                               of the switching time in "continuous" RENS implementations
    @type intermediate_steps: int

    @param protocol: Switching protocol determining the time dependence of the
                     switching parameter. It is a function f taking the running
                     time t and the switching time tau to yield a value in [0, 1]
                     with f(0, tau) = 0 and f(tau, tau) = 1
    @type protocol: callable

    @param mass_matrix: mass matrix for kinetic energy definition
    @type mass_matrix: L{InvertibleMatrix}

    @param integrator: Integration scheme to be utilized
    @type integrator: l{AbstractIntegrator}
    """

    def __init__(self, sampler1, sampler2, timestep, hmc_traj_length, hmc_iterations, 
                 gradient, intermediate_steps, parametrization=None, protocol=None,
                 mass_matrix=None, integrator=FastLeapFrog):

        super(HMCStepRENSSwapParameterInfo, self).__init__(sampler1, sampler2, protocol)

        self._mass_matrix = None
        self.mass_matrix = mass_matrix
        if self.mass_matrix is None:
            d = len(sampler1.state.position)
            self.mass_matrix = csb.numeric.InvertibleMatrix(numpy.eye(d), numpy.eye(d))

        self._hmc_traj_length = None
        self.hmc_traj_length = hmc_traj_length
        self._gradient = None
        self.gradient = gradient
        self._timestep = None
        self.timestep = timestep
        self._hmc_iterations = None
        self.hmc_iterations = hmc_iterations
        self._intermediate_steps = None
        self.intermediate_steps = intermediate_steps
        self._integrator = None
        self.integrator = integrator
    
    @property
    def timestep(self):
        """
        Integration timestep.
        """
        return self._timestep
    @timestep.setter
    def timestep(self, value):
        self._timestep = float(value)

    @property
    def hmc_traj_length(self):
        """
        HMC trajectory length in number of integration steps.
        """
        return self._hmc_traj_length
    @hmc_traj_length.setter
    def hmc_traj_length(self, value):
        self._hmc_traj_length = int(value)

    @property
    def gradient(self):
        """
        Gradient which governs the equations of motion.
        """
        return self._gradient
    @gradient.setter
    def gradient(self, value):
        self._gradient = value

    @property
    def mass_matrix(self):
        return self._mass_matrix
    @mass_matrix.setter
    def mass_matrix(self, value):
        self._mass_matrix = value

    @property
    def hmc_iterations(self):
        return self._hmc_iterations
    @hmc_iterations.setter
    def hmc_iterations(self, value):
        self._hmc_iterations = value

    @property
    def intermediate_steps(self):
        return self._intermediate_steps
    @intermediate_steps.setter
    def intermediate_steps(self, value):
        self._intermediate_steps = value

    @property
    def integrator(self):
        return self._integrator
    @integrator.setter
    def integrator(self, value):
        self._integrator = value


class AbstractSwapScheme(object):
    """
    Provides the interface for classes defining schemes according to which swaps in
    Replica Exchange-like simulations are performed.

    @param algorithm: Exchange algorithm that performs the swaps
    @type algorithm: L{AbstractExchangeMC}
    """

    __metaclass__ = ABCMeta

    def __init__(self, algorithm):

        self._algorithm = algorithm

    @abstractmethod
    def swap_all(self):
        """
        Advises the Replica Exchange-like algorithm to perform swaps according to
        the schedule defined here.
        """
        
        pass


class AlternatingAdjacentSwapScheme(AbstractSwapScheme):
    """
    Provides a swapping scheme in which tries exchanges between neighbours only
    following the scheme 1 <-> 2, 3 <-> 4, ... and after a sampling period 2 <-> 3, 4 <-> 5, ...

    @param algorithm: Exchange algorithm that performs the swaps
    @type algorithm: L{AbstractExchangeMC}
    """

    def __init__(self, algorithm):

        super(AlternatingAdjacentSwapScheme, self).__init__(algorithm)
        
        self._current_swap_list = None
        self._swap_list1 = []
        self._swap_list2 = []
        self._create_swap_lists()
    
    def _create_swap_lists(self):

        if len(self._algorithm.param_infos) == 1:
            self._swap_list1.append(0)
            self._swap_list2.append(0)
        else:
            i = 0
            while i < len(self._algorithm.param_infos):
                self._swap_list1.append(i)
                i += 2
                
            i = 1
            while i < len(self._algorithm.param_infos):
                self._swap_list2.append(i)
                i += 2

        self._current_swap_list = self._swap_list1

    def swap_all(self):
        
        for x in self._current_swap_list:
            self._algorithm.swap(x)

        if self._current_swap_list == self._swap_list1:
            self._current_swap_list = self._swap_list2
        else:
            self._current_swap_list = self._swap_list1


class SingleSwapStatistics(object):
    """
    Tracks swap statistics of a single sampler pair.

    @param param_info: ParameterInfo instance holding swap parameters
    @type param_info: L{AbstractSwapParameterInfo}
    """
    
    def __init__(self, param_info):
        self._total_swaps = 0
        self._accepted_swaps = 0

    @property
    def total_swaps(self):
        return self._total_swaps
    
    @property
    def accepted_swaps(self):
        return self._accepted_swaps
    
    @property
    def acceptance_rate(self):
        """
        Acceptance rate of the sampler pair.
        """
        if self.total_swaps > 0:
            return float(self.accepted_swaps) / float(self.total_swaps)
        else:
            return 0.

    def update(self, accepted):
        """
        Updates swap statistics.
        """        
        self._total_swaps += 1
        self._accepted_swaps += int(accepted)


class SwapStatistics(object):
    """
    Tracks swap statistics for an AbstractExchangeMC subclass instance.

    @param param_infos: list of ParameterInfo instances providing information
                        needed for performing swaps
    @type param_infos: list of L{AbstractSwapParameterInfo}
    """
    
    def __init__(self, param_infos):        
        self._stats = [SingleSwapStatistics(x) for x in param_infos]
        
    @property
    def stats(self):
        return tuple(self._stats)

    @property
    def acceptance_rates(self):
        """
        Returns acceptance rates for all swaps.
        """        
        return [x.acceptance_rate for x in self._stats]
        

class InterpolationFactory(object):
    """
    Produces interpolations for functions changed during non-equilibrium
    trajectories.
    
    @param protocol: protocol to be used to generate non-equilibrium trajectories
    @type protocol: function mapping t to [0...1] for fixed tau
    @param tau: switching time
    @type tau: float    
    """
    
    def __init__(self, protocol, tau):
        
        self._protocol = None
        self._tau = None
        
        self.protocol = protocol
        self.tau = tau
        
    @property
    def protocol(self):
        return self._protocol
    @protocol.setter
    def protocol(self, value):
        if not hasattr(value, '__call__'):
            raise TypeError(value)
        self._protocol = value
                    
    @property
    def tau(self):
        return self._tau
    @tau.setter
    def tau(self, value):
        self._tau = float(value)
        
    def build_gradient(self, gradient):
        """
        Create a gradient instance with according to given protocol
        and switching time.
        
        @param gradient: gradient with G(0) = G_1 and G(1) = G_2
        @type gradient: callable    
        """
        return Gradient(gradient, self._protocol, self._tau)
    
    def build_temperature(self, temperature):
        """
        Create a temperature function according to given protocol and
        switching time.

        @param temperature: temperature with T(0) = T_1 and T(1) = T_2
        @type temperature: callable        
        """
        return lambda t: temperature(self.protocol(t, self.tau))
        

class Gradient(AbstractGradient):
    
    def __init__(self, gradient, protocol, tau):
        
        self._protocol = protocol
        self._gradient = gradient
        self._tau = tau
    
    def evaluate(self, q, t):
        return self._gradient(q, self._protocol(t, self._tau))


class ReplicaHistory(object):
    '''
    Replica history object, works with both RE and RENS for
    the AlternatingAdjacentSwapScheme.

    @param samples: list holding ensemble states
    @type samples: list

    @param swap_interval: interval with which swaps were attempted, e.g.,
                          5 means that every 5th regular MC step is replaced
                          by a swap
    @type swap_interval: int

    @param first_swap: sample index of the first sample generated by a swap attempt.
                       If None, the first RE sampled is assumed to have sample index
                       swap_interval. If specified, it has to be greater than zero
    @type first_swap: int
    '''
    
    def __init__(self, samples, swap_interval, first_swap=None):
        self.samples = samples
        self.swap_interval = swap_interval
        if first_swap == None:
            self.first_swap = swap_interval - 1
        elif first_swap > 0:
            self.first_swap = first_swap - 1
        else:
            raise(ValueError("Sample index of first swap has to be greater than zero!"))
        self.n_replicas = len(samples[0])

    @staticmethod
    def _change_direction(x):
        if x == 1:
            return -1
        if x == -1:
            return 1

    def calculate_history(self, start_ensemble):
        '''
        Calculates the replica history of the first state of ensemble #start_ensemble.

        @param start_ensemble: index of the ensemble to start at, zero-indexed
        @type start_ensemble: int

        @return: replica history as a list of ensemble indices
        @rtype: list of ints
        '''
        
        sample_counter = 0

        # determine the direction (up = 1, down = -1) in the "temperature ladder" of
        # the first swap attempt. Remember: first swap series is always 0 <-> 1, 2 <-> 3, ...
        if start_ensemble % 2 == 0:
            direction = +1
        else:
            direction = -1

        # if number of replicas is not even and the start ensemble is the highest-temperature-
        # ensemble, the first swap will be attempted "downwards"
        if start_ensemble % 2 == 0 and start_ensemble == self.n_replicas - 1:
            direction = -1

        # will store the indices of the ensembles the state will visit in chronological order
        history = []

        # the ensemble the state is currently in
        ens = start_ensemble
        
        while sample_counter < len(self.samples):
            if self.n_replicas == 2:
                if (sample_counter - self.first_swap - 1) % self.swap_interval == 0 and \
                   sample_counter >= self.first_swap:
                    ## swap attempt: determine whether it was successfull or not
                    # state after swap attempt
                    current_sample = self.samples[sample_counter][ens]

                    # state before swap attempt
                    previous_sample = self.samples[sample_counter - 1][history[-1]]

                    # swap was accepted when position of the current state doesn't equal
                    # the position of the state before the swap attempt, that is, the last 
                    # state in the history
                    swap_accepted = not numpy.all(current_sample.position == 
                                                  previous_sample.position)

                    if swap_accepted:
                        if ens == 0:
                            ens = 1
                        else:
                            ens = 0
                    history.append(ens)                    
                else:
                    history.append(ens)

            else:
                
                if (sample_counter - self.first_swap - 1) % self.swap_interval == 0 and \
                   sample_counter >= self.first_swap:
                    # state after swap attempt
                    current_sample = self.samples[sample_counter][ens]

                    # state before swap attempt
                    previous_sample = self.samples[sample_counter - 1][ens]

                    # swap was accepted when position of the current state doesn't equal
                    # the position of the state before the swap attempt, that is, the last 
                    # state in the history
                    swap_accepted = not numpy.all(current_sample.position == previous_sample.position)

                    if swap_accepted:
                        ens += direction
                    else:
                        if ens == self.n_replicas - 1:
                            # if at the top of the ladder, go downwards again
                            direction = -1
                        elif ens == 0:
                            # if at the bottom of the ladder, go upwards
                            direction = +1
                        else:
                            # in between, reverse the direction of the trajectory
                            # in temperature space
                            direction = self._change_direction(direction)

                history.append(ens)

            sample_counter += 1

        return history

    def calculate_projected_trajectories(self, ensemble):
        '''
        Calculates sequentially correlated trajectories projected on a specific ensemble.

        @param ensemble: ensemble index of ensemble of interest, zero-indexed
        @type ensemble: int

        @return: list of Trajectory objects containg sequentially correlated trajectories
        @rtype: list of L{Trajectory} objects.
        '''
        
        trajectories = []

        for i in range(self.n_replicas):
            history = self.calculate_history(i)
            traj = [x[ensemble] for k, x in enumerate(self.samples) if history[k] == ensemble]
            trajectories.append(Trajectory(traj))

        return trajectories

    def calculate_trajectories(self):
        '''
        Calculates sequentially correlated trajectories.

        @return: list of Trajectory objects containg sequentially correlated trajectories
        @rtype: list of L{Trajectory} objects.
        '''
        
        trajectories = []

        for i in range(self.n_replicas):
            history = self.calculate_history(i)
            traj = [x[history[k]] for k, x in enumerate(self.samples)]
            trajectories.append(Trajectory(traj))

        return trajectories