feat: add Monte Carlo density evolution engine
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
52
model/test_density_evolution.py
Normal file
52
model/test_density_evolution.py
Normal file
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Tests for density evolution optimizer."""
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
|
||||
|
||||
class TestDensityEvolution:
|
||||
"""Tests for the Monte Carlo DE engine."""
|
||||
|
||||
def test_de_known_good_converges(self):
|
||||
"""DE with original staircase profile at lam_s=10 should converge easily."""
|
||||
from density_evolution import run_de, ORIGINAL_STAIRCASE_PROFILE
|
||||
np.random.seed(42)
|
||||
converged, error_frac = run_de(
|
||||
ORIGINAL_STAIRCASE_PROFILE, lam_s=10.0, lam_b=0.1,
|
||||
z_pop=10000, max_iter=50
|
||||
)
|
||||
assert converged, f"DE should converge at lam_s=10, error_frac={error_frac}"
|
||||
|
||||
def test_de_known_bad_fails(self):
|
||||
"""DE at very low lam_s=0.1 should not converge."""
|
||||
from density_evolution import run_de, ORIGINAL_STAIRCASE_PROFILE
|
||||
np.random.seed(42)
|
||||
converged, error_frac = run_de(
|
||||
ORIGINAL_STAIRCASE_PROFILE, lam_s=0.1, lam_b=0.1,
|
||||
z_pop=10000, max_iter=50
|
||||
)
|
||||
assert not converged, f"DE should NOT converge at lam_s=0.1, error_frac={error_frac}"
|
||||
|
||||
def test_de_population_shape(self):
|
||||
"""Verify belief arrays have correct shapes after one step."""
|
||||
from density_evolution import de_channel_init, density_evolution_step
|
||||
np.random.seed(42)
|
||||
n_base = 8
|
||||
m_base = 7
|
||||
z_pop = 1000
|
||||
|
||||
# Original staircase H_base profile
|
||||
from density_evolution import ORIGINAL_STAIRCASE_PROFILE
|
||||
beliefs, msg_memory = de_channel_init(ORIGINAL_STAIRCASE_PROFILE, z_pop, lam_s=5.0, lam_b=0.1)
|
||||
|
||||
# beliefs should be (n_base, z_pop)
|
||||
assert beliefs.shape == (n_base, z_pop), f"Expected ({n_base}, {z_pop}), got {beliefs.shape}"
|
||||
|
||||
# Run one step
|
||||
beliefs = density_evolution_step(beliefs, msg_memory, ORIGINAL_STAIRCASE_PROFILE, z_pop)
|
||||
assert beliefs.shape == (n_base, z_pop), f"Shape changed after step: {beliefs.shape}"
|
||||
Reference in New Issue
Block a user