Skip to main content

PredictiveEncoder

Struct PredictiveEncoder 

Source
pub struct PredictiveEncoder { /* private fields */ }
Expand description

Encodes based on causal predictive error from expected values.

This encoder is best understood as an adaptive EWMA anomaly detector with predictive-coding-style signed error spikes. It keeps per-channel history, predicts the next sample from prior samples only, and fires a spike when the signed prediction error is large enough. Positive errors emit polarity: true; negative errors emit polarity: false.

§Mathematical Model

Tracks an exponentially weighted moving average of recent history means per channel. The first five samples are a warm-up period: they update history and initialize the prediction baseline but never emit spikes. After warm-up, each input is evaluated against the predictor state formed before that input is inserted, so the current observation cannot leak into its own prediction.

if history.len() < 5:
    push value; initialize prediction when five samples are available; no spike
else:
    prediction = threshold[i]
    error = value - prediction
    spike if |error| > threshold, with polarity = error >= 0
    push value
    threshold[i] = 0.9 * threshold[i] + 0.1 * mean(history[-5:])

§When to Use

  • Anomaly detection in sensor streams
  • Learning patterns and detecting deviations
  • Adaptive encoding that adjusts to baseline activity

§Parameters

  • history_depth: Number of past values to track per channel
  • deviation_thresholds: Vec of (threshold, spike_value) pairs
  • num_channels: Number of input channels

§Examples

use axon_encoder::prelude::*;
let mut enc = PredictiveEncoder::try_new(8, vec![(0.5, 1)], 1)?;
// First five samples warm up without spikes.
for v in [1.0, 1.0, 1.0, 1.0, 1.0] {
    assert!(enc.encode_step(&[v]).spikes.is_empty());
}
// A large jump after warm-up can emit a prediction-error spike.
let _ = enc.encode_step(&[3.0]);

Implementations§

Source§

impl PredictiveEncoder

Source

pub fn new( history_depth: usize, deviation_thresholds: Vec<(f32, u16)>, num_channels: usize, ) -> Result<Self, PredictiveEncoderError>

Creates a new PredictiveEncoder.

Retains the historical PredictiveEncoderError surface for source compatibility. Prefer try_new for the unified EncoderError type used by other fallible constructors.

§Errors
Source

pub fn try_new( history_depth: usize, deviation_thresholds: Vec<(f32, u16)>, num_channels: usize, ) -> Result<Self, EncoderError>

Creates a new PredictiveEncoder, returning the unified EncoderError.

Prefer this over new when propagating constructor failures alongside other encoders via EncoderError. Each deviation_threshold must be finite and non-negative (same rule as TemporalEncoder::try_new).

Source

pub fn encode_with_modulators( &mut self, input: &[f32], modulators: &NeuroModulators, gain_curves: &NeuromodulatorGainCurves, ) -> EncodedOutput

Encodes input using neuromodulator-driven gain curves.

Inherent wrapper so callers need not import ModulatedEncoder.

Source

pub fn encode_step_with_modulators( &mut self, input: &[f32], modulators: &NeuroModulators, gain_curves: &NeuromodulatorGainCurves, ) -> EncodedOutput

Step-wise variant of encode_with_modulators.

Trait Implementations§

Source§

impl Clone for PredictiveEncoder

Source§

fn clone(&self) -> PredictiveEncoder

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PredictiveEncoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Encoder for PredictiveEncoder

Source§

fn encode(&mut self, input: &[f32]) -> EncodedOutput

Encodes a slice of analog values into spike events (batch mode).
Source§

fn encode_step(&mut self, input: &[f32]) -> EncodedOutput

Encodes a single step incrementally (streaming mode). Read more
Source§

fn reset(&mut self)

Resets the encoder to its initial state
Source§

impl ModulatedEncoder for PredictiveEncoder

Source§

fn encode_with_gains( &mut self, input: &[f32], gains: EncodingGains, ) -> EncodedOutput

Encodes input using already evaluated encoding gains. Read more
Source§

fn encode_step_with_gains( &mut self, input: &[f32], gains: EncodingGains, ) -> EncodedOutput

Encodes one streaming step using already evaluated encoding gains. Read more
Source§

fn encode_with_modulators( &mut self, input: &[f32], modulators: &NeuroModulators, gain_curves: &NeuromodulatorGainCurves, ) -> EncodedOutput

Encodes input using neuromodulator-driven gain curves.
Source§

fn encode_step_with_modulators( &mut self, input: &[f32], modulators: &NeuroModulators, gain_curves: &NeuromodulatorGainCurves, ) -> EncodedOutput

Encodes one streaming step using neuromodulator-driven gain curves.
Source§

impl PartialEq for PredictiveEncoder

Source§

fn eq(&self, other: &PredictiveEncoder) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for PredictiveEncoder

Auto Trait Implementations§

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.