ARMA (p, q) モデル
自己回帰移動平均モデル(ARMA(p, q))は、自己回帰(AR(p))と移動平均(MA(q))を組み合わせたものです。シミュレーションするベクトルの現在の値は、そのベクトルの過去の値と、ノイズベクトルの過去の値の両方に依存します。
arma() の関数定義を完成させてください。
この演習はコースの一部です
Rcpp で R コードを最適化する
演習の手順
- 整数変数
startを、pとqの最大値に 1 を足した値として定義します。max()はstd名前空間にあることを思い出してください。 - 外側の for ループ内で、
double型の変数valueを、muにi番目のノイズ値を足したものとして定義します。 - 最初の内側の for ループ内で、
valueに、thetaのj番目の要素 かけるepsの「iひくjひく1」番目の要素を加算します。 - 2 番目の内側の for ループ内で、
valueに、phiのj番目の要素 かけるxの「i ひく j ひく 1」番目の要素を加算します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
#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()
*/