Skip to main content

PredictionModel

Struct PredictionModel 

Source
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] + b

with 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>

Source

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.

Source

pub fn with_forgetting_factor( model_order: usize, forgetting_factor: A, ) -> Result<Self>

Create a model with an explicit exponential forgetting factor.

Source

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).

Source

pub fn coefficients(&self) -> &Array1<A>

The fitted autoregressive coefficients [a_1, ..., a_p, b].

Source

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.

Source

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).

Source

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.

Source

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.

Source

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>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V