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을 뺀" 인덱스(eps[i-j-1])의 곱을 더하세요. - 두 번째 안쪽 for 루프에서는,
value에phi의j번째 원소와x의 "i에서 j와 1을 뺀" 인덱스(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()
*/