# Kernel-based Time-varying Regression - Part I¶

**Kernel-based time-varying regression (KTR)** is a time series model to address

time-varying regression coefficients

complex seasonality pattern

The full details of the model structure with an application in marketing media mix modeling can be found in Ng, Wang and Dai (2021). The core of KTR is the use of latent variables to define a smooth time varying representation of model coefficients, which bears a similar ideas in Kernel Smoothsing. The KTR approach sharply reduces the number of parameters compared to typical dynamic linear models such as Harvey (1989), and Durbin and Koopman (2002). The reduced number of parameters improves the computation speed, and allows for handling of high dimensional data and detecting small variances.

To topics covered here in **Part I**, are

KTR model structure

syntax to initialize, fit and predict a model

fit a model with complex seasonality

visualization of prediction and decomposed components

```
[1]:
```

```
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import orbit
from orbit.models import KTR
from orbit.diagnostics.plot import plot_predicted_data, plot_predicted_components
from orbit.utils.dataset import load_electricity_demand
%matplotlib inline
pd.set_option('display.float_format', lambda x: '%.5f' % x)
```

```
[2]:
```

```
print(orbit.__version__)
```

```
1.1.3dev
```

## Model Structure¶

This section gives the mathematical structure of the KTR model. In short, it considers a time-series (\(y_t\)) as the linear combination of three parts which are the local-trend (\(l_t\)), seasonality (\(s_t\)), and regression (\(r_t\)) terms. Mathematically,

where the \(\epsilon_t\) comprise a stationary random error process.

In **KTR**, the distinction between the local-trend, seasonality, and regressors while useful is semi-arbitrary and the time-series can also be considered as

where \(\beta_t\) is a \(P\)-dimensional vector of coefficients that vary over time (i.e., \(\beta_i\) is almost certainly different from \(\beta_j\) for \(i \neq j\)) and \(X_t\) \(P\)-dimensional covariate vector (i.e., the \(t\)th row of \(X\), the design matrix).

To reduce the total number of parameters in the model (potentially \(P \times T\)) the \(\beta_t\) are parameterized with a weighted sum of \(J\) local latent variables (\(b_1,..b_j,..b_J\)). That is

where - *coefficient matrix* \(B\) has size \(T \times P\) with rows equal to the \(\beta_t\). - *knot matrix* \(b\) with size \(P\times J\); each entry is a latent variable \(b_{p, j}\). The \(b_j\) can be viewed as the “knots” from the perspective of spline regression and \(j\) is a time index such that \(t_j \in [1, \cdots, T]\). - *kernel matrix* \(K\) with size \(T\times J\) where the \(i\)th row and \(j\)th element can be viewed as
the normalized weight \(k(t_j, t) / \sum_{j=1}^{J} k(t_j, t)\)

For the level/trend,

It can also be viewed as a dynamic intercept (where the regressor is a vector of ones).

For the seasonality,

We use Fourier series to handle the seasonality; i.e., sin waves with varying periods are used for the columns of \(X_{ \text{seas}}\).

The details for the additional regressors are given in **Part II**, as they are not used in this tutorial. Note this includes different choices of kernel function (which determines the kernel matrix \(K\)) and prior for matrix \(b\).

## Data¶

To illustrate the usage of KTR, consider the daily series of electricity demand in Turkey from the 2000 - 2008.

```
[3]:
```

```
# from 2000-01-01 to 2008-12-31
df = load_electricity_demand()
date_col = 'date'
response_col = 'electricity'
df[response_col] = np.log(df[response_col])
print(df.shape)
df.head()
```

```
(3288, 2)
```

```
/var/folders/g0/77v1jc9s455cj14mkqcht0m00000gn/T/ipykernel_30865/982097363.py:2: UserWarning: Parsing '31/12/2008' in DD/MM/YYYY format. Provide format or specify infer_datetime_format=True for consistent parsing.
df = load_electricity_demand()
```

```
[3]:
```

date | electricity | |
---|---|---|

0 | 2000-01-01 | 9.43760 |

1 | 2000-01-02 | 9.50130 |

2 | 2000-01-03 | 9.63565 |

3 | 2000-01-04 | 9.65392 |

4 | 2000-01-05 | 9.66089 |

```
[4]:
```

```
print(f'starts with {df[date_col].min()}\nends with {df[date_col].max()}\nshape: {df.shape}')
```

```
starts with 2000-01-01 00:00:00
ends with 2008-12-31 00:00:00
shape: (3288, 2)
```

### Train / Test Split¶

Split the data into a training set and test set for model validation.

```
[5]:
```

```
test_size=365
train_df=df[:-test_size]
test_df=df[-test_size:]
```

## A Quick Start on KTR¶

Here the Similar to other model types in Orbit, KTR follows sklearn model API style. First an instance of the Orbit class `KTR`

is created. Second fit and predict methods are called for that instance. Note that unlike version `<=1.0.15`

, the fitting API arg are within the function; thus, `KTR`

is called directly.

```
[6]:
```

```
ktr = KTR(
response_col=response_col,
date_col=date_col,
seed=2021,
estimator='pyro-svi',
# bootstrap sampling to capture uncertainties
n_bootstrap_draws=1e4,
# pyro training config
num_steps=301,
message=100,
)
```

```
[7]:
```

```
ktr.fit(train_df)
```

```
INFO:orbit:Optimizing (PyStan) with algorithm: LBFGS.
INFO:orbit:Using SVI (Pyro) with steps: 301, samples: 100, learning rate: 0.1, learning_rate_total_decay: 1.0 and particles: 100.
INFO:root:Guessed max_plate_nesting = 1
INFO:orbit:step 0 loss = -1946.6, scale = 0.093118
INFO:orbit:step 100 loss = -3138.2, scale = 0.010067
INFO:orbit:step 200 loss = -3140.1, scale = 0.0098406
INFO:orbit:step 300 loss = -3140.5, scale = 0.0097046
```

```
[7]:
```

```
<orbit.forecaster.svi.SVIForecaster at 0x156a4ebb0>
```

We can take a look how the level is fitted with the data.

```
[8]:
```

```
predicted_df = ktr.predict(df=df)
predicted_df.head()
```

```
[8]:
```

date | prediction_5 | prediction | prediction_95 | |
---|---|---|---|---|

0 | 2000-01-01 | 9.42910 | 9.56391 | 9.70109 |

1 | 2000-01-02 | 9.42530 | 9.56144 | 9.69644 |

2 | 2000-01-03 | 9.42537 | 9.56209 | 9.69717 |

3 | 2000-01-04 | 9.42738 | 9.56233 | 9.69677 |

4 | 2000-01-05 | 9.42404 | 9.56193 | 9.69950 |

One can use `.get_posterior_samples()`

to extract the samples for all sampling parameters.

```
[9]:
```

```
ktr.get_posterior_samples().keys()
```

```
[9]:
```

```
dict_keys(['lev_knot', 'lev', 'yhat', 'obs_scale'])
```

```
[10]:
```

```
_ = plot_predicted_data(training_actual_df=train_df, predicted_df=predicted_df,
date_col=date_col, actual_col=response_col,
test_actual_df=test_df, markersize=20, lw=.5)
```

It can also be helpful to see the trend knot locations and levels. This is done with the `plot_lev_knots`

function.

```
[11]:
```

```
_ = ktr.plot_lev_knots()
```

## Fitting with Complex Seasonality¶

The previous model fit is not satisfactory as there is clear seasonality in the electrical demand time-series that is not accounted for. In this modelling example the electrical demand data is fit with a dual seasonality for weekly and yearly patterns. Since the data is daily, the seasonality periods are 7 and 365.25. These are added into the KTR object as a list through the `seasonality`

arg. Otherwise the process is the same as the previous example.

```
[12]:
```

```
ktr_with_seas = KTR(
response_col=response_col,
date_col=date_col,
seed=2021,
seasonality=[7, 365.25],
estimator='pyro-svi',
n_bootstrap_draws=1e4,
# pyro training config
num_steps=301,
message=100,
)
```

```
[ ]:
```

```
ktr_with_seas.fit(train_df)
```

```
INFO:orbit:Optimizing (PyStan) with algorithm: LBFGS.
INFO:orbit:Using SVI (Pyro) with steps: 301, samples: 100, learning rate: 0.1, learning_rate_total_decay: 1.0 and particles: 100.
INFO:root:Guessed max_plate_nesting = 1
INFO:orbit:step 0 loss = -2190.9, scale = 0.093667
INFO:orbit:step 100 loss = -4379.9, scale = 0.0069612
```

```
[ ]:
```

```
predicted_df = ktr_with_seas.predict(df=df, decompose=True)
```

```
[ ]:
```

```
predicted_df.head(5)
```

Tips: there is an additional arg `seasonality_fs_order`

to control the number of orders in `fourier series terms`

we want to approximate the seasonality. In general, they cannot violate the condition that \(2 \times \text{fourier series order} < \text{seasonality}\) since each order represents adding a pair of sine and cosine regressors.

## More Diagnostic and Visualization¶

Here are a few more diagnostic and visualization. The fit is decomposed into components, the local trend and both periods of seasonality.

```
[ ]:
```

```
_ = plot_predicted_data(training_actual_df=train_df, predicted_df=predicted_df,
date_col=date_col, actual_col=response_col,
test_actual_df=test_df, markersize=10, lw=.5)
```

```
[ ]:
```

```
_ = plot_predicted_components(predicted_df=predicted_df, date_col=date_col, plot_components=['trend', 'seasonality_7', 'seasonality_365.25'])
```

## References¶

Ng, Wang and Dai (2021) Bayesian Time Varying Coefficient Model with Applications to Marketing Mix Modeling, arXiv preprint arXiv:2106.03322

Hastie, Trevor and Tibshirani, Robert. (1990), Generalized Additive Models, New York: Chapman and Hall.

Wood, S. N. (2006), Generalized Additive Models: an introduction with R, Boca Raton: Chapman & Hall/CRC

Harvey, C. A. (1989). Forecasting, Structural Time Series and the Kalman Filter, Cambridge University Press.

Durbin, J., Koopman, S. J.. (2001). Time Series Analysis by State Space Methods, Oxford Statistical Science Series