LoslegenKostenlos loslegen

ARMA(p, q)-Modell

Ein Modell der Art Auto-Regressive Moving Average (ARMA(p, q)) kombiniert die autoregressiven (AR(p)) und gleitenden Durchschnitts-Modelle (MA(q)) zu einem. Der aktuelle Wert des simulierten Vektors hängt sowohl von früheren Werten desselben Vektors als auch von früheren Werten des Rauschvektors ab.

Vervollständige die Funktionsdefinition von arma().

Diese Übung ist Teil des Kurses

R-Code mit Rcpp optimieren

Kurs anzeigen

Anleitung zur Übung

  • Definiere eine Ganzzahlvariable start als das Maximum aus p und q plus eins. Hinweis: max() ist im std-Namespace.
  • Definiere innerhalb der äußeren for-Schleife eine double-Variable value als mu plus den i-ten Rauschwert.
  • Erhöhe innerhalb der ersten inneren for-Schleife value um das j-te Element von theta mal das „i minus j minus 1“-te Element von eps.
  • Erhöhe innerhalb der zweiten inneren for-Schleife value um das j-te Element von phi mal das „i minus j minus 1“-te Element von x.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

#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()
*/
Code bearbeiten und ausführen