Hands-On Financial Trading with Python by Jiri Pik Sourav Ghosh

Hands-On Financial Trading with Python by Jiri Pik Sourav Ghosh

Author:Jiri Pik, Sourav Ghosh
Language: eng
Format: epub
Publisher: Packt Publishing Limited
Published: 2021-04-29T00:00:00+00:00


Figure 6.4 – Plot displaying synthetic prices with ETS components

In the preceding screenshot, we do see the seasonality component very clearly—the oscillation up and down from the median value. We also see the error noise since the oscillations are not perfect. Finally, we see that the values are increasing—the trend component.

The Hodrick-Prescott filter

The Hodrick-Prescott (HP) filter is used to separate the trend and cyclical components from time series data by removing short-term fluctuations from the longer-term trend. In statsmodels, this is implemented as statsmodels.api.tsa.filters.hpfilter(...).

Let's use it with a lamb=129600 smoothing parameter to perform the decomposition (the value 129600 is the recommended value for monthly data). We use a pair of series values returned to generate a DataFrame with Price, hp_cycle, and hp_trend fields to represent the price, the seasonal component, and the trend components, as illustrated in the following code snippet:

hp_cycle, hp_trend =
sm.tsa.filters.hpfilter(dataset['Price'], lamb=129600)

decomp = dataset[['Price']]

decomp['HP_Cycle'] = hp_cycle

decomp['HP_Trend'] = hp_trend

decomp

The decomp DataFrame contains the following data:

Price HP_Cycle HP_Trend

2000-01-31 96.392059 -4.731153 101.123212

2000-02-29 99.659426 -1.839262 101.498688

... ... ... ...

2019-11-30 190.067039 -8.350371 198.417410

2019-12-31 190.676568 -8.107701 198.784269

240 rows × 3 columns

In the next section, we will look at the UnobservedComponents model.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.