#![allow(non_snake_case)]
extern crate easy_ml;
#[cfg(test)]
mod linear_regression {
use easy_ml::linear_algebra;
use easy_ml::matrices::Matrix;
#[test]
fn linear_regression() {
let x = Matrix::column(vec![
0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
]);
let y = Matrix::column(vec![
1.0, 3.0, 3.5, 8.0, 9.0, 11.0, 13.0, 15.5, 17.5, 19.0, 21.0, 23.0, 25.0,
]);
let mut X = x.clone();
X.insert_column(0, 1.0);
println!("X = {}", &X);
#[rustfmt::skip]
let w = linear_algebra::inverse::<f32>(&(X.transpose() * &X))
.unwrap()
* (X.transpose() * &y);
let error = error_function(&w, &X, &y);
println!("error {:?}", error);
println!("w = {}", w);
println!("y = {}\nprediction = {}", y, (&X * &w));
assert!(error < 3.7);
assert!(error > 3.5);
}
fn error_function(w: &Matrix<f32>, X: &Matrix<f32>, y: &Matrix<f32>) -> f32 {
let error = y - (X * w);
(error.transpose() * error).scalar()
}
}