Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use full mass matrix for globals in SIR example #2428

Merged
merged 2 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions examples/sir_hmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def discrete_model(data, population):
# parameter values and poutine.trace to record sample observations.

def generate_data(args):
logging.info("Generating data...")
params = {"R0": torch.tensor(args.basic_reproduction_number),
"tau": torch.tensor(args.recovery_time),
"rho": torch.tensor(args.response_rate)}
Expand All @@ -110,11 +111,14 @@ def generate_data(args):
data = torch.stack([site["value"]
for site in tr.trace.nodes.values()
if site["name"].startswith("obs_")])
if data.sum() > 2:
break
logging.info("Generated {:0.0f} observed infections:\n{}"
.format(data.sum(), " ".join([str(int(x)) for x in data])))
return data
if data.sum() >= args.min_observations:
logging.info("Generated {:0.0f} observed infections:\n{}"
.format(data.sum(), " ".join([str(int(x)) for x in data])))
return data

raise ValueError("Failed to generate {} observations. Try increasing "
"--population or decreasing --min-observations"
.format(args.min_observations))


# Inference
Expand Down Expand Up @@ -189,6 +193,7 @@ def infer_hmc_enum(args, data):
def _infer_hmc(args, data, model, init_values={}):
logging.info("Running inference...")
kernel = NUTS(model,
full_mass=[("R0", "tau", "rho")],
max_tree_depth=args.max_tree_depth,
init_strategy=init_to_value(values=init_values),
jit_compile=args.jit, ignore_jit_warnings=True)
Expand Down Expand Up @@ -564,6 +569,7 @@ def main(args):
assert pyro.__version__.startswith('1.3.1')
parser = argparse.ArgumentParser(description="SIR outbreak modeling using HMC")
parser.add_argument("-p", "--population", default=10, type=int)
parser.add_argument("-m", "--min-observations", default=3, type=int)
parser.add_argument("-d", "--duration", default=10, type=int)
parser.add_argument("-f", "--forecast", default=0, type=int)
parser.add_argument("-R0", "--basic-reproduction-number", default=1.5, type=float)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
'rsa/schelling.py --num-samples=10',
'rsa/schelling_false.py --num-samples=10',
'rsa/semantic_parsing.py --num-samples=10',
'sir_hmc.py -w=2 -n=4 -d=2 --enum',
'sir_hmc.py -w=2 -n=4 -d=2 -m=1 --enum',
'sir_hmc.py -w=2 -n=4 -d=2 -p=10000 --sequential',
'sir_hmc.py -w=2 -n=4 -d=100 -p=10000 -f 2',
'sir_hmc.py -w=2 -n=4 -d=100 -p=10000 --dct',
Expand Down Expand Up @@ -119,7 +119,7 @@
'hmm.py --num-steps=1 --truncate=10 --model=4 --tmc --tmc-num-samples=2 --cuda',
'hmm.py --num-steps=1 --truncate=10 --model=5 --tmc --tmc-num-samples=2 --cuda',
'hmm.py --num-steps=1 --truncate=10 --model=6 --tmc --tmc-num-samples=2 --cuda',
'sir_hmc.py -w=2 -n=4 -d=2 --enum --cuda',
'sir_hmc.py -w=2 -n=4 -d=2 -m=1 --enum --cuda',
'sir_hmc.py -w=2 -n=4 -d=2 -p=10000 --sequential --cuda',
'sir_hmc.py -w=2 -n=4 -d=100 -p=10000 --cuda',
'sir_hmc.py -w=2 -n=4 -d=100 -p=10000 --dct --cuda',
Expand Down Expand Up @@ -160,7 +160,7 @@ def xfail_jit(*args):
'lda.py --num-steps=2 --num-words=100 --num-docs=100 --num-words-per-doc=8 --jit',
'minipyro.py --backend=pyro --jit',
'minipyro.py --jit',
'sir_hmc.py -w=2 -n=4 -d=2 --enum --jit',
'sir_hmc.py -w=2 -n=4 -d=2 -m=1 --enum --jit',
'sir_hmc.py -w=2 -n=4 -d=2 -p=10000 --sequential --jit',
'sir_hmc.py -w=2 -n=4 -p=10000 --jit',
'sir_hmc.py -w=2 -n=4 -p=10000 --dct --jit',
Expand Down