Spaces:
Configuration error
Configuration error
File size: 14,279 Bytes
f898792 | 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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | """
Test suite for the Financial Statement Anomaly Detection environment.
Covers: data generation, anomaly injection, grading, environment API,
and determinism.
"""
import json
import sys
import os
# Add project root to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from src.models import (
Action, ActionType, AnomalyFlag, AnomalyType, GroundTruthAnomaly,
Observation, QuarterData, State,
)
from src.generator import generate_company_data
from src.anomaly_injector import inject_anomalies
from src.grader import grade
from src.environment import FinancialAnomalyEnv
from src.utils import (
balance_sheet_balanced, cash_flow_reconciles,
current_assets_sum_check, income_statement_check,
benfords_chi_squared, leading_digit,
)
def test_generator_produces_valid_data():
"""Generated data should have internally consistent financial statements."""
metadata, quarters = generate_company_data(seed=42, num_quarters=6)
assert metadata.name != ""
assert len(quarters) == 6
for q in quarters:
# Balance sheet must balance
assert q.total_assets == q.total_liabilities + q.equity, \
f"{q.quarter_label}: assets={q.total_assets} != liab+eq={q.total_liabilities + q.equity}"
# Cash flow must reconcile
assert q.net_cash_change == q.cf_operating + q.cf_investing + q.cf_financing, \
f"{q.quarter_label}: net_cash_change mismatch"
# Revenue should be positive
assert q.revenue > 0, f"{q.quarter_label}: revenue should be positive"
print("PASS: test_generator_produces_valid_data")
def test_generator_deterministic():
"""Same seed should produce identical data."""
_, q1 = generate_company_data(seed=99, num_quarters=4)
_, q2 = generate_company_data(seed=99, num_quarters=4)
for a, b in zip(q1, q2):
assert a.revenue == b.revenue
assert a.total_assets == b.total_assets
assert a.net_income == b.net_income
print("PASS: test_generator_deterministic")
def test_anomaly_injection_easy():
"""Easy injection should produce 3+ anomalies."""
_, quarters = generate_company_data(seed=42, num_quarters=4)
_, anomalies = inject_anomalies(quarters, difficulty="easy", seed=1042)
assert len(anomalies) >= 2, f"Expected >=2 anomalies, got {len(anomalies)}"
for a in anomalies:
assert a.anomaly_id != ""
assert a.severity >= 1
assert a.severity <= 5
print("PASS: test_anomaly_injection_easy")
def test_anomaly_injection_medium():
"""Medium injection should produce more anomalies than easy."""
_, quarters = generate_company_data(seed=137, num_quarters=6)
_, anomalies = inject_anomalies(quarters, difficulty="medium", seed=1137)
assert len(anomalies) >= 3, f"Expected >=3 anomalies, got {len(anomalies)}"
print("PASS: test_anomaly_injection_medium")
def test_anomaly_injection_hard():
"""Hard injection should produce 5+ anomalies."""
_, quarters = generate_company_data(seed=256, num_quarters=8)
_, anomalies = inject_anomalies(quarters, difficulty="hard", seed=1256)
assert len(anomalies) >= 4, f"Expected >=4 anomalies, got {len(anomalies)}"
# Should have some high-severity anomalies
max_sev = max(a.severity for a in anomalies)
assert max_sev >= 4, f"Hard task should have severity >= 4, got max {max_sev}"
print("PASS: test_anomaly_injection_hard")
def test_grader_perfect_score():
"""Perfect flags should score close to 1.0."""
truths = [
GroundTruthAnomaly(
anomaly_id="a1", line_item="revenue", quarter="Q1 2023",
severity=3, anomaly_type=AnomalyType.ARITHMETIC_ERROR,
),
GroundTruthAnomaly(
anomaly_id="a2", line_item="inventory", quarter="Q2 2023",
severity=4, anomaly_type=AnomalyType.INVENTORY_TURNOVER,
),
]
flags = [
AnomalyFlag(
line_item="revenue", quarter="Q1 2023",
severity=3, anomaly_type=AnomalyType.ARITHMETIC_ERROR,
explanation="Totals don't add up",
),
AnomalyFlag(
line_item="inventory", quarter="Q2 2023",
severity=4, anomaly_type=AnomalyType.INVENTORY_TURNOVER,
explanation="Inventory spike",
),
]
result = grade(flags, truths)
assert result["score"] >= 0.9, f"Perfect flags should score >= 0.9, got {result['score']}"
assert result["precision"] == 1.0
assert result["recall"] == 1.0
assert result["false_positives"] == 0
print("PASS: test_grader_perfect_score")
def test_grader_partial_credit():
"""Right location, wrong type should get partial credit."""
truths = [
GroundTruthAnomaly(
anomaly_id="a1", line_item="revenue", quarter="Q1 2023",
severity=3, anomaly_type=AnomalyType.CHANNEL_STUFFING,
),
]
flags = [
AnomalyFlag(
line_item="revenue", quarter="Q1 2023",
severity=3, anomaly_type=AnomalyType.IMPOSSIBLE_CHANGE, # wrong type
explanation="Revenue jumped too much",
),
]
result = grade(flags, truths)
assert 0.3 < result["score"] < 0.9, f"Partial credit score unexpected: {result['score']}"
assert result["precision"] == 0.5 # partial match
print("PASS: test_grader_partial_credit")
def test_grader_false_positives():
"""All wrong flags should score low."""
truths = [
GroundTruthAnomaly(
anomaly_id="a1", line_item="revenue", quarter="Q1 2023",
severity=3, anomaly_type=AnomalyType.ARITHMETIC_ERROR,
),
]
flags = [
AnomalyFlag(
line_item="cash", quarter="Q3 2023",
severity=1, anomaly_type=AnomalyType.NEGATIVE_VALUE,
explanation="Wrong guess",
),
AnomalyFlag(
line_item="inventory", quarter="Q2 2023",
severity=2, anomaly_type=AnomalyType.DUPLICATE_ENTRY,
explanation="Another wrong guess",
),
]
result = grade(flags, truths)
assert result["score"] < 0.2, f"All wrong flags should score < 0.2, got {result['score']}"
assert result["false_positives"] == 2
print("PASS: test_grader_false_positives")
def test_grader_empty_flags():
"""No flags submitted should score 0."""
truths = [
GroundTruthAnomaly(
anomaly_id="a1", line_item="revenue", quarter="Q1 2023",
severity=3, anomaly_type=AnomalyType.ARITHMETIC_ERROR,
),
]
result = grade([], truths)
assert result["score"] == 0.0, f"Empty flags should score 0, got {result['score']}"
print("PASS: test_grader_empty_flags")
def test_grader_determinism():
"""Grader should produce identical scores across 100 runs."""
truths = [
GroundTruthAnomaly(
anomaly_id="a1", line_item="revenue", quarter="Q1 2023",
severity=3, anomaly_type=AnomalyType.ARITHMETIC_ERROR,
),
GroundTruthAnomaly(
anomaly_id="a2", line_item="inventory", quarter="Q2 2023",
severity=4, anomaly_type=AnomalyType.INVENTORY_TURNOVER,
),
]
flags = [
AnomalyFlag(
line_item="revenue", quarter="Q1 2023",
severity=3, anomaly_type=AnomalyType.ARITHMETIC_ERROR,
explanation="test",
),
AnomalyFlag(
line_item="cash", quarter="Q1 2023",
severity=1, anomaly_type=AnomalyType.NEGATIVE_VALUE,
explanation="false positive",
),
]
first_score = grade(flags, truths)["score"]
for i in range(100):
score = grade(flags, truths)["score"]
assert score == first_score, f"Run {i}: score {score} != {first_score}"
print("PASS: test_grader_determinism (100 runs)")
def test_env_reset():
"""reset() should return a valid observation."""
env = FinancialAnomalyEnv(task_id="easy")
obs = env.reset()
assert obs.company is not None
assert len(obs.quarters) >= 4
assert obs.step_number == 0
assert len(obs.prior_flags) == 0
print("PASS: test_env_reset")
def test_env_step_flag():
"""step() with flag_anomaly should update state."""
env = FinancialAnomalyEnv(task_id="easy")
env.reset()
action = Action(
action_type=ActionType.FLAG_ANOMALY,
flag=AnomalyFlag(
line_item="revenue",
quarter="Q1 2022",
severity=2,
anomaly_type=AnomalyType.ARITHMETIC_ERROR,
explanation="Test flag",
),
)
obs, reward, done, info = env.step(action)
assert info.flags_so_far == 1
assert obs.step_number == 1
assert not done
print("PASS: test_env_step_flag")
def test_env_step_submit():
"""step() with submit_report should end the episode."""
env = FinancialAnomalyEnv(task_id="easy")
env.reset()
action = Action(action_type=ActionType.SUBMIT_REPORT)
obs, reward, done, info = env.step(action)
assert done is True
assert info.done_reason == "submitted"
print("PASS: test_env_step_submit")
def test_env_step_request_detail():
"""step() with request_detail should return detail and cost -0.1."""
env = FinancialAnomalyEnv(task_id="easy")
obs = env.reset()
quarter_label = obs.quarters[0].quarter_label
action = Action(
action_type=ActionType.REQUEST_DETAIL,
detail_line_item="revenue",
detail_quarter=quarter_label,
)
obs, reward, done, info = env.step(action)
assert reward.step_reward == -0.1
assert obs.detail_response is not None
assert "components" in obs.detail_response
print("PASS: test_env_step_request_detail")
def test_env_state():
"""state() should return accurate episode info."""
env = FinancialAnomalyEnv(task_id="easy")
env.reset()
state = env.state()
assert state.step_number == 0
assert state.done is False
assert state.task_id == "easy"
print("PASS: test_env_state")
def test_env_max_steps():
"""Episode should end with penalty at max steps."""
env = FinancialAnomalyEnv(task_id="easy", max_steps=3)
env.reset()
for i in range(3):
action = Action(
action_type=ActionType.FLAG_ANOMALY,
flag=AnomalyFlag(
line_item="cash", quarter="Q1 2022",
severity=1, anomaly_type=AnomalyType.NEGATIVE_VALUE,
explanation="test",
),
)
obs, reward, done, info = env.step(action)
assert done is True
assert info.done_reason == "max_steps"
print("PASS: test_env_max_steps")
def test_env_full_episode():
"""Run a complete episode: reset -> flags -> submit -> score."""
env = FinancialAnomalyEnv(task_id="easy")
obs = env.reset()
gt = env.get_ground_truth()
# Flag all known anomalies
for anomaly in gt:
action = Action(
action_type=ActionType.FLAG_ANOMALY,
flag=AnomalyFlag(
line_item=anomaly.line_item,
quarter=anomaly.quarter,
severity=anomaly.severity,
anomaly_type=anomaly.anomaly_type,
explanation="Detected anomaly",
),
)
obs, reward, done, info = env.step(action)
# Submit
action = Action(action_type=ActionType.SUBMIT_REPORT)
obs, reward, done, info = env.step(action)
assert done is True
score = env.get_final_score()
assert score["score"] >= 0.9, f"Perfect detection should score >= 0.9, got {score['score']}"
assert score["recall"] == 1.0
print(f"PASS: test_env_full_episode (score={score['score']})")
def test_utils_benfords():
"""Benford's chi-squared should detect non-natural distributions."""
# Natural-ish distribution
natural = [123, 234, 345, 156, 267, 189, 112, 298, 143, 178,
210, 320, 145, 256, 190, 115, 225, 335, 142, 260]
chi2_natural = benfords_chi_squared(natural)
# Fabricated: all start with 5 or 6
fabricated = [500, 600, 550, 650, 520, 620, 580, 640, 510, 690,
560, 630, 570, 610, 540, 660, 530, 670, 590, 680]
chi2_fabricated = benfords_chi_squared(fabricated)
assert chi2_fabricated > chi2_natural, \
f"Fabricated data should have higher chi2: {chi2_fabricated} vs {chi2_natural}"
print("PASS: test_utils_benfords")
def test_utils_leading_digit():
"""Leading digit extraction should work correctly."""
assert leading_digit(123) == 1
assert leading_digit(9876) == 9
assert leading_digit(-456) == 4
assert leading_digit(0) == 0
assert leading_digit(7) == 7
print("PASS: test_utils_leading_digit")
# ---------------------------------------------------------------------------
# Run all tests
# ---------------------------------------------------------------------------
if __name__ == "__main__":
tests = [
test_generator_produces_valid_data,
test_generator_deterministic,
test_anomaly_injection_easy,
test_anomaly_injection_medium,
test_anomaly_injection_hard,
test_grader_perfect_score,
test_grader_partial_credit,
test_grader_false_positives,
test_grader_empty_flags,
test_grader_determinism,
test_env_reset,
test_env_step_flag,
test_env_step_submit,
test_env_step_request_detail,
test_env_state,
test_env_max_steps,
test_env_full_episode,
test_utils_benfords,
test_utils_leading_digit,
]
passed = 0
failed = 0
errors = []
print("=" * 60)
print("Financial Anomaly Detection - Test Suite")
print("=" * 60)
print()
for test_fn in tests:
try:
test_fn()
passed += 1
except Exception as e:
failed += 1
errors.append((test_fn.__name__, str(e)))
print(f"FAIL: {test_fn.__name__}: {e}")
print()
print("=" * 60)
print(f"Results: {passed} passed, {failed} failed, {len(tests)} total")
print("=" * 60)
if errors:
print("\nFailures:")
for name, err in errors:
print(f" {name}: {err}")
sys.exit(1)
else:
print("\nAll tests passed!")
sys.exit(0)
|