"""Tests for chunking.py. Run: python -m pytest -q""" import numpy as np import pytest from chunking import ( TemporalEnsembler, ensemble_weights, make_noisy_predictor, run_open_loop_chunks, run_temporal_ensemble, step_change, ) def test_weights_follow_exp_minus_m_i_oldest_first() -> None: w = ensemble_weights(4, 0.5) raw = np.exp(-0.5 * np.arange(4)) assert np.allclose(w, raw / raw.sum()) assert w[0] > w[-1] # m > 0 favours the oldest prediction assert np.allclose(ensemble_weights(3, 0.0), 1 / 3) assert ensemble_weights(3, -1.0)[-1] > ensemble_weights(3, -1.0)[0] # m < 0 favours the newest def test_ensembler_hand_computed() -> None: m = 0.1 ens = TemporalEnsembler(m) # Chunks of length 3 predicted at t = 0, 1, 2 (one action dimension). a0 = ens.step(0, [[1.0], [2.0], [3.0]]) a1 = ens.step(1, [[10.0], [20.0], [30.0]]) a2 = ens.step(2, [[100.0], [200.0], [300.0]]) assert a0 == pytest.approx([1.0]) w2 = np.exp(-m * np.arange(2)) assert a1 == pytest.approx([(w2 @ [2.0, 10.0]) / w2.sum()]) w3 = np.exp(-m * np.arange(3)) assert a2 == pytest.approx([(w3 @ [3.0, 20.0, 100.0]) / w3.sum()]) assert 2 not in ens.buffer # used entries are freed def test_ensemble_of_identical_chunks_is_exact() -> None: target = np.linspace(0, 1, 50) def predict(t: int) -> np.ndarray: idx = np.minimum(np.arange(t, t + 10), 49) return target[idx][:, None] assert np.allclose(run_temporal_ensemble(predict, 50, 0.01)[:, 0], target) assert np.allclose(run_open_loop_chunks(predict, 50, 10)[:, 0], target) def test_open_loop_length_with_partial_last_chunk() -> None: predict = make_noisy_predictor(np.zeros(23), 10, 0.0, 0.0, seed=0) assert run_open_loop_chunks(predict, 23, 10).shape == (23, 1) def test_predictor_is_deterministic_per_time_step() -> None: predict = make_noisy_predictor(np.zeros(30), 5, 0.1, 0.01, seed=3) assert np.array_equal(predict(7), predict(7)) assert not np.array_equal(predict(7), predict(8)) def test_ensembling_is_smoother_and_more_accurate_than_open_loop_chunks() -> None: horizon, k = 200, 20 target = 0.5 * np.sin(2 * np.pi * np.arange(horizon) / horizon) predict = make_noisy_predictor(target, k, offset_std=0.03, noise_std=0.005, seed=1) a_open = run_open_loop_chunks(predict, horizon, k)[:, 0] a_ens = run_temporal_ensemble(predict, horizon, 0.01)[:, 0] rms_open = np.sqrt(np.mean((a_open - target) ** 2)) rms_ens = np.sqrt(np.mean((a_ens - target) ** 2)) assert rms_ens < 0.5 * rms_open assert step_change(a_ens)[1] < 0.5 * step_change(a_open)[1] # no jumps at chunk boundaries