Documentation

# `stats`::`hodrickPrescottFilter`

The Hodrick-Prescott filter

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

## Syntax

```stats::hodrickPrescottFilter(`[x1, x2, …]`, `p`)
stats::hodrickPrescottFilter(`s`, <`c`>, `p`)
```

## Description

```stats::hodrickPrescottFilter([x1, x2, …], p)``` returns a list of data from which cyclic variations of the time series given by the input data x1, x2 etc. are eliminated using the Hodrick-Prescott filter process.

The Hodrick-Prescott filter scheme tries to split a time series consisting of the data x1, x2 etc. into a “trend” that is approximately linear in time plus a “cyclic” contribution. The data returned by `stats::hodrickPrescottFilter` describe the trend. The cyclic part c may be computed by

`x := [x1, x2, ...]:`

`y := stats::HodrickPrescottFilter(x, p):`

`c := zip(x, y, _subtract):`

Thus, xi = yi + ci.

Large values of the penalty parameter p lead to smooth straight trend curves. Cf. Example 5.

If the data are provided by a `stats::sample` object containing only one non-string column, the column index `c` is optional. Cf. Example 3.

External statistical data stored in an ASCII file can be imported into a MuPAD® session via `import::readdata`. In particular, see Example 1 of the corresponding help page.

## Examples

### Example 1

We apply the Hodrick-Prescott filter to some data. The result shows an obvious trend towards increasing data values:

`stats::hodrickPrescottFilter([1, 2, 3, 2, 3, 4, 3, 4, 5], 10)`
` `

### Example 2

We create a sample:

```s := stats::sample([[i + frandom() - 0.5, -i + frandom() - 0.5] \$ i = 1..10])```
```0.7703581656 -0.6689628213 1.653156516 -1.505187219 2.766272902 -3.319835772 3.952083055 -3.821218044 4.854984926 -4.818141187 6.221918655 -6.026170226 7.288981492 -7.288474164 8.355687175 -8.455102606 9.379160127 -8.580615152 10.23505742 -9.712454973 ```

The Hodrick-Prescott filter process applied to the data in the first column yields:

```p := 10: stats::hodrickPrescottFilter(s, 1, p)```
` `
`stats::hodrickPrescottFilter(s, 2, p)`
` `
`delete s, p:`

### Example 3

We create a sample consisting of one string column and one non-string column:

```s := stats::sample([["1996", 1242], ["1997", 1353], ["1998", 1142], ["1999", 1255], ["2000", 1417], ["2001", 1312], ["2002", 1440], ["2003", 1422], ["2004", 1470] ])```
```"1996" 1242 "1997" 1353 "1998" 1142 "1999" 1255 "2000" 1417 "2001" 1312 "2002" 1440 "2003" 1422 "2004" 1470 ```

We apply the Hodrick-Prescott filter to the second column. In this case, this column needs not be specified, since it is the only non-string column:

`y := stats::hodrickPrescottFilter(s, 10)`
` `

We convert this list to a sample object:

`y := stats::sample(y)`
```1239.848378 1255.015604 1270.397993 1296.009146 1329.022865 1362.512038 1398.347268 1433.347951 1468.498758 ```

We create a new sample consisting of the filtered data:

`stats::concatCol(stats::col(s, 1), y)`
```"1996" 1239.848378 "1997" 1255.015604 "1998" 1270.397993 "1999" 1296.009146 "2000" 1329.022865 "2001" 1362.512038 "2002" 1398.347268 "2003" 1433.347951 "2004" 1468.498758 ```
`delete s, y:`

### Example 4

We model monthly data with a decaying trend of , where i is the index of the month. These trend data are obscured by cyclic contributions and random noise:

```monthlyData:= i -> ( 1/(1 + 0.01*i) // the trend + 0.7*cos(i * 1.12*2*float(PI)) // cycle + 0.3*sin(i * 2.04*4*float(PI)) // cycle + 0.2*cos(i * 1.01*6*float(PI)) // cycle + 2.3*frandom() // random noise ):```

We provide monthly data for 10 years, i.e., 120 months. The cyclic contributions and the noise are eliminated from the time series by the Hodrick-Prescott filter process:

```n := 120: x := [monthlyData(i) \$ i = 1..n]: trend := stats::hodrickPrescottFilter(x, 10^5): cycle := zip(x, trend, _subtract):```

We visualize the splitting of the time series (black) into the approximately linear trend contribution (red) plus the cyclic part (blue):

```plot( plot::Listplot([[i, x[i]] \$ i = 1..n], Color = RGB::Black), plot::Listplot([[i, trend[i]] \$ i = 1..n], Color = RGB::Red), plot::Listplot([[i, cycle[i]] \$ i = 1..n], Color = RGB::Blue) )``` We use a scatterplot to visualize a linear regression of the unfiltered data. The regression line is in good accordance with the trend line above:

`plot(plot::Scatterplot([[i, x[i]] \$ i = 1..n]))` `delete monthlyData, n, x, trend, cycle:`

### Example 5

We demonstrate the effect of the penalty parameter p by an animated plot:

```delete p: n := 100: data := [1/(1 + 0.01*i) + frandom() \$ i = 1..n]: for i from 0 to 30 step 1/5 do trend := stats::hodrickPrescottFilter(data, 10^(0.2*i)); L[i] := plot::Listplot([[i, trend[i]] \$ i = 1..n], Color = RGB::Red, VisibleFromTo = i .. i + 0.2); T[i] := plot::Text2d(expr2text(p = 10^(0.2*i)), [70, 1.7], VisibleFromTo = i .. i + 0.2); end_for: plot(plot::Listplot([[i, data[i]] \$ i= 1..n], Color=RGB::Black), L[i] \$ i = 0..30 step 1/5, T[i] \$ i = 0..30 step 1/5)``` Large penalty parameters p result in trend curves that are close to a straight line. This is not always the desired information. The following animation features a time series with a parabolic trend curve obscured by random noise. Too large values of p produce a trend curve that just displays the mean of the data:

```data := [8*frandom() + 5 - (i - 50)^2/100 \$ i = 1..n]: for i from 0 to 50 do trend := stats::hodrickPrescottFilter(data, 10^(0.2*i)); L[i] := plot::Listplot([[i, trend[i]] \$ i = 1..n], Color = RGB::Red, VisibleFromTo = i/5 .. (i + 1)/5); T[i] := plot::Text2d(expr2text(p = 10^(0.2*i)), [50, -5], VisibleFromTo = i/5 .. (i + 1)/5); end_for: plot(plot::Listplot([[i, data[i]] \$ i= 1..n], Color=RGB::Black), L[i] \$ i = 0..50, T[i] \$ i = 0..50)``` `delete n, data, i, trend, L, T:`

## Parameters

 `x1, x2, …` The statistical data (time series): arithmetical expressions. `s` A sample of domain type `stats::sample`. `c` An integer representing a column index of the sample s. This column provides the data x1, x2 etc. `p` The penalty parameter of the Hodrick-Prescott scheme: a real positive numerical value. If the data x1, x2 etc. represent monthly measurements, the literature recommends values of p between 100 0 and 140 0. If the data represent quaterly measurements, values of p around 1600 are recommended. If the data represent yearly measurements, values of p between 6 and 14 are recommended.

## Return Values

List of floating-point data.

## References

Robert Hodrick and Edwad C. Prescott, “Postwar U.S. Business Cycles: An Empirical Investigation.” Journal of Money, Credit and Banking, 1997.

Maravall, Agustin and Ana del Rio, “Time Aggregation and the Hodrick-Prescott Filter”, Banco de Espana, 2001.