"""Numerical squared-error check for the fixed two-state source. Enumerate histories through a finite reset age and assign older mass to representatives at the cutoff. The calculation uses floating point on that finite distribution. It evaluates squared coordinate error. See the paper for its comparison with whole-future relative entropy. """ import argparse import numpy as np CONTINUATIONS = np.array([[[3, 1], [2, 2]], [[2, 2], [1, 3]]]) / 16 RESETS = np.array([[[3, 3], [1, 1]], [[1, 1], [3, 3]]]) / 16 BUDGETS = (8, 16, 32, 64) def posterior_atoms(continuations, resets, depth): """Return the sorted coordinates and masses at the chosen cutoff.""" stationary = np.array([0.5, 0.5]) masses = {} for reset in resets: row = stationary @ reset level = [(row / row.sum(), row.sum())] for age in range(depth + 1): for posterior, mass in level: coordinate = posterior[0] - posterior[1] # The geometric age tail has the same mass as this level. factor = 2 if age == depth else 1 masses[coordinate] = masses.get(coordinate, 0.0) + factor * mass if age == depth: break next_level = [] for posterior, mass in level: for continuation in continuations: row = posterior @ continuation probability = row.sum() next_level.append((row / probability, mass * probability)) level = next_level coordinates = np.array(sorted(masses)) weights = np.array([masses[coordinate] for coordinate in coordinates]) return coordinates, weights / weights.sum() def quantization_losses(coordinates, weights): """Minimize weighted squared distance over contiguous groups.""" size = len(coordinates) interval_costs = np.full((size + 1, size + 1), np.inf) weights_in_interval = np.zeros(size) means = np.zeros(size) errors = np.zeros(size) for end, (coordinate, weight) in enumerate(zip(coordinates, weights), start=1): starts = slice(end) old_weights = weights_in_interval[starts] new_weights = old_weights + weight gap = coordinate - means[starts] # Update the central moment directly to avoid subtracting raw moments. errors[starts] += old_weights * weight / new_weights * gap**2 means[starts] += weight / new_weights * gap weights_in_interval[starts] = new_weights interval_costs[starts, end] = errors[starts] previous = np.full(size + 1, np.inf) previous[0] = 0.0 losses = [] for budget in range(1, BUDGETS[-1] + 1): previous = np.min(previous[:, None] + interval_costs, axis=0) if budget in BUDGETS: losses.append(previous[-1]) return np.array(losses) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "--depth", type=int, default=9, help="Reset-age cutoff. Working memory grows quadratically in the number of points.", ) args = parser.parse_args() if args.depth < 6: parser.error("use --depth at least 6 for memory budgets through 64") for name, continuations, resets in ( ("forward", CONTINUATIONS, RESETS), ("reverse", CONTINUATIONS.swapaxes(1, 2), RESETS.swapaxes(1, 2)), ): coordinates, weights = posterior_atoms(continuations, resets, args.depth) losses = quantization_losses(coordinates, weights) slopes = np.diff(np.log(losses)) / np.diff(np.log(BUDGETS)) print(f"{name}, depth {args.depth}, {len(coordinates)} points") print(" losses: " + ", ".join(f"L={budget}: {loss:.8e}" for budget, loss in zip(BUDGETS, losses))) print(" local slopes: " + ", ".join(f"{slope:.5f}" for slope in slopes)) if __name__ == "__main__": main()