pub struct PredictionModel<A: Float + Send + Sync> { /* private fields */ }Expand description
Autoregressive prediction model for streaming data, fitted online.
The model is a vector autoregression with shared scalar coefficients:
the next observation vector is predicted from the previous model_order
observed vectors as
x[t] ~= a_1 * x[t-1] + a_2 * x[t-2] + ... + a_p * x[t-p] + bwith one scalar coefficient per lag plus an intercept, shared across all coordinates. Sharing the coefficients keeps the parameter count independent of the feature dimensionality (so a wide stream does not need a quadratic number of parameters to be trainable from a short window) while still capturing the temporal structure — trends, decay and oscillation — that a “repeat the last value” predictor cannot represent at all.
Fitting uses exponentially-weighted Recursive Least Squares: every observed coordinate of every new sample performs one exact rank-1 update of the least-squares solution, so the model is always the exact (forgetting-weighted) least-squares fit of everything it has seen. That is what makes it converge in a handful of periods rather than needing a learning-rate schedule.
Implementations§
Source§impl<A: Float + Send + Sync> PredictionModel<A>
impl<A: Float + Send + Sync> PredictionModel<A>
Sourcepub fn new(model_order: usize) -> Result<Self>
pub fn new(model_order: usize) -> Result<Self>
Create a model of the given autoregressive order with the default forgetting factor.
Note that the argument is the autoregressive order (number of lags). The previous signature took a “feature dimension” and sized the never-trained weight vector with it; the real feature dimensionality is now adopted from the data on the first observation.
Sourcepub fn with_forgetting_factor(
model_order: usize,
forgetting_factor: A,
) -> Result<Self>
pub fn with_forgetting_factor( model_order: usize, forgetting_factor: A, ) -> Result<Self>
Create a model with an explicit exponential forgetting factor.
Sourcepub fn train_updates(&self) -> usize
pub fn train_updates(&self) -> usize
Number of RLS updates applied so far (one per observed coordinate of every sample that had a full lag window available).
Sourcepub fn coefficients(&self) -> &Array1<A>
pub fn coefficients(&self) -> &Array1<A>
The fitted autoregressive coefficients [a_1, ..., a_p, b].
Sourcepub fn mean_absolute_error(&self) -> A
pub fn mean_absolute_error(&self) -> A
EWMA of the absolute one-step-ahead prediction error measured on live data before each sample was used for training, i.e. an honest out-of-sample error estimate.
Sourcepub fn normalized_error(&self) -> A
pub fn normalized_error(&self) -> A
Measured out-of-sample error relative to the observed signal scale. Zero for an untrained model (it has made no errors yet because it has made no predictions).
Sourcepub fn confidence(&self) -> A
pub fn confidence(&self) -> A
Confidence in [0, 1], derived from the measured out-of-sample error
relative to the observed signal scale: 1 / (1 + error / scale). An
untrained model reports zero — it has no basis for any claim.
Sourcepub fn observe(&mut self, point: &StreamingDataPoint<A>) -> Result<()>
pub fn observe(&mut self, point: &StreamingDataPoint<A>) -> Result<()>
Feed one newly observed data point to the model: measure the out-of-sample error of the prediction the current weights would have made for it, then perform the RLS update that folds it into the fit.
Must be called once per sample, in arrival order.
Sourcepub fn predict(
&self,
data: &VecDeque<StreamingDataPoint<A>>,
horizon: usize,
) -> Result<Vec<StreamingDataPoint<A>>>
pub fn predict( &self, data: &VecDeque<StreamingDataPoint<A>>, horizon: usize, ) -> Result<Vec<StreamingDataPoint<A>>>
Roll the fitted model forward horizon steps from data, feeding each
prediction back in as the next lag (the standard iterated multi-step
forecast).
Returns fewer than horizon points when the model has not been
trained yet, when data is shorter than the autoregressive order, or
when the recursion stops producing finite values — never a fabricated
copy of the present.
Auto Trait Implementations§
impl<A> Freeze for PredictionModel<A>where
A: Freeze,
impl<A> RefUnwindSafe for PredictionModel<A>where
A: RefUnwindSafe,
impl<A> Send for PredictionModel<A>
impl<A> Sync for PredictionModel<A>
impl<A> Unpin for PredictionModel<A>where
A: Unpin,
impl<A> UnsafeUnpin for PredictionModel<A>where
A: UnsafeUnpin,
impl<A> UnwindSafe for PredictionModel<A>where
A: UnwindSafe + RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.