RLS Learning Model¶
This module contains the implementation of the Recursive Least Squares algorithm for online linear regression.
Recursive Least Squares Class¶
- class source.model.incremental.RecursiveLeastSquares(num_variables, forgetting_factor, initial_delta)¶
Bases:
objectRecurrent Least Squares algorithm for online linear regression.
- Parameters:
num_variables (int) – Number of variables including the constant.
forgetting_factor (float) – Forgetting factor (lambda), usually close to 1.
initial_delta (float) – Controls the initial state.
Initialize the RecurrentLeastSquares object.
- Parameters:
num_variables (int) – Number of variables including the constant.
forgetting_factor (float) – Forgetting factor (lambda), usually close to 1.
initial_delta (float) – Controls the initial state.
- __init__(num_variables, forgetting_factor, initial_delta)¶
Initialize the RecurrentLeastSquares object.
- Parameters:
num_variables (int) – Number of variables including the constant.
forgetting_factor (float) – Forgetting factor (lambda), usually close to 1.
initial_delta (float) – Controls the initial state.
- fit(observations, labels)¶
Fit the model to a set of observations and labels.
- Parameters:
observations (list of numpy arrays) – List of observation vectors.
labels (list of floats) – List of true labels corresponding to the observations.
- predict(observation)¶
Predict the value of a new observation.
- Parameters:
observation (numpy array) – Observation vector.
- Returns:
prediction – Predicted value for the observation.
- Return type:
float
- update(observation, label)¶
Update the model with a new observation and label.
- Parameters:
observation (numpy array) – Observation vector.
label (float) – True label corresponding to the observation.
Example Usage¶
from source.model.incremental import RecursiveLeastSquares
import numpy as np
# Initialize model
model = RecursiveLeastSquares(num_features=3, lambda_factor=0.99, delta=1.0)
# Simulate streaming data
for _ in range(100):
# Generate random data
X_new = np.random.rand(1, 3) # New feature vector
y_new = np.random.rand().reshape(-1, 1) # New target value
# Make prediction
y_pred = model.predict(X_new)
print(f"Predicted: {y_pred}, Actual: {y_new}")
# Update model with new data
model.update(X_new, y_new)
print(f"Updated Weights: {model.weights}")