Skip to content

Commit

Permalink
Update rls.py
Browse files Browse the repository at this point in the history
update
  • Loading branch information
W0lfgunbl00d committed Nov 17, 2024
1 parent 7189973 commit dd269a7
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions river/linear_model/rls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RLS:
----------
p : int
Filter order.
l : float
forgetting_factor : float
Forgetting factor.
delta : float
Initialization value for P(0).
Expand All @@ -50,7 +50,7 @@ class RLS:
>>> import numpy as np
>>> # Initialize the RLS filter with order 2, forgetting factor 0.98, and delta 1e6
>>> rls = linear_model.RLS(p=2, l=0.98, delta=1e6)
>>> rls = linear_model.RLS(p=2, forgetting_factor=0.98, delta=1e6)
>>> # Simulate some data
>>> np.random.seed(42)
Expand All @@ -65,21 +65,21 @@ class RLS:
>>> print("Final Weights:", rls.estimates[-1].flatten())
Final Weights: [ 3.48065382 -6.15301727 3.3361416 ]
"""
def __init__(self, p: int, l=0.99, delta=1000000):
def __init__(self, p: int, forgetting_factor=0.99, delta=1000000):
"""
Initializes the Recursive Least Squares (RLS) filter.
Parameters
----------
p : int
Filter order (number of coefficients).
l : float, optional
forgetting_factor : float, optional
Forgetting factor (default is 0.99).
delta : float, optional
Initial value for the inverse correlation matrix (default is 1,000,000).
"""
self.p = p # Filter order
self.l = l # Forgetting factor
self.forgetting_factor = forgetting_factor # Forgetting factor
self.delta = delta # Value to initialise P(0)

self.currentStep = 0
Expand Down Expand Up @@ -117,14 +117,14 @@ def estimate(self, xn: float, dn: float):
wn_prev = self.estimates[-1]

# Compute gain vector
denominator = self.l + self.x.T @ self.Pks[-1] @ self.x
denominator = self.forgetting_factor + self.x.T @ self.Pks[-1] @ self.x
gn = (self.Pks[-1] @ self.x) / denominator

# Compute a priori error
alpha = dn - (self.x.T @ wn_prev)

# Update inverse correlation matrix
Pn = (self.Pks[-1] - gn @ self.x.T @ self.Pks[-1]) / self.l
Pn = (self.Pks[-1] - gn @ self.x.T @ self.Pks[-1]) / self.forgetting_factor
self.Pks.append(Pn)

# Update weight vector
Expand Down

0 comments on commit dd269a7

Please sign in to comment.