basis_select

Macro basis_select 

Source
macro_rules! basis_select {
    ($data:expr, $degree_bound:expr, $method:expr) => { ... };
    ($data:expr, $degree_bound:expr, $method:expr, options = [ $( $basis:path $( = $name:literal)? ),+ $(,)? ]) => { ... };
}
Expand description

Automatically fits a dataset against multiple polynomial bases and reports the best fits.

§Syntax

basis_select!(data, options = [Basis1<T>, Basis2<T>, …]);
basis_select!(data); // Uses default [MonomialBasis<f64>, ChebyshevBasis<f64>]

§Behavior

  • Tries to construct a CurveFit<Basis> for each basis in the provided list.

  • Uses CurveFit::new_auto with for each basis with the provided DegreeBound and scoring method (crate::score).

  • For each successful fit, computes:

    • r² value against the source data
    • p-value for residual normality test
    • A combined rating (0.75 * r² + 0.25 * p-value) for overall quality.
    • A star rating out of 5 based on the combined rating.
    • An equation string representation.
  • Displays a summary table of the top 3 fits

  • For each of the top 3 fits, prints:

    • A plot if the plotting feature is enabled
    • The equation string

§Parameters

  • $data: A slice of (x, y) points or any type accepted by CurveFit.
  • $degree_bound: The degree bound to use for fitting (see crate::statistics::DegreeBound).
  • $method: The scoring method to use for fitting (see crate::score).
  • options: Optional. List of basis types to compare. Default is all supported bases.

§Example

function!(test(x) = 2.0 x^3 + 3.0 x^2 - 4.0 x + 5.0);
let data = test
    .solve_range(0.0..=100.0, 1.0)
    .apply_normal_noise(Strength::Relative(0.1), None);
basis_select!(&data, DegreeBound::Relaxed, &Aic);

The example above will output something like:

[ Evaluating 100 data points against 3 basis options ]

     Basis      |     R²     | Residuals Normality  | Rating
--------------- | ---------- | -------------------- | ----------
ChebyshevBasis  | 99.10%     | 62.07%               | 90% ☆☆★★★
 MonomialBasis  | 99.10%     | 62.07%               | 90% ☆☆★★★
 FourierBasis   | 82.41%     | 0.00%                | 62% ☆☆☆☆★

ChebyshevBasis: xₛ = 2(x - a) / (b - a) - 1, a=0.00e0, b=99.00, y(x) = 5.38e4T₃(xₛ) + 3.65e5T₂(xₛ) + 9.09e5T₁(xₛ) + 6.12e5
Wrote plot to target\plot_output\chebyshevbasis_src_test.rs_line_307_1757796134.png

MonomialBasis: y(x) = 1.77x³ + 34.28x² - 1.33e3x + 1.45e4
Wrote plot to target\plot_output\monomialbasis_src_test.rs_line_307_1757796135.png

FourierBasis: xₛ = 2(x - a) / (b - a) - 1, a=0.00e0, b=99.00, y(x) = 1.72e4cos(2π4xₛ) - 1.42e5sin(2π4xₛ) + 4.27e4cos(2π3xₛ) - 1.98e5sin(2π3xₛ) + 6.79e4cos(2π2xₛ) - 2.94e5sin(2π2xₛ) + 2.98e5cos(2πxₛ) - 5.30e5sin(2πxₛ) + 4.92e5
Wrote plot to target\plot_output\fourierbasis_src_test.rs_line_307_1757796135.png