Original link:tecdat.cn/?p=6762

Original source:Tuo End number according to the tribe public number

 

1 Time Series

Time series refers to a series of numbers arranged by successive observations of the same phenomenon at different times. In statistics, a time series is the realization of a random process. According to its statistical characteristics, time series can be divided into stationary time series and non-stationary time series. Most of the sequences encountered in real life are not stationary.

Explanation: A sequence is stationary if its mean and variance are always constant. The unsteady time series should be transformed into stationary series before estimating the time series model. The stationarity of a time series can be judged by data graph and autocorrelation function graph. If the data graph shows a linear or quadratic trend shape, the time series is not stationary. If the autocorrelation function drops toward zero after the first few values, the sequence is stationary. If, after the first few values, the autocorrelation function does not decline toward 0, but decreases progressively, and the value is larger than the fixed random period, the sequence is not stationary.


2. State space model

As mentioned above, in real life, most data appear in non-stationary form, which involves the decomposition of time series formed by dynamic data. The purpose of time series decomposition is to estimate and extract the deterministic components Tt(long-term trend),St(seasonal term),Ct(cyclic term) so that residual It(random term) is a stationary process. Then, the suitable probability model of the random term is obtained, its properties are analyzed, and the purpose of fitting and forecasting is achieved together with Tt,St and Ct. The state space model can decompose the time series and separate Tt,St,Ct and It from the time series.


2.1 State space description of time series

Generally, a time series {YT} can be decomposed into the following addition model or multiplication model form directly or after function transformation:

 

Where, (Tt) is the long-term trend, (St) is the seasonal term, (Ct) is the cyclic term, and (It) is the irregular term in the table. Generally, the multiplication model is adopted for the series where the trend is obviously exponential growth and the seasonal fluctuation amplitude also increases with time.

For general dynamic system of constant (time-invariant), assuming it has the n dimensional direction finding {yt} {xt} and m d state vector, measure direction finding are variables can be observed through certain physical method, and the state vector is used to describe the system dynamic characteristic variable, generally cannot be observed variables, only state vector and measure direction finding, Only then can the dynamic characteristics of the system be described completely and adequately. The state space model can achieve this dynamic description.


2.2 State space model

The equation of state describes the change rule of the state of the dynamic system from the previous moment to the current moment, while the measurement equation describes the change relationship between the observed value and the state of the system. φ is called the state transition matrix, A is the measurement matrix, WT is the state noise, vt is the measurement noise (generally can be set as normal white noise).

Assuming that both A and φ are constant coefficient matrices that do not change with time, the state space has the following characteristics:

  • The state space model can not only reflect the internal state of the system, but also reveal the relationship between the internal state of the system and external input and output variables.
  • The state space model processes multiple variable time series into vector time series, and the transformation from variable to vector is more suitable for solving the modeling problem in the case of multiple input and output variables.
  • The state space model can describe the state of the system with the smallest information form of present and past, so it does not need a lot of historical data, which saves time and effort.

Relevant quantities in equations (3) and (4) can be decomposed into combined forms:

 

Subscripts j=1,2 and 3 correspond to trend term, cyclic term and seasonal term respectively. That is:

  • φ 1,A1,X1t and W1t all correspond to the trend term.
  • φ 2,A2,X2t,w2t all correspond to the cyclic term;
  • φ 3,A3,X3t and W3T all correspond to the seasonal term.
  • φ j,Aj,Xjt, and WJT can all be calculated from the corresponding model of the corresponding term, and they are all in matrix form.

As long as the trend term (trend difference fraction K), cyclic term (order P of cyclic term AR model) and seasonal term (order L, d) are determined, the corresponding state space model can be written conveniently. When k=2,p=2,d=1, examples are as follows:





If monthly data (L=12) is used, the dimension of state vector Xr in the above equation m=15; If quarterly data (L=4) is used, Xt is m= 7-dimensional vector. The corresponding noise variance matrix is as follows:

Equation (5) is the parameter to be estimated of the state space model, which is generally called hyperparameter. α2ϵ,α2η,α2ζ are the state noise variances of trend, cycle and season respectively,α1… αp is the autoregressive model parameter of the fitting cycle term.

These hyperparameters need to be obtained by maximum likelihood estimation or EM algorithm.

All that remains is to get the sequence of state vectors {Xt}. If {Xt} is estimated, the decomposition of the time series {yt} is completed. In this way, the non-stationary time series can be extrapolated, interpolated and smoothed by Kalman filtering and other methods to calculate the state vector at each moment.


3. R language state space model: Time series of Kalman filter KFAS modeling

Take the money markets. The pair could have an overall uptrend and then fall sharply during a sell-off. Traditional time series models do not necessarily solve this problem immediately, and it may take several periods before sudden changes in trends are allowed for.

Therefore, we wanted to use a time series model that could actually explain such shocks. Let’s look at a model called the Kalman filter.

Kalman filter is a state space model that can adjust shocks to time series more quickly. Let’s see how to use an example.

In January 2015, money markets suffered one of the biggest shocks in history when the Swiss National Bank decided to remove the Swiss franc from its currency. As a result, the Swiss franc soared while other major currencies tumbled.

Let’s see how the Kalman filter adjusts for this shock.

 

4 Kalman filter: USD/CHF

First, let’s download USD/CHF data for January 2015.


> currency = Quandl("FRED/DEXSZUS", start_date="2010-01-01",end_date="2018-09-29",type="xts")
> currency=data.frame(currency)
> currency=(log(currency$currency))
Copy the code

We will now attempt to model this time series using the Kalman filter using the KFAS library.

 

Draw time series:

> ts.plot(ts(exp(currency[1232:1274]), exp(out$a[1232:1274]), exp(out$att[1232:1274]), exp(out$alpha[1232:1274])
Copy the code

 

For comparison purposes, we will also calculate a 10-day moving average to compare the smoothness performance with that of the Kalman filter.

data.frame(SMA(exp(currency),n=10))
Copy the code

Now let’s combine that with our original series and see what we came up with:

Here is the generated data box:

In some cases, high-frequency data – or filtering to extract information from noise signals and predict future states – is the most suitable use for Kalman filters. Smoothing, on the other hand, relies more on past data because, in some cases, averaging the most recent measurements may be more accurate than using the most recent measurements.

This makes intuitive sense as the currency was trading at 0.9658 a month ago. In this respect, the smoothing estimator allows better signal prediction than using the filter estimator A, which does not adjust for shock until time period 33.

Example: Pound/dollar

Thus, we have seen how the Kalman filter ADAPTS to sudden movements in USD/CHF. Let’s take another example of a currency shock. When the UK voted for “Brexit” in June 2016, we saw sterling/DOLLAR subsequently tumble.

As in the USD/CHF example, we download our GBP/USD data from Quandl and run the Kalman filter:

This is our data graph. Similarly, we see alpha adjusting down to the 1.438 level the day before the shock at t = 22:

Here are a, ATT and alpha statistics:

Similarly, we see that the 10-day SMA takes nearly 10 days to fully adjust to the oscillation, again demonstrating that the smoothing parameter α still proves superior in adjusting for large changes in the currency level.

6 the conclusion

 

  • The importance of adjusting for time series shocks
  • How to use KFAS to implement Kalman filter in R
  • How to interpret the output of the Kalman filter
  • Why is kalman filter an appropriate model for modeling time series shocks

Most welcome insight

1. Use LSTM and PyTorch for time series prediction in Python

2. Long and short-term memory model LSTM is used in Python for time series prediction analysis

3. Time series (ARIMA, exponential smoothing) analysis using R language

4. R language multivariate Copula – Garch – model time series prediction

5. R language Copulas and financial time series cases

6. Use R language random wave model SV to process random fluctuations in time series

7. Tar threshold autoregressive model for R language time series

8. R language K-Shape time series clustering method for stock price time series clustering

Python3 uses ARIMA model for time series prediction