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 channeldeviation_thresholds: Vec of (threshold, spike_value) pairsnum_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
impl PredictiveEncoder
Sourcepub fn new(
history_depth: usize,
deviation_thresholds: Vec<(f32, u16)>,
num_channels: usize,
) -> Result<Self, PredictiveEncoderError>
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
PredictiveEncoderError::HistoryDepthTooSmallifhistory_depth < 5PredictiveEncoderError::NumChannelsTooLargeifnum_channels > u16::MAX as usize + 1(spikechannelIDs areu16, so indices must stay in0..=u16::MAX)PredictiveEncoderError::InvalidDeviationThresholdif any threshold is non-finite or negative
Sourcepub fn try_new(
history_depth: usize,
deviation_thresholds: Vec<(f32, u16)>,
num_channels: usize,
) -> Result<Self, EncoderError>
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).
Sourcepub fn encode_with_modulators(
&mut self,
input: &[f32],
modulators: &NeuroModulators,
gain_curves: &NeuromodulatorGainCurves,
) -> EncodedOutput
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.
Sourcepub fn encode_step_with_modulators(
&mut self,
input: &[f32],
modulators: &NeuroModulators,
gain_curves: &NeuromodulatorGainCurves,
) -> EncodedOutput
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
impl Clone for PredictiveEncoder
Source§fn clone(&self) -> PredictiveEncoder
fn clone(&self) -> PredictiveEncoder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more