# Algorithms for prediction of consumption, based on previous data

I have data for a few stores which sell apples. For each store I have an averaged value of how many kilos of apples users have bought per day for this month. So it looks like this:

Store 1:
-------
January - average of 10 kilos of apples per day
February - average of 18.7 kilos of apples per day
March - average of 24.5 kilos of apples per day
April - ...

Store 2:
--------
January - average of 3.23 kilos of apples per day
February - average of 2.9 kilos of apples per day
March - average of 7.89 kilos of apples per day
...


For each store I want to make a prediction - how many kilos of apples per day will be sold in the next few months (4, 5 months in the future).

The problem is that sometimes the data is like this: (it is increasing and maybe it's good to use the method of least squares fitting to fit the data values with line and find the function)

but other times the data is like this: and the least squares won't work :(

What I would like to ask is:

• to make the prediction, is it always true that I should find a line or a curve to fit the data best or that's not always the case, and the prediction will be very wrong if I use this approach
• for the second type of graphics, what algorithms can I use :(
• how to decide which algorithm to use to fit the data best and if I should use a line for fitting, or a parabola or a third degree polynomial and so on

The ideal solution I want to write is to have one algorithm which takes the data and decides which algorithm (from a set) will give the best prediction and then give the data to the chosen algorithm for predictions.

(For example give data like (2, 3, 4, 5, 6, 7) and the first algorithm will say "use the method of least squares" and then I will give the data (2, 3, 4, 5, 6, 7...) to the method of least squares to get the next 4-5 values from it.)

I know it's a big question, but any kind of information will be very useful! Thank you very much in advance!

First of all, I would like to quote George E. P. Box

All models are wrong but some are useful

Actually your example shows one of the job of a statistician. You have data and need to fit a model so that you can do prediction. In your first example, you saw that you can use standard linear regression to do this. I agree with you that it seems appropriate (but you still have to check that it is the case). The second example shows you the limitation of the linear regression when dealing with Time Series. There exists different model (e.g. AR/MA/ARIMA/sARMA/etc...) that you can use to fit this kind of data. I feel that this is too complicated to explain it in a single post, so here is a reference :

Tsay, R. S. (2010) Analysis of Financial Time Series. Third Edition. New York: Wiley.


I'm sure there are lots of other great books, but I like this one.

PS: I'm not aware of any algorithm that can do the job for you.

• Thank you very much for your answer! Do you think that the book is suitable for a beginner? May 29 '14 at 14:02
• Good question. It should be suitable for a beginner but it is not going to be easy. It depends on how big is your motivation ;) May 29 '14 at 14:07
• I'm not an expert on this subject, but I would think that sale volume is affected by other aspects, price being one of them (but also some demographic aspects). If I were you, I would probably spend more time defining the problem and what affects sale volume and then trying to account for those aspects. At some point you may want to look into generalized least squares where you can account for the correlation in the error term. May 29 '14 at 15:02

It is possible to handle such situations. For example you could try and use a simple exponential moving average to predict the next sample point, and then use it again to predict the next to next point and so on. Holt winters is another scheme you could use. Here is a list of time series analysis books if you are looking. Hope that helps.