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
Instrukcje do ćwiczenia
- Zdefiniuj zmienną całkowitą
startrówną maksimum zpiq, powiększonemu o jeden. Pamiętaj, żemax()należy do przestrzeni nazwstd. - Wewnątrz zewnętrznej pętli for zdefiniuj zmienną
doubleo nazwievaluejakomuplusi-ta wartość szumu. - Wewnątrz pierwszej wewnętrznej pętli for zwiększ
valueoj-ty elementthetarazy elementepso indeksie "iminusjminus1". - Wewnątrz drugiej wewnętrznej pętli for zwiększ
valueoj-ty elementphirazy elementxo 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()
*/