Skip to content

Commit d45177d

Browse files
committed
Created using Colaboratory
1 parent d36b36e commit d45177d

File tree

1 file changed

+107
-36
lines changed

1 file changed

+107
-36
lines changed

examples/4a_averages_and_ses.ipynb

Lines changed: 107 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"name": "4a_averages_and_ses.ipynb",
77
"provenance": [],
88
"collapsed_sections": [],
9-
"authorship_tag": "ABX9TyP+mgtJZoqPKv7k/JkZPGz4",
9+
"authorship_tag": "ABX9TyN0MCV9CSEvVqalgvPpkNxv",
1010
"include_colab_link": true
1111
},
1212
"kernelspec": {
@@ -44,7 +44,7 @@
4444
"base_uri": "https://localhost:8080/"
4545
},
4646
"id": "qyrssun2kgSi",
47-
"outputId": "f07f1e2e-9055-42aa-c770-474996ab25a2"
47+
"outputId": "15bf8f9f-666a-4cd4-ff0f-131f9e75b5dd"
4848
},
4949
"source": [
5050
"import pandas as pd\n",
@@ -69,29 +69,29 @@
6969
"\n",
7070
"from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt"
7171
],
72-
"execution_count": 156,
72+
"execution_count": 162,
7373
"outputs": [
7474
{
7575
"output_type": "stream",
7676
"text": [
7777
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
7878
" Dload Upload Total Spent Left Speed\n",
79-
"100 477 100 477 0 0 3785 0 --:--:-- --:--:-- --:--:-- 3785\n",
79+
"100 477 100 477 0 0 3755 0 --:--:-- --:--:-- --:--:-- 3726\n",
8080
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
8181
" Dload Upload Total Spent Left Speed\n",
82-
"100 629 100 629 0 0 4952 0 --:--:-- --:--:-- --:--:-- 4992\n",
82+
"100 629 100 629 0 0 4250 0 --:--:-- --:--:-- --:--:-- 4278\n",
8383
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
8484
" Dload Upload Total Spent Left Speed\n",
85-
"100 3867 100 3867 0 0 37182 0 --:--:-- --:--:-- --:--:-- 37182\n",
85+
"100 3867 100 3867 0 0 37911 0 --:--:-- --:--:-- --:--:-- 37911\n",
8686
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
8787
" Dload Upload Total Spent Left Speed\n",
88-
"100 456 100 456 0 0 3931 0 --:--:-- --:--:-- --:--:-- 3931\n",
88+
"100 456 100 456 0 0 5010 0 --:--:-- --:--:-- --:--:-- 5010\n",
8989
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
9090
" Dload Upload Total Spent Left Speed\n",
91-
"100 1456 100 1456 0 0 13866 0 --:--:-- --:--:-- --:--:-- 13866\n",
91+
"100 1456 100 1456 0 0 18200 0 --:--:-- --:--:-- --:--:-- 18200\n",
9292
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
9393
" Dload Upload Total Spent Left Speed\n",
94-
"100 151 100 151 0 0 1659 0 --:--:-- --:--:-- --:--:-- 1659\n"
94+
"100 151 100 151 0 0 1556 0 --:--:-- --:--:-- --:--:-- 1556\n"
9595
],
9696
"name": "stdout"
9797
}
@@ -116,24 +116,24 @@
116116
"height": 541
117117
},
118118
"id": "mbtL6K0tIPOM",
119-
"outputId": "899b4e3a-aaab-48ec-e33f-9a24abe8d1e2"
119+
"outputId": "877583d9-a0b9-4f39-ed54-2b965a792991"
120120
},
121121
"source": [
122122
"can_openers.plot()"
123123
],
124-
"execution_count": 5,
124+
"execution_count": 163,
125125
"outputs": [
126126
{
127127
"output_type": "execute_result",
128128
"data": {
129129
"text/plain": [
130-
"<matplotlib.axes._subplots.AxesSubplot at 0x7f0bb96716a0>"
130+
"<matplotlib.axes._subplots.AxesSubplot at 0x7f0b9d3c1908>"
131131
]
132132
},
133133
"metadata": {
134134
"tags": []
135135
},
136-
"execution_count": 5
136+
"execution_count": 163
137137
},
138138
{
139139
"output_type": "display_data",
@@ -170,16 +170,16 @@
170170
"metadata": {
171171
"colab": {
172172
"base_uri": "https://localhost:8080/",
173-
"height": 228
173+
"height": 0
174174
},
175175
"id": "Wy5VyH48klYK",
176-
"outputId": "dc7cea7d-c76f-40bd-f6d2-c2984cfa812b"
176+
"outputId": "6c9ac18a-7131-4aa1-cf8b-703d20a9cf30"
177177
},
178178
"source": [
179179
"can_openers['mean_forecast'] = can_openers.expanding().mean().shift(1)\n",
180180
"can_openers.head()"
181181
],
182-
"execution_count": 6,
182+
"execution_count": 164,
183183
"outputs": [
184184
{
185185
"output_type": "execute_result",
@@ -255,7 +255,7 @@
255255
"metadata": {
256256
"tags": []
257257
},
258-
"execution_count": 6
258+
"execution_count": 164
259259
}
260260
]
261261
},
@@ -264,10 +264,10 @@
264264
"metadata": {
265265
"colab": {
266266
"base_uri": "https://localhost:8080/",
267-
"height": 541
267+
"height": 0
268268
},
269269
"id": "fsK3fqIIktfJ",
270-
"outputId": "aba0cb1d-d729-4fd1-8360-41b7dc506d92"
270+
"outputId": "23f58cb3-4d64-4f08-b6e2-d73c29c09d7f"
271271
},
272272
"source": [
273273
"can_openers['mean_forecast'].plot()\n",
@@ -279,19 +279,19 @@
279279
"can_openers['mean_value'].plot(color='red', ax=ax)\n",
280280
"plt.legend()"
281281
],
282-
"execution_count": 9,
282+
"execution_count": 165,
283283
"outputs": [
284284
{
285285
"output_type": "execute_result",
286286
"data": {
287287
"text/plain": [
288-
"<matplotlib.legend.Legend at 0x7f0bb937d940>"
288+
"<matplotlib.legend.Legend at 0x7f0b9ba1a2e8>"
289289
]
290290
},
291291
"metadata": {
292292
"tags": []
293293
},
294-
"execution_count": 9
294+
"execution_count": 165
295295
},
296296
{
297297
"output_type": "display_data",
@@ -339,10 +339,10 @@
339339
"metadata": {
340340
"colab": {
341341
"base_uri": "https://localhost:8080/",
342-
"height": 378
342+
"height": 0
343343
},
344344
"id": "xWRta9Dzk-Tg",
345-
"outputId": "4fedca51-cbb5-4c9f-cf89-f13441457292"
345+
"outputId": "734d7aa2-f1c8-4c66-a3ab-656b77b82260"
346346
},
347347
"source": [
348348
"can_openers = pd.read_csv('can_openers.csv', parse_dates=['month']).set_index('month')\n",
@@ -352,7 +352,7 @@
352352
"can_openers['MA5_forecast'] = can_openers['shipments'].rolling(5, center=True).mean().shift(3)\n",
353353
"can_openers.head(10)"
354354
],
355-
"execution_count": 44,
355+
"execution_count": 166,
356356
"outputs": [
357357
{
358358
"output_type": "execute_result",
@@ -470,7 +470,7 @@
470470
"metadata": {
471471
"tags": []
472472
},
473-
"execution_count": 44
473+
"execution_count": 166
474474
}
475475
]
476476
},
@@ -479,10 +479,10 @@
479479
"metadata": {
480480
"colab": {
481481
"base_uri": "https://localhost:8080/",
482-
"height": 541
482+
"height": 0
483483
},
484484
"id": "5Rnf3YpLLtt-",
485-
"outputId": "edfe6411-1da6-4c26-9583-23a53d7a47b9"
485+
"outputId": "affb0ae3-c083-4978-bde7-9c85bae0dc4e"
486486
},
487487
"source": [
488488
"can_openers['MA3_forecast'].plot()\n",
@@ -492,19 +492,19 @@
492492
"\n",
493493
"plt.legend()"
494494
],
495-
"execution_count": 45,
495+
"execution_count": 167,
496496
"outputs": [
497497
{
498498
"output_type": "execute_result",
499499
"data": {
500500
"text/plain": [
501-
"<matplotlib.legend.Legend at 0x7f0bb11f36a0>"
501+
"<matplotlib.legend.Legend at 0x7f0b9b9d2748>"
502502
]
503503
},
504504
"metadata": {
505505
"tags": []
506506
},
507-
"execution_count": 45
507+
"execution_count": 167
508508
},
509509
{
510510
"output_type": "display_data",
@@ -550,7 +550,7 @@
550550
"height": 699
551551
},
552552
"id": "ZdpzoBlwcO6s",
553-
"outputId": "cd35cf75-d706-4665-e675-54f2619d18a0"
553+
"outputId": "f7b5cbce-5be3-402c-8e5b-eb752bf0dab0"
554554
},
555555
"source": [
556556
"# Reload a fresh copy of our data\n",
@@ -568,7 +568,7 @@
568568
"\n",
569569
"can_openers.plot()"
570570
],
571-
"execution_count": 160,
571+
"execution_count": 168,
572572
"outputs": [
573573
{
574574
"output_type": "stream",
@@ -588,13 +588,13 @@
588588
"output_type": "execute_result",
589589
"data": {
590590
"text/plain": [
591-
"<matplotlib.axes._subplots.AxesSubplot at 0x7f0b9bc4e6d8>"
591+
"<matplotlib.axes._subplots.AxesSubplot at 0x7f0b9b90f780>"
592592
]
593593
},
594594
"metadata": {
595595
"tags": []
596596
},
597-
"execution_count": 160
597+
"execution_count": 168
598598
},
599599
{
600600
"output_type": "display_data",
@@ -611,6 +611,77 @@
611611
}
612612
]
613613
},
614+
{
615+
"cell_type": "code",
616+
"metadata": {
617+
"colab": {
618+
"base_uri": "https://localhost:8080/"
619+
},
620+
"id": "Hh5S264iJFG5",
621+
"outputId": "6098d250-7255-4e1e-946e-b016b913fa74"
622+
},
623+
"source": [
624+
"can_openers.shape"
625+
],
626+
"execution_count": 169,
627+
"outputs": [
628+
{
629+
"output_type": "execute_result",
630+
"data": {
631+
"text/plain": [
632+
"(12, 4)"
633+
]
634+
},
635+
"metadata": {
636+
"tags": []
637+
},
638+
"execution_count": 169
639+
}
640+
]
641+
},
642+
{
643+
"cell_type": "markdown",
644+
"metadata": {
645+
"id": "NY8QCfKkIoIM"
646+
},
647+
"source": [
648+
"We can calculate the MSE to determine the goodness of fit for each of the smoothed fit values. Remember that we'll want to use a **test set** to perform our evaluation.\n",
649+
"\n",
650+
"For our purposes, we'll consider the last four observations to be the test set, and we can access this part of the `pandas` `DataFrame` with the `.tail(N)` function (`.tail(4)`).\n",
651+
"\n",
652+
"We can then calculate the MSE between each of the fits and the original data as follows:"
653+
]
654+
},
655+
{
656+
"cell_type": "code",
657+
"metadata": {
658+
"colab": {
659+
"base_uri": "https://localhost:8080/"
660+
},
661+
"id": "cj8petGxJctJ",
662+
"outputId": "ad7a5e17-fe65-4209-c074-bff1983723f4"
663+
},
664+
"source": [
665+
"test = can_openers.tail(4)\n",
666+
"test.head()\n",
667+
"\n",
668+
"mse_1 = ((test['alpha_0.1'] - test['shipments'])**2).mean()\n",
669+
"mse_5 = ((test['alpha_0.5'] - test['shipments'])**2).mean()\n",
670+
"mse_9 = ((test['alpha_0.9'] - test['shipments'])**2).mean()\n",
671+
"\n",
672+
"print(mse_1, mse_5, mse_9)"
673+
],
674+
"execution_count": 174,
675+
"outputs": [
676+
{
677+
"output_type": "stream",
678+
"text": [
679+
"2228.2308984507677 3050.129555940628 3431.988106158692\n"
680+
],
681+
"name": "stdout"
682+
}
683+
]
684+
},
614685
{
615686
"cell_type": "markdown",
616687
"metadata": {
@@ -648,7 +719,7 @@
648719
"\n",
649720
"can_openers.plot()"
650721
],
651-
"execution_count": 161,
722+
"execution_count": null,
652723
"outputs": [
653724
{
654725
"output_type": "stream",

0 commit comments

Comments
 (0)