arima_sim

Function arima_sim 

Source
pub fn arima_sim<T: Rng>(
    n: usize,
    ar: Option<&[f64]>,
    ma: Option<&[f64]>,
    d: usize,
    noise_fn: &dyn Fn(&mut T) -> f64,
    rng: &mut T,
) -> Result<Vec<f64>>
Expand description

Simulate an ARIMA model time series

§Arguments

  • n - Length of the time series
  • ar - Model parameters for the AR part
  • ma - Model parameters for the MA part
  • d - Model parameter for the differences
  • noise_fn - Function that takes a `Rng’ as input and returns noise
  • rng - Reference to a mutable Rng.

§Returns

  • Output vector of length n containing the time series data.

§Example

use rand::prelude::*;
use rand_distr::{Distribution, Normal};

let normal = Normal::new(0.0, 2.0).unwrap();

let x = arima::sim::arima_sim(
    100,
    Some(&[0.9, -0.3, 0.2]),
    Some(&[0.4, 0.2]),
    1,
    &|mut rng| { normal.sample(&mut rng) },
    &mut thread_rng()
).unwrap();
Examples found in repository?
examples/ar3_estimation.rs (lines 14-21)
6fn main() {
7    // initialize RNG with seed
8    let mut rng: StdRng = SeedableRng::from_seed([100; 32]);
9
10    // our noise should be normally distributed
11    let normal = Normal::new(10.0, 2.0).unwrap();
12
13    // simulate time series
14    let ts = sim::arima_sim(
15        1000,                               // number of samples
16        Some(&[0.7, 0.2]),                  // AR parameters
17        None,                               // MA parameters
18        0,                                  // difference parameter
19        &|mut rng| normal.sample(&mut rng), // noise fn
20        &mut rng,                           // RNG
21    )
22    .unwrap();
23
24    // estimate AR parameters
25    let ar = acf::ar(&ts, Some(2)).unwrap();
26
27    println!("Estimated parameters: {:?}", ar);
28    // Estimated parameters: [0.7436892808499717, 0.14774749031248915]
29}