Batch 3 of the expansion-v2 roadmap, all deterministic/offline/std-only with honest capability boundaries. crates/lawsynth-reduce (new): deterministic symmetry & separability detection. Numerical-derivative probe (finite differences replace AI-Feynman's trained NN gradients) over a reconstructed Cartesian grid; additive/multiplicative separability + difference/sum/product/ratio symmetries. Every detection is a HYPOTHESIS verified by reconstruction residual, never a proof; non-grid data is honestly rejected (NotReconstructed). 17 tests. crates/lawsynth-score: two-part MDL objective (L_data + L_model in nats) and O(n log n) pareto_front_2d (sort-then-sweep), verified equal to the O(n^2) front across 12 deterministic sets. +5 tests; crate fmt-normalized to config. python/lawsynth export.py (new): to_sympy/to_torch/to_jax + dep-free numeric_derivatives anchor. Constants+params hoisted into one trainable vector so autograd reaches every coefficient (Neural-ODE vector field). Backends absent here degrade gracefully (MissingDependencyError subclasses ImportError). Faithful to native simulate at 1.1e-16. 10 tests + 6 honest skips. benchmarks: SRBench Feynman (15: 12 static algebraic relations as honest capability-boundaries, 3 dynamics-framed ODEs executed at R2~=1.0) and black-box (4 systems, predictive R2 + parsimony, no ground truth) families. Fixes a pre-existing _runner.py failure where Strogatz cases leaked into the legacy contract suite. 31 benchmark tests. specs/structural-reductions: boundary spec (detection-is-a-hypothesis contract).
56 lines
2.4 KiB
Rust
56 lines
2.4 KiB
Rust
//! Black-box coverage for the public MDL objective and the O(n log n)
|
|
//! two-objective Pareto front against the general O(n^2) filter.
|
|
|
|
use lawsynth_core::Identifier;
|
|
use lawsynth_expr::Expr;
|
|
use lawsynth_score::{
|
|
CandidateMetrics, DescriptionLength, ModelDescription, description_length, most_parsimonious,
|
|
pareto_front, pareto_front_2d,
|
|
};
|
|
|
|
fn symbol(name: &str) -> Expr {
|
|
Expr::symbol(Identifier::new(name).unwrap())
|
|
}
|
|
|
|
#[test]
|
|
fn mdl_selects_the_parsimonious_model_over_an_overfit_alternative() {
|
|
// Simple model: a*x + b (5 nodes, 2 constants), fits with a little residual.
|
|
let simple = ModelDescription::from_expression(&Expr::sum(
|
|
Expr::product(Expr::constant(2.0), symbol("x")),
|
|
Expr::constant(1.0),
|
|
));
|
|
// Bloated model: many extra operators and constants that fit no better.
|
|
let bloated = ModelDescription::from_expression(&Expr::sum(
|
|
Expr::product(Expr::constant(2.0), symbol("x")),
|
|
Expr::sum(
|
|
Expr::product(Expr::constant(0.0), Expr::product(symbol("x"), symbol("x"))),
|
|
Expr::sum(Expr::constant(1.0), Expr::product(Expr::constant(0.0), symbol("y"))),
|
|
),
|
|
));
|
|
let observations = 100;
|
|
let residual = 0.5;
|
|
|
|
let simple_dl: DescriptionLength = description_length(observations, residual, &simple).unwrap();
|
|
let bloated_dl: DescriptionLength =
|
|
description_length(observations, residual, &bloated).unwrap();
|
|
|
|
// Same fit, so the data terms match and the model term breaks the tie.
|
|
assert_eq!(simple_dl.data_code_length, bloated_dl.data_code_length);
|
|
assert!(simple_dl.total < bloated_dl.total);
|
|
assert_eq!(most_parsimonious(&[bloated_dl, simple_dl]), Some(1));
|
|
}
|
|
|
|
#[test]
|
|
fn two_dimensional_front_equals_the_quadratic_front() {
|
|
let candidates = vec![
|
|
CandidateMetrics { mean_squared_error: 0.4, complexity: 2 },
|
|
CandidateMetrics { mean_squared_error: 0.4, complexity: 2 }, // exact duplicate
|
|
CandidateMetrics { mean_squared_error: 0.2, complexity: 5 },
|
|
CandidateMetrics { mean_squared_error: 0.2, complexity: 9 }, // dominated tie on error
|
|
CandidateMetrics { mean_squared_error: 0.1, complexity: 9 },
|
|
CandidateMetrics { mean_squared_error: 0.5, complexity: 5 }, // dominated interior
|
|
];
|
|
assert_eq!(pareto_front_2d(&candidates), pareto_front(&candidates));
|
|
assert_eq!(pareto_front_2d(&candidates), vec![0, 1, 2, 4]);
|
|
}
|