the autocorrelation is the correlation of a process $X$ and its lagged version, hence you have to consider it from a probabilistic viewpoint.
Use the $\sigma_\ell$ notation for the operator that shifts a process:
$${\cal A}(X;\ell):=\frac{\mathbb{E}\big((X - \mathbb{E}X) \cdot (\sigma_\ell\circ X - \mathbb{E}\sigma_\ell\circ X)\big)}{\sqrt{\mathbb{E}(X - \mathbb{E}X)^2 \cdot \mathbb{E}(\sigma_\ell\circ X - \mathbb{E}\sigma_\ell\circ X)^2}}.$$
This is the true definition. Now you need to use empirical estimators for all these quantities.
Usually we take $\frac{1}{N}\sum_{n=1}^N X_n$ as an estimator for $\mathbb{E}X$ over a sample of size $N$.
I let you replace and you will get the first formula you snapshotted in your question.
Now think about the set of information you have: you know $X_t$ from $t=1$ to $t=N+\ell$. When it is about estimating quantities that are not a function of the dependencies between $X$ and $\sigma_\ell\circ X$, isn't it better to use all the available information?
i.e. $\frac{1}{N+\ell}\sum_{n=1}^{N+\ell} X_n$ may be a better estimator of $\mathbb{E}X$, no? simply because you use more observations (of course it is submitted to some stationarity assumptions, like it has been underlined in one remark).
Here we talk about $\mathbb{E}X$, $\mathbb{E}\sigma_\ell\circ X$, $\mathbb{E}(X - \mathbb{E}X)^2$ and $\mathbb{E}(\sigma_\ell\circ X - \mathbb{E}\sigma_\ell\circ X)^2$ that are all concerning $X$ only. You can estimate them using as many observation as possible. Of course if you care about outliers, you can also use a bootstrap method (especially for the variance terms, since bootstrap is designed that for), or any method you like.
If you do so, then you recover the last formula of your question:
- at the numerator you use $\bar X=1/N\sum_n X_n$ for $\mathbb{E}X$ and for $\mathbb{E}\sigma_l\circ X$:
$$\mathbb{E}\big((X - \mathbb{E}X) \cdot (\sigma_\ell\circ X - \mathbb{E}\sigma_\ell\circ X)\big)\simeq \mathbb{E}\big((X - \bar X) \cdot (\sigma_\ell\circ X - \bar X)\big).$$
- the denominator boils down to
$$\sqrt{\mathbb{E}(X - \mathbb{E}X)^2 \cdot \mathbb{E}(\sigma_\ell\circ X - \mathbb{E}\sigma_\ell\circ X)^2}\simeq \mathbb{E}(X - \bar X)^2.$$
- When you put them together
$${\cal A}(X;\ell)\simeq \frac{\sum_n (X_n - \bar X) (X_{n+\ell}-\bar X)}{\sum_n (X_n - \bar X)^2}.$$
What is important to understand is that: both formula are ok since they are estimators of the same statistic ${\cal A}(X;\ell)$, that is the true formula. It depends how you want to build the estimators. Which one is the best? it depends on the true (not known) distribution of the $X_t$.
I would say that
- if $X$ is stationary, the second one is the best,
- whereas if $X$ is not stationary, the first one may be better.