-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsbi_demo_utils.py
235 lines (186 loc) · 9.15 KB
/
sbi_demo_utils.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
from hydra import initialize, compose
import hydra
import os
import torch
from src.model.base import BaseTransformer
from sbi.inference.snpe.snpe_c import SNPE_C
import numpy as np
from scipy import stats
def update_plot_style():
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams.update({
'font.family': 'times',
'font.size': 14.0,
'lines.linewidth': 2,
'lines.antialiased': True,
'axes.facecolor': 'fdfdfd',
'axes.edgecolor': '777777',
'axes.linewidth': 1,
'axes.titlesize': 'medium',
'axes.labelsize': 'medium',
'axes.axisbelow': True,
'xtick.major.size': 0, # major tick size in points
'xtick.minor.size': 0, # minor tick size in points
'xtick.major.pad': 6, # distance to major tick label in points
'xtick.minor.pad': 6, # distance to the minor tick label in points
'xtick.color': '333333', # color of the tick labels
'xtick.labelsize': 'medium', # fontsize of the tick labels
'xtick.direction': 'in', # direction: in or out
'ytick.major.size': 0, # major tick size in points
'ytick.minor.size': 0, # minor tick size in points
'ytick.major.pad': 6, # distance to major tick label in points
'ytick.minor.pad': 6, # distance to the minor tick label in points
'ytick.color': '333333', # color of the tick labels
'ytick.labelsize': 'medium', # fontsize of the tick labels
'ytick.direction': 'in', # direction: in or out
'axes.grid': False,
'grid.alpha': 0.3,
'grid.linewidth': 1,
'legend.fancybox': True,
'legend.fontsize': 'Small',
'figure.figsize': (2.5, 2.5),
'figure.facecolor': '1.0',
'figure.edgecolor': '0.5',
'hatch.linewidth': 0.1,
'text.usetex': True
})
plt.rcParams['text.latex.preamble'] = r'\usepackage{times}'
def load_config_and_model(
path, config_name="config.yaml", ckpt_name="ckpt.tar"
):
"""
Loads configuration and model from a specified path. Instantiates the model components
(embedder, encoder, and head) based on the configuration, and loads the model's checkpoint.
"""
config_path = path+".hydra/"
with initialize(version_base=None, config_path=config_path):
cfg = compose(config_name=config_name)
embedder = hydra.utils.instantiate(
cfg.embedder,
dim_xc=cfg.dataset.dim_input,
dim_yc=cfg.dataset.dim_tar,
num_latent=cfg.dataset.num_latent,
)
encoder = hydra.utils.instantiate(cfg.encoder)
head = hydra.utils.instantiate(cfg.target_head)
model = BaseTransformer(embedder, encoder, head)
ckpt = torch.load(os.path.join(path, ckpt_name), map_location="cpu")
model.load_state_dict(ckpt["model"])
return cfg, model
def train_npe(prior, theta_npe, x_npe):
"""
Trains a neural posterior estimator (NPE) using the provided prior and simulations,
then builds and returns the posterior distribution.
"""
inference = SNPE_C(prior=prior)
density_estimator = inference.append_simulations(theta_npe, x_npe).train()
posterior = inference.build_posterior(density_estimator)
return posterior
def RMSE(gt, samples):
"""
Computes the Root Mean Squared Error (RMSE) between the ground truth and a set of samples.
"""
gt = gt.expand(-1, -1, samples.shape[-1])
dist = torch.sqrt(torch.mean((gt-samples)**2))
return dist
def get_coverage_probs(z, u):
"""Vectorized function to compute the minimal coverage probability for uniform ECDFs given evaluation points z and a sample of samples u."""
N = u.shape[1]
F_m = np.sum((z[:, np.newaxis] >= u[:, np.newaxis, :]), axis=-1) / u.shape[1]
bin1 = stats.binom(N, z).cdf(N * F_m)
bin2 = stats.binom(N, z).cdf(N * F_m - 1)
gamma = 2 * np.min(np.min(np.stack([bin1, 1 - bin2], axis=-1), axis=-1), axis=-1)
return gamma
def simultaneous_ecdf_bands(num_samples, num_points=None, num_simulations=1000, confidence=0.95, eps=1e-5, max_num_points=1000):
"""Computes the simultaneous ECDF bands through simulation according to the algorithm described."""
N = num_samples
if num_points is None:
K = min(N, max_num_points)
else:
K = min(num_points, max_num_points)
M = num_simulations
z = np.linspace(0 + eps, 1 - eps, K)
u = np.random.uniform(size=(M, N))
alpha = 1 - confidence
gammas = get_coverage_probs(z, u)
gamma = np.percentile(gammas, 100 * alpha)
L = stats.binom(N, z).ppf(gamma / 2) / N
U = stats.binom(N, z).ppf(1 - gamma / 2) / N
return alpha, z, L, U
def plot_sbc_ecdf_diff(ax, theta_prior, theta_posterior, theta_posterior_pi, num_points=20):
"""
Plot the ECDF difference for Simulation-Based Calibration (SBC).
Args:
- theta_prior: torch.Tensor, shape [num_samples, 1], samples drawn from the prior distribution.
- theta_posterior: torch.Tensor, shape [num_samples, num_posterior_samples], posterior samples for each theta_prior.
- num_points: int, optional, the number of points along the x-axis to control precision (default is 20).
"""
num_samples, num_posterior_samples = theta_posterior.shape
# Expand theta_prior to match the shape of theta_posterior
theta_prior_expanded = theta_prior.expand(-1, num_posterior_samples)
# Calculate the rank of each theta_prior in the corresponding posterior samples
less_than = (theta_posterior < theta_prior_expanded).long()
ranks = torch.sum(less_than, dim=1)
less_than_pi = (theta_posterior_pi < theta_prior_expanded).long()
ranks_pi = torch.sum(less_than_pi, dim=1)
# Calculate the fractional rank (normalize ranks by num_posterior_samples)
fractional_ranks = ranks.float() / num_posterior_samples
fractional_ranks_pi = ranks_pi.float() / num_posterior_samples
# Calculate the ECDF of the fractional ranks
sorted_ranks = torch.sort(fractional_ranks)[0].numpy()
sorted_ranks_pi = torch.sort(fractional_ranks_pi)[0].numpy()
ecdf = np.arange(1, num_samples + 1) / num_samples
# Calculate the difference between ECDF and the uniform distribution
uniform_cdf = sorted_ranks # In uniform distribution, CDF is the same as the rank values
uniform_cdf_pi = sorted_ranks_pi
ecdf_diff = ecdf - uniform_cdf
ecdf_diff_pi = ecdf - uniform_cdf_pi
# Generate points for the x-axis (fractional rank statistics)
x_points = np.linspace(0, 1, num_points)
# Interpolate ECDF difference for these x_points
ecdf_diff_interpolated = np.interp(x_points, sorted_ranks, ecdf_diff)
ecdf_diff_interpolated_pi = np.interp(x_points, sorted_ranks_pi, ecdf_diff_pi)
_, z, L, U = simultaneous_ecdf_bands(num_samples=num_samples, num_points=num_points, num_simulations=1000, confidence=0.95)
L -= z
U -= z
# Plot the ECDF difference curve
ax.plot(x_points, ecdf_diff_interpolated, color='purple', label='ACE', linewidth=3)
ax.plot(x_points, ecdf_diff_interpolated_pi, color='green', label='ACEP', linewidth=3)
ax.set_ylim(-0.12, 0.12)
ax.fill_between(z, L, U, color='gray', alpha=0.3)
ax.set_xlabel('Fractional Rank', fontsize=18)
def plot_sbc_ecdf_diff_no_pi(ax, theta_prior, theta_posterior, num_points=20):
"""
Plot the ECDF difference for Simulation-Based Calibration (SBC).
Args:
- theta_prior: torch.Tensor, shape [num_samples, 1], samples drawn from the prior distribution.
- theta_posterior: torch.Tensor, shape [num_samples, num_posterior_samples], posterior samples for each theta_prior.
- num_points: int, optional, the number of points along the x-axis to control precision (default is 20).
"""
num_samples, num_posterior_samples = theta_posterior.shape
# Expand theta_prior to match the shape of theta_posterior
theta_prior_expanded = theta_prior.expand(-1, num_posterior_samples)
# Calculate the rank of each theta_prior in the corresponding posterior samples
less_than = (theta_posterior < theta_prior_expanded).long()
ranks = torch.sum(less_than, dim=1)
# Calculate the fractional rank (normalize ranks by num_posterior_samples)
fractional_ranks = ranks.float() / num_posterior_samples
# Calculate the ECDF of the fractional ranks
sorted_ranks = torch.sort(fractional_ranks)[0].numpy()
ecdf = np.arange(1, num_samples + 1) / num_samples
# Calculate the difference between ECDF and the uniform distribution
uniform_cdf = sorted_ranks # In uniform distribution, CDF is the same as the rank values
ecdf_diff = ecdf - uniform_cdf
# Generate points for the x-axis (fractional rank statistics)
x_points = np.linspace(0, 1, num_points)
# Interpolate ECDF difference for these x_points
ecdf_diff_interpolated = np.interp(x_points, sorted_ranks, ecdf_diff)
_, z, L, U = simultaneous_ecdf_bands(num_samples=num_samples, num_points=num_points, num_simulations=1000, confidence=0.95)
L -= z
U -= z
# Plot the ECDF difference curve
ax.plot(x_points, ecdf_diff_interpolated, color='purple', label='ACE', linewidth=3)
ax.set_ylim(-0.07, 0.07)
ax.fill_between(z, L, U, color='gray', alpha=0.3)
ax.set_xlabel('Fractional Rank', fontsize=18)