# Kalman Filter: Modelling Time Series Shocks with KFAS in R

Kalman Filter: Modelling Time Series Shocks with KFAS in RMichael Grogan (MGCodesandStats)BlockedUnblockFollowFollowingFeb 19When it comes to time series forecasts, conventional models such as ARIMA are often a popular option.

While these models can prove to have high degrees of accuracy, they have one major shortcoming — they do not typically account for “shocks”, or sudden changes in a time series.

Let’s see how we can potentially alleviate this problem using a model known as the Kalman Filter.

Time Series ShocksLet’s take the currency market as an example.

An currency pair could have an overall upward trend, and then spike sharply downwards during a sell-off.

A conventional time series model wouldn’t necessarily account for this right away, and it would likely take several periods into the future before the sudden change in trend would be taken into account.

Therefore, we wish to use a time series model that is indeed capable of accounting for such shocks.

Let’s take a look at a handy model known as the Kalman Filter.

The Kalman Filter is a state-space model that adjusts more quickly for shocks to a time series.

Let’s see how this works using an example.

In January 2015, currency markets underwent one of the biggest shocks ever endured, when the Swiss National Bank decided to depeg the Swiss franc from the euro.

As a result, the Swiss franc soared in value while other major currencies plummeted.

Let’s see how the Kalman Filter adjusts for such a shock.

Date, as.

Date.

numeric> currency = Quandl("FRED/DEXSZUS", start_date="2010-01-01",end_date="2018-09-29",type="xts")> currency=data.

frame(currency)> currency=(log(currency\$currency))We are converting the currency data into a data frame, and then converting into log format to structure our time series in terms of returns.

Now, we will attempt to model this time series with the Kalman Filter using the KFAS library.

> #Kalman Filter> library(KFAS)Warning message:package ‘KFAS’ was built under R version 3.

5.

1 > logmodel <- SSModel(currency ~ SSMtrend(1, Q = 0.

01), H = 0.

01)> out <- KFS(logmodel)> ts.

plot(ts(exp(currency[1232:1274]), exp(out\$a[1232:1274]), exp(out\$att[1232:1274]), exp(out\$alpha[1232:1274])> title("USD/CHF")Let’s go through the above.

SSModel denotes “state space model”, and observe that we are regressing the USD/CHF time series against the SSMtrend, which denotes our smoothed estimates, or state predictions one-step ahead of that of the actual series.

Q and H denote our unconstrained time-invariant covariance estimates.

The steps to estimate these can be quite complex, so for our purposes I’m going to set these to a default value of 0.

01.

When we plot our time series, here is what we come up with:We can see that our a, att, and alpha series are adjusting to the shock instantaneously.

a: One-step-ahead predictions of statesatt: Filtered estimates of statesalphahat: Smoothed estimates of statesFor comparison purposes, we will also compute a 10-day moving average to compare smoothing performance with that of the Kalman Filter.

library(TTR)sma10=data.

frame(SMA(exp(currency),n=10))col_headings<-c("sma10")names(sma10)<-col_headingsView(sma10)Let’s now combine the above into a data frame along with our original series and see what we come up with:> df<-data.

frame(exp(currency[1232:1274]), exp(out\$a[1232:1274]), exp(out\$att[1232:1274]), exp(out\$alpha[1232:1274]),sma10\$sma10[1232:1274])> View(df)> col_headings<-c("currency","a","att","alpha","sma10")> names(df)<-col_headings> View(df)Here is the data frame generated:As elaborated in this post on Quora, there are some instances in which high-frequency data — or filtering to extract information from a noisy signal and predict the future state, is the most appropriate use of the Kalman Filter.

On the other hand, smoothing relies more on past data, since there are some instances where averaging recent measurements may prove more accurate than using the most recent one.

In this particular instance, we can see that at time period 31 (which was one day before the sharp fall in the USD/CHF), the value of alpha (the smoothing estimate of state) dropped significantly from 1.

002 to 0.

97, and then down to 0.

915 on the day of the actual shock.

This makes sense intuitively, as the currency was trading at a level of 0.

9658 roughly one month prior.

In this regard, the smoothing estimator allowed for a better prediction of the signal than using the filtering estimate a, which did not adjust for the shock until time period 33.

Additionally, notice that a 10-day SMA is also being used.

Even though alpha is being used as a smoother, we see that alpha still adjusted for the shock much quicker than the simple moving average — meaning that the Kalman Filter has given a better prediction than simply using a basic smoothing technique.

Another Example: GBP/USDSo, we’ve seen how the Kalman Filter adjusted to the sudden movement in the USD/CHF.

Let’s take another example of a currency shock.

When Britain voted for “Brexit” in June 2016, we saw the GBP/USD subsequently plunge.

How well would the Kalman Filter have modelled this?.Let’s find out!As in the example of USD/CHF, we download our GBP/USD data from Quandl and run the Kalman Filter:require(Quandl)library(KFAS)gbpusd = Quandl("FRED/DEXUSUK", start_date="2016-01-01",end_date="2016-12-31",type="xts")gbpusd=data.

frame(gbpusd)gbpusd=(log(gbpusd\$gbpusd))logmodel <- SSModel(gbpusd ~ SSMtrend(1, Q = 0.

01), H = 0.

01)out <- KFS(logmodel)df<-data.

plot(ts(exp(currency[100:150])), exp(out\$a[100:150]), exp(out\$att[100:150]), exp(out\$alpha[100:150]), col = 1:4)title("GBP/USD")Here is a plot of our data.

Again, we see that alpha adjusts downwards to a level of 1.

438 one day before the shock at t=22:Here are the a, att, and alpha statistics:Again, we see that the 10-day SMA takes nearly 10 days to adjust fully for the shock, indicating once again that the smoothing parameter alpha has still proven superior in adjusting for the large change in the currency level.

ConclusionIn this tutorial, you have learned:Importance of adjusting for time series shocksHow to implement a Kalman Filter using KFAS in RHow to interpret output from a Kalman FilterWhy the Kalman Filter is a suitable model for modelling time-series shocksMany thanks for reading this tutorial, and please leave any questions you may have in the comments below.

You can also find the original post along with further machine learning content at michaeljgrogan.

com.

.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8677476,"math_prob":0.93829024,"size":6984,"snap":"2019-51-2020-05","text_gpt3_token_len":1766,"char_repetition_ratio":0.12550144,"word_repetition_ratio":0.018036073,"special_character_ratio":0.25329325,"punctuation_ratio":0.12870571,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96624094,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-11T01:19:59Z\",\"WARC-Record-ID\":\"<urn:uuid:14be4320-e387-4add-9a8d-64e4669aefbb>\",\"Content-Length\":\"37361\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2e8f6df7-ffb3-4b6b-aaa4-b8ef6ea509a2>\",\"WARC-Concurrent-To\":\"<urn:uuid:1729f0cb-e145-4a91-bb93-8bfe2e99bc65>\",\"WARC-IP-Address\":\"78.47.26.210\",\"WARC-Target-URI\":\"http://datascience.sharerecipe.net/2019/02/20/kalman-filter-modelling-time-series-shocks-with-kfas-in-r/\",\"WARC-Payload-Digest\":\"sha1:OJJJIGT3JFLOP2O4LHLEGLURJZQCGBX2\",\"WARC-Block-Digest\":\"sha1:UEGBBJA2SXQRDIRZ4S6UQOCIU65JQEOM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540529516.84_warc_CC-MAIN-20191210233444-20191211021444-00005.warc.gz\"}"}