2

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?

4
  • What criteria are you using that assumes 443 to 445 with 95% confidence?
    – pygeek
    Commented Sep 12, 2020 at 17:44
  • 1
    You can do a simple rolling average or exponential smoothening of the previous values to roughly predict the next value.
    – Raghul Raj
    Commented Sep 12, 2020 at 17:46
  • I updated my answer. Let me know if either solution is what you're looking for. Generally, what you're looking for is likely available in statsmodels.
    – pygeek
    Commented Sep 12, 2020 at 18:25
  • Rolling average totally makes sense. I was over-thinking the problem. I have a related question. I found a nice solution here: kite.com/python/answers/… I'm trying to feed my 'Loans' variable into the 'Numbers' variable. I tried to slice the data frame like this: numbers = df.loc[:, ['Commercial and industrial loans']] Now, I am getting this error: TypeError: unsupported operand type(s) for +: 'int' and 'str'
    – ASH
    Commented Sep 12, 2020 at 20:14

2 Answers 2

5

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

5
  • I think this is definitely an option. How can I feed in my 'Loans' variable as an array?
    – ASH
    Commented Sep 12, 2020 at 20:22
  • @ASH is it a csv or text file? Just need to parse it into an array via split either way.
    – pygeek
    Commented Sep 12, 2020 at 20:28
  • It's just a small data frame with an index and one column. I did this: arr = new_df.iloc[:,0:].values I got this: array([[393.3], [383.2], [455.4], [542. ], [510. ], [483.5], [465.5], [448.2], [451.1], [447.5], [442.3], [444.7]]) I copied/pasted your function into my IDE and then called it like this: print(predict, 3) This is the result that I get: <function predict at 0x0000026A9D6BF9D8> 3
    – ASH
    Commented Sep 12, 2020 at 20:33
  • 1
    @ASH flatten the array using tier tools like so: stackoverflow.com/a/953097/806876
    – pygeek
    Commented Sep 12, 2020 at 20:39
  • @pygeek is n = amount of items in the array? Commented Jan 30 at 12:11
1

First, you need to do some preprocessing to convert your independent variable to some number scale. Maybe the number of days form 1st Aug 2019. Then you can do the regression.

2
  • I thought about that, but I feel like the dates are chosen almost arbitrarily. Or, whatever, they definitely jump around a lot. It goes from Aug 2019 to Feb 20202 to Mar 2020. That doesn't seem like 1, 2, 3 to me.
    – ASH
    Commented Sep 12, 2020 at 20:18
  • You will not be able to get a meaningful correct prediction if you disregard the independent variable, maybe you can use something like average loan or total loan for each month.
    – gihan
    Commented Sep 12, 2020 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.