Original link:tecdat.cn/?p=6762

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

 

For time series prediction, traditional models such as ARIMA are often a popular choice. While these models can prove to have a high degree of accuracy, they have one major drawback – they generally do not account for “shocks” or sudden changes in time series. Let’s see how we can solve this problem using a model called a Kalman filter.

 

The time series

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 look at an example.

When the Swiss National Bank decided to scrap the Franc in January 2015, money markets suffered one of the biggest shocks in history. As a result, the Swiss franc soared while other major currencies tumbled.

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

 

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 sequence and see what we have:

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 forecast may be more accurate than using the most recent forecast.

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 period 33.

Example: Sterling/DOLLAR

Thus, we have seen how the Kalman filter fits 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 oscillations, again indicating that the smoothing parameter α is fine for adjusting for large changes in the currency level.

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