Course Content
Introduction to Finance with Python
Introduction to Finance with Python
Basic Definitions
What is a time series?
Here is an example of time series:
Usually, values of time series correspond to the measurements of a specific variable, taken at equally spaced moments of time.
As another example of time series, we can take, simply, stock prices of a specific company, where values of these prices are taken every day at the same time.
Components of time series
Now, we are going to describe several components of the time series.
As an example of time series with seasonality we can take prices on a winter warm clothes, which, obviously, raising in the winter, when its demand is on a peak, due to cold weather, but also going downside at summer, due to hot weather, and absence of necessity in warm clothes.
Here also we can see several examples of time series, which includes or excludes this patterns.
First of all, we can see example of time series, which, at least at the first look, has neither trend or seasonality, as long as there is no clear direction of its values change in a longterm perspective, and there is also no regular and repeated patterns:
Now we are going to see an example of time series which has trend, as long as despite fluctuations of values, during this whole period they are generally going upside. But as previously - there are no significant regular changes.
The third example - is a time series with already obvious seasonal patterns, as long as we can see, that through the more-less equally spaced moments of times values increase dramatically. But here we don't have trend, since values, generally, don't either increase or decrease in longterm, but oscillate around same value.
Lastly, we are going to see time series, which has both trend and seasonal patterns. First of all, our values are generally increasing, secondly - we are seeing solid regularity in values downfalls and uprisings.
Stationarity
We also should notice, that special case of time series - is one, which has no trend, seasonality and any other such components.
In this case, time series is called stationary.
To be more exact, here is a complete definition:
To check, if time series is stationary - we have several methods.
First one - is to use statistical ADF(Augmented Dickey-Fuller) test, which takes as null hypothesis that time series is not stationary.
You can see here how to use ADF test in code:
Another option - is KPSS(Kwiatkowski–Phillips–Schmidt–Shin) test, which tests the null hypothesis that time series is stationary).
Let's see an example of using this test in code(here, as well as in all other examples in this chapter, we are assuming, that we have a DataFrame df
, which contains values of time series in column Price
and corresponding time moments of datetime
type into Date
column).
We need to use corresponding function of statsmodels
library.
Decomposition
Also, it should be better to mention, that we can decompose time series on its components, receiving trend(practical - averaged value), seasonality and residual values.
We can perform either additive decomposition (original time series represented as sum of trend, seasonality and residuals) or multiplicative (represented as a product).
Now, let's see code example for both:
Also, here we can see graphical representation of decomposition for both cases:
Last important thing, which we should mention, it is that we can use multiplicative decomposition only in case, when all values of time series are greater than zero.
Thanks for your feedback!