-
Notifications
You must be signed in to change notification settings - Fork 5
/
plant_pol_inference.py
141 lines (117 loc) · 4.82 KB
/
plant_pol_inference.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Python functions to interact with the Stan model.
Author:
Jean-Gabriel Young <jgyou@umich.edu>
"""
import numpy as np
import pickle
import pystan
import os
abs_path = os.path.dirname(os.path.abspath(__file__))
# =============================================================================
# Model functions
# =============================================================================
def compile_stan_model(force=False):
"""Autocompile Stan model."""
source_path = os.path.join(abs_path, 'model.stan')
target_path = os.path.join(abs_path, 'model.bin')
if os.path.exists(target_path):
# Test whether the model has changed and only compile if it did
with open(target_path, 'rb') as f:
current_model = pickle.load(f)
with open(source_path, 'r') as f:
file_content = "".join([line for line in f])
if file_content != current_model.model_code or force:
print(target_path, "[Compiling]", ["", "[Forced]"][force])
model = pystan.StanModel(source_path, model_name="plant_pol")
with open(target_path, 'wb') as f:
pickle.dump(model, f)
else:
print(target_path, "[Skipping --- already compiled]")
else:
# If model binary does not exist, compile it
print(target_path, "[Compiling]")
model = pystan.StanModel(source_path, model_name="plant_pol")
with open(target_path, 'wb') as f:
pickle.dump(model, f)
def load_model():
"""Load the model to memory."""
compile_stan_model()
with open(os.path.join(abs_path, "model.bin"), 'rb') as f:
return pickle.load(f)
# =============================================================================
# Sampling functions
# =============================================================================
def generate_sample(M, model, num_chains=4, warmup=5000, num_samples=500):
"""Run sampling for data matrix M."""
# Prepare the data dictionary
data = dict()
data = {"n_p": M.shape[0],
"n_a": M.shape[1],
"M": M}
samples = model.sampling(data=data,
chains=4,
iter=warmup + num_samples,
warmup=warmup,
control={'max_treedepth': 15})
return samples
def save_samples(samples, fpath='samples.bin'):
"""Save samples as binaries, with pickle.
Warning
-------
To retrieve this data, one has to load *the exact version of the model*
used to generate the samples in memory. Hence, re-compiling the model will
make the data inaccessible.
"""
with open(fpath, 'wb') as f:
pickle.dump(samples, f)
def load_samples(fpath='samples.bin'):
"""Load samples from binaries, with pickle.
Warning
-------
Must have loaded *the same version of the model* in memory.
"""
with open(fpath, 'rb') as f:
return pickle.load(f)
def test_samples(samples, tol=0.1, num_chains=4):
"""Verify that no chain has a markedly lower average log-probability."""
n = len(samples['lp__']) // num_chains # number of samples per chain
log_probs = [samples['lp__'][i * n:(i + 1) * n] for i in range(num_chains)]
log_probs_means = np.array([np.mean(lp) for lp in log_probs])
return np.alltrue(log_probs_means - (1 - tol) * max(log_probs_means) > 0)
# =============================================================================
# Inference functions
# =============================================================================
def get_posterior_predictive_matrix(samples):
"""Calculate the posterior predictive matrix."""
Q = samples['Q']
C = samples['C']
r = samples['r']
ones = np.ones((len(samples['lp__']), Q.shape[1], Q.shape[2]))
sigma_tau = np.einsum('ki,kj->kij', samples['sigma'], samples['tau'])
accu = (1 - Q) * np.einsum('kij,k->kij', ones, C) * sigma_tau
accu += Q * np.einsum('kij,k->kij', ones, C * (1 + r)) * sigma_tau
return np.mean(accu, axis=0)
def estimate_network(samples):
"""Return the matrix of edge probabilities P(B_ij=1)."""
return np.mean(samples['Q'], axis=0)
def get_network_property_distribution(samples, property, num_net=10):
"""Return the average posterior value of an arbitrary network property.
Input
-----
samples: StanFit object
The posterior samples.
property: function
This function should take an incidence matrix as input and return a
scalar.
num_net: int
Number of networks to generate for each parameter samples.
"""
values = np.zeros(len(samples['lp__']) * num_net)
for i, Q in enumerate(samples['Q']):
for j in range(num_net):
B = np.random.binomial(n=1, p=Q)
values[i * num_net + j] = property(B)
return values