# filter

Filter disturbances through conditional variance model

## Syntax

``````[V,Y] = filter(Mdl,Z)``````
``Tbl2 = filter(Mdl,Tbl1)``
``[___] = filter(___,Name,Value)``

## Description

example

``````[V,Y] = filter(Mdl,Z)``` returns the numeric arrays of conditional variance paths `V` and response paths `Y` from filtering the numeric array of disturbance paths `Z` through the fully specified conditional variance model `Mdl`. `Mdl` can be a `garch`, `egarch`, or `gjr` model.```

example

````Tbl2 = filter(Mdl,Tbl1)` returns the table or timetable `Tbl2` containing the results from filtering the paths of disturbances in the input table or timetable `Tbl1` through `Mdl`. The disturbance variable in `Tbl1` is associated with the model innovations process through `Mdl`. (since R2023a)`filter` selects the response variable named in `Mdl.SeriesName` or the sole variable in `Tbl1`. To select a different disturbance variable in `Tbl1` to filter through the model, use the `DisturbanceVariable` name-value argument.```

example

````[___] = filter(___,Name,Value)` specifies options using one or more name-value arguments in addition to any of the input argument combinations in previous syntaxes. `filter` returns the output argument combination for the corresponding input arguments. For example, `filter(Mdl,Z,Z0=PS)` filters the numeric vector of disturbances `Z` through the conditional variance model `Mdl` and specifies the numeric vector of presample disturbance data `PS` to initialize the model.```

## Examples

collapse all

Demonstrate that `simulate` and `filter` can return equal quantities. Supply data in a numeric vector.

Specify a GARCH(1,1) model with Gaussian innovations.

`Mdl = garch(Constant=0.005,GARCH=0.8,ARCH=0.1);`

Simulate the model using Monte Carlo simulation. Then, standardize the simulated innovations and filter them.

```rng(1) % For reproducibility [vs,es] = simulate(Mdl,100,E0=0,V0=0.05); Z = es./sqrt(vs); [vf,ef] = filter(Mdl,Z,Z0=0,V0=0.05);```

Confirm that the outputs of `simulate` and `filter` are identical.

`norm(vs-vf)`
```ans = 0 ```

A norm of 0 indicates that the two outputs are identical.

Since R2023a

Fit a GARCH(1,1) model to the daily close NASDAQ Composite Index returns, and then filter a randomly generated series of disturbances through the estimated model. Supply timetables of data throughout the process.

Load the NASDAQ data included with the toolbox. Convert the index to returns.

```load Data_EquityIdx DTTRet = price2ret(DataTimeTable); DTTRet.Interval = []; T = height(DTTRet); figure plot(DTTRet.Time,DTTRet.NASDAQ) title("NASDAQ Returns")``` The returns exhibit volatility clustering.

Specify a GARCH(1,1) model, and fit it to the series. Name the response series of the model `NASDAQ` by using dot notation.

```Mdl = garch(1,1); Mdl.SeriesName = "NASDAQ"; EstMdl = estimate(Mdl,DTTRet);```
``` GARCH(1,1) Conditional Variance Model (Gaussian Distribution): Value StandardError TStatistic PValue __________ _____________ __________ __________ Constant 2.0101e-06 5.4314e-07 3.7008 0.0002149 GARCH{1} 0.8833 0.0084528 104.5 0 ARCH{1} 0.10919 0.007662 14.251 4.4112e-46 ```

`estimate` fits the model to the response data in the `NASDAQ` variable of `DTTRet` because the name matches the name of the response variable in `Mdl.SeriesName`. Alternatively, you can specify the response variable by using the `ResponseVariable` name-value argument.

Generate 2 random, independent series of length `T` from the standard Gaussian distribution. Store the matrix of series as one variable in `DTTRet`.

```rng(1) % For reproducibility DTTRet.Z = randn(T,2);```

`DTTRet` contains a new variable called `Z` containing a `T`-by-2 matrix of five disturbance paths.

Filter the paths of disturbances through the estimated GARCH model. Specify the table variable name containing the disturbance paths.

`Tbl2 = filter(EstMdl,DTTRet,DisturbanceVariable="Z")`
```Tbl2=3027×5 timetable Time NYSE NASDAQ Z NASDAQ_Variance NASDAQ_Response ___________ __________ __________ ____________________ ________________________ ________________________ 03-Jan-1990 -0.0010106 0.0034122 -0.64901 0.39457 0.0002712 0.00024 -0.010688 0.0061126 04-Jan-1990 -0.0076633 -0.0032816 1.1812 0.18862 0.00025404 0.00021808 0.018826 0.0027855 05-Jan-1990 -0.0084415 -0.0025501 -0.75845 -0.38106 0.0002651 0.00019549 -0.012349 -0.0053278 06-Jan-1990 0.0035387 0.0010688 -1.1096 -0.5688 0.00025282 0.00017778 -0.017643 -0.0075841 07-Jan-1990 -0.010188 -0.0042382 -0.84555 1.3083 0.00025932 0.00016532 -0.013616 0.016822 08-Jan-1990 -0.0063818 -0.013378 -0.57266 0.24805 0.00025131 0.00017894 -0.0090783 0.0033181 09-Jan-1990 0.0034295 -0.0040909 -0.55868 -0.81397 0.00023299 0.00016127 -0.0085277 -0.010337 10-Jan-1990 -0.023407 -0.020573 0.17838 -0.3446 0.00021575 0.00015613 0.0026201 -0.0043057 11-Jan-1990 -0.008586 -0.0070291 -0.19686 0.69902 0.00019333 0.00014194 -0.0027372 0.008328 12-Jan-1990 0.0088515 0.0080292 0.58644 -0.70041 0.0001736 0.00013496 0.0077267 -0.0081367 13-Jan-1990 -0.0080484 -0.0033681 -0.85189 -1.1917 0.00016187 0.00012845 -0.010838 -0.013506 14-Jan-1990 0.0011232 -0.0026478 0.80032 -1.0762 0.00015781 0.00013538 0.010054 -0.012522 15-Jan-1990 0.0026159 0.0076503 -1.5094 1.0449 0.00015244 0.00013872 -0.018636 0.012307 16-Jan-1990 -0.02352 -0.020486 0.87587 0.34 0.00017458 0.00014108 0.011573 0.0040383 17-Jan-1990 0.002181 -0.0035252 -0.24279 -0.10721 0.00017084 0.0001284 -0.0031734 -0.0012149 18-Jan-1990 -0.0052425 -0.011121 0.16681 0.8233 0.00015402 0.00011559 0.0020702 0.0088514 ⋮ ```

`Tbl2` is a 3027-by-5 timetable containing all variables in `DTTRet`, the two filtered conditional variance paths `NASDAQ_Variance`, and the two filtered response paths `NASDAQ_Response`.

Specify an EGARCH(1,1) model with Gaussian innovations.

```Mdl = egarch(Constant=-0.1,GARCH=0.8,ARCH=0.3, ... Leverage=-0.05);```

Simulate 25 series of standard Gaussian observations for 100 periods.

```rng(1); % For reproducibility Z = randn(100,25);```

`Z` represents 25 paths of synchronized disturbances for 100 periods.

Obtain 25 paths of conditional variances by filtering the disturbance paths through the EGARCH(1,1) model.

`V = filter(Mdl,Z);`

Plot the paths of conditional variances.

```figure; plot(V); title("Conditional Variance Paths"); xlabel("Periods");``` Specify a GJR(1,2) model with Gaussian innovations.

```Mdl = gjr(Constant=0.005,GARCH=0.8,ARCH={0.1 0.01}, ... Leverage={0.05 0.01});```

Simulate 25 series of standard Gaussian observations for 102 periods.

```rng(1); % For reproducibility Z = randn(102,25);```

`Z` represents 25 paths of synchronized disturbances for 102 periods.

Obtain 25, 100 period paths of conditional variances by filtering the disturbance paths through the GJR(1,2) model. Specify the first two disturbances as presample observations.

`V = filter(Mdl,Z(3:end,:),Z0=Z(1:2,:));`

Plot the paths of conditional variances.

```figure plot(V) title("Conditional Variance Paths"); xlabel("Periods");``` ## Input Arguments

collapse all

Conditional variance model without any unknown parameters, specified as a `garch`, `egarch`, or `gjr` model object.

`Mdl` cannot contain any properties that have `NaN` value.

Disturbance paths zt used to drive the output innovation process εt, specified as a `numobs`-by-1 numeric vector or `numobs`-by-`numpaths` numeric matrix. Given the variance process σt2, the innovation process is

`${\epsilon }_{t}={\sigma }_{t}{z}_{t}.$`

As a column vector, `Z` represents a single path of the underlying disturbance series.

As a matrix, the rows of `Z` correspond to periods. The columns correspond to separate paths. The observations across any row occur simultaneously.

The last element or row of `Z` contains the latest observation.

Since R2023a

Time series data containing observed disturbance variable zt, associated with the model innovations process εt, specified as a table or timetable with `numvars` variables and `numobs` rows. You can optionally select a disturbance variable by using the `DisturbanceVariable` name-value argument.

Given the variance process σt2, the innovation process is

`${\epsilon }_{t}={\sigma }_{t}{z}_{t}.$`

The selected variable is a single path (`numobs`-by-1 vector) or multiple paths (`numobs`-by-`numpaths` matrix) of `numobs` observations of disturbance data. Each row is an observation, and measurements in each row occur simultaneously.

Each path (column) of the selected variable is independent of the other paths.

If `Tbl1` is a timetable, it must represent a sample with a regular datetime time step (see `isregular`), and the datetime vector `Tbl1.Time` must be strictly ascending or descending.

If `Tbl1` is a table, the last row contains the latest observation.

### Name-Value Arguments

Specify optional pairs of arguments as `Name1=Value1,...,NameN=ValueN`, where `Name` is the argument name and `Value` is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose `Name` in quotes.

Example: `filter(Mdl,Z,Z0=[1 1;0.5 0.5],V0=[1 0.5;1 0.5])` specifies two equivalent presample paths of disturbances and two different presample paths of conditional variances.

Since R2023a

Variable to select from `Tbl1` to treat as the disturbance variable zt to filter through `Mdl`, specified as one of the following data types:

• String scalar or character vector containing a variable name in `Tbl1.Properties.VariableNames`

• Variable index (integer) to select from `Tbl1.Properties.VariableNames`

• A length `numvars` logical vector, where ```DisturbanceVariable(j) = true``` selects variable `j` from `Tbl1.Properties.VariableNames`, and `sum(DisturbanceVariable)` is `1`

The selected variable must be a numeric vector and cannot contain missing values (`NaN`).

If `Tbl1` has one variable, the default specifies that variable. Otherwise, the default matches the variable to names in `Mdl.SeriesName`.

Example: `DisturbanceVariable="StockRateDist"`

Example: ```DisturbanceVariable=[false false true false]``` or `DisturbanceVariable=3` selects the third table variable as the disturbance variable.

Data Types: `double` | `logical` | `char` | `cell` | `string`

Presample disturbance paths zt, specified as a `numpreobs`-by-1 numeric vector or `numpreobs`-by-`numprepaths` matrix. `Z0` provides initial values for the input disturbance paths `Z`. Use `Z0` only when you supply the numeric array of disturbances `Z`.

`numpreobs` is the number of presample observations. `numprepaths` is the number of presample response paths.

Each row is a presample observation, and measurements in each row occur simultaneously. The last row contains the latest presample observation. `numpreobs` must be at least `Mdl.Q`. If `numpreobs` > `Mdl.Q`, `filter` uses the latest required number of observations only. The last element or row contains the latest observation.

• If `Z0` is a column vector, it represents a single path of the underlying disturbance series. `filter` applies it to each output path.

• If `Z0` is a matrix, each column represents a presample path of the underlying disturbance series. `numprepaths` must be at least `numpaths`. If `numprepaths` > `numpaths`, `filter` uses the first `size(Z,2)` columns only.

By default, `filter` sets any necessary presample disturbances to an independent sequence of standardized disturbances drawn from `Mdl.Distribution`.

Data Types: `double`

Positive presample conditional variance paths σt2, specified as a `numpreobs`-by-1 positive column vector or `numpreobs`-by-`numprepaths` positive matrix. `V0` provides initial values for the conditional variances in the model. Use `V0` only when you supply the numeric array of disturbances `Z`.

To initialize the conditional variance model, `numpreobs` must be at least ```max([Mdl.P Mdl.Q])```. If `numpreobs` > `max([Mdl.P Mdl.Q])`, `filter` uses the latest required number of observations only. The last element or row contains the latest observation.

• If `V0` is a column vector, it represents a single path of the conditional variance series. `filter` applies it to each output path.

• If `V0` is a matrix, `numprepaths` must be at least `numpaths`. If `numprepaths` > `numpaths`, `filter` uses the first `size(Z,2)` columns only.

By default, `filter` sets any necessary presample conditional variances to the unconditional variance of the process.

Data Types: `double`

Since R2023a

Presample data for innovation paths εt or conditional variance paths σt2 to initialize the model, specified as a table or timetable, the same type as `Tbl1`, with `numprevars` variables and `numpreobs` rows. Use `Presample` only when you supply a table or timetable of data `Tbl1`.

Each selected variable is a single path (`numpreobs`-by-1 vector) or multiple paths (`numpreobs`-by-`numprepaths` matrix) of `numpreobs` observations representing the presample of disturbance or conditional variance paths for `DisturbanceVariable`, the selected disturbance variable in `Tbl1`.

Each row is a presample observation, and measurements in each row occur simultaneously. `numpreobs` must be one of the following values:

• `Mdl.Q` when `Presample` provides only presample disturbances

• `max([Mdl.P Mdl.Q])` when `Presample` provides presample conditional variances

If you supply more rows than necessary, `filter` uses the latest required number of observations only.

If `Presample` is a timetable, all the following conditions must be true:

• `Presample` must represent a sample with a regular datetime time step (see `isregular`).

• The inputs `Tbl1` and `Presample` must be consistent in time such that `Presample` immediately precedes `Tbl1` with respect to the sampling frequency and order.

• The datetime vector of sample timestamps `Presample.Time` must be ascending or descending.

If `Presample` is a table, the last row contains the latest presample observation.

By default, `filter` sets any necessary presample disturbances to an independent sequence of standardized disturbances drawn from `Mdl.Distribution`, and it sets any necessary presample conditional variances to the unconditional variance of the process characterized by `Mdl`.

If you specify the `Presample`, you must specify the presample disturbance or conditional variance names by using the `PresampleDisturbanceVariable` or `PresampleVarianceVariable` name-value argument.

Since R2023a

Variable of `Presample` containing presample disturbance paths zt, specified as one of the following data types:

• String scalar or character vector containing a variable name in `Presample.Properties.VariableNames`

• Variable index (integer) to select from `Presample.Properties.VariableNames`

• A length `numprevars` logical vector, where ```PresampleDisturbanceVariable(j) = true``` selects variable `j` from `Presample.Properties.VariableNames`, and `sum(PresampleDisturbanceVariable)` is `1`

The selected variable must be a numeric matrix and cannot contain missing values (`NaN`).

If you specify presample disturbance data by using the `Presample` name-value argument, you must specify `PresampleDisturbanceVariable`.

Example: `PresampleDisturbanceVariable="StockRateDist0"`

Example: ```PresampleDisturbanceVariable=[false false true false]``` or `PresampleDisturbanceVariable=3` selects the third table variable as the presample disturbance variable.

Data Types: `double` | `logical` | `char` | `cell` | `string`

Since R2023a

Variable of `Presample` containing data for the presample conditional variances σt2, specified as one of the following data types:

• String scalar or character vector containing a variable name in `Presample.Properties.VariableNames`

• Variable index (integer) to select from `Presample.Properties.VariableNames`

• A length `numprevars` logical vector, where `PresampleVarianceVariable(j) = true` selects variable `j` from `Presample.Properties.VariableNames`, and `sum(PresampleVarianceVariable)` is `1`

The selected variable must be a numeric vector and cannot contain missing values (`NaN`).

If you specify presample conditional variance data by using the `Presample` name-value argument, you must specify `PresampleVarianceVariable`.

Example: `PresampleVarianceVariable="StockRateVar0"`

Example: `PresampleVarianceVariable=[false false true false]` or `PresampleVarianceVariable=3` selects the third table variable as the presample conditional variance variable.

Data Types: `double` | `logical` | `char` | `cell` | `string`

Note

• `NaN` values in `Z`, `Z0`, and `V0` indicate missing values. `filter` removes missing values from specified data by list-wise deletion.

• For the presample, `filter` horizontally concatenates `Z0` and `V0`, and then it removes any row of the concatenated matrix containing at least one `NaN`.

• For in-sample data `Z`, `filter` removes any row containing at least one `NaN`.

This type of data reduction reduces the effective sample size and can create an irregular time series.

• For numeric data inputs, `filter` assumes that you synchronize the presample data such that the latest observations occur simultaneously.

• `filter` issues an error when any table or timetable input contains missing values.

## Output Arguments

collapse all

Filtered conditional variance paths σt2, returned as a `numobs`-by-1 numeric column vector or `numobs`-by-`numpaths` numeric matrix. `V` represents the conditional variances of the mean-zero, heteroscedastic innovations associated with `Y`. `filter` returns `V` only when you supply the input `Z`.

The dimensions of `V` and `Z` are equivalent. If `Z` is a matrix, then the columns of `V` are the conditional variance paths corresponding to the columns of `Z`.

Rows of `V` are periods corresponding to the periodicity of `Z`.

Filtered response paths yt, returned as a `numobs`-by-1 numeric column vector or `numobs`-by-`numpaths`. `Y` usually represents a mean-zero, heteroscedastic time series of innovations with conditional variances given in `V`. `filter` returns `Y` only when you supply the input `Z`.

`Y` can also represent a time series of mean-zero, heteroscedastic innovations plus an offset. If `Mdl` includes an offset, then `filter` adds the offset to the underlying mean-zero, heteroscedastic innovations. Therefore, `Y` represents a time series of offset-adjusted innovations.

If `Z` is a matrix, then the columns of `Y` are the response paths corresponding to the columns of `Z`.

Rows of `Y` are periods corresponding to the periodicity of `Z`.

Since R2023a

Filtered conditional variance σt2 and response yt paths, returned as a table or timetable, the same data type as `Tbl1`. `filter` returns `Tbl2` only when you supply the input `Tbl1`.

`Tbl2` contains the following variables:

• The filtered conditional variances paths, which are in a `numobs`-by-`numpaths` numeric matrix, with rows representing observations and columns representing independent paths, each corresponding to the input observations and paths of the disturbance variable in `Tbl1`. `filter` names the filtered conditional variance variable in `Tbl2` `responseName_Variance`, where `responseName` is `Mdl.SeriesName`. For example, if `Mdl.SeriesName` is `StockReturns`, `Tbl2` contains a variable for the corresponding filtered conditional variance paths with the name `StockReturns_Variance`.

• The filtered response paths, which are in a `numobs`-by-`numpaths` numeric matrix, with rows representing observations and columns representing independent paths, each corresponding to the input observations and paths of the disturbance variable in `Tbl1`. `filter` names the filtered response variable in `Tbl2` `responseName_Response`, where `responseName` is `Mdl.SeriesName`. For example, if `Mdl.SeriesName` is `StockReturns`, `Tbl2` contains a variable for the corresponding filtered conditional variance paths with the name `StockReturns_Response`.

• All variables `Tbl1`.

If `Tbl1` is a timetable, row times of `Tbl1` and `Tbl2` are equal.

## Alternatives

`filter` generalizes `simulate`. Both function filter a series of disturbances to produce output responses and conditional variances. However, `simulate` autogenerates a series of mean-zero, unit-variance, independent and identically distributed (iid) disturbances according to the distribution in the conditional variance model object, `Mdl`. In contrast, `filter` lets you directly specify your own disturbances.

 Bollerslev, T. “Generalized Autoregressive Conditional Heteroskedasticity.” Journal of Econometrics. Vol. 31, 1986, pp. 307–327.

 Bollerslev, T. “A Conditionally Heteroskedastic Time Series Model for Speculative Prices and Rates of Return.” The Review of Economics and Statistics. Vol. 69, 1987, pp. 542–547.

 Box, G. E. P., G. M. Jenkins, and G. C. Reinsel. Time Series Analysis: Forecasting and Control. 3rd ed. Englewood Cliffs, NJ: Prentice Hall, 1994.

 Enders, W. Applied Econometric Time Series. Hoboken, NJ: John Wiley & Sons, 1995.

 Engle, R. F. “Autoregressive Conditional Heteroskedasticity with Estimates of the Variance of United Kingdom Inflation.” Econometrica. Vol. 50, 1982, pp. 987–1007.

 Hamilton, J. D. Time Series Analysis. Princeton, NJ: Princeton University Press, 1994.