Skip to main content

Module fracdiff

Module fracdiff 

Source
Expand description

Fractional differentiation (AFML chapter 5).

Differencing a series d times for real-valued d keeps part of its memory while (for a large enough d) making it stationary: AFML §5.2’s stationarity-versus-memory dilemma. The weights come from the binomial expansion of (1 - B)^d (Snippet 5.1), applied either on an expanding window (frac_diff, Snippet 5.2) or on a fixed-width window (frac_diff_ffd, Snippet 5.3). The fixed-width form is the one to use for features, because every output is the same function of the same number of lags.

Conventions:

  • Inputs are levels (log prices or prices), oldest first. Passing returns differences them a second time.
  • Weight vectors are returned oldest lag first: the last element is w_0 = 1 and the one before it is -d.
  • Leading outputs without enough history are f64::NAN; the output has the same length as the input.
  • Nothing is validated: a negative d integrates instead of differencing, and a NaN in the input poisons every output whose window covers it.
use openquant::fracdiff::{frac_diff_ffd, get_weights};

assert_eq!(get_weights(0.5, 4), vec![-0.0625, -0.125, -0.5, 1.0]);

// d = 1 is the ordinary first difference.
let series: Vec<f64> = (1..=5).map(f64::from).collect();
let diffed = frac_diff_ffd(&series, 1.0, 1e-5);
assert!(diffed[0].is_nan());
assert_eq!(&diffed[1..], &[1.0, 1.0, 1.0, 1.0]);

Functions§

frac_diff
Fractionally differences series with an expanding window (AFML Snippet 5.2).
frac_diff_ffd
Fractionally differences series with a fixed-width window (AFML Snippet 5.3).
get_weights
Returns the first size fractional-differencing weights for order diff_amt (AFML Snippet 5.1).
get_weights_ffd
Returns the fixed-width-window weights for order diff_amt (AFML Snippet 5.3).