-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_active_inference_math.py
More file actions
364 lines (300 loc) · 14.6 KB
/
test_active_inference_math.py
File metadata and controls
364 lines (300 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
"""
Unit tests for Active Inference mathematical functions in post_simulation.py.
Tests Shannon entropy, KL divergence, variational free energy,
expected free energy, information gain, and the analysis metrics aggregator
using known-correct mathematical inputs/outputs.
"""
from typing import Any
import numpy as np
import pytest
# Import the functions under test
from analysis.post_simulation import (
analyze_active_inference_metrics,
compute_expected_free_energy,
compute_information_gain,
compute_kl_divergence,
compute_shannon_entropy,
compute_variational_free_energy,
)
from gnn.pomdp_extractor import POMDPStateSpace
from render.processor import normalize_matrices
# =============================================================================
# Shannon Entropy Tests
# =============================================================================
class TestShannonEntropy:
"""Tests for compute_shannon_entropy."""
def test_uniform_distribution(self) -> None:
"""Uniform distribution over N states should have entropy ln(N)."""
for n in [2, 4, 8]:
p = np.ones(n) / n
expected = np.log(n)
result = compute_shannon_entropy(p)
assert abs(result - expected) < 1e-6, (
f"Uniform({n}) entropy should be {expected:.4f}, got {result:.4f}"
)
def test_dirac_distribution(self) -> None:
"""Concentrated distribution should have near-zero entropy."""
p = np.array([1.0, 0.0, 0.0, 0.0])
result = compute_shannon_entropy(p)
assert result < 0.01, f"Dirac entropy should be ~0, got {result}"
def test_binary_distribution(self) -> None:
"""Binary distribution (0.5, 0.5) should equal ln(2)."""
p = np.array([0.5, 0.5])
expected = np.log(2)
result = compute_shannon_entropy(p)
assert abs(result - expected) < 1e-6
def test_asymmetric_binary(self) -> None:
"""Entropy of (0.9, 0.1) should be less than ln(2)."""
p = np.array([0.9, 0.1])
result = compute_shannon_entropy(p)
assert result < np.log(2), "Asymmetric should have less entropy than uniform"
assert result > 0, "Entropy should be positive"
def test_non_negative(self) -> None:
"""Entropy should always be non-negative."""
rng = np.random.default_rng(42)
for _ in range(10):
p = rng.dirichlet(np.ones(5))
assert compute_shannon_entropy(p) >= 0
# =============================================================================
# KL Divergence Tests
# =============================================================================
class TestKLDivergence:
"""Tests for compute_kl_divergence."""
def test_same_distribution_is_zero(self) -> None:
"""D_KL(P || P) = 0."""
p = np.array([0.3, 0.5, 0.2])
result = compute_kl_divergence(p, p)
assert abs(result) < 1e-4, f"D_KL(P||P) should be ~0, got {result}"
def test_non_negative(self) -> None:
"""KL divergence is always non-negative (Gibbs' inequality)."""
rng = np.random.default_rng(42)
for _ in range(20):
p = rng.dirichlet(np.ones(4))
q = rng.dirichlet(np.ones(4))
result = compute_kl_divergence(p, q)
assert result >= -1e-10, f"D_KL should be >= 0, got {result}"
def test_asymmetric(self) -> None:
"""KL divergence is generally asymmetric: D_KL(P||Q) != D_KL(Q||P)."""
p = np.array([0.9, 0.1])
q = np.array([0.1, 0.9])
pq = compute_kl_divergence(p, q)
qp = compute_kl_divergence(q, p)
# Both should be positive
assert pq > 0
assert qp > 0
# They are equal in this symmetric case, but that's fine —
# the point is the function handles both directions
def test_peaked_vs_uniform(self) -> None:
"""KL from peaked to uniform should be positive."""
p = np.array([0.99, 0.01])
q = np.array([0.5, 0.5])
result = compute_kl_divergence(p, q)
assert result > 0
# =============================================================================
# Variational Free Energy Tests
# =============================================================================
class TestVariationalFreeEnergy:
"""Tests for compute_variational_free_energy."""
def test_returns_float(self) -> None:
"""Should return a finite float."""
beliefs = np.array([0.5, 0.5])
A = np.array([[0.9, 0.1], [0.1, 0.9]])
obs = np.array([1, 0])
result = compute_variational_free_energy(obs, beliefs, A)
assert isinstance(result, float)
assert np.isfinite(result)
def test_certain_belief_lower_energy(self) -> None:
"""A belief matching the true generative model should have lower free energy
than a mismatched belief (given the same A matrix and observations)."""
A = np.array([[0.9, 0.1], [0.1, 0.9]])
obs = np.array([1, 0])
# Belief matching state 0 (correct for obs=[1,0] with this A)
correct_belief = np.array([0.9, 0.1])
# Belief matching state 1 (wrong)
wrong_belief = np.array([0.1, 0.9])
fe_correct = compute_variational_free_energy(obs, correct_belief, A)
fe_wrong = compute_variational_free_energy(obs, wrong_belief, A)
# The correct belief should yield lower (or equal) free energy
assert fe_correct <= fe_wrong + 1e-6, (
f"Correct belief FE ({fe_correct:.4f}) should be <= wrong belief FE ({fe_wrong:.4f})"
)
def test_uniform_prior(self) -> None:
"""When no prior is given, should use uniform prior."""
beliefs = np.array([0.5, 0.5])
A = np.eye(2)
obs = np.array([1, 0])
# Should not raise
result = compute_variational_free_energy(obs, beliefs, A)
assert np.isfinite(result)
# =============================================================================
# Expected Free Energy Tests
# =============================================================================
class TestExpectedFreeEnergy:
"""Tests for compute_expected_free_energy."""
def test_returns_float(self) -> None:
"""Should return a finite float."""
beliefs = np.array([0.5, 0.3, 0.2])
A = np.eye(3)
B = np.stack([np.eye(3)] * 2, axis=2) # 2 actions, identity transitions
C = np.zeros(3)
result = compute_expected_free_energy(beliefs, A, B, C, policy=0)
assert isinstance(result, float)
assert np.isfinite(result)
def test_preferred_outcome_lower_efe(self) -> None:
"""A policy leading to preferred observations should have lower EFE."""
beliefs = np.array([1.0, 0.0]) # Agent is certain it's in state 0
A = np.eye(2) # Identity observation
# Action 0: stay in state 0, Action 1: move to state 1
B = np.zeros((2, 2, 2))
B[0, 0, 0] = 1.0 # Action 0 keeps in state 0
B[1, 0, 1] = 1.0 # Action 1 moves to state 1
B[0, 1, 0] = 1.0
B[1, 1, 1] = 1.0
# Prefer observation 0 (state 0)
C = np.array([1.0, -1.0]) # Log preference for obs 0
efe_stay = compute_expected_free_energy(beliefs, A, B, C, policy=0)
efe_move = compute_expected_free_energy(beliefs, A, B, C, policy=1)
# Staying should be preferred (lower EFE)
assert efe_stay < efe_move, (
f"Stay EFE ({efe_stay:.4f}) should be < Move EFE ({efe_move:.4f})"
)
def test_2d_b_matrix_fallback(self) -> None:
"""Should handle 2D B matrices (no action dimension)."""
beliefs = np.array([0.5, 0.5])
A = np.eye(2)
B = np.eye(2) # 2D, no action dimension
C = np.zeros(2)
result = compute_expected_free_energy(beliefs, A, B, C, policy=0)
assert np.isfinite(result)
# =============================================================================
# Information Gain Tests
# =============================================================================
class TestInformationGain:
"""Tests for compute_information_gain."""
def test_no_update_zero_gain(self) -> None:
"""If posterior equals prior, information gain is zero."""
p = np.array([0.3, 0.7])
result = compute_information_gain(p, p)
assert abs(result) < 1e-4
def test_positive_gain(self) -> None:
"""Updating beliefs should yield positive information gain."""
prior = np.array([0.5, 0.5])
posterior = np.array([0.9, 0.1])
result = compute_information_gain(prior, posterior)
assert result > 0, "Information gain should be positive for belief update"
def test_equals_kl_posterior_prior(self) -> None:
"""IG should equal D_KL(posterior || prior)."""
prior = np.array([0.4, 0.6])
posterior = np.array([0.8, 0.2])
ig = compute_information_gain(prior, posterior)
kl = compute_kl_divergence(posterior, prior)
assert abs(ig - kl) < 1e-6
# =============================================================================
# Analyze Active Inference Metrics Tests
# =============================================================================
class TestAnalyzeActiveInferenceMetrics:
"""Tests for analyze_active_inference_metrics."""
@pytest.fixture
def sample_trajectory(self) -> Any:
"""Create a realistic belief trajectory that converges."""
rng = np.random.default_rng(42)
beliefs = []
# Start uniform, converge to state 0
for t in range(20):
w = min(t / 15.0, 1.0)
b = np.array([(1 - w) * 0.25 + w * 0.9,
(1 - w) * 0.25 + w * 0.05,
(1 - w) * 0.25 + w * 0.03,
(1 - w) * 0.25 + w * 0.02])
b = b / b.sum()
beliefs.append(b.tolist())
free_energy = [5.0 - 0.2 * t + rng.normal(0, 0.05) for t in range(20)]
actions = [rng.integers(0, 3) for _ in range(20)]
return beliefs, free_energy, actions
def test_output_structure(self, sample_trajectory: Any) -> None:
"""Result should contain expected keys."""
beliefs, fe, actions = sample_trajectory
result = analyze_active_inference_metrics(beliefs, fe, actions, "test_model")
assert "model_name" in result
assert result["model_name"] == "test_model"
assert "num_timesteps" in result
assert result["num_timesteps"] == 20
assert "metrics" in result
assert "belief_entropy" in result["metrics"]
assert "information_gain" in result["metrics"]
assert "free_energy" in result["metrics"]
assert "action_distribution" in result["metrics"]
def test_entropy_decreasing_trend(self, sample_trajectory: Any) -> None:
"""For converging beliefs, entropy should show decreasing trend."""
beliefs, fe, actions = sample_trajectory
result = analyze_active_inference_metrics(beliefs, fe, actions, "test_model")
entropy_data = result["metrics"]["belief_entropy"]
assert entropy_data["trend"] == "decreasing", (
f"Expected 'decreasing' trend, got '{entropy_data['trend']}'"
)
def test_information_gain_positive(self, sample_trajectory: Any) -> None:
"""Total information gain should be positive for converging beliefs."""
beliefs, fe, actions = sample_trajectory
result = analyze_active_inference_metrics(beliefs, fe, actions, "test_model")
ig = result["metrics"]["information_gain"]
assert ig["total"] > 0
def test_empty_trajectory(self) -> None:
"""Should handle empty input gracefully."""
result = analyze_active_inference_metrics([], [], [], "empty_model")
assert result["num_timesteps"] == 0
assert result["metrics"] == {}
# =============================================================================
# normalize_matrices Tests
# =============================================================================
class TestNormalizeMatrices:
"""Tests for normalize_matrices in render/processor.py."""
def test_2d_a_matrix_normalization(self) -> None:
"""Columns of 2D A matrix should sum to 1 after normalization."""
import logging
log = logging.getLogger("test")
A = np.array([[2.0, 1.0], [2.0, 3.0]]) # Columns sum to 4
pomdp = POMDPStateSpace(num_states=2, num_observations=2, num_actions=1, A_matrix=A)
result = normalize_matrices(pomdp, log)
col_sums = result.A_matrix.sum(axis=0)
np.testing.assert_allclose(col_sums, [1.0, 1.0], atol=1e-10)
def test_3d_b_matrix_normalization(self) -> None:
"""Columns of each action slice in 3D B should sum to 1."""
import logging
log = logging.getLogger("test")
B = np.ones((3, 3, 2)) # (next_state, curr_state, action), all ones
pomdp = POMDPStateSpace(num_states=3, num_observations=3, num_actions=2, B_matrix=B)
result = normalize_matrices(pomdp, log)
for a in range(2):
col_sums = result.B_matrix[:, :, a].sum(axis=0)
np.testing.assert_allclose(col_sums, np.ones(3), atol=1e-10)
def test_zero_column_uniform_fallback(self) -> None:
"""Zero-sum columns should be filled with uniform distribution."""
import logging
log = logging.getLogger("test")
A = np.array([[0.0, 1.0], [0.0, 1.0]]) # Column 0 is all zeros
pomdp = POMDPStateSpace(num_states=2, num_observations=2, num_actions=1, A_matrix=A)
result = normalize_matrices(pomdp, log)
# Column 0 should now be uniform (0.5, 0.5)
np.testing.assert_allclose(result.A_matrix[:, 0], [0.5, 0.5], atol=1e-10)
# Column 1 should be (0.5, 0.5)
np.testing.assert_allclose(result.A_matrix[:, 1], [0.5, 0.5], atol=1e-10)
def test_factorial_a_matrix(self) -> None:
"""Should handle list-of-arrays (factorial) A matrix."""
import logging
log = logging.getLogger("test")
A_list = [np.array([[3.0, 1.0], [1.0, 3.0]]),
np.array([[2.0, 2.0], [2.0, 2.0]])]
pomdp = POMDPStateSpace(num_states=2, num_observations=2, num_actions=1, A_matrix=A_list)
result = normalize_matrices(pomdp, log)
assert isinstance(result.A_matrix, list)
for a in result.A_matrix:
col_sums = a.sum(axis=0)
np.testing.assert_allclose(col_sums, np.ones(2), atol=1e-10)
def test_passthrough_no_matrices(self) -> None:
"""Should handle POMDP with no matrices without error."""
import logging
log = logging.getLogger("test")
pomdp = POMDPStateSpace(num_states=2, num_observations=2, num_actions=1)
result = normalize_matrices(pomdp, log)
assert result.A_matrix is None
assert result.B_matrix is None