import matplotlib.pyplot as plt
import numpy as np
from clocks import (
ClockArray, InferenceConfig, MassConfig, NoiseConfig,
PriorConfig, SimulationConfig, infer, simulate,
)
from clocks.viz import THEMES, show, style
clock_array = ClockArray(positions=np.array([[-5.0], [0.0], [5.0]]), track_offset=1.0)
truth = MassConfig(positions=np.array([[2.5]]), masses=np.array([0.10]))
sim = simulate(SimulationConfig(
clock_array=clock_array, ground_truth=truth,
noise=NoiseConfig(observation_std=0.005), n_observations=25, seed=42,
))
result = infer(sim.observations, InferenceConfig(
clock_array=clock_array, noise=NoiseConfig(observation_std=0.005),
prior=PriorConfig(position_range=(-8.0, 8.0), mass_range=(0.005, 0.15)),
n_particles=400, n_masses=1,
ess_target=0.8, rejuvenation_steps=2, proposal_scale=2.38, seed=42,
))
steps = np.arange(1, len(result.history) + 1)
means = np.array([entry.mean for entry in result.history])
stds = np.array([entry.std for entry in result.history])
for theme in THEMES:
with style(theme) as colors:
fig, axes = plt.subplots(2, 1, sharex=True, figsize=(7.5, 5.4))
for j, (ax, label, true_val) in enumerate(
zip(axes, ["position x", "mass M"], [2.5, 0.10])
):
ax.fill_between(
steps, means[:, j] - stds[:, j], means[:, j] + stds[:, j],
alpha=0.15, color=colors.belief, linewidth=0,
)
# Drawn before the mean, so the estimate stays visible where
# it lies on the truth.
ax.axhline(true_val, color=colors.truth, linewidth=1.3)
ax.plot(steps, means[:, j], color=colors.belief)
ax.set_ylabel(label)
axes[-1].set_xlabel("observation #")
for ax, true_val in zip(axes, [2.5, 0.10]):
ax.annotate(
"truth", xy=(ax.get_xlim()[1], true_val),
xytext=(-2, 3), textcoords="offset points",
ha="right", va="bottom", color=colors.truth,
)
show(fig)