-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_sbi_save.py
240 lines (170 loc) · 6.58 KB
/
python_sbi_save.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
import numpy as np
import scipy as scp
import scipy.stats as ss
from scipy.optimize import minimize
import matplotlib.pyplot as plt
from scipy import sparse
from scipy.sparse.linalg import spsolve
from mpl_toolkits import mplot3d
from matplotlib import cm
import scipy.special as scsp
from scipy.integrate import quad
from scipy.interpolate import RegularGridInterpolator
import torch
import torch.nn as nn
import torch.nn.functional as F
from sbi import utils
from sbi import analysis
from sbi import inference
from sbi.inference import SNPE, simulate_for_sbi, prepare_for_sbi
from sbi.inference.base import infer
from matplotlib import pyplot as plt
seed = 0
torch.manual_seed(seed)
#hyper parameters
## how many convolution layers, number of channels, and kernel size
## Pooling kernel size and stride
## Fully connected layer out features
## As an example lets give 1000 point time series
## With a single convolution layer, 6 kernels kernel size of 10 - lets read up on the justification of the chossen kernel size and the desired architecture, make sure to include padding to avoid cyclical convolution
##
## Max pooling layer, benifits of max pooling in 1d, pick artbitrary say reduce 1000 timeseries to 100
## Finally use the fully connected layer to condense it down to the desired summary statistics of dimension 2 or 3, 2 if we have enough data, 3 if we do not
class SummaryNet(nn.Module):
def __init__(self):
super().__init__()
# 2D convolutional layer
self.conv1 = nn.Conv1d(in_channels=1, out_channels=6, kernel_size=100)
# Maxpool layer that reduces time series from large to small
self.pool = nn.MaxPool1d(kernel_size=10, stride=10)
# Fully connected layer taking as input the 6 flattened output arrays from the maxpooling layer
self.fc = nn.Linear(in_features=2940, out_features=3)
def forward(self, x):
x = x.view(-1, 1, 5000)
x = self.pool(F.relu(self.conv1(x)))
x = x.view(-1, 2940)
x = F.relu(self.fc(x))
return x
embedding_net = SummaryNet()
#OU process, without noise we use this directly as our simulator
def ou_process(params):
params = np.asarray(params)
N = 5000 # time steps
paths = 2 # number of paths
T = 50
T_vec, dt = np.linspace(0, T, N, retstep=True)
kappa = params[0]
theta = 0
sigma = params[1]
std_asy = np.sqrt(sigma**2 / (2 * kappa)) # asymptotic standard deviation
X0 = 2
X = np.zeros((paths, N))
X[:, 0] = X0
W = ss.norm.rvs(loc=0, scale=1, size=(paths, N - 1))
# Uncomment for Euler Maruyama
# for t in range(0,N-1):
# X[:,t+1] = X[:,t] + kappa*(theta - X[:,t])*dt + sigma * np.sqrt(dt) * W[:,t]
std_dt = np.sqrt(sigma**2 / (2 * kappa) * (1 - np.exp(-2 * kappa * dt)))
std_dt = np.sqrt(sigma**2 / (2 * kappa) * (1 - np.exp(-2 * kappa * dt)))
for t in range(0, N - 1):
X[:, t + 1] = theta + np.exp(-kappa * dt) * (X[:, t] - theta) + std_dt * W[:, t]
X_T = X[:, -1] # values of X at time T
return X[1, :]
# Implement Known summary Statistics for ann OU process
def OU_summary(data):
if len(data) == 0:
raise ValueError("Input array is empty")
# Calculate the sample mean
mean = np.mean(data)
# Calculate the sample variance
variance = np.var(data, ddof=1)
# Calculate the sample covariance matrix
autocorrelation_at_lag1 = np.corrcoef(data[:-1], data[1:])[0, 1]
#to deal with shorter samples
initial_value = data[0]
return [mean, variance, autocorrelation_at_lag1, initial_value]
#implement new simulator with handpicked summary statistics
def OU_summary_hand(params):
return OU_summary(ou_process(params))
# Test Stuff to make sure these functions are working
test_summary = OU_summary_hand(ou_process([10,11]))
### SBI implementation/training
#set the simulation number
sim_num = 300000
## Defining the prior
num_dim = 2
prior = utils.BoxUniform(low=0 * torch.ones(num_dim), high=5 * torch.ones(num_dim))
# make a SBI-wrapper on the simulator object for compatibility
simulator_wrapper, prior = prepare_for_sbi(OU_summary_hand, prior)
#generate an observation
observation = OU_summary_hand([1, 1])
observation2 = OU_summary_hand([2,3])
observation3 = OU_summary_hand([4,1])
# For teting purposes, check if our simulator is consistent with expectations
plt.plot(observation)
plt.savefig("ou_1_1_1.png")
'''
#define the arhitecture of the conditional neural density estimato
neural_posterior = utils.posterior_nn(
model="maf", embedding_net=embedding_net, hidden_features=10, num_transforms=2
)
'''
#define the arhitecture of the conditional neural density estimato
neural_posterior = utils.posterior_nn(
model="maf", hidden_features=10, num_transforms=2
)
#Create the inference object
inference = SNPE(prior=prior, density_estimator=neural_posterior)
#Sample the parameters from out simulator
a, b = simulate_for_sbi(simulator_wrapper, prior, num_simulations=sim_num)
torch.save((a,b), f'./Models/OU_samples_{sim_num}')
#Give the simulator our parameters
inference = inference.append_simulations(a, b)
#train the neural network
density_estimator = inference.train(stop_after_epochs=100)
#save our model
torch.save(density_estimator, './Models/test_save')
torch.save(embedding_net, './Models/embedding_save')
#generate the posterior from our neural density estimator
posterior = inference.build_posterior(density_estimator)
#sample from our posterior
posterior_samples = posterior.sample((50000,), x=observation)
### Graphing Stuff
post_graph = analysis.pairplot(
posterior_samples,
points=torch.tensor([1, 1]),
limits=[[0, 5], [0, 5]],
points_colors="r",
points_offdiag={"markersize": 6},
figsize=(6, 6)
)
plt.savefig(f"plt_graph_111_summary_3_nonn_{sim_num}.png")
posterior_samples2 = posterior.sample((50000,), x=observation2)
### Graphing Stuff
post_graph = analysis.pairplot(
posterior_samples2,
points=torch.tensor([2, 3]),
limits=[[0, 5], [0, 5]],
points_colors="r",
points_offdiag={"markersize": 6},
figsize=(6, 6)
)
plt.savefig(f"plt_graph_23_summary_3_nonn_{sim_num}.png")
posterior_samples3 = posterior.sample((50000,), x=observation3)
### Graphing Stuff
post_graph = analysis.pairplot(
posterior_samples3,
points=torch.tensor([4, 1]),
limits=[[0, 5], [0, 5]],
points_colors="r",
points_offdiag={"markersize": 6},
figsize=(6, 6)
)
plt.savefig(f"plt_graph_41_summary_3_nonn_{sim_num}.png")
''' Debugging Code
# List the methods of the object using dir()
methods = dir(density_estimator)
# Print the list of methods
for method in methods:
print(method)
'''