Vec ArModel::simulate(int n, const Vec &y0) const { if(y0.size() != number_of_lags()){ ostringstream err; err << "Error in ArModel::simulate." << endl << "Initial state value y0 was size " << y0.size() << ", but the model has " << number_of_lags() << " lags." << endl; report_error(err.str()); } const Vec &phi(this->phi()); std::deque<double> lags(y0.rbegin(), y0.rend()); Vec ans; ans.reserve(n); for(int i = 0; i < n; ++i) { double mu = 0; for(int lag = 0; lag < number_of_lags(); ++lag) { mu += phi[lag] * lags[lag]; } double y = rnorm(mu, sigma()); lags.push_front(y); lags.pop_back(); ans.push_back(y); } return ans; }
Vec ArModel::simulate(int n) const { int p = number_of_lags(); Vec acf = autocovariance(p); Spd Sigma(p); Sigma.diag() = acf[0]; for(int i = 1; i < p; ++i) { Sigma.subdiag(i) = acf[i]; Sigma.superdiag(i) = acf[i]; } Vec zero(p, 0.0); Vec y0 = rmvn(zero, Sigma); return simulate(n, y0); }
Vector ArModel::simulate(int n, RNG &rng) const { int p = number_of_lags(); Vector acf = autocovariance(p); SpdMatrix Sigma(p); Sigma.diag() = acf[0]; for (int i = 1; i < p; ++i) { Sigma.subdiag(i) = acf[i]; Sigma.superdiag(i) = acf[i]; } Vector zero(p, 0.0); Vector y0 = rmvn(zero, Sigma); return simulate(n, y0, rng); }