# How many data points to acurately approximate the average of recent values in a time series?

I have some time series data, where each data point in the time series has a timestamp and a value. For example:

Time 1    value 5
Time 2    Value 7
Time 3    Value 10


Now at a given time X, I want to obtain an approximation on the average of recent values in the time series. For example, consider that the time series is temperature readings for a machine. I want to calculate the average temperature readings in recent days. To do this, I must choose the number of data points to be used to calculate the average.

On the one hand, if I choose to use a smaller number of values such as the two last values of the time series to calculate the average, the average will not be very reliable because these data points may be outliers.

On the other hand, if I choose to use all previous values of the time series, this may not reflect recent trends in the time series, but the average would better tolerate outliers.

Thus, my question is how can a determine a minimum number of data points to calculate an average of recent values in a time series that is considered "reliable". The concept of "reliable" should be expressed in terms of a confidence interval, if possible.

Of course, the concept of "recent values" is fuzzy, but there are certainly some lower bound on the number of data points that should be use to calculate an average reliably.

Just to make my question clearer, I will tell you the solution that I have thought about, which is in my opinion not good. I have though about using the Hoeffding bound ( https://en.wikipedia.org/wiki/Hoeffding%27s_inequality), as it "provides an upper bound on the probability that the sum of independent random variables deviates from its expected value". But I think that it is inappropriate for my problem with a time series as events may not be independent or the time series may not be stationnary. Thus, what could be used as a better technique?

Edit: I have edited the question since some people have decided put the question on hold.

• What is your definition of a "good" approximation? – Dave2e Jul 18 '17 at 2:27
• If we don't use enough data points, then the average is meaningless. For example, if the time series is temperature readings, using only 2 data points would probably be meaningless as these points could be outliers which would influence too much the average. Perhaps that we should use at least 10 data points to get something more stable... But I do not want to use a fixed value like 10. I would like to calculate something like a confidence interval on how good the average is approximated using a given amount of data points – Phil Jul 18 '17 at 2:31
• I also do not make any assumption about the distribution of values in the time series. – Phil Jul 18 '17 at 2:34
• So in other words, a good approximation for me would be an approximation that is not influenced too much by outlier values, and that also use enough data points to consider that the average as representative of the recent values in the time series. – Phil Jul 18 '17 at 2:37
• I have thought about using the Hoeffding bound for this. But I am not sure if it is a good idea – Phil Jul 18 '17 at 2:43

I agree with Carl that fitting a model to the data would be an ideal solution, if it makes sense in your context. But I also want to suggest that the following way of thinking about your problem might be helpful.

Suppose that I have a time-varying process that looks like a random walk (so that the location is autocorrelated through time). Suppose I measure this position with some measurement error (which is an IID random variable) at equally spaced time points. I understand your question to mean:

If the random walk careens around wildly and the measurement error is small, then the answer might well be use only the most recent point. Using previous points in the average would introduce lots of extra variation due to the random walk, and a single point is already a reasonably good approximation of the process' true location. If the measurement error is large and the random walk moves slowly, then the answer will be use a lot of points. The random walk is relatively stationary, and you need to average over the noise in the measurement. Interestingly, if your measurement noise is extremely fat-tailed (i.e. Cauchy distributed), then the answer will always be use just the most recent point (because the average of multiple points does not provide a better approximation to the central tendency of that distribution than any single point does!).

It should be possible to work out the ideal number of points to use in special cases where the distribution followed by the random walk and the distribution of the measurement error are both known. However, this is precisely the case where a model, as suggested by Carl, would be useful.

Edit Carl's comment also made me realize that it's very likely that a weighted average (that weights more recent points more heavily) could outperform an average that introduces some hard-threshold cutoff for inclusion.

• OK, +1, as a second step perhaps when tendencies in the data are excluded. To exclude them with certainty is a matter of showing that the data is truly random, which implies knowing what the noise in the data is, e.g., Gaussian, Poisson, whatever. In that case, if the noise explains all of the data, then there is no latent information. – Carl Jul 25 '17 at 0:30
• +1 to your answer as well. The model fitting approach works well even in the pure noise case, I think (the model in this case fits a random walk with measurement error to the data, and thus decomposes the time-series into process and sampling noise), so even though there's no latent information to leverage, it still gives a good answer. My reason for posting this was just to try to answer the original question more narrowly--if I'm going to attack this issue by averaging recent points (and not even using a weighted average!), how many should I use? – Jacob Socolar Jul 25 '17 at 0:35

Q1: How many points?

All of them. The best way, is to fit a model to the data. If the model is a good one, i.e., if an accurate $T(t)$ can be found, where $T$ is the temperature as a function of $t$ time, then the problem is solved, just choose a $t$, and $T$ is predicted.

In a temperature time series this may require one or more delay terms in the auto regressive integrated moving average, ARIMA, sense. However, in general, use of a model is superior to taking averages.

True outliers are rare. If needed outlier testing can be performed. More frequently, when the data is not normally distributed on the $T$-axis, a transformation of variables may be needed to make conditions more normal, among other examples using $\ln(T)$, or using $\dfrac{1}{T}$ or using $\sqrt{T}$ instead of plain old $T$ for the regression equation target.
• ARIMA is useful as far as an error correlation model. It may be worth mentioning useful model classes for the function $T(t)$ itself -- for example, penalized splines. – eric_kernfeld Jul 26 '17 at 15:12