Using an ensemble WLS + ARMA

This method shows an ensemble method that combines the use of Weighted Linear Regression (WLS) to fit the main function and ARMA to fit the residuals.

Warning

Not working yet!

"\n# Import class.\nimport numpy as np\nimport pandas as pd\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nimport statsmodels.api as sm\nimport statsmodels.robust.norms as norms\n\nfrom statsmodels.tsa.arima_model import ARIMA\n\n\n# import weights.\nfrom pyamr.datasets.load import make_timeseries\nfrom pyamr.metrics.weights import SigmoidA\nfrom pyamr.core.regression.wls import WLSWrapper\nfrom pyamr.core.regression.wlsarma import WLSARMAWrapper\n\n# Matplotlib options\nmpl.rc('legend', fontsize=6)\nmpl.rc('xtick', labelsize=6)\nmpl.rc('ytick', labelsize=6)\n\n# Set pandas configuration.\npd.set_option('display.max_colwidth', 14)\npd.set_option('display.width', 150)\npd.set_option('display.precision', 4)\n\n# Create timeseries data\nx, y, f = make_timeseries()\n\n# Create method to compute weights from frequencies\nW = SigmoidA(r=200, g=0.5, offset=0.0, scale=1.0)\n\n# Note that the function fit will call M.weights(weights) inside and will\n# store the M converter in the instance. Therefore, the code execute is\n# equivalent to <weights=M.weights(f)> with the only difference being that\n# the weight converter is not saved.\nwls = WLSWrapper(estimator=sm.WLS).fit(     exog=x, endog=y, trend='c', weights=f,\n    W=W, missing='raise')\n\n# Create WLS + ARIMA model.\nwlsarima = WLSARMAWrapper().fit(endog=y, exog=x, weights=f,\n    wls_kwargs={'W': W, 'trend': 'c', 'missing': 'raise'},\n     arima_kwargs={'exog': None, 'max_ar': 1,\n                   'max_ma': 1, 'max_d': 0, 'ic': 'bic',\n                   'order': (1,0,0)})\n\npred1 = wlsarima.get_prediction(start=None, end=140)\npred2 = wls.get_prediction(start=None, end=140)\n\nendog = getattr(wlsarima._wls, 'endog')\nexog = getattr(wlsarima._wls, 'exog')[:, 1]\n\n# Create figures.\nfig, axes = plt.subplots(1, 2, figsize=(10, 4), sharey=True, sharex=True)\n# Plot observations.\naxes[0].plot(exog, endog, color='#A6CEE3', alpha=0.5, marker='o',\n             markeredgecolor='k', markeredgewidth=0.5,\n             markersize=5, linewidth=0.75, label='Observed')\naxes[0].plot(pred1[0, :], pred1[1, :], color='#39ac73',\n             alpha=0.95, marker='o', markersize=2, linewidth=1.0,\n             markeredgewidth=0.2, markeredgecolor='k',\n             label='WLSARMA')\naxes[1].plot(pred2[0, :], pred2[1, :], color='r',\n             alpha=0.95, marker='o', markersize=2, linewidth=1.0,\n             markeredgewidth=0.2, markeredgecolor='k',\n             label='WLS')\n\nplt.legend()\nplt.show()\n\nimport sys\n\nsys.exit()\n\n# Create WLS + SARIMAX model.\nwlssarimax = WLSARMAWrapper().fit(endog=y, exog=x, weights=f,\n                                  wls_kwargs={'W': W, 'trend': 'c', 'missing': 'raise'},\n                                  sarimax_kwargs={'exog': None, 'ic': 'bic', 'max_ar': 1, 'max_ma': 1, 'max_d': 0,\n                                                  'max_P': 0, 'max_D': 0, 'max_Q': 0, 'list_s': [12]})\n\n# Create best SARIMAX model\nmodels, sarimax = SARIMAXWrapper().fit(endog=y, ic='bic',\n                                       max_ar=1, max_ma=1, max_d=1,\n                                       max_P=0, max_D=0, max_Q=0,\n                                       list_s=[0], return_fits=True)\n\n# Print series.\nprint(wlsarima.as_series())\n\n# Print summaries.\nprint(wlsarima.as_summary())\nprint(wlssarimax.as_summary())\nprint(wlsarima._arma.as_summary())\nprint(wlssarimax._arma.as_summary())\n\n# -----------------\n# Save & Load\n# -----------------\n# File location\nfname_wlsarima = '../../examples/saved/wlsarima-sample.pickle'\nfname_wlssarimax = '../../examples/saved/wlssarimax-sample.pickle'\n\n# Save\nwlsarima.save(fname=fname_wlsarima)\n# wlssarimax.save(fname=fname_wlssarimax) (zstatespace can't be pickled)\n\n# Load\nwlsarima = WLSARMAWrapper().load(fname=fname_wlsarima)\n# wlssarimax = WLSARMAWrapper().load(fname=fname_wlssarimax)\n\n# -----------------\n#  Predictions\n# -----------------\n# Variables.\nstart, end = None, None\n\n# Compute predictions.\npreds_wls = wlsarima.get_prediction(start=start, end=end, ptype='wls')\npreds_arima = wlsarima.get_prediction(start=start, end=end, ptype='arma')\npreds_sarimax = wlssarimax.get_prediction(start=start, end=end, ptype='arma')\npreds_wlsarima = wlsarima.get_prediction(start=start, end=end)\npreds_wlssarimax = wlssarimax.get_prediction(start=start, end=end)\npreds_sarimax = sarimax.get_prediction(start=start, end=end)\n\n# Create figures.\nfig, axes = plt.subplots(1, 2, figsize=(10, 4))\n\n# Subplot 0\n# ---------\n# Plot observed values\nendog = getattr(wlsarima._wls, 'endog')\nexog = getattr(wlsarima._wls, 'exog')[:, 1]\naxes[0].plot(exog, endog, color='#A6CEE3', alpha=0.5, marker='o',\n             markeredgecolor='k', markeredgewidth=0.5,\n             markersize=5, linewidth=0.75, label='Observed')\n\n# Plot forecasted values (WLS-ARIMA).\naxes[0].plot(preds_wlsarima[0, :], preds_wlsarima[1, :], color='#39ac73',\n             alpha=0.95, marker='o', markersize=2, linewidth=1.0,\n             markeredgewidth=0.2, markeredgecolor='k',\n             label=wlsarima._identifier(short=True))\n\n# Plot confidence intervals (WLS-ARIMA)\naxes[0].fill_between(preds_wlsarima[0, 3:],\n                     preds_wlsarima[2, 3:],\n                     preds_wlsarima[3, 3:],\n                     color='#39ac73', alpha=0.1)\n\n# Plot forecasted values (WLS-SARIMAX).\naxes[0].plot(preds_wlssarimax[0, :], preds_wlssarimax[1, :], color='b',\n             alpha=0.95, marker='o', markersize=2, linewidth=1.0,\n             markeredgewidth=0.2, markeredgecolor='k',\n             label=wlssarimax._identifier(short=True))\n\n# Plot confidence intervals (WLS-SARIMAX)\naxes[0].fill_between(preds_wlssarimax[0, 3:],\n                     preds_wlssarimax[2, 3:],\n                     preds_wlssarimax[3, 3:],\n                     color='b', alpha=0.1)\n\n# Plot forecasted values and confidence intervals (SARIMAX).\naxes[0].plot(preds_sarimax[0, :], preds_sarimax[1, :], color='#FF0000',\n             alpha=0.95, marker='o', markersize=2, linewidth=1.0,\n             markeredgewidth=0.2, markeredgecolor='k',\n             label=sarimax._identifier())\n\n# Plot confidence intervals\naxes[0].fill_between(preds_sarimax[0, 3:],\n                     preds_sarimax[2, 3:],\n                     preds_sarimax[3, 3:],\n                     color='#FF0000', alpha=0.1)\n\n# Subplot 1\n# ---------\n# Plot observed values.\naxes[1].plot(x, y, color='#A6CEE3', alpha=0.5, marker='o',\n             markeredgecolor='k', markeredgewidth=0.5,\n             markersize=5, linewidth=0.75, label='Observed')\n\n# Plot wls separately.\naxes[1].plot(preds_wls[0, :], preds_wls[1, :], color='#39ac73',\n             alpha=0.95, marker='o', markersize=2, linewidth=1.0,\n             markeredgewidth=0.2, markeredgecolor='k',\n             label=wlsarima._wls._identifier(short=True))\n\naxes[1].fill_between(preds_wls[0, :],\n                     preds_wls[2, :],\n                     preds_wls[3, :],\n                     color='#39ac73', alpha=0.1)\n\n# Plot arma separately.\naxes[1].plot(preds_arima[0, :], preds_arima[1, :], color='#FF0000',\n             alpha=0.95, marker='o', markersize=2, linewidth=1.0,\n             markeredgewidth=0.2, markeredgecolor='k',\n             label=wlsarima._arma._identifier())\n\naxes[1].fill_between(preds_arima[0, :],\n                     preds_arima[2, :],\n                     preds_arima[3, :],\n                     color='#FF0000', alpha=0.1)\n\n# Grid\naxes[0].grid(linestyle='--', linewidth=0.35, alpha=0.5)\naxes[1].grid(linestyle='--', linewidth=0.35, alpha=0.5)\n\n# Legend\naxes[0].legend()\naxes[1].legend()\n\n# Show\nplt.show()\n"

 13 """
 14 # Import class.
 15 import numpy as np
 16 import pandas as pd
 17 import matplotlib as mpl
 18 import matplotlib.pyplot as plt
 19 import statsmodels.api as sm
 20 import statsmodels.robust.norms as norms
 21
 22 from statsmodels.tsa.arima_model import ARIMA
 23
 24
 25 # import weights.
 26 from pyamr.datasets.load import make_timeseries
 27 from pyamr.metrics.weights import SigmoidA
 28 from pyamr.core.regression.wls import WLSWrapper
 29 from pyamr.core.regression.wlsarma import WLSARMAWrapper
 30
 31 # Matplotlib options
 32 mpl.rc('legend', fontsize=6)
 33 mpl.rc('xtick', labelsize=6)
 34 mpl.rc('ytick', labelsize=6)
 35
 36 # Set pandas configuration.
 37 pd.set_option('display.max_colwidth', 14)
 38 pd.set_option('display.width', 150)
 39 pd.set_option('display.precision', 4)
 40
 41 # Create timeseries data
 42 x, y, f = make_timeseries()
 43
 44 # Create method to compute weights from frequencies
 45 W = SigmoidA(r=200, g=0.5, offset=0.0, scale=1.0)
 46
 47 # Note that the function fit will call M.weights(weights) inside and will
 48 # store the M converter in the instance. Therefore, the code execute is
 49 # equivalent to <weights=M.weights(f)> with the only difference being that
 50 # the weight converter is not saved.
 51 wls = WLSWrapper(estimator=sm.WLS).fit( \
 52     exog=x, endog=y, trend='c', weights=f,
 53     W=W, missing='raise')
 54
 55 # Create WLS + ARIMA model.
 56 wlsarima = WLSARMAWrapper().fit(endog=y, exog=x, weights=f,
 57     wls_kwargs={'W': W, 'trend': 'c', 'missing': 'raise'},
 58      arima_kwargs={'exog': None, 'max_ar': 1,
 59                    'max_ma': 1, 'max_d': 0, 'ic': 'bic',
 60                    'order': (1,0,0)})
 61
 62 pred1 = wlsarima.get_prediction(start=None, end=140)
 63 pred2 = wls.get_prediction(start=None, end=140)
 64
 65 endog = getattr(wlsarima._wls, 'endog')
 66 exog = getattr(wlsarima._wls, 'exog')[:, 1]
 67
 68 # Create figures.
 69 fig, axes = plt.subplots(1, 2, figsize=(10, 4), sharey=True, sharex=True)
 70 # Plot observations.
 71 axes[0].plot(exog, endog, color='#A6CEE3', alpha=0.5, marker='o',
 72              markeredgecolor='k', markeredgewidth=0.5,
 73              markersize=5, linewidth=0.75, label='Observed')
 74 axes[0].plot(pred1[0, :], pred1[1, :], color='#39ac73',
 75              alpha=0.95, marker='o', markersize=2, linewidth=1.0,
 76              markeredgewidth=0.2, markeredgecolor='k',
 77              label='WLSARMA')
 78 axes[1].plot(pred2[0, :], pred2[1, :], color='r',
 79              alpha=0.95, marker='o', markersize=2, linewidth=1.0,
 80              markeredgewidth=0.2, markeredgecolor='k',
 81              label='WLS')
 82
 83 plt.legend()
 84 plt.show()
 85
 86 import sys
 87
 88 sys.exit()
 89
 90 # Create WLS + SARIMAX model.
 91 wlssarimax = WLSARMAWrapper().fit(endog=y, exog=x, weights=f,
 92                                   wls_kwargs={'W': W, 'trend': 'c', 'missing': 'raise'},
 93                                   sarimax_kwargs={'exog': None, 'ic': 'bic', 'max_ar': 1, 'max_ma': 1, 'max_d': 0,
 94                                                   'max_P': 0, 'max_D': 0, 'max_Q': 0, 'list_s': [12]})
 95
 96 # Create best SARIMAX model
 97 models, sarimax = SARIMAXWrapper().fit(endog=y, ic='bic',
 98                                        max_ar=1, max_ma=1, max_d=1,
 99                                        max_P=0, max_D=0, max_Q=0,
100                                        list_s=[0], return_fits=True)
101
102 # Print series.
103 print(wlsarima.as_series())
104
105 # Print summaries.
106 print(wlsarima.as_summary())
107 print(wlssarimax.as_summary())
108 print(wlsarima._arma.as_summary())
109 print(wlssarimax._arma.as_summary())
110
111 # -----------------
112 # Save & Load
113 # -----------------
114 # File location
115 fname_wlsarima = '../../examples/saved/wlsarima-sample.pickle'
116 fname_wlssarimax = '../../examples/saved/wlssarimax-sample.pickle'
117
118 # Save
119 wlsarima.save(fname=fname_wlsarima)
120 # wlssarimax.save(fname=fname_wlssarimax) (zstatespace can't be pickled)
121
122 # Load
123 wlsarima = WLSARMAWrapper().load(fname=fname_wlsarima)
124 # wlssarimax = WLSARMAWrapper().load(fname=fname_wlssarimax)
125
126 # -----------------
127 #  Predictions
128 # -----------------
129 # Variables.
130 start, end = None, None
131
132 # Compute predictions.
133 preds_wls = wlsarima.get_prediction(start=start, end=end, ptype='wls')
134 preds_arima = wlsarima.get_prediction(start=start, end=end, ptype='arma')
135 preds_sarimax = wlssarimax.get_prediction(start=start, end=end, ptype='arma')
136 preds_wlsarima = wlsarima.get_prediction(start=start, end=end)
137 preds_wlssarimax = wlssarimax.get_prediction(start=start, end=end)
138 preds_sarimax = sarimax.get_prediction(start=start, end=end)
139
140 # Create figures.
141 fig, axes = plt.subplots(1, 2, figsize=(10, 4))
142
143 # Subplot 0
144 # ---------
145 # Plot observed values
146 endog = getattr(wlsarima._wls, 'endog')
147 exog = getattr(wlsarima._wls, 'exog')[:, 1]
148 axes[0].plot(exog, endog, color='#A6CEE3', alpha=0.5, marker='o',
149              markeredgecolor='k', markeredgewidth=0.5,
150              markersize=5, linewidth=0.75, label='Observed')
151
152 # Plot forecasted values (WLS-ARIMA).
153 axes[0].plot(preds_wlsarima[0, :], preds_wlsarima[1, :], color='#39ac73',
154              alpha=0.95, marker='o', markersize=2, linewidth=1.0,
155              markeredgewidth=0.2, markeredgecolor='k',
156              label=wlsarima._identifier(short=True))
157
158 # Plot confidence intervals (WLS-ARIMA)
159 axes[0].fill_between(preds_wlsarima[0, 3:],
160                      preds_wlsarima[2, 3:],
161                      preds_wlsarima[3, 3:],
162                      color='#39ac73', alpha=0.1)
163
164 # Plot forecasted values (WLS-SARIMAX).
165 axes[0].plot(preds_wlssarimax[0, :], preds_wlssarimax[1, :], color='b',
166              alpha=0.95, marker='o', markersize=2, linewidth=1.0,
167              markeredgewidth=0.2, markeredgecolor='k',
168              label=wlssarimax._identifier(short=True))
169
170 # Plot confidence intervals (WLS-SARIMAX)
171 axes[0].fill_between(preds_wlssarimax[0, 3:],
172                      preds_wlssarimax[2, 3:],
173                      preds_wlssarimax[3, 3:],
174                      color='b', alpha=0.1)
175
176 # Plot forecasted values and confidence intervals (SARIMAX).
177 axes[0].plot(preds_sarimax[0, :], preds_sarimax[1, :], color='#FF0000',
178              alpha=0.95, marker='o', markersize=2, linewidth=1.0,
179              markeredgewidth=0.2, markeredgecolor='k',
180              label=sarimax._identifier())
181
182 # Plot confidence intervals
183 axes[0].fill_between(preds_sarimax[0, 3:],
184                      preds_sarimax[2, 3:],
185                      preds_sarimax[3, 3:],
186                      color='#FF0000', alpha=0.1)
187
188 # Subplot 1
189 # ---------
190 # Plot observed values.
191 axes[1].plot(x, y, color='#A6CEE3', alpha=0.5, marker='o',
192              markeredgecolor='k', markeredgewidth=0.5,
193              markersize=5, linewidth=0.75, label='Observed')
194
195 # Plot wls separately.
196 axes[1].plot(preds_wls[0, :], preds_wls[1, :], color='#39ac73',
197              alpha=0.95, marker='o', markersize=2, linewidth=1.0,
198              markeredgewidth=0.2, markeredgecolor='k',
199              label=wlsarima._wls._identifier(short=True))
200
201 axes[1].fill_between(preds_wls[0, :],
202                      preds_wls[2, :],
203                      preds_wls[3, :],
204                      color='#39ac73', alpha=0.1)
205
206 # Plot arma separately.
207 axes[1].plot(preds_arima[0, :], preds_arima[1, :], color='#FF0000',
208              alpha=0.95, marker='o', markersize=2, linewidth=1.0,
209              markeredgewidth=0.2, markeredgecolor='k',
210              label=wlsarima._arma._identifier())
211
212 axes[1].fill_between(preds_arima[0, :],
213                      preds_arima[2, :],
214                      preds_arima[3, :],
215                      color='#FF0000', alpha=0.1)
216
217 # Grid
218 axes[0].grid(linestyle='--', linewidth=0.35, alpha=0.5)
219 axes[1].grid(linestyle='--', linewidth=0.35, alpha=0.5)
220
221 # Legend
222 axes[0].legend()
223 axes[1].legend()
224
225 # Show
226 plt.show()
227 """

Total running time of the script: ( 0 minutes 0.001 seconds)

Gallery generated by Sphinx-Gallery