///|
/// Recursive Least Squares (RLS) model for online linear regression.
pub struct RLS {
weights : Array[Double]
/// Inverse covariance matrix
p : Array[Array[Double]]
/// Forgetting factor (0 < lambda <= 1)
lambda : Double
} derive(ToJson, FromJson)
///|
/// Create a new RLS model with `dim` features.
/// `lambda` is the forgetting factor (default 1.0).
/// `alpha` is the initial ridge regression penalty (default 1.0).
pub fn RLS::new(
dim : Int,
lambda? : Double = 1.0,
alpha? : Double = 1.0,
) -> RLS {
let weights = Array::make(dim, 0.0)
let p = Array::make(dim, Array::make(dim, 0.0))
// Initialize P to alpha^-1 * I
for i in 0.. Double {
let mut pred = 0.0
let dim = self.weights.length()
for i in 0.. Unit {
let dim = self.weights.length()
// Calculate gain vector K = P * x / (lambda + x^T * P * x)
let p_x = Array::make(dim, 0.0)
let mut x_p_x = 0.0
for i in 0..