-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path02-estimate_params_mcmc.py
More file actions
190 lines (157 loc) · 4.74 KB
/
02-estimate_params_mcmc.py
File metadata and controls
190 lines (157 loc) · 4.74 KB
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
# %%
# !%load_ext autoreload
# !%autoreload 2
import corner
import emcee
import matplotlib.pyplot as plt
import numpy as np
import scienceplots
from scipy.linalg import inv
from scipy.optimize import minimize
from statsmodels.tools.numdiff import approx_hess
from dcsem.utils import stim_boxcar
from utils import (
add_noise,
add_underscore,
filter_params,
get_param_colors,
initialize_parameters,
set_style,
simulate_bold,
)
set_style()
# %%
def plot_bold_signals(time, bold_true, bold_noisy, bold_estimated):
"""
Plot the observed, true, and estimated BOLD signals for each region of
interest (ROI).
Args:
time (ndarray): A 1D array representing time points.
bold_true (ndarray): The ground truth BOLD signal, a 2D array where
each column corresponds to an ROI.
bold_noisy (ndarray): The observed BOLD signal with added noise,
in the same shape as bold_true.
bold_estimated (ndarray): The estimated BOLD signal, also a 2D array
with the same shape as bold_true.
"""
num_rois = bold_true.shape[1]
_, axs = plt.subplots(1, num_rois, figsize=(10, 4))
for i in range(num_rois):
axs[i].plot(time, bold_noisy[:, i], label="Observed", lw=2)
axs[i].plot(time, bold_true[:, i], label="Ground Truth", lw=2)
axs[i].plot(
time,
bold_estimated[:, i],
label="Estimated",
ls="--",
lw=2,
c="tomato",
)
axs[i].set_title(f"ROI {i}")
axs[i].set_xlabel("Time (s)")
axs[i].legend()
axs[0].set_ylabel("BOLD Signal")
plt.tight_layout()
plt.show()
def log_probability(param, bold_signal, est_name, all_params, bounds):
"""
Log-probability function for MCMC.
Args:
param (list): Parameter values to evaluate.
bold_signal (ndarray): Observed noisy BOLD signal.
est_name (list): Names of parameters to estimate.
all_params (dict): All parameters, including fixed ones.
bounds (list of tuples): Bounds for each parameter.
Returns:
float: Log-probability value.
"""
# Check if parameters are within bounds
for p, v, (low, high) in zip(est_name, param, bounds):
if not (low <= v <= high):
return -np.inf # Outside bounds → log-probability is -∞
# Update parameter dictionary
for p, v in zip(est_name, param):
all_params[p] = v
# Simulate BOLD signal
bold_simulated = simulate_bold(
all_params,
time=time,
u=u,
num_rois=NUM_ROIS,
)
# Compute likelihood (negative squared error)
residuals = bold_simulated - bold_signal
likelihood = -0.5 * np.sum(residuals**2)
return likelihood
time = np.arange(100)
u = stim_boxcar([[0, 30, 1]])
# Model parameters
NUM_ROIS = 2
NUM_LAYERS = 1
RANDOM = False
# Parameters to use in the simulation and estimation
params_to_set = ["a01", "a10", "c0", "c1"]
params_to_est = ["a01"]
# Ground truth parameter values
true_params = {
"a01": 0.6,
"a10": 0.4,
"c0": 0.5,
"c1": 0.5,
}
true_params = filter_params(true_params, params_to_set)
# Bounds for the parameters
bounds = {
"a01": (0, 1),
"a10": (0, 1),
"c0": (0, 1),
"c1": (0, 1),
}
bounds = filter_params(bounds, params_to_est)
initial_values = initialize_parameters(bounds, params_to_est, random=RANDOM)
bounds = [(bounds[param]) for param in params_to_est]
bold_true = simulate_bold(
true_params,
time=time,
u=u,
num_rois=NUM_ROIS,
)
bold_noisy = add_noise(bold_true, snr_db=100)
# Number of dimensions (parameters to estimate)
n_dim = len(params_to_est)
# Number of walkers (chains)
n_walkers = 32
# Burn-in phase + Sampling
n_burn = 500 # Samples to discard
n_samples = 1000 # Samples to keep
# Initialize walkers around the initial guess
initial_guess = [
initial_values + 0.01 * np.random.randn(n_dim) for _ in range(n_walkers)
]
# Run MCMC
sampler = emcee.EnsembleSampler(
n_walkers,
n_dim,
log_probability,
args=(bold_noisy, params_to_est, true_params.copy(), bounds),
)
sampler.run_mcmc(initial_guess, n_burn + n_samples, progress=True)
# %%
# Discard burn-in samples and reshape
samples = sampler.get_chain(discard=n_burn, flat=True)
# %%
# Compute mean and standard deviation for each parameter
means = np.mean(samples, axis=0)
stds = np.std(samples, axis=0)
estimated_params = dict(zip(params_to_est, means))
# Print results
print("True Parameters:", true_params)
print("Estimated Parameters (MCMC):", estimated_params)
# Visualize posterior distributions
corner.corner(
samples,
labels=params_to_est,
truths=[true_params[p] for p in params_to_est],
)
plt.show()
# %%