-
-
Notifications
You must be signed in to change notification settings - Fork 982
/
cjs.py
386 lines (334 loc) · 14 KB
/
cjs.py
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
# Copyright (c) 2017-2019 Uber Technologies, Inc.
# SPDX-License-Identifier: Apache-2.0
"""
We show how to implement several variants of the Cormack-Jolly-Seber (CJS)
[4, 5, 6] model used in ecology to analyze animal capture-recapture data.
For a discussion of these models see reference [1].
We make use of two datasets:
-- the European Dipper (Cinclus cinclus) data from reference [2]
(this is Norway's national bird).
-- the meadow voles data from reference [3].
Compare to the Stan implementations in [7].
References
[1] Kery, M., & Schaub, M. (2011). Bayesian population analysis using
WinBUGS: a hierarchical perspective. Academic Press.
[2] Lebreton, J.D., Burnham, K.P., Clobert, J., & Anderson, D.R. (1992).
Modeling survival and testing biological hypotheses using marked animals:
a unified approach with case studies. Ecological monographs, 62(1), 67-118.
[3] Nichols, Pollock, Hines (1984) The use of a robust capture-recapture design
in small mammal population studies: A field example with Microtus pennsylvanicus.
Acta Theriologica 29:357-365.
[4] Cormack, R.M., 1964. Estimates of survival from the sighting of marked animals.
Biometrika 51, 429-438.
[5] Jolly, G.M., 1965. Explicit estimates from capture-recapture data with both death
and immigration-stochastic model. Biometrika 52, 225-247.
[6] Seber, G.A.F., 1965. A note on the multiple recapture census. Biometrika 52, 249-259.
[7] https://github.com/stan-dev/example-models/tree/master/BPA/Ch.07
"""
import argparse
import os
import numpy as np
import torch
import pyro
import pyro.distributions as dist
import pyro.poutine as poutine
from pyro.infer import SVI, TraceEnum_ELBO, TraceTMC_ELBO
from pyro.infer.autoguide import AutoDiagonalNormal
from pyro.optim import Adam
"""
Our first and simplest CJS model variant only has two continuous
(scalar) latent random variables: i) the survival probability phi;
and ii) the recapture probability rho. These are treated as fixed
effects with no temporal or individual/group variation.
"""
def model_1(capture_history, sex):
N, T = capture_history.shape
phi = pyro.sample("phi", dist.Uniform(0.0, 1.0)) # survival probability
rho = pyro.sample("rho", dist.Uniform(0.0, 1.0)) # recapture probability
with pyro.plate("animals", N, dim=-1):
z = torch.ones(N)
# we use this mask to eliminate extraneous log probabilities
# that arise for a given individual before its first capture.
first_capture_mask = torch.zeros(N).bool()
for t in pyro.markov(range(T)):
with poutine.mask(mask=first_capture_mask):
mu_z_t = first_capture_mask.float() * phi * z + (
1 - first_capture_mask.float()
)
# we use parallel enumeration to exactly sum out
# the discrete states z_t.
z = pyro.sample(
"z_{}".format(t),
dist.Bernoulli(mu_z_t),
infer={"enumerate": "parallel"},
)
mu_y_t = rho * z
pyro.sample(
"y_{}".format(t), dist.Bernoulli(mu_y_t), obs=capture_history[:, t]
)
first_capture_mask |= capture_history[:, t].bool()
"""
In our second model variant there is a time-varying survival probability phi_t for
T-1 of the T time periods of the capture data; each phi_t is treated as a fixed effect.
"""
def model_2(capture_history, sex):
N, T = capture_history.shape
rho = pyro.sample("rho", dist.Uniform(0.0, 1.0)) # recapture probability
z = torch.ones(N)
first_capture_mask = torch.zeros(N).bool()
# we create the plate once, outside of the loop over t
animals_plate = pyro.plate("animals", N, dim=-1)
for t in pyro.markov(range(T)):
# note that phi_t needs to be outside the plate, since
# phi_t is shared across all N individuals
phi_t = (
pyro.sample("phi_{}".format(t), dist.Uniform(0.0, 1.0)) if t > 0 else 1.0
)
with animals_plate, poutine.mask(mask=first_capture_mask):
mu_z_t = first_capture_mask.float() * phi_t * z + (
1 - first_capture_mask.float()
)
# we use parallel enumeration to exactly sum out
# the discrete states z_t.
z = pyro.sample(
"z_{}".format(t),
dist.Bernoulli(mu_z_t),
infer={"enumerate": "parallel"},
)
mu_y_t = rho * z
pyro.sample(
"y_{}".format(t), dist.Bernoulli(mu_y_t), obs=capture_history[:, t]
)
first_capture_mask |= capture_history[:, t].bool()
"""
In our third model variant there is a survival probability phi_t for T-1
of the T time periods of the capture data (just like in model_2), but here
each phi_t is treated as a random effect.
"""
def model_3(capture_history, sex):
def logit(p):
return torch.log(p) - torch.log1p(-p)
N, T = capture_history.shape
phi_mean = pyro.sample(
"phi_mean", dist.Uniform(0.0, 1.0)
) # mean survival probability
phi_logit_mean = logit(phi_mean)
# controls temporal variability of survival probability
phi_sigma = pyro.sample("phi_sigma", dist.Uniform(0.0, 10.0))
rho = pyro.sample("rho", dist.Uniform(0.0, 1.0)) # recapture probability
z = torch.ones(N)
first_capture_mask = torch.zeros(N).bool()
# we create the plate once, outside of the loop over t
animals_plate = pyro.plate("animals", N, dim=-1)
for t in pyro.markov(range(T)):
phi_logit_t = (
pyro.sample(
"phi_logit_{}".format(t), dist.Normal(phi_logit_mean, phi_sigma)
)
if t > 0
else torch.tensor(0.0)
)
phi_t = torch.sigmoid(phi_logit_t)
with animals_plate, poutine.mask(mask=first_capture_mask):
mu_z_t = first_capture_mask.float() * phi_t * z + (
1 - first_capture_mask.float()
)
# we use parallel enumeration to exactly sum out
# the discrete states z_t.
z = pyro.sample(
"z_{}".format(t),
dist.Bernoulli(mu_z_t),
infer={"enumerate": "parallel"},
)
mu_y_t = rho * z
pyro.sample(
"y_{}".format(t), dist.Bernoulli(mu_y_t), obs=capture_history[:, t]
)
first_capture_mask |= capture_history[:, t].bool()
"""
In our fourth model variant we include group-level fixed effects
for sex (male, female).
"""
def model_4(capture_history, sex):
N, T = capture_history.shape
# survival probabilities for males/females
phi_male = pyro.sample("phi_male", dist.Uniform(0.0, 1.0))
phi_female = pyro.sample("phi_female", dist.Uniform(0.0, 1.0))
# we construct a N-dimensional vector that contains the appropriate
# phi for each individual given its sex (female = 0, male = 1)
phi = sex * phi_male + (1.0 - sex) * phi_female
rho = pyro.sample("rho", dist.Uniform(0.0, 1.0)) # recapture probability
with pyro.plate("animals", N, dim=-1):
z = torch.ones(N)
# we use this mask to eliminate extraneous log probabilities
# that arise for a given individual before its first capture.
first_capture_mask = torch.zeros(N).bool()
for t in pyro.markov(range(T)):
with poutine.mask(mask=first_capture_mask):
mu_z_t = first_capture_mask.float() * phi * z + (
1 - first_capture_mask.float()
)
# we use parallel enumeration to exactly sum out
# the discrete states z_t.
z = pyro.sample(
"z_{}".format(t),
dist.Bernoulli(mu_z_t),
infer={"enumerate": "parallel"},
)
mu_y_t = rho * z
pyro.sample(
"y_{}".format(t), dist.Bernoulli(mu_y_t), obs=capture_history[:, t]
)
first_capture_mask |= capture_history[:, t].bool()
"""
In our final model variant we include both fixed group effects and fixed
time effects for the survival probability phi:
logit(phi_t) = beta_group + gamma_t
We need to take care that the model is not overparameterized; to do this
we effectively let a single scalar beta encode the difference in male
and female survival probabilities.
"""
def model_5(capture_history, sex):
N, T = capture_history.shape
# phi_beta controls the survival probability differential
# for males versus females (in logit space)
phi_beta = pyro.sample("phi_beta", dist.Normal(0.0, 10.0))
phi_beta = sex * phi_beta
rho = pyro.sample("rho", dist.Uniform(0.0, 1.0)) # recapture probability
z = torch.ones(N)
first_capture_mask = torch.zeros(N).bool()
# we create the plate once, outside of the loop over t
animals_plate = pyro.plate("animals", N, dim=-1)
for t in pyro.markov(range(T)):
phi_gamma_t = (
pyro.sample("phi_gamma_{}".format(t), dist.Normal(0.0, 10.0))
if t > 0
else 0.0
)
phi_t = torch.sigmoid(phi_beta + phi_gamma_t)
with animals_plate, poutine.mask(mask=first_capture_mask):
mu_z_t = first_capture_mask.float() * phi_t * z + (
1 - first_capture_mask.float()
)
# we use parallel enumeration to exactly sum out
# the discrete states z_t.
z = pyro.sample(
"z_{}".format(t),
dist.Bernoulli(mu_z_t),
infer={"enumerate": "parallel"},
)
mu_y_t = rho * z
pyro.sample(
"y_{}".format(t), dist.Bernoulli(mu_y_t), obs=capture_history[:, t]
)
first_capture_mask |= capture_history[:, t].bool()
models = {
name[len("model_") :]: model
for name, model in globals().items()
if name.startswith("model_")
}
def main(args):
pyro.set_rng_seed(0)
pyro.clear_param_store()
# load data
if args.dataset == "dipper":
capture_history_file = (
os.path.dirname(os.path.abspath(__file__)) + "/dipper_capture_history.csv"
)
elif args.dataset == "vole":
capture_history_file = (
os.path.dirname(os.path.abspath(__file__))
+ "/meadow_voles_capture_history.csv"
)
else:
raise ValueError("Available datasets are 'dipper' and 'vole'.")
capture_history = torch.tensor(
np.genfromtxt(capture_history_file, delimiter=",")
).float()[:, 1:]
N, T = capture_history.shape
print(
"Loaded {} capture history for {} individuals collected over {} time periods.".format(
args.dataset, N, T
)
)
if args.dataset == "dipper" and args.model in ["4", "5"]:
sex_file = os.path.dirname(os.path.abspath(__file__)) + "/dipper_sex.csv"
sex = torch.tensor(np.genfromtxt(sex_file, delimiter=",")).float()[:, 1]
print("Loaded dipper sex data.")
elif args.dataset == "vole" and args.model in ["4", "5"]:
raise ValueError(
"Cannot run model_{} on meadow voles data, since we lack sex "
"information for these animals.".format(args.model)
)
else:
sex = None
model = models[args.model]
# we use poutine.block to only expose the continuous latent variables
# in the models to AutoDiagonalNormal (all of which begin with 'phi'
# or 'rho')
def expose_fn(msg):
return msg["name"][0:3] in ["phi", "rho"]
# we use a mean field diagonal normal variational distributions (i.e. guide)
# for the continuous latent variables.
guide = AutoDiagonalNormal(poutine.block(model, expose_fn=expose_fn))
# since we enumerate the discrete random variables,
# we need to use TraceEnum_ELBO or TraceTMC_ELBO.
optim = Adam({"lr": args.learning_rate})
if args.tmc:
elbo = TraceTMC_ELBO(max_plate_nesting=1)
tmc_model = poutine.infer_config(
model,
lambda msg: (
{"num_samples": args.tmc_num_samples, "expand": False}
if msg["infer"].get("enumerate", None) == "parallel"
else {}
),
) # noqa: E501
svi = SVI(tmc_model, guide, optim, elbo)
else:
elbo = TraceEnum_ELBO(
max_plate_nesting=1, num_particles=20, vectorize_particles=True
)
svi = SVI(model, guide, optim, elbo)
losses = []
print(
"Beginning training of model_{} with Stochastic Variational Inference.".format(
args.model
)
)
for step in range(args.num_steps):
loss = svi.step(capture_history, sex)
losses.append(loss)
if step % 20 == 0 and step > 0 or step == args.num_steps - 1:
print("[iteration %03d] loss: %.3f" % (step, np.mean(losses[-20:])))
# evaluate final trained model
elbo_eval = TraceEnum_ELBO(
max_plate_nesting=1, num_particles=2000, vectorize_particles=True
)
svi_eval = SVI(model, guide, optim, elbo_eval)
print("Final loss: %.4f" % svi_eval.evaluate_loss(capture_history, sex))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="CJS capture-recapture model for ecological data"
)
parser.add_argument(
"-m",
"--model",
default="1",
type=str,
help="one of: {}".format(", ".join(sorted(models.keys()))),
)
parser.add_argument("-d", "--dataset", default="dipper", type=str)
parser.add_argument("-n", "--num-steps", default=400, type=int)
parser.add_argument("-lr", "--learning-rate", default=0.002, type=float)
parser.add_argument(
"--tmc",
action="store_true",
help="Use Tensor Monte Carlo instead of exact enumeration "
"to estimate the marginal likelihood. You probably don't want to do this, "
"except to see that TMC makes Monte Carlo gradient estimation feasible "
"even with very large numbers of non-reparametrized variables.",
)
parser.add_argument("--tmc-num-samples", default=10, type=int)
args = parser.parse_args()
main(args)