Lotka-Volterra toy model

The LotkaVolterraModel describes the relationship between two interacting species, where one preys on the other. A good description of its history and interpretation can be found on Wikipedia.

The model has 2 states $x$ and $y$, where $x$ represents a population of prey, and $y$ represents a population of predators. It is described by the ODEs:

$$ \frac{dx}{dt} = ax - bxy $$

and

$$ \frac{dy}{dt} = -cy + dxy $$

where $a, b, c$, and $d$ are the four model parameters.


In [1]:
import numpy as np
import matplotlib.pyplot as plt
import pints
import pints.toy
import time
import pints.plot
from scipy.interpolate import interp1d

model = pints.toy.LotkaVolterraModel()

print('Outputs: ' + str(model.n_outputs()))
print('Parameters: ' + str(model.n_parameters()))


Outputs: 2
Parameters: 4

The model comes pre-packaged with lynx-hare pelt count data collected by the Hudson's Bay Company in Canada in the early twentieth century, which is taken from [1]. The data given here corresponds to annual observations taken from 1900-1920 (inclusive). We now plot this data.

[1] Howard, P. (2009). Modeling basics. Lecture Notes for Math 442, Texas A&M University


In [2]:
times = model.suggested_times() + 1900
values = model.suggested_values()

plt.figure()
plt.xlabel('Time')
plt.ylabel('Population size')
plt.plot(times, values)
plt.legend(['hare', 'lynx'])
plt.show()


In this set-up, the first state represents the prey, and the second the predators. When there is no prey, the predators begin to die out, which allows the prey population to recover.

To show the cyclical nature more clearly, these two populations are often plotted against each other:


In [3]:
plt.figure()
plt.xlim(0, 80)
plt.ylim(0, 80)
plt.xlabel('hare')
plt.ylabel('lynx')
plt.plot(values[:, 0], values[:, 1])
plt.show()


We now use PINTS to fit the Lotka-Volterra model (with fixed initial conditions $[x,y]=[30, 4]$) to the pelts data on lynxs and hares. Previous work has showed that a multiplicative noise model is more appropriate to fit these data [2]. As such, we fit the model to the log of the series.

[2] Predator-Prey Population Dynamics: the Lotka-Volterra model in Stan. Carpenter, B. https://mc-stan.org/users/documentation/case-studies/lotka-volterra-predator-prey.html


In [52]:
# Create an object with links to the model and time series
problem = pints.MultiOutputProblem(model, times, np.log(values))

# Create a log posterior
log_prior_theta = pints.UniformLogPrior(lower_or_boundaries=0, upper=2)
log_prior_sigma = pints.GaussianLogPrior(mean=0, sd=3)
log_prior = pints.ComposedLogPrior(log_prior_theta, log_prior_theta, log_prior_theta, log_prior_theta,
                                   log_prior_sigma, log_prior_sigma)
log_likelihood = pints.GaussianLogLikelihood(problem)
log_posterior = pints.LogPosterior(log_likelihood, log_prior)

# Run MCMC on the noisy data
x0 = [[0.43, 0.16, 0.9, 0.27, 0.28, 0.26]] * 4
mcmc = pints.MCMCController(log_posterior, 4, x0)
mcmc.set_max_iterations(4000)
#mcmc.set_log_to_screen(False)

start = time.time()
print('Running')
chains = mcmc.run()
print('Done!')

end = time.time()
diff=end-start


Running
Using Haario-Bardenet adaptive covariance MCMC
Generating 4 chains.
Running in sequential mode.
Iter. Eval. Accept.   Accept.   Accept.   Accept.   Time m:s
0     4      0         0         0         0          0:00.0
1     8      0         0         0         0          0:00.0
2     12     0         0         0         0          0:00.0
3     16     0         0.25      0         0          0:00.1
20    84     0.0476    0.0476    0.0476    0.0476     0:00.3
40    164    0.0488    0.0244    0.0244    0.0244     0:00.6
60    244    0.0328    0.0164    0.0328    0.0164     0:00.8
80    324    0.0247    0.0247    0.0247    0.0123     0:01.1
100   404    0.0198    0.0198    0.0198    0.0099     0:01.4
120   484    0.0165    0.0248    0.0165    0.00826    0:01.6
140   564    0.0142    0.0284    0.0142    0.00709    0:01.9
160   644    0.0186    0.0248    0.0124    0.00621    0:02.2
180   724    0.0221    0.0221    0.011     0.00552    0:02.5
Initial phase completed.
200   804    0.0249    0.0199    0.00995   0.00498    0:02.7
220   884    0.0633    0.0498    0.0498    0.0407     0:03.0
240   964    0.0954    0.0954    0.0871    0.0747     0:03.2
260   1044   0.115     0.123     0.111     0.0958     0:03.5
280   1124   0.132     0.13879   0.128     0.125      0:03.8
300   1204   0.143     0.159     0.146     0.133      0:04.0
320   1284   0.146     0.171     0.152648  0.131      0:04.3
340   1364   0.147     0.170088  0.161     0.135      0:04.5
360   1444   0.15      0.186     0.172     0.144      0:04.8
380   1524   0.15      0.189     0.178     0.139      0:05.0
400   1604   0.165     0.182     0.172     0.142      0:05.3
420   1684   0.164     0.185     0.173     0.152019   0:05.6
440   1764   0.159     0.181     0.179     0.152      0:05.8
460   1844   0.163     0.184     0.18      0.152      0:06.1
480   1924   0.162     0.183     0.177     0.15       0:06.4
500   2004   0.174     0.19      0.176     0.162      0:06.6
520   2084   0.184261  0.186     0.177     0.163      0:06.9
540   2164   0.189     0.183     0.174     0.165      0:07.1
560   2244   0.189     0.178     0.169     0.166      0:07.4
580   2324   0.186     0.176     0.174     0.164      0:07.7
600   2404   0.186     0.173     0.176     0.166      0:07.9
620   2484   0.185     0.172     0.172     0.167      0:08.2
640   2564   0.184     0.178     0.173     0.165      0:08.5
660   2644   0.189     0.177     0.172466  0.166      0:08.7
680   2724   0.185022  0.174743  0.173     0.173      0:09.0
700   2804   0.183     0.173     0.175     0.171184   0:09.3
720   2884   0.182     0.171     0.176     0.176      0:09.5
740   2964   0.182     0.168691  0.178     0.174      0:09.8
760   3044   0.181     0.167     0.177     0.176      0:10.0
780   3124   0.184379  0.166     0.181     0.179      0:10.3
800   3204   0.184769  0.174     0.181     0.181      0:10.6
820   3284   0.180268  0.174     0.180268  0.180268   0:10.8
840   3364   0.185     0.17717   0.17717   0.181      0:11.1
860   3444   0.187     0.174216  0.18      0.182      0:11.3
880   3524   0.191     0.174     0.177     0.182      0:11.6
900   3604   0.188     0.178     0.175     0.18       0:11.9
920   3684   0.19      0.177     0.173     0.179      0:12.1
940   3764   0.187     0.179     0.17      0.18       0:12.4
960   3844   0.185     0.18      0.169615  0.178      0:12.6
980   3924   0.183     0.185     0.168     0.17737    0:12.9
1000  4004   0.183     0.191     0.17      0.176      0:13.1
1020  4084   0.182     0.189     0.169     0.176      0:13.4
1040  4164   0.182     0.187     0.17      0.176      0:13.7
1060  4244   0.184     0.189     0.175     0.182      0:13.9
1080  4324   0.185     0.191     0.173913  0.181      0:14.2
1100  4404   0.186     0.194     0.174     0.183      0:14.4
1120  4484   0.186     0.194     0.176628  0.186      0:14.7
1140  4564   0.185     0.195     0.176     0.183      0:15.0
1160  4644   0.185     0.193     0.176     0.184      0:15.2
1180  4724   0.187     0.19221   0.18      0.184      0:15.5
1200  4804   0.187     0.191     0.18      0.182348   0:15.7
1220  4884   0.187     0.19      0.18      0.183      0:16.0
1240  4964   0.186     0.189     0.182917  0.184      0:16.3
1260  5044   0.183981  0.188     0.183981  0.183981   0:16.5
1280  5124   0.184     0.188     0.184     0.183      0:16.8
1300  5204   0.185     0.188     0.184     0.183      0:17.1
1320  5284   0.185     0.186     0.185     0.183      0:17.3
1340  5364   0.182     0.19      0.183     0.185      0:17.6
1360  5444   0.18      0.191036  0.184     0.184      0:17.8
1380  5524   0.17958   0.192614  0.183     0.185      0:18.1
1400  5604   0.181     0.192     0.184868  0.186      0:18.3
1420  5684   0.179     0.194     0.184     0.186      0:18.6
1440  5764   0.18      0.192     0.184     0.187      0:18.9
1460  5844   0.18      0.192334  0.186     0.188      0:19.1
1480  5924   0.179     0.192     0.188     0.187711   0:19.4
1500  6004   0.18      0.191     0.191     0.187      0:19.6
1520  6084   0.181     0.191     0.193     0.187      0:19.9
1540  6164   0.182     0.192     0.192732  0.185      0:20.2
1560  6244   0.183     0.194     0.195     0.187      0:20.4
1580  6324   0.185     0.196     0.195     0.188      0:20.7
1600  6404   0.186     0.196     0.193629  0.189      0:20.9
1620  6484   0.186     0.199     0.194     0.191      0:21.2
1640  6564   0.187081  0.197     0.195003  0.191      0:21.5
1660  6644   0.188     0.194     0.196     0.191      0:21.7
1680  6724   0.189     0.193     0.194     0.192      0:22.0
1700  6804   0.189     0.192     0.195     0.19       0:22.3
1720  6884   0.189     0.191749  0.196     0.189      0:22.5
1740  6964   0.19      0.190695  0.196     0.188      0:22.8
1760  7044   0.19      0.19      0.196     0.189665   0:23.0
1780  7124   0.189     0.188     0.197     0.188      0:23.3
1800  7204   0.188784  0.188     0.195447  0.188      0:23.6
1820  7284   0.188358  0.188     0.197     0.188      0:23.8
1840  7364   0.189     0.188     0.198     0.189      0:24.1
1860  7444   0.189683  0.19      0.199     0.189      0:24.4
1880  7524   0.192     0.19      0.198     0.19       0:24.6
1900  7604   0.191     0.188     0.199     0.19       0:24.9
1920  7684   0.192     0.187923  0.199     0.192      0:25.1
1940  7764   0.191     0.188     0.198     0.194745   0:25.4
1960  7844   0.192     0.189     0.198     0.193      0:25.7
1980  7924   0.194     0.188     0.197     0.192      0:25.9
2000  8004   0.193     0.187     0.196     0.191904   0:26.2
2020  8084   0.194     0.188     0.196     0.191      0:26.5
2040  8164   0.194     0.189123  0.196     0.192      0:26.7
2060  8244   0.194     0.19      0.195     0.192      0:27.0
2080  8324   0.194618  0.191     0.194     0.192      0:27.3
2100  8404   0.195     0.191     0.194     0.192      0:27.5
2120  8484   0.194248  0.19      0.194248  0.191      0:27.8
2140  8564   0.194     0.189     0.193     0.192      0:28.1
2160  8644   0.19528   0.189727  0.194     0.193429   0:28.3
2180  8724   0.196     0.191     0.193     0.193      0:28.6
2200  8804   0.195     0.191731  0.194     0.194      0:28.9
2220  8884   0.196308  0.191     0.194507  0.194507   0:29.1
2240  8964   0.198     0.191     0.194     0.195      0:29.4
2260  9044   0.198     0.192     0.193     0.194      0:29.7
2280  9124   0.198     0.193     0.193     0.194      0:29.9
2300  9204   0.197     0.191     0.193     0.196      0:30.2
2320  9284   0.197     0.192     0.194     0.196467   0:30.5
2340  9364   0.198     0.194     0.195     0.196      0:30.7
2360  9444   0.199     0.197     0.195     0.197      0:31.0
2380  9524   0.198236  0.197816  0.194     0.197      0:31.3
2400  9604   0.199     0.197     0.194     0.198      0:31.5
2420  9684   0.2       0.196     0.194     0.197026   0:31.8
2440  9764   0.2       0.197     0.192544  0.196      0:32.1
2460  9844   0.201     0.195449  0.193011  0.197      0:32.3
2480  9924   0.201     0.196     0.193     0.197501   0:32.6
2500  10004  0.2       0.196     0.194     0.197      0:32.9
2520  10084  0.200714  0.197144  0.192     0.196      0:33.1
2540  10164  0.2       0.197     0.192     0.196      0:33.4
2560  10244  0.2       0.195     0.193     0.196      0:33.7
2580  10324  0.201     0.197     0.193     0.196      0:33.9
2600  10404  0.201461  0.196     0.194     0.197      0:34.2
2620  10484  0.201     0.196     0.195     0.196      0:34.5
2640  10564  0.202     0.197     0.195     0.195      0:34.7
2660  10644  0.202     0.196     0.195     0.195      0:35.0
2680  10724  0.203     0.197     0.195     0.195      0:35.3
2700  10804  0.204     0.196     0.196     0.196      0:35.5
2720  10884  0.205     0.196     0.196     0.196      0:35.8
2740  10964  0.204305  0.196     0.195     0.195      0:36.1
2760  11044  0.204     0.197     0.195     0.195      0:36.3
2780  11124  0.205     0.198     0.195     0.195      0:36.6
2800  11204  0.205     0.199     0.195     0.195      0:36.8
2820  11284  0.204     0.197     0.194     0.196      0:37.1
2840  11364  0.206     0.197     0.194     0.197      0:37.4
2860  11444  0.205     0.196     0.194     0.197      0:37.6
2880  11524  0.205     0.196     0.195     0.197      0:37.9
2900  11604  0.206     0.195     0.195     0.197      0:38.2
2920  11684  0.205     0.195     0.196     0.196      0:38.4
2940  11764  0.206     0.194     0.196     0.197      0:38.7
2960  11844  0.207     0.195     0.195542  0.197      0:39.0
2980  11924  0.207     0.195     0.196     0.196      0:39.2
3000  12004  0.206931  0.196     0.197     0.197      0:39.5
3020  12084  0.206     0.197     0.198     0.196      0:39.8
3040  12164  0.206     0.197     0.197     0.197      0:40.0
3060  12244  0.206     0.197     0.197     0.196      0:40.3
3080  12324  0.206     0.198     0.198     0.195      0:40.6
3100  12404  0.206     0.198     0.198     0.197      0:40.8
3120  12484  0.206     0.197     0.199     0.197      0:41.1
3140  12564  0.205     0.197     0.199     0.197      0:41.4
3160  12644  0.205     0.198     0.2       0.2        0:41.6
3180  12724  0.205     0.198     0.201     0.199      0:41.9
3200  12804  0.206     0.198     0.201     0.198      0:42.2
3220  12884  0.205     0.198     0.201     0.198      0:42.4
3240  12964  0.204875  0.198087  0.2       0.197      0:42.7
3260  13044  0.204     0.197     0.199632  0.198      0:43.0
3280  13124  0.205     0.198     0.199     0.197196   0:43.2
3300  13204  0.205     0.198     0.198     0.198      0:43.5
3320  13284  0.205     0.199     0.197832  0.199      0:43.8
3340  13364  0.206     0.199     0.198     0.2        0:44.0
3360  13444  0.205296  0.199     0.198     0.199      0:44.3
3380  13524  0.205     0.199     0.198     0.2        0:44.5
3400  13604  0.206     0.2       0.198177  0.2        0:44.8
3420  13684  0.206     0.2       0.198     0.201      0:45.1
3440  13764  0.206626  0.201     0.2       0.201      0:45.3
3460  13844  0.207     0.201     0.2       0.201      0:45.6
3480  13924  0.206     0.202     0.2       0.203      0:45.9
3500  14004  0.206     0.202     0.2       0.203      0:46.1
3520  14084  0.205     0.202     0.2       0.203      0:46.4
3540  14164  0.205874  0.203     0.2       0.20305    0:46.7
3560  14244  0.206     0.204     0.201     0.202      0:46.9
3580  14324  0.205     0.204     0.201     0.201      0:47.2
3600  14404  0.204     0.204     0.2       0.201      0:47.5
3620  14484  0.205     0.204     0.202     0.201      0:47.7
3640  14564  0.205     0.204     0.202     0.201      0:48.0
3660  14644  0.205     0.204     0.202     0.202      0:48.2
3680  14724  0.206     0.204564  0.202     0.201304   0:48.5
3700  14804  0.206     0.205     0.202     0.201      0:48.8
3720  14884  0.207     0.205     0.202     0.202      0:49.0
3740  14964  0.207     0.205     0.202     0.202      0:49.3
3760  15044  0.207     0.206     0.202     0.202      0:49.6
3780  15124  0.207617  0.206     0.202     0.202      0:49.8
3800  15204  0.20784   0.206     0.203     0.202      0:50.1
3820  15284  0.208     0.207     0.203     0.202      0:50.3
3840  15364  0.208     0.207     0.203     0.202      0:50.6
3860  15444  0.208     0.207     0.203     0.201      0:50.9
3880  15524  0.207     0.206     0.204     0.201      0:51.1
3900  15604  0.207     0.206     0.205     0.201      0:51.4
3920  15684  0.209     0.206     0.204     0.202      0:51.7
3940  15764  0.21      0.207     0.204     0.202      0:51.9
3960  15844  0.209543  0.207     0.204     0.202979   0:52.2
3980  15924  0.21      0.206732  0.203     0.203      0:52.5
4000  16000  0.21      0.2065    0.2035    0.2035     0:52.7
Halting: Maximum number of iterations (4000) reached.
Done!

In [53]:
import pints.plot
pints.plot.trace(chains)
plt.show()



In [54]:
results = pints.MCMCSummary(chains=chains, parameter_names=["a", "b", "c", "d", "sigma_1", "sigma_2"], time=diff)
print(results)


param    mean    std.    2.5%    25%    50%    75%    97.5%    rhat    ess    ess per sec.
-------  ------  ------  ------  -----  -----  -----  -------  ------  -----  --------------
a        0.32    0.05    0.25    0.29   0.31   0.34   0.46     1.03    22.71  0.43
b        0.13    0.02    0.10    0.11   0.12   0.14   0.19     1.03    23.12  0.44
c        1.26    0.19    0.85    1.17   1.28   1.38   1.60     1.02    24.95  0.47
d        0.38    0.06    0.26    0.35   0.39   0.42   0.48     1.02    25.45  0.48
sigma_1  0.28    0.06    0.19    0.23   0.26   0.31   0.42     1.04    35.08  0.67
sigma_2  0.42    0.07    0.28    0.37   0.42   0.47   0.56     1.01    59.55  1.13

We can also compare the predictions with these values to what we found: looks like a reasonable fit.


In [45]:
# Select first chain
chain1 = chains[0]

# Remove burn-in
chain1 = chain1[500:]

# Plot some predictions with these samples
num_lines = 100
hare = np.zeros((len(times), num_lines))
lynx = np.zeros((len(times), num_lines))
for i in range(num_lines):
    temp = np.exp(model.simulate(times=times, parameters=chain1[i, :4]))
    hare[:, i] = temp[:, 0]
    lynx[:, i] = temp[:, 1]
plt.plot(times, hare, color="b", alpha=0.1)
plt.plot(times, lynx, color="orange", alpha=0.1)
plt.plot(times, values, 'o-')
plt.show()


Since this is a tricky model to fit, let's use HMC to fit the same data.


In [46]:
# Run MCMC on the noisy data
x0 = [[0.43, 0.16, 0.9, 0.27, 0.28, 0.26]] * 4
mcmc = pints.MCMCController(log_posterior, 4, x0, method=pints.HamiltonianMCMC)
mcmc.set_max_iterations(200)
mcmc.set_log_interval(1)

for sampler in mcmc.samplers():
    sampler.set_leapfrog_step_size([0.1, 0.01, 0.1, 0.03, 0.05, 0.05])
    sampler.set_leapfrog_steps(10)

start = time.time()
print('Running')
chains = mcmc.run()
print('Done!')
end = time.time()
diff=end-start


Running
Using Hamiltonian Monte Carlo
Generating 4 chains.
Running in sequential mode.
Iter. Eval. Accept.   Accept.   Accept.   Accept.   Time m:s
0     4      0         0         0         0          0:00.1
1     44     0.333     0.333     0.333     0          0:01.3
2     84     0.5       0.5       0.5       0.25       0:02.5
3     124    0.6       0.6       0.6       0.4        0:03.7
4     164    0.667     0.667     0.667     0.5        0:04.9
5     204    0.571     0.714     0.714     0.571      0:06.0
6     244    0.625     0.625     0.75      0.625      0:07.2
7     284    0.556     0.667     0.778     0.667      0:08.4
8     324    0.6       0.6       0.8       0.7        0:09.6
9     364    0.636     0.545     0.818     0.727      0:10.7
10    404    0.667     0.5       0.75      0.75       0:11.9
11    444    0.692     0.462     0.769     0.769      0:13.1
12    484    0.714     0.429     0.786     0.786      0:14.3
13    524    0.733     0.4       0.8       0.8        0:15.5
14    564    0.75      0.375     0.8125    0.8125     0:16.7
15    604    0.706     0.412     0.824     0.824      0:17.9
16    644    0.722     0.389     0.778     0.833      0:19.1
17    684    0.684     0.368     0.737     0.842      0:20.3
18    724    0.7       0.4       0.75      0.85       0:21.5
19    764    0.714     0.381     0.762     0.81       0:22.6
20    804    0.682     0.409     0.773     0.818      0:23.8
21    844    0.696     0.435     0.739     0.783      0:25.0
22    884    0.708     0.417     0.708     0.75       0:26.2
23    924    0.72      0.4       0.72      0.76       0:27.4
24    964    0.692     0.385     0.731     0.769      0:28.6
25    1004   0.667     0.407     0.704     0.741      0:29.8
26    1044   0.643     0.429     0.679     0.714      0:31.0
27    1084   0.621     0.448     0.655     0.724      0:32.2
28    1124   0.6       0.467     0.633     0.7        0:33.4
29    1164   0.581     0.483871  0.645     0.71       0:34.6
30    1204   0.59375   0.5       0.625     0.71875    0:35.8
31    1244   0.606     0.515     0.636     0.727      0:37.0
32    1284   0.618     0.529     0.618     0.735      0:38.2
33    1324   0.6       0.514     0.6       0.743      0:39.4
34    1364   0.611     0.528     0.583     0.722      0:40.6
35    1404   0.595     0.541     0.568     0.73       0:41.8
36    1444   0.605     0.526     0.553     0.711      0:43.0
37    1484   0.615     0.513     0.538     0.718      0:44.2
38    1524   0.625     0.5       0.525     0.725      0:45.4
39    1564   0.634     0.512     0.512     0.707      0:46.6
40    1604   0.619     0.524     0.524     0.714      0:47.8
41    1644   0.605     0.512     0.512     0.721      0:49.0
42    1684   0.614     0.523     0.5       0.705      0:50.3
43    1724   0.6       0.511     0.489     0.711      0:51.4
44    1764   0.587     0.5       0.478     0.696      0:52.6
45    1804   0.596     0.511     0.468     0.702      0:53.9
46    1844   0.604     0.5       0.458     0.6875     0:55.1
47    1884   0.612     0.49      0.449     0.673      0:56.3
48    1924   0.62      0.5       0.44      0.68       0:57.5
49    1964   0.627451  0.51      0.431     0.667      0:58.7
50    2004   0.635     0.5       0.423     0.654      0:59.9
51    2044   0.623     0.490566  0.415     0.642      1:01.2
52    2084   0.611     0.5       0.426     0.63       1:02.4
53    2124   0.6       0.491     0.418     0.618      1:03.6
54    2164   0.589     0.5       0.429     0.625      1:04.8
55    2204   0.579     0.491     0.421     0.632      1:06.0
56    2244   0.569     0.483     0.431     0.621      1:07.2
57    2284   0.559322  0.475     0.424     0.627      1:08.4
58    2324   0.567     0.483     0.417     0.617      1:09.6
59    2364   0.574     0.475     0.41      0.623      1:10.9
60    2404   0.581     0.468     0.403     0.629      1:12.1
61    2444   0.571     0.476     0.397     0.619      1:13.3
62    2484   0.5625    0.46875   0.390625  0.609375   1:14.5
63    2524   0.554     0.477     0.385     0.6        1:15.6
64    2564   0.545     0.469697  0.394     0.591      1:16.8
65    2604   0.552     0.463     0.388     0.582      1:18.0
66    2644   0.559     0.456     0.397     0.574      1:19.2
67    2684   0.551     0.449     0.391     0.565      1:20.4
68    2724   0.557     0.443     0.386     0.557      1:21.6
69    2764   0.549     0.437     0.394     0.549      1:22.8
70    2804   0.556     0.431     0.403     0.542      1:23.9
71    2844   0.548     0.425     0.397     0.534      1:25.1
72    2884   0.541     0.419     0.405     0.541      1:26.3
73    2924   0.533     0.427     0.413     0.533      1:27.5
74    2964   0.539     0.421     0.408     0.526      1:28.7
75    3004   0.545     0.416     0.416     0.519      1:29.9
76    3044   0.551     0.423     0.41      0.513      1:31.0
77    3084   0.544     0.418     0.405     0.506      1:32.2
78    3124   0.55      0.4125    0.4       0.5        1:33.4
79    3164   0.543     0.407     0.395     0.494      1:34.6
80    3204   0.549     0.402439  0.39      0.488      1:35.8
81    3244   0.554     0.41      0.386     0.482      1:37.0
82    3284   0.56      0.405     0.381     0.476      1:38.2
83    3324   0.565     0.4       0.376     0.471      1:39.4
84    3364   0.558     0.395     0.372093  0.477      1:40.5
85    3404   0.552     0.391     0.368     0.483      1:41.7
86    3444   0.557     0.398     0.375     0.477      1:42.9
87    3484   0.551     0.393     0.371     0.472      1:44.1
88    3524   0.544     0.389     0.367     0.467      1:45.3
89    3564   0.538     0.396     0.363     0.462      1:46.5
90    3604   0.533     0.391     0.359     0.457      1:47.6
91    3644   0.527     0.387     0.366     0.452      1:48.9
92    3684   0.521     0.383     0.362     0.447      1:50.0
93    3724   0.516     0.379     0.368     0.442      1:51.2
94    3764   0.521     0.375     0.365     0.4375     1:52.4
95    3804   0.526     0.371134  0.361     0.433      1:53.6
96    3844   0.52      0.367     0.367     0.429      1:54.8
97    3884   0.525     0.364     0.364     0.424      1:56.0
98    3924   0.53      0.36      0.36      0.42       1:57.2
99    3964   0.525     0.356     0.356     0.416      1:58.3
100   4004   0.529     0.363     0.353     0.412      1:59.4
101   4044   0.524     0.359     0.35      0.407767   2:00.6
102   4084   0.519     0.365     0.346     0.404      2:01.8
103   4124   0.524     0.371     0.343     0.41       2:03.0
104   4164   0.519     0.368     0.34      0.406      2:04.2
105   4204   0.523     0.364486  0.336     0.402      2:05.4
106   4244   0.528     0.37      0.333     0.407      2:06.6
107   4284   0.532     0.376     0.33      0.412844   2:07.8
108   4324   0.527     0.373     0.336     0.409      2:09.0
109   4364   0.532     0.369     0.333     0.414      2:10.2
110   4404   0.536     0.366     0.339     0.411      2:11.4
111   4444   0.531     0.363     0.336     0.416      2:12.6
112   4484   0.526     0.36      0.342     0.421      2:13.8
113   4524   0.522     0.365     0.339     0.417      2:15.0
114   4564   0.526     0.362069  0.345     0.414      2:16.2
115   4604   0.521     0.368     0.342     0.41       2:17.4
116   4644   0.517     0.364     0.339     0.415      2:18.6
117   4684   0.512605  0.361     0.336     0.42       2:19.8
118   4724   0.517     0.367     0.342     0.417      2:21.0
119   4764   0.521     0.372     0.338843  0.413      2:22.2
120   4804   0.516     0.377     0.336     0.41       2:23.4
121   4844   0.512     0.382     0.341     0.407      2:24.6
122   4884   0.508     0.387     0.339     0.411      2:25.8
123   4924   0.504     0.392     0.336     0.408      2:27.0
124   4964   0.5       0.389     0.333     0.405      2:28.2
125   5004   0.496063  0.394     0.339     0.402      2:29.4
126   5044   0.492     0.398     0.336     0.398      2:30.6
127   5084   0.488     0.403     0.333     0.403      2:31.8
128   5124   0.485     0.408     0.338     0.4        2:33.0
129   5164   0.480916  0.405     0.344     0.405      2:34.2
130   5204   0.477     0.409     0.348     0.409      2:35.4
131   5244   0.481203  0.406015  0.346     0.406015   2:36.6
132   5284   0.485     0.41      0.343     0.41       2:37.8
133   5324   0.489     0.407     0.348     0.415      2:39.1
134   5364   0.493     0.412     0.346     0.419      2:40.3
135   5404   0.489     0.416     0.343     0.416      2:41.5
136   5444   0.486     0.42      0.341     0.413      2:42.7
137   5484   0.489     0.424     0.338     0.41       2:43.9
138   5524   0.486     0.421     0.343     0.414      2:45.1
139   5564   0.482     0.418     0.34      0.418      2:46.3
140   5604   0.479     0.415493  0.338     0.415493   2:47.5
141   5644   0.476     0.413     0.336     0.413      2:48.7
142   5684   0.472     0.417     0.333     0.41       2:49.9
143   5724   0.469     0.421     0.331     0.414      2:51.1
144   5764   0.466     0.418     0.329     0.411      2:52.3
145   5804   0.462585  0.414966  0.327     0.408      2:53.5
146   5844   0.459     0.412     0.324     0.412      2:54.7
147   5884   0.456     0.409396  0.322     0.409396   2:55.9
148   5924   0.453     0.413     0.32      0.407      2:57.1
149   5964   0.45      0.417     0.318     0.410596   2:58.4
150   6004   0.447     0.414     0.316     0.414      2:59.6
151   6044   0.444     0.412     0.314     0.412      3:00.8
152   6084   0.442     0.409     0.312     0.409      3:01.9
153   6124   0.439     0.406     0.31      0.406      3:03.1
154   6164   0.436     0.404     0.308     0.404      3:04.3
155   6204   0.433121  0.401     0.312     0.401      3:05.5
156   6244   0.43      0.399     0.316     0.399      3:06.7
157   6284   0.427673  0.396     0.321     0.396      3:07.8
158   6324   0.425     0.39375   0.325     0.39375    3:09.0
159   6364   0.422     0.391     0.329     0.391      3:10.2
160   6404   0.426     0.389     0.333     0.389      3:11.4
161   6444   0.423     0.387     0.337     0.392638   3:12.6
162   6484   0.421     0.384     0.341     0.396      3:13.8
163   6524   0.418     0.382     0.339     0.394      3:15.0
164   6564   0.416     0.38      0.337     0.392      3:16.3
165   6604   0.413     0.377     0.341     0.389      3:17.5
166   6644   0.411     0.375     0.345     0.393      3:18.6
167   6684   0.408284  0.373     0.343     0.391      3:19.8
168   6724   0.406     0.371     0.347     0.388      3:21.1
169   6764   0.404     0.368     0.345     0.386      3:22.3
170   6804   0.401     0.366     0.349     0.39       3:23.4
171   6844   0.399     0.37      0.347     0.387      3:24.6
172   6884   0.397     0.368     0.351     0.385      3:25.9
173   6924   0.4       0.366     0.349     0.383      3:27.1
174   6964   0.398     0.364     0.347     0.386      3:28.3
175   7004   0.401     0.367     0.35      0.39       3:29.5
176   7044   0.404     0.365     0.348     0.388      3:30.7
177   7084   0.408     0.363     0.352     0.385      3:31.8
178   7124   0.411     0.361     0.356     0.389      3:33.1
179   7164   0.409     0.359116  0.359116  0.387      3:34.3
180   7204   0.407     0.357     0.363     0.385      3:35.5
181   7244   0.404     0.355     0.366     0.383      3:36.7
182   7284   0.402     0.353     0.37      0.38       3:37.8
183   7324   0.4       0.351     0.368     0.378      3:39.0
184   7364   0.398     0.349     0.371     0.376      3:40.2
185   7404   0.396     0.348     0.374     0.38       3:41.4
186   7444   0.393617  0.351     0.372     0.378      3:42.6
187   7484   0.392     0.354     0.37      0.381      3:43.8
188   7524   0.389     0.358     0.368     0.379      3:45.1
189   7564   0.387     0.356     0.366     0.382199   3:46.2
190   7604   0.385     0.359375  0.37      0.38       3:47.4
191   7644   0.383     0.363     0.373057  0.378      3:48.6
192   7684   0.381     0.361     0.371134  0.381      3:49.8
193   7724   0.379     0.364     0.369     0.379      3:51.0
194   7764   0.377551  0.362     0.372449  0.383      3:52.2
195   7804   0.376     0.36      0.371     0.381      3:53.4
196   7844   0.374     0.364     0.369     0.384      3:54.6
197   7884   0.377     0.367     0.367     0.382      3:55.8
198   7924   0.38      0.37      0.365     0.38       3:57.0
199   7964   0.383     0.368     0.363     0.378      3:58.2
200   7964   0.383     0.368     0.363     0.378      3:58.2
Halting: Maximum number of iterations (200) reached.
Done!

In [47]:
import pints.plot
pints.plot.trace(chains)
plt.show()


We get similar results as with adaptive covariance; except, the efficiency suffers due to having to calculate the sensitivities. Overall, for this problem, adaptive covariance performs favourably.


In [51]:
results = pints.MCMCSummary(chains=chains, parameter_names=["a", "b", "c", "d", "sigma_1", "sigma_2"], time=diff)
print(results)


param    mean    std.    2.5%    25%    50%    75%    97.5%    rhat    ess    ess per sec.
-------  ------  ------  ------  -----  -----  -----  -------  ------  -----  --------------
a        0.30    0.04    0.22    0.27   0.29   0.33   0.41     1.04    15.74  0.07
b        0.12    0.02    0.09    0.11   0.12   0.13   0.17     1.03    16.79  0.07
c        1.34    0.19    0.94    1.21   1.36   1.47   1.71     1.05    15.04  0.06
d        0.40    0.06    0.29    0.37   0.41   0.44   0.51     1.05    14.74  0.06
sigma_1  0.28    0.04    0.21    0.25   0.28   0.31   0.37     1.06    44.07  0.19
sigma_2  0.51    0.09    0.37    0.44   0.50   0.57   0.69     1.11    24.75  0.10