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
Anleitung zur Übung
- Definiere eine Ganzzahlvariable
startals das Maximum auspundqplus eins. Hinweis:max()ist imstd-Namespace. - Definiere innerhalb der äußeren for-Schleife eine
double-Variablevaluealsmuplus den i-ten Rauschwert. - Erhöhe innerhalb der ersten inneren for-Schleife
valueum das j-te Element vonthetamal das „i minus j minus 1“-te Element voneps. - Erhöhe innerhalb der zweiten inneren for-Schleife
valueum das j-te Element vonphimal das „i minus j minus 1“-te Element vonx.
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()
*/