Zacznij terazZacznij za darmo

Model ARMA(p, q)

Model autoregresyjny ze średnią ruchomą (ARMA(p, q)) łączy modele autoregresji (AR(p)) i średniej ruchomej (MA(q)) w jeden. Bieżąca wartość symulowanego wektora zależy zarówno od poprzednich wartości tego samego wektora, jak i od poprzednich wartości wektora szumów.

Uzupełnij definicję funkcji arma().

To ćwiczenie jest częścią kursu

Optymalizacja kodu R za pomocą Rcpp

Zobacz kurs

Instrukcje do ćwiczenia

  • Zdefiniuj zmienną całkowitą start równą maksimum z p i q, powiększonemu o jeden. Pamiętaj, że max() należy do przestrzeni nazw std.
  • Wewnątrz zewnętrznej pętli for zdefiniuj zmienną double o nazwie value jako mu plus i-ta wartość szumu.
  • Wewnątrz pierwszej wewnętrznej pętli for zwiększ value o j-ty element theta razy element eps o indeksie "i minus j minus 1".
  • Wewnątrz drugiej wewnętrznej pętli for zwiększ value o j-ty element phi razy element x o indeksie "i minus j minus 1".

Interaktywne ćwiczenie praktyczne

Spróbuj tego ćwiczenia, uzupełniając ten przykładowy kod.

#include 
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector arma(int n, double mu, NumericVector phi, NumericVector theta, double sd) {
  int p = phi.size();
  int q = theta.size();   
  NumericVector x(n);
  
  // Generate the noise vector
  NumericVector eps = rnorm(n, 0.0, sd);
  
  // Start at the max of p and q plus 1
  ___
  
  // Loop i from start to n
  for(int i = start; i < n; i++) {
    // Value is mean plus noise
    ___
    
    // The MA(q) part
    for(int j = 0; j < q; j++) {
      // Increase by the jth element of theta times
      // the "i minus j minus 1"th element of eps
      ___
    }
    
    // The AR(p) part
    for(int j = 0; j < p; j++) {
      // Increase by the jth element of phi times
      // the "i minus j minus 1"th element of x
      ___
    }
    
    x[i] = value;
  }
  return x;
}

/*** R
d <- data.frame(
  x = 1:50,
  y = arma(50, 10, c(1, -0.5), c(1, -0.5), 1)
)
ggplot(d, aes(x, y)) + geom_line()
*/
Edytuj i uruchom kod