Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
FilippoMB committed Apr 26, 2024
1 parent 2f1d99f commit 32dcc0d
Show file tree
Hide file tree
Showing 31 changed files with 619 additions and 558 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 9 additions & 2 deletions _sources/notebooks/00/resources.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Resources
# Resources and acknowledgments

The following resources served as inspiration and provided very useful content to write some of the sections of the book.

Expand All @@ -18,4 +18,11 @@ The following resources served as inspiration and provided very useful content t
- IPython Cookbook, Second Edition (2018) [[Link](https://ipython-books.github.io/121-plotting-the-bifurcation-diagram-of-a-chaotic-dynamical-system/)].
- Introduction to Taken's Embedding [[link](https://www.kaggle.com/code/tigurius/introduction-to-taken-s-embedding/notebook)].
- An introduction to Dynamic Time Warping [[link](https://rtavenar.github.io/blog/dtw.html)].
- An intuitive approach to DTW — Dynamic Time Warping [[link](https://towardsdatascience.com/an-intuitive-approach-to-dtw-dynamic-time-warping-f660ccb77ff4)].
- An intuitive approach to DTW — Dynamic Time Warping [[link](https://towardsdatascience.com/an-intuitive-approach-to-dtw-dynamic-time-warping-f660ccb77ff4)].

## Acknowledgments

I whish to thank:

- [Simone Scardapane](https://www.sscardapane.it/) for spotting many typos, giving feedback, and suggestions.
- Jonas Berg Hansen for helping preparing the exercises.
64 changes: 41 additions & 23 deletions _sources/notebooks/03/smoothing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"tags": []
},
"source": [
"- In this lecture, we will build that knowledge and explore another important concept called **smoothing**.\n",
"- In this lecture, we will build upon that knowledge and explore another important concept called **smoothing**.\n",
"- In particular, we will cover:\n",
" 1. An introduction to smoothing and why it is necessary.\n",
" 2. Common smoothing techniques.\n",
Expand Down Expand Up @@ -299,7 +299,7 @@
"\n",
"- The approximation with the mean seems reasonable in this case.\n",
"- In general we want to measure how far off our estimate is from reality. \n",
"- A common way of doing it is by calculating *Mean Squared Error* (MSE) \n",
"- A common way of doing it is by calculating the *Mean Squared Error* (MSE) \n",
"\n",
"$$MSE = \\frac{1}{T}\\sum_{t=1}^{T} (X(t) - \\hat{X}(t))^2$$\n",
"\n",
Expand All @@ -319,7 +319,7 @@
"- Let's consider the following example.\n",
"- Say we have a time series of observed values $X = [0, 1, 3, 2]$.\n",
"- The predictions given by our model are $\\hat{X} = [1, 1, 2, 4]$. \n",
"- We calculate MSE as: \n",
"- We calculate the MSE as: \n",
"\n",
"$$(0-1)^{2} + (1-1)^{2} + (3-2)^{2} + (2-4)^{2} = 6$$"
]
Expand Down Expand Up @@ -351,7 +351,7 @@
"- The MSE allows to compare different estimates to see which is best. \n",
"- In this case, the first model gives us a better estimate than the second one. \n",
"- This idea of measuring how a model performs is important in machine learning and we will use it often in this course.\n",
"- Let's create a function to calculate MSE that we will use in as we go forward."
"- Let's create a function to calculate MSE that we will use as we go forward."
]
},
{
Expand Down Expand Up @@ -684,7 +684,7 @@
},
"source": [
"- Note that the output of the MA is shorter than the original data.\n",
"- The reason is because the moving window is not centered on the first and last elements of the time series.\n",
"- The reason is that the moving window is not centered on the first and last elements of the time series.\n",
"- Given a window of size $P$, the MA will be $P-1$ steps shorter than the original time series."
]
},
Expand Down Expand Up @@ -719,7 +719,7 @@
"tags": []
},
"source": [
"- Now, we try the MA on a period time series, which could represent a seasonality."
"- Now, we try the MA on a periodic time series, which could represent a seasonality."
]
},
{
Expand Down Expand Up @@ -777,7 +777,7 @@
},
"source": [
"- It's not perfect but clearly picks up the periodic pattern.\n",
"- Lastly, let's see how moving average handles trend, seasonality, and a bit of noise."
"- Lastly, let's see how MA handles trend, seasonality, and a bit of noise."
]
},
{
Expand Down Expand Up @@ -898,15 +898,29 @@
"tags": []
},
"source": [
"- There are many ways to create the weights.\n",
"- A simple way is to define them as follows:\n",
"- There are many ways to set the weights.\n",
"- For example, we could define them with the following system of equations: \n",
"\n",
"$$\n",
"\\begin{cases}\n",
"& w_1 + w_2 + w_3 = 1\\\\ \n",
"& w_2 = (w_1)^2 \\\\ \n",
"& w_3 = (w_1)^3\n",
"\\end{cases}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- This gives the following weights:\n",
" \n",
"$$\n",
"\\begin{aligned}\n",
"& w + w^2 + w^3 = 1\\\\ \n",
"& w_1 = w \\approx 0.543 \\\\ \n",
"& w_2 = w^2 \\approx 0.294 \\\\ \n",
"& w_3 = w^3 \\approx 0.16 \n",
"& w_1 \\approx 0.543 & \\text{weights associated to $t-1$}\\\\ \n",
"& w_2 \\approx 0.294 & \\text{weights associated to $t-2$} \\\\ \n",
"& w_3 \\approx 0.16 & \\text{weights associated to $t-3$}\n",
"\\end{aligned}\n",
"$$\n",
"\n",
Expand Down Expand Up @@ -939,7 +953,7 @@
"### Forecasting with MA\n",
"\n",
"- Instead of pulling out the inherent pattern within a series, the smoothing functions can be used to create *forecasts*. \n",
"- The forecast for the next time step is compute as follow \n",
"- The forecast for the next time step is computed as follows:\n",
"\n",
"$$\\hat{X}(t+1) = \\frac{X(t) + X(t-1) + \\dots + X(t-P+1)}{P}$$ \n",
"\n",
Expand Down Expand Up @@ -1056,7 +1070,7 @@
"- One might reduce the window size to obtain a more responsive model. \n",
"- However, a window size that's too small will chase noise in the data as opposed to extracting the pattern. \n",
"- There is a tradeoff between *responsiveness* and *robustness to noise*. \n",
"- The best answer lies somewhere between and requires careful tuning to determine which setup is best for a given dataset and problem at hand. "
"- The best answer lies somewhere in between and requires careful tuning to determine which setup is best for a given dataset and problem at hand. "
]
},
{
Expand Down Expand Up @@ -1363,8 +1377,8 @@
"- There are many initialization strategies. \n",
"- A simple one is to set $S(0) = X(0)$. \n",
"- Another strategy is to find the mean of the first couple of observations, e.g., $S(0) = \\frac{X(0) + X(1) + X(2)}{3}$. \n",
"- Once you've initialized, you simply use the update rule above to calculate all values. \n",
"- In practice, we don't this process by hand, but we use the functions in ``statsmodels``. "
"- Once you've initialized it, you can use the update rule above to calculate all values. \n",
"- In practice, we don't perform this process by hand, but we use the functions in ``statsmodels``. "
]
},
{
Expand All @@ -1376,10 +1390,10 @@
"tags": []
},
"source": [
"#### Setting Alpha\n",
"#### Setting $\\alpha$\n",
"- Choosing the optimal value for $\\alpha$ is also done by ``statsmodels``. \n",
"- A solver uses a metric like MSE to find the optimal $\\alpha$. \n",
"- Even if we do not choose the value manually, is goot to have a basic idea of what's happening under the hood."
"- Even if we do not choose the value manually, is good to have a basic idea of what's happening under the hood."
]
},
{
Expand Down Expand Up @@ -1436,7 +1450,7 @@
"### Triple exponential smoothing\n",
"\n",
"- 💡 It has all the benefits of Double Exponential with the ability to also model the seasonality.\n",
"- It does by adding a third component that smooths out seasonality of length $L$.\n",
"- It does this by adding a third component that smooths out seasonality of length $L$.\n",
"- There are two variants in the model:\n",
" - additive seasonality,\n",
" - multiplicative seasonality."
Expand All @@ -1452,6 +1466,8 @@
"source": [
"**Additive seasonality model**\n",
"\n",
"- This method is preferred when the seasonal variations are roughly constant through the series.\n",
"\n",
"$$\n",
"\\begin{aligned} \n",
"S(t) = \\alpha \\big(X(t) + c(t-L)\\big) + (1-\\alpha) \\big(S(t-1) + b(t-1)\\big) & \\hspace{2cm} \\text{Smoothed values} \\\\\n",
Expand All @@ -1472,6 +1488,8 @@
"source": [
"**Multiplicative seasonality model**\n",
"\n",
"- This method is preferred when the seasonal variations are changing proportional to the level of the series.\n",
"\n",
"$$\n",
"\\begin{aligned} \n",
"S(t) = \\alpha \\frac{X(t)}{c(t-L)} + (1-\\alpha) \\big(S(t-1) + b(t-1)\\big) & \\hspace{2cm} \\text{Smoothed values} \\\\\n",
Expand All @@ -1493,7 +1511,7 @@
"**Initialization**\n",
"\n",
"- $b(0) = \\frac{1}{L} \\left( \\frac{X(L+1) - X(1)}{L} + \\frac{X(L+2) - X(2)}{L} + \\dots + \\frac{X(L+L) - X(L)}{L} \\right)$\n",
"- $c(t) = \\frac{1}{N} \\sum_{j=1}^N \\frac{X(L(j-1)+t)}{A_j}$ for $t=1,2,\\dots,L$, where $N$ is the number of complete cycles in the data and $A_j = \\frac{\\sum_{t=1}^L X(2(j-1)+t)}{L}$\n",
"- $c(t) = \\frac{1}{N} \\sum_{j=1}^N \\frac{X(L(j-1)+t)}{A_j}$ for $t=1,2,\\dots,L$, where $N$ is the total number of seasonal cycles in the data and $A_j = \\frac{\\sum_{t=1}^L X(2(j-1)+t)}{L}$\n",
"\n",
"---"
]
Expand Down Expand Up @@ -1872,7 +1890,7 @@
"source": [
"- Triple Exponential Smoothing pickups trend and seasonality. \n",
"- Clearly, this is the most suitable approach for this data.\n",
"- We can summarize the results in the following"
"- We can summarize the results in the following table:"
]
},
{
Expand Down Expand Up @@ -1929,7 +1947,7 @@
"In this lecture we learned\n",
"\n",
"1. What is smoothing and why it is necessary.\n",
"2. The common smoothing techniques.\n",
"2. Some common smoothing techniques.\n",
"3. A basic understanding of how to smooth time series data with Python and generate forecasts.\n",
"---"
]
Expand Down
Loading

0 comments on commit 32dcc0d

Please sign in to comment.