项目作者: raphaelsty

项目描述 :
Autoregressive Bayesian linear model
高级语言: Python
项目地址: git://github.com/raphaelsty/abayes.git
创建时间: 2020-09-08T17:07:06Z
项目社区:https://github.com/raphaelsty/abayes

开源协议:MIT License

下载



Online autoregressive bayesian linear regression




This minimalist tool is dedicated to the bayesian linear regression implemented by Max Halford in his blog post Bayesian linear regression for practitioners. I slightly modified the code to obtain an autoregressive version of the original model.

For the time being, the model will systematically assume that the residues follow a normal distribution. It will be necessary to wait for the next updates of the library to include new distributions.

Bayesian linear regression has many advantages. It allows to measure the uncertainty of the model and to build confidence intervals. The simplicity of this model and its ability to answer “I don’t know” makes it practical and adapted to many concrete problems.

Its online autoregressive counterpart is a nice tool for the toolbox of programmers, hackers and practitioners.

Installation:

  1. pip install git+https://github.com/raphaelsty/abayes

Example:

Let’s try to predict my ice cream consumption. I have created a dummy dataset where I record my ice cream consumption for each month of the year and for 3 years.

  1. from abayes import dataset
  2. df = dataset.LoadIceCream()
  3. df.head()

drawing



We initialise the auto-regressive model with a periodicity of 24. We will use the years n-1 and n-2 to predict my ice consumption in year n.

  1. from abayes import linear
  2. model = linear.AbayesLr(
  3. p = 24,
  4. alpha = 0.3,
  5. beta = 1.,
  6. )
  7. model.learn(df['y'].values)
  8. forecast = model.forecast(12)
  9. lower_bound, upper_bound = model.forecast_interval(12, alpha = 0.90)
plot

python3 import matplotlib.pyplot as plt %config InlineBackend.figure_format = 'retina' range_train = range(len(df['y'])) range_forecast = range(len(df['y']), len(df['y']) + len(forecast)) fig, ax = plt.subplots(figsize=(15, 6)) ax.plot(range_train, df['y'], color='deepskyblue', label ='train') ax.plot(range_forecast, forecast, color='red', linestyle='--', label ='forecast') ax.fill_between( x = range_forecast, y1 = lower_bound, y2 = upper_bound, alpha = 0.1, color = 'red', label = 'confidence' ) plt.xticks( range(len(df['y']) + len(forecast)), df['time'].tolist() + [f"2020/{'%02d' % i}" for i in range(1, 13)], rotation='vertical' ) ax.set_title('Quantity of ice cream') ax.set_xlabel('Period') ax.set_ylabel('Quantity') ax.legend() plt.show()

The model can also learn by mini-batch.

  1. import numpy as np
  2. from abayes import linear
  3. model = linear.AbayesLr(
  4. p = 24,
  5. alpha = 0.3,
  6. beta = 1,
  7. )
  8. for time, y in enumerate(df['y'].values):
  9. # Online autoregressive model needs to store enough data to make a prediction (> period).
  10. if time > 24:
  11. lower_bound, upper_bound = model.forecast_interval(1, alpha = 0.9)
  12. y_pred = model.forecast(1)
  13. print('\n')
  14. print(f'time: {time}')
  15. print(f'\t y: {y}')
  16. print(f'\t Most likely value: {y_pred[0]:6f}')
  17. print(f'\t Confidence interval: [{lower_bound[0]:6f} ; {upper_bound[0]:6f}]')
  18. model.learn(np.array([y]))
  1. time: 25
  2. y: 46
  3. Most likely value: 16.066921
  4. Confidence interval: [-231.651593 ; 263.785435]
  5. time: 26
  6. y: 27
  7. Most likely value: 50.016886
  8. Confidence interval: [-179.828306 ; 279.862078]
  9. time: 27
  10. y: 30
  11. Most likely value: 16.442225
  12. Confidence interval: [-214.224545 ; 247.108994]
  13. time: 28
  14. y: 101
  15. Most likely value: 21.766659
  16. Confidence interval: [-205.891318 ; 249.424635]
  17. time: 29
  18. y: 135
  19. Most likely value: 106.731389
  20. Confidence interval: [-124.906692 ; 338.369470]
  21. time: 30
  22. y: 250
  23. Most likely value: 143.074596
  24. Confidence interval: [-75.066693 ; 361.215885]
  25. time: 31
  26. y: 210
  27. Most likely value: 231.908803
  28. Confidence interval: [38.905614 ; 424.911993]
  29. time: 32
  30. y: 127
  31. Most likely value: 165.226916
  32. Confidence interval: [-17.324602 ; 347.778435]
  33. time: 33
  34. y: 50
  35. Most likely value: 78.684325
  36. Confidence interval: [-75.679416 ; 233.048066]
  37. time: 34
  38. y: 14
  39. Most likely value: -14.522046
  40. Confidence interval: [-157.498855 ; 128.454763]
  41. time: 35
  42. y: 56
  43. Most likely value: 52.063575
  44. Confidence interval: [-50.776582 ; 154.903732]

License

This project is free and open-source software licensed under the MIT license.