(AR)=
:::{post} Jan 7, 2023 :tags: time series, autoregressive :category: intermediate, :author: Ed Herbst, Chris Fonnesbeck :::
Consider the following AR(2) process, initialized in the infinite past:
where $\epsilon_t \overset{iid}{\sim} {\cal N}(0,1)$. Suppose you'd like to learn about $\rho$ from a a sample of observations $Y^T = { y_0, y_1,\ldots, y_T }$.
First, let's generate some synthetic sample data. We simulate the 'infinite past' by generating 10,000 samples from an AR(2) process and then discarding the first 5,000:
Let's start by trying to fit the wrong model! Assume that we do no know the generative model and so simply fit an AR(1) model for simplicity.
This generative process is quite straight forward to implement in PyMC. Since we wish to include an intercept term in the AR process, we must set constant=True otherwise PyMC will assume that we want an AR2 process when rho is of size 2. Also, by default a $N(0, 100)$ distribution will be used as the prior for the initial value. We can override this by passing a distribution (not a full RV) to the init_dist argument.
We can see that even though we assumed the wrong model, the parameter estimates are actually not that far from the true values.
Now let's fit the correct underlying model, an AR(2):
The AR distribution infers the order of the process thanks to the size the of rho argument passed to AR (including the mean).
We will also use the standard deviation of the innovations (rather than the precision) to parameterize the distribution.
The posterior plots show that we have correctly inferred the generative model parameters.
You can also pass the set of AR parameters as a list, if they are not identically distributed.
:::{include} ../page_footer.md :::