Skip to main content

simple_linear_regression

Function simple_linear_regression 

Source
pub fn simple_linear_regression(
    x: &[f64],
    y: &[f64],
) -> Option<SimpleRegressionResult>
Expand description

Computes simple linear regression (OLS closed-form).

§Algorithm

β₁ = cov(x,y) / var(x) β₀ = ȳ - β₁·x̄

§Returns

None if fewer than 3 observations, slices differ in length, x has zero variance, or inputs contain non-finite values.

§References

Draper & Smith (1998). “Applied Regression Analysis”, 3rd edition.

§Examples

use u_analytics::regression::simple_linear_regression;

let x = [1.0, 2.0, 3.0, 4.0, 5.0];
let y = [2.0, 4.0, 6.0, 8.0, 10.0];
let r = simple_linear_regression(&x, &y).unwrap();
assert!((r.slope - 2.0).abs() < 1e-10);
assert!((r.intercept).abs() < 1e-10);
assert!((r.r_squared - 1.0).abs() < 1e-10);