Search code examples
pythonpython-3.xprediction

Can we predict the next number in a sequence, based on a single column of numbers?


I am trying to find a way to predict the next number in a sequence of numbers. Normally I would go use liner regression for this, but as you can see, there are dates and one single column of data. There is no dependent variable, there is only an independent variable (Loans). Is there a simple way to predict what the next number could be, or maybe a range of two numbers, based on a know sequence of numbers? Also, is there a way to get the probability of the outcome, like 90% or 95% confident?

Here is my data.

Account                            Loans
2019 Aug                           393.3
2020 Feb                           383.2
2020 Mar                           455.4
2020 Apr                           542.0
2020 May                           510.0
2020 Jun                           483.5
2020 Jul                           465.5
2020 Aug                           448.2
Aug 12                             451.1
Aug 19                             447.5
Aug 26                             442.3
Sep 02                             444.7

Ultimately I would like to see something like: 443 to 445 with 95% confidence. Is that possible?


Solution

  • Solution

    Option 1 - rolling average

    Take the average of the last n values (a). Subtract a from last number (l) as (s). The end result should be l-s or l+s.

    Example

    
    def predict(arr, n):
      l = arr[-1]
      a = sum(arr[:n]) / n
      s = abs(a - l)
      lower_bounds = l - s
      upper_bounds = l + s
      
      return (upper_bounds, lower_bounds)
      
    

    Option 2 - exponential smoothing

    Consider using exponential smoothing from stats models

    Example

    from statsmodels.tsa.api import SimpleExpSmoothing
    
    def predict(arr, sl)
        return SimpleExpSmoothing(arr).fit(smoothing_level=sl).fitted_values
    

    References

    statsmodels (Simple Exponential Smoothing): https://www.statsmodels.org/stable/examples/notebooks/generated/exponential_smoothing.html

    Python Simple Exponential Smoothing

    NumPy version of "Exponential weighted moving average", equivalent to pandas.ewm().mean()

    calculate exponential moving average in python