Function arima::acf::ar

source ·
pub fn ar<T: Float + From<u32> + From<f64> + Into<f64> + Copy + AddAssign>(
    x: &[T],
    order: Option<usize>
) -> Result<(Vec<T>, T)>
Expand description

Calculate the auto-regressive coefficients of a time series of length n. If you already calculated the auto-correlation coefficients (ACF), consider using ar_rho instead.

Arguments

  • &x - Reference to input vector slice of length n.
  • order - Order of the AR model.

Returns

  • Output vector of length order containing the AR coefficients.

Example

use arima::acf;
let x = [1.0, 1.2, 1.4, 1.6];
let (ar, _var) = acf::ar(&x, Some(2)).unwrap();
assert!((ar[0] - 0.3466667).abs() < 1.0e-7);
assert!((ar[1] - -0.3866667).abs() < 1.0e-7);
Examples found in repository?
examples/ar3_estimation.rs (line 25)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
fn main() {
    // initialize RNG with seed
    let mut rng: StdRng = SeedableRng::from_seed([100; 32]);

    // our noise should be normally distributed
    let normal = Normal::new(10.0, 2.0).unwrap();

    // simulate time series
    let ts = sim::arima_sim(
        1000,                               // number of samples
        Some(&[0.7, 0.2]),                  // AR parameters
        None,                               // MA parameters
        0,                                  // difference parameter
        &|mut rng| normal.sample(&mut rng), // noise fn
        &mut rng,                           // RNG
    )
    .unwrap();

    // estimate AR parameters
    let ar = acf::ar(&ts, Some(2)).unwrap();

    println!("Estimated parameters: {:?}", ar);
    // Estimated parameters: [0.7436892808499717, 0.14774749031248915]
}