"""Why mean-squared-error regression fails on multimodal demonstrations (1D toy). The gripper must pass an obstacle. Half of the demonstrations go left, half go right, and the lateral offset y grows with the input x (say, the obstacle size, 0 to 1): y = s * (0.5 + 0.5 x) + noise, s = -1 or +1 with equal probability Two models: MSE regression a cubic polynomial fitted by least squares; it predicts the conditional mean, which is about 0: straight into the obstacle mixture model two linear-Gaussian components fitted by expectation maximisation; sampling from it gives actions in one mode or the other Run as a script for the numbers from the lesson: python bimodal.py """ from __future__ import annotations from dataclasses import dataclass import numpy as np from numpy.typing import NDArray def make_data(n: int = 400, noise: float = 0.05, seed: int = 0) -> tuple[NDArray[np.float64], NDArray[np.float64]]: rng = np.random.default_rng(seed) x = rng.uniform(0.0, 1.0, n) side = rng.choice([-1.0, 1.0], n) y = side * (0.5 + 0.5 * x) + rng.normal(0.0, noise, n) return x, y def fit_mse(x: NDArray[np.float64], y: NDArray[np.float64], degree: int = 3) -> np.polynomial.Polynomial: """Least-squares polynomial: the model that minimises mean squared error.""" return np.polynomial.Polynomial.fit(x, y, degree) @dataclass class LinearMixture: """p(y | x) = sum_k pi_k N(y; a_k x + b_k, sigma_k^2).""" pi: NDArray[np.float64] a: NDArray[np.float64] b: NDArray[np.float64] sigma: NDArray[np.float64] def component_pdf(self, x: NDArray[np.float64], y: NDArray[np.float64]) -> NDArray[np.float64]: """pi_k N(y_i; a_k x_i + b_k, sigma_k^2), shape (n, K).""" mu = x[:, None] * self.a + self.b z = (y[:, None] - mu) / self.sigma return self.pi * np.exp(-0.5 * z**2) / (np.sqrt(2 * np.pi) * self.sigma) def log_likelihood(self, x: NDArray[np.float64], y: NDArray[np.float64]) -> float: return float(np.sum(np.log(self.component_pdf(x, y).sum(axis=1)))) def sample(self, x: NDArray[np.float64], rng: np.random.Generator) -> NDArray[np.float64]: k = rng.choice(len(self.pi), size=len(x), p=self.pi) return self.a[k] * x + self.b[k] + self.sigma[k] * rng.normal(size=len(x)) def fit_mixture( x: NDArray[np.float64], y: NDArray[np.float64], n_components: int = 2, iters: int = 100 ) -> LinearMixture: """Expectation maximisation for a mixture of linear regressions. Starts from flat lines at evenly spaced quantiles of y, which is deterministic. """ K = n_components model = LinearMixture( pi=np.full(K, 1.0 / K), a=np.zeros(K), b=np.quantile(y, (np.arange(K) + 0.5) / K), sigma=np.full(K, np.std(y)), ) A = np.column_stack([x, np.ones_like(x)]) for _ in range(iters): # E-step: responsibility of each component for each sample. p = model.component_pdf(x, y) r = p / np.maximum(p.sum(axis=1, keepdims=True), 1e-300) # M-step: weighted least squares per component. for k in range(K): w = r[:, k] Aw = A * w[:, None] model.a[k], model.b[k] = np.linalg.solve(A.T @ Aw + 1e-9 * np.eye(2), Aw.T @ y) resid = y - (model.a[k] * x + model.b[k]) model.sigma[k] = max(np.sqrt(np.sum(w * resid**2) / w.sum()), 1e-3) model.pi = r.mean(axis=0) return model def gap_fraction(y: NDArray[np.float64], half_width: float = 0.3) -> float: """Fraction of actions inside the band |y| < half_width, where no demonstration goes.""" return float(np.mean(np.abs(y) < half_width)) def main() -> None: x, y = make_data() grid = np.linspace(0.0, 1.0, 11) mse = fit_mse(x, y) mix = fit_mixture(x, y) order = np.argsort(mix.b) print(f"demonstrations in the gap |y| < 0.3: {gap_fraction(y):.3f}") print(f"MSE fit on the grid: {np.round(mse(grid), 3)}") print(f"MSE predictions in the gap: {gap_fraction(mse(grid)):.3f}") print(f"mixture weights {np.round(mix.pi[order], 3)}, slopes {np.round(mix.a[order], 3)}, " f"intercepts {np.round(mix.b[order], 3)}, sigmas {np.round(mix.sigma[order], 3)}") samples = mix.sample(np.repeat(grid, 100), np.random.default_rng(1)) print(f"mixture samples in the gap: {gap_fraction(samples):.3f}") if __name__ == "__main__": main()