Skip to content

Commit 7573a4d

Browse files
authored
Fix benchmark (#597)
* fix sir * fix lotka volterra
1 parent 901d42b commit 7573a4d

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

bayesflow/experimental/stable_consistency_model/stable_consistency_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,9 @@ def _inverse(self, z: Tensor, conditions: Tensor = None, **kwargs) -> Tensor:
222222
z : Tensor
223223
Samples from a standard normal distribution
224224
conditions : Tensor, optional, default: None
225-
Conditions for a approximate conditional distribution
225+
Conditions for an approximate conditional distribution
226226
**kwargs : dict, optional, default: {}
227-
Additional keyword arguments. Include `steps` (default: 30) to
227+
Additional keyword arguments. Include `steps` (default: 15) to
228228
adjust the number of sampling steps.
229229
230230
Returns

bayesflow/simulators/benchmark_simulators/lotka_volterra.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ def __init__(
1010
X0: int = 30,
1111
Y0: int = 1,
1212
T: int | None = 20,
13-
subsample: int = 10,
13+
subsample: int | str = "original",
1414
flatten: bool = True,
1515
obs_noise: float = 0.1,
16-
dt: float = None,
16+
dt: float = 0.1,
1717
rng: np.random.Generator = None,
1818
):
1919
"""Lotka Volterra simulated benchmark.
@@ -27,14 +27,17 @@ def __init__(
2727
Initial number of predator species.
2828
T: int, optional, default: 20
2929
The duration (time horizon) of the simulation.
30-
subsample: int or None, optional, default: 10
30+
subsample: int, str or None, optional, default: 'original'
3131
The number of evenly spaced time points to return.
3232
If None, no subsampling will be performed and all T timepoints will be returned.
33+
If 'original', the original benchmark task subsampling of 20 points is used.
3334
flatten: bool, optional, default: True
3435
A flag to indicate whether a 1D (`flatten=True`) or 2D (`flatten=False`)
3536
representation of the simulated data is returned.
3637
obs_noise: float, optional, default: 0.1
3738
The standard deviation of the log-normal likelihood.
39+
dt: float, optional, default: 0.1
40+
The time step size for the ODE solver.
3841
rng: np.random.Generator or None, optional, default: None
3942
An optional random number generator to use.
4043
"""
@@ -95,21 +98,23 @@ def observation_model(self, params: np.ndarray) -> np.ndarray:
9598
# Unpack parameter vector into scalars
9699
alpha, beta, gamma, delta = params
97100

98-
# Prepate time vector between 0 and T of length T
99-
t_vec = np.linspace(0, self.T, int(1 / self.dt))
101+
# Prepare time vector between 0 and T of length T
102+
t_vec = np.arange(0, self.T + self.dt, self.dt)
100103

101104
# Integrate using scipy and retain only infected (2-nd dimension)
102105
pp = odeint(self._deriv, x0, t_vec, args=(alpha, beta, gamma, delta))
103106

104107
# Subsample evenly the specified number of points, if specified
105-
if self.subsample is not None:
108+
if self.subsample == "original":
109+
pp = pp[::21]
110+
elif self.subsample is not None:
106111
pp = pp[:: (self.T // self.subsample)]
107112

108-
# Ensure minimum count is 0, which will later pass by log(0 + 1)
109-
pp[pp < 0] = 0.0
113+
# Ensure minimum count is 0
114+
pp = np.clip(pp, a_min=1e-10, a_max=10000.0)
110115

111116
# Add noise, decide whether to flatten and return
112-
x = self.rng.lognormal(np.log1p(pp), sigma=self.obs_noise)
117+
x = self.rng.lognormal(pp, sigma=self.obs_noise)
113118
if self.flatten:
114119
return x.flatten()
115120
return x

bayesflow/simulators/benchmark_simulators/sir.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(
1111
T: int = 160,
1212
I0: float = 1.0,
1313
R0: float = 0.0,
14-
subsample: int = None,
14+
subsample: int | str = "original",
1515
total_count: int = 1000,
1616
scale_by_total: bool = True,
1717
rng: np.random.Generator = None,
@@ -27,15 +27,17 @@ def __init__(
2727
The size of the simulated population.
2828
T: int, optional, default: 160
2929
The duration (time horizon) of the simulation.
30+
The last time-point is not included.
3031
I0: float, optional, default: 1.0
3132
The number of initially infected individuals.
3233
R0: float, optional, default: 0.0
3334
The number of initially recovered individuals.
34-
subsample: int or None, optional, default: 10
35+
subsample: int, str or None, optional, default: 'original'
3536
The number of evenly spaced time points to return. If `None`,
3637
no subsampling will be performed, all `T` timepoints will be returned
3738
and a trailing dimension will be added. If an integer is provided,
3839
subsampling is performed and no trailing dimension will be added.
40+
'original' reproduces the original benchmark task subsampling of 10 points.
3941
total_count: int, optional, default: 1000
4042
The `N` parameter of the binomial noise distribution. Used just
4143
for scaling the data and magnifying the effect of noise, such that
@@ -100,14 +102,16 @@ def observation_model(self, params: np.ndarray):
100102
# Unpack parameter vector into scalars
101103
beta, gamma = params
102104

103-
# Prepate time vector between 0 and T of length T
104-
t_vec = np.linspace(0, self.T, self.T)
105+
# Prepare time vector between 0 and T of length T
106+
t_vec = np.arange(0, self.T)
105107

106108
# Integrate using scipy and retain only infected (2-nd dimension)
107109
irt = odeint(self._deriv, x0, t_vec, args=(self.N, beta, gamma))[:, 1]
108110

109111
# Subsample evenly the specified number of points, if specified
110-
if self.subsample is not None:
112+
if self.subsample == "original":
113+
irt = irt[::17]
114+
elif self.subsample is not None:
111115
irt = irt[:: (self.T // self.subsample)]
112116
else:
113117
irt = irt[:, None]

0 commit comments

Comments
 (0)