In [1]:
import pandas as pd
import numpy as np

import pyaf.ForecastEngine as autof
import pyaf.Bench.TS_datasets as tsds
import datetime

%matplotlib inline

In [2]:
trainfile = "../data/sof_example.csv";
df = pd.read_csv(trainfile, sep=r',', engine='python', skiprows=0);
df['Date'] = df['Date'].apply(lambda x : datetime.datetime.strptime(x, "%m/%d/%Y"))

print(df.head());

lDateVar = 'Date'
lSignalVar = 'Used'


        Date  Used
0 2011-11-01   587
1 2011-11-02   578
2 2011-11-03   600
3 2011-11-04   599
4 2011-11-05   678

In [3]:
df.head()


Out[3]:
Date Used
0 2011-11-01 587
1 2011-11-02 578
2 2011-11-03 600
3 2011-11-04 599
4 2011-11-05 678

In [4]:
df.describe()


Out[4]:
Used
count 61.000000
mean 627.885246
std 51.750394
min 550.000000
25% 600.000000
50% 610.000000
75% 650.000000
max 800.000000

In [ ]:


In [5]:
lEngine = autof.cForecastEngine()
lEngine.mOptions.mEnableSeasonals = True;
lEngine

#lEngine.mOptions.enable_slow_mode()
#lEngine.mOptions.mCycle_Criterion = "L2";
#lEngine.mOptions.mCycle_Criterion_Threshold = 10000.2;


Out[5]:
<pyaf.ForecastEngine.cForecastEngine at 0x7f429ef48ba8>

In [6]:
H=10
lEngine.train(df , lDateVar , lSignalVar, H);

In [7]:
lEngine.getModelInfo()

In [ ]:


In [8]:
lEngine.mSignalDecomposition.mTrPerfDetails


Out[8]:
Transformation Model Complexity FitCount FitL2 FitMAPE ForecastCount ForecastL2 ForecastMAPE TestCount TestL2 TestMAPE
0 _Used _Used_ConstantTrend_residue_zeroCycle_residue_... 15 40 29.116208 0.0360 11 15.415689 0.0219 10 89.821088 0.0880
1 _Used _Used_LinearTrend_residue_zeroCycle_residue_AR... 31 40 28.983499 0.0356 11 15.659559 0.0225 10 87.814051 0.0859
2 _Used _Used_PolyTrend_residue_zeroCycle_residue_AR(15) 31 40 28.778715 0.0355 11 20.154482 0.0247 10 114.631185 0.1178
3 _Used _Used_Lag1Trend_residue_zeroCycle_residue_AR(15) 47 40 34.431435 0.0409 11 21.252883 0.0254 10 71.118418 0.0732
4 _Used _Used_ConstantTrend_residue_bestCycle_byL2_res... 23 40 28.527889 0.0333 11 22.760217 0.0320 10 83.419970 0.0910
5 _Used _Used_LinearTrend_residue_bestCycle_byL2_resid... 39 40 28.472139 0.0331 11 23.062294 0.0328 10 81.276072 0.0894
6 _Used _Used_PolyTrend_residue_bestCycle_byL2_residue... 39 40 28.307328 0.0333 11 25.707915 0.0345 10 106.057287 0.1121
7 _Used _Used_Lag1Trend_residue_bestCycle_byL2_residue... 55 40 31.831669 0.0379 11 34.157523 0.0362 10 68.680397 0.0792
8 CumSum_Used CumSum_Used_LinearTrend_residue_zeroCycle_resi... 63 40 50.205301 0.0593 11 35.005995 0.0372 10 72.795394 0.0846
9 _Used _Used_LinearTrend_residue_zeroCycle_residue_NoAR 16 40 37.875781 0.0481 11 32.316264 0.0393 10 96.510608 0.1121
10 _Used _Used_Lag1Trend_residue_zeroCycle_residue_NoAR 32 40 47.209374 0.0560 11 37.165478 0.0406 10 74.621713 0.0801
11 CumSum_Used CumSum_Used_Lag1Trend_residue_zeroCycle_residu... 64 40 102.853294 0.0806 11 37.165478 0.0406 10 74.621713 0.0801
12 Diff_Used Diff_Used_Lag1Trend_residue_zeroCycle_residue_... 64 40 47.209374 0.0560 11 37.165478 0.0406 10 74.621713 0.0801
13 RelDiff_Used RelDiff_Used_Lag1Trend_residue_zeroCycle_resid... 64 40 47.209374 0.0560 11 37.165478 0.0406 10 74.621713 0.0801
14 _Used _Used_ConstantTrend_residue_zeroCycle_residue_... 0 40 37.928148 0.0485 11 33.468353 0.0408 10 99.593125 0.1133
15 Diff_Used Diff_Used_ConstantTrend_residue_zeroCycle_resi... 32 40 42.167831 0.0468 11 34.596953 0.0408 10 96.920433 0.1108
16 _Used _Used_ConstantTrend_residue_bestCycle_byL2_res... 8 40 37.107403 0.0465 11 36.769147 0.0434 10 97.717530 0.1141
17 Diff_Used Diff_Used_ConstantTrend_residue_bestCycle_byL2... 40 40 43.894387 0.0511 11 40.403311 0.0439 10 97.948974 0.1134
18 _Used _Used_LinearTrend_residue_bestCycle_byL2_resid... 24 40 37.058568 0.0462 11 35.859048 0.0442 10 94.546065 0.1113
19 _Used _Used_PolyTrend_residue_zeroCycle_residue_NoAR 16 40 37.630081 0.0477 11 37.255878 0.0454 10 116.027741 0.1258
20 _Used _Used_PolyTrend_residue_bestCycle_byL2_residue... 24 40 36.800068 0.0457 11 40.120099 0.0455 10 114.330288 0.1296
21 CumSum_Used CumSum_Used_ConstantTrend_residue_zeroCycle_re... 47 40 118.970470 0.0965 11 44.777346 0.0458 10 89.599974 0.1012
22 CumSum_Used CumSum_Used_Lag1Trend_residue_zeroCycle_residu... 79 40 120.940081 0.1034 11 42.364416 0.0464 10 92.546470 0.1084
23 _Used _Used_Lag1Trend_residue_bestCycle_byL2_residue... 40 40 45.762259 0.0566 11 43.416227 0.0470 10 72.859389 0.0832
24 RelDiff_Used RelDiff_Used_Lag1Trend_residue_bestCycle_byL2_... 72 40 48.671529 0.0611 11 43.653019 0.0482 10 72.417358 0.0830
25 CumSum_Used CumSum_Used_PolyTrend_residue_zeroCycle_residu... 63 40 76.489088 0.0834 11 49.826364 0.0504 10 96.461466 0.1130
26 Diff_Used Diff_Used_LinearTrend_residue_zeroCycle_residu... 48 40 37.982534 0.0442 11 45.507880 0.0565 10 137.288723 0.1542
27 Diff_Used Diff_Used_Lag1Trend_residue_bestCycle_byL2_res... 72 40 47.055121 0.0591 11 46.841276 0.0566 10 71.530173 0.0827
28 Diff_Used Diff_Used_Lag1Trend_residue_zeroCycle_residue_... 79 40 43.647119 0.0564 11 44.282761 0.0572 10 100.215203 0.1096
29 Diff_Used Diff_Used_PolyTrend_residue_zeroCycle_residue_... 48 40 37.912148 0.0441 11 46.217028 0.0577 10 139.519829 0.1573
... ... ... ... ... ... ... ... ... ... ... ... ...
34 Diff_Used Diff_Used_ConstantTrend_residue_bestCycle_byL2... 55 40 41.872539 0.0519 11 53.992276 0.0739 10 163.189625 0.1806
35 CumSum_Used CumSum_Used_Lag1Trend_residue_bestCycle_byL2_r... 72 40 134.143067 0.1251 11 56.250909 0.0752 10 99.846507 0.1134
36 CumSum_Used CumSum_Used_PolyTrend_residue_bestCycle_byL2_r... 71 40 117.800847 0.1263 11 67.137841 0.0783 10 126.671718 0.1462
37 Diff_Used Diff_Used_ConstantTrend_residue_zeroCycle_resi... 47 40 55.830144 0.0689 11 61.404151 0.0835 10 148.476693 0.1550
38 CumSum_Used CumSum_Used_Lag1Trend_residue_bestCycle_byL2_r... 87 40 117.899272 0.1217 11 63.493840 0.0943 10 111.651648 0.1338
39 Diff_Used Diff_Used_Lag1Trend_residue_bestCycle_byL2_res... 87 40 91.632554 0.1390 11 64.929533 0.0958 10 72.610239 0.0888
40 Diff_Used Diff_Used_LinearTrend_residue_bestCycle_byL2_r... 71 40 40.076587 0.0530 11 73.022132 0.0976 10 215.206553 0.2500
41 Diff_Used Diff_Used_PolyTrend_residue_bestCycle_byL2_res... 71 40 40.214487 0.0531 11 74.449924 0.0994 10 218.493880 0.2553
42 Diff_Used Diff_Used_LinearTrend_residue_zeroCycle_residu... 63 40 46.356755 0.0576 11 85.471696 0.1167 10 208.277117 0.2426
43 Diff_Used Diff_Used_PolyTrend_residue_zeroCycle_residue_... 63 40 45.884787 0.0572 11 87.002297 0.1189 10 211.762735 0.2482
44 RelDiff_Used RelDiff_Used_PolyTrend_residue_bestCycle_byL2_... 71 40 81.127934 0.1119 11 93.437754 0.1402 10 102.077622 0.1422
45 RelDiff_Used RelDiff_Used_LinearTrend_residue_bestCycle_byL... 71 40 79.291585 0.1091 11 96.949159 0.1468 10 99.801701 0.1379
46 RelDiff_Used RelDiff_Used_PolyTrend_residue_zeroCycle_resid... 63 40 88.674336 0.1230 11 100.805901 0.1557 10 102.580681 0.1408
47 RelDiff_Used RelDiff_Used_LinearTrend_residue_zeroCycle_res... 63 40 86.687023 0.1199 11 104.657762 0.1628 10 101.399129 0.1369
48 RelDiff_Used RelDiff_Used_PolyTrend_residue_bestCycle_byL2_... 56 40 93.716509 0.1331 11 118.877762 0.1824 10 93.707959 0.1236
49 RelDiff_Used RelDiff_Used_LinearTrend_residue_bestCycle_byL... 56 40 92.096714 0.1306 11 122.442838 0.1886 10 96.461286 0.1268
50 RelDiff_Used RelDiff_Used_PolyTrend_residue_zeroCycle_resid... 48 40 106.030607 0.1507 11 136.476601 0.2142 10 105.773124 0.1420
51 RelDiff_Used RelDiff_Used_ConstantTrend_residue_bestCycle_b... 55 40 65.648994 0.0850 11 139.563559 0.2186 10 144.444641 0.1958
52 RelDiff_Used RelDiff_Used_LinearTrend_residue_zeroCycle_res... 48 40 104.307669 0.1480 11 140.284271 0.2207 10 111.025017 0.1456
53 CumSum_Used CumSum_Used_LinearTrend_residue_zeroCycle_resi... 48 40 437.391966 0.3223 11 149.577669 0.2310 10 208.767240 0.2750
54 RelDiff_Used RelDiff_Used_ConstantTrend_residue_zeroCycle_r... 47 40 71.058086 0.0897 11 150.163019 0.2381 10 148.858632 0.2031
55 RelDiff_Used RelDiff_Used_Lag1Trend_residue_bestCycle_byL2_... 87 40 129.170487 0.1911 11 155.267999 0.2434 10 114.437433 0.1531
56 RelDiff_Used RelDiff_Used_Lag1Trend_residue_zeroCycle_resid... 79 40 105.971204 0.1530 11 157.416588 0.2487 10 135.199659 0.1853
57 RelDiff_Used RelDiff_Used_ConstantTrend_residue_bestCycle_b... 40 40 76.099557 0.1001 11 160.125381 0.2516 10 149.345238 0.2036
58 RelDiff_Used RelDiff_Used_ConstantTrend_residue_zeroCycle_r... 32 40 86.512855 0.1145 11 170.048322 0.2698 10 149.345238 0.2036
59 CumSum_Used CumSum_Used_LinearTrend_residue_bestCycle_byL2... 56 40 469.745403 0.3261 11 343.697303 0.3316 10 366.838961 0.2931
60 CumSum_Used CumSum_Used_PolyTrend_residue_bestCycle_byL2_r... 56 40 570.201617 0.4659 11 482.995302 0.7308 10 772.487418 1.1555
61 CumSum_Used CumSum_Used_PolyTrend_residue_zeroCycle_residu... 48 40 578.097265 0.4378 11 490.409284 0.7699 10 762.912904 1.1450
62 CumSum_Used CumSum_Used_ConstantTrend_residue_zeroCycle_re... 32 40 1996.273272 1.4870 11 628.958157 1.0000 10 679.223086 1.0000
63 CumSum_Used CumSum_Used_ConstantTrend_residue_bestCycle_by... 40 40 2140.158057 1.3631 11 1600.563702 1.4212 10 1438.039751 0.9357

64 rows × 12 columns


In [9]:
lEngine.standrdPlots()


/usr/lib/python3/dist-packages/matplotlib/__init__.py:1403: UserWarning:  This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

  warnings.warn(_use_error_msg)