{"id":19580,"date":"2019-09-26T11:20:11","date_gmt":"2019-09-26T15:20:11","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=19580"},"modified":"2022-11-21T09:44:17","modified_gmt":"2022-11-21T14:44:17","slug":"how-to-make-a-kalman-filter-in-r-for-pairs-trading","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\/","title":{"rendered":"How To Make A Kalman Filter in R for Pairs Trading"},"content":{"rendered":"\n<p><em><strong>Excerpt<\/strong><\/em><\/p>\n\n\n\n<p>The Kalman filter is a state space model for estimating an unknown (\u2018hidden\u2019) variable using observations of related variables and models of those relationships. The Kalman filter is underpinned by Bayesian probability theory and enables an estimate of the hidden variable in the presence of noise.<\/p>\n\n\n\n<p>There are plenty of tutorials online that describe the mathematics of the Kalman filter, so I won\u2019t repeat those here (<a href=\"https:\/\/www.bzarg.com\/p\/how-a-kalman-filter-works-in-pictures\/\" target=\"_blank\" rel=\"noreferrer noopener\">this article is a wonderful read<\/a>). Instead, I\u2019ll show you how to implement the Kalman filter framework to provide a&nbsp;<em><strong>dynamic estimate of the hedge ratio in a pairs trading strategy<\/strong><\/em>. I\u2019ll provide just enough math as is necessary to follow the implementation.<\/p>\n\n\n\n<p>To implement our Kalman filter, we need four variables:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>A vector of our observed variable<\/li><li>A vector of our hidden variable<\/li><li>A state transition model (which describes how the hidden variable evolves from one state to the next)<\/li><li>An observation model (a matrix of coefficients for the other variable \u2013 we use a hedge coefficient and an intercept)<\/li><\/ol>\n\n\n\n<p>For our hedge ratio\/pairs trading application, the observed variable is one of our price series&nbsp;p1&nbsp;and the hidden variable is our hedge ratio,&nbsp;\u03b2. The observed and hidden variables are related by the familiar spread equation:p1=\u03b2\u2217p2+\u03f5, where&nbsp;\u03f5&nbsp;is noise (in our pairs trading framework, we are essentially making bets on the mean reversion of&nbsp;\u03f5). In the Kalman framework, the other price series,&nbsp;p2&nbsp;provides our observation model.<\/p>\n\n\n\n<p>We also need to define a state transition model that describes the evolution of&nbsp;\u03b2&nbsp;from one time period to the next. If we assume that&nbsp;\u03b2&nbsp;follows a random walk, then our state transition model is simply \u03b2t=\u03b2t\u22121+\u03c9<\/p>\n\n\n\n<p><em>Visit Robot Wealth <\/em><a href=\"https:\/\/robotwealth.com\/kalman-filter-pairs-trading-r\/\"><em>website<\/em><\/a><em> to read the rest of the article, and to download the sample R code.<br><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kris Longmore shows  how to implement the Kalman filter framework in a trading example.<\/p>\n","protected":false},"author":271,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[343,338,350,341,344,342],"tags":[4272,487,4273],"contributors-categories":[13676],"class_list":{"0":"post-19580","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-programing-languages","7":"category-ibkr-quant-news","8":"category-quant-asia-pacific","9":"category-quant-development","10":"category-quant-regions","11":"category-r-development","12":"tag-kalman-filter","13":"tag-r","14":"tag-state-space-model","15":"contributors-categories-robot-wealth"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How To Make A Kalman Filter in R for Pairs Trading<\/title>\n<meta name=\"description\" content=\"Kris Longmore shows how to implement the Kalman filter framework in a trading example. He\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/19580\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Make A Kalman Filter in R for Pairs Trading | Quant Blog\" \/>\n<meta property=\"og:description\" content=\"Kris Longmore shows how to implement the Kalman filter framework in a trading example. He\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-26T15:20:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-21T14:44:17+00:00\" \/>\n<meta name=\"author\" content=\"Kris Longmore\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kris Longmore\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Kris Longmore\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/79c2a2775a70a4da1accf0068d731933\"\n\t            },\n\t            \"headline\": \"How To Make A Kalman Filter in R for Pairs Trading\",\n\t            \"datePublished\": \"2019-09-26T15:20:11+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:44:17+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\\\/\"\n\t            },\n\t            \"wordCount\": 320,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"keywords\": [\n\t                \"Kalman filter\",\n\t                \"R\",\n\t                \"State Space Model\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Programming Languages\",\n\t                \"Quant\",\n\t                \"Quant Asia Pacific\",\n\t                \"Quant Development\",\n\t                \"Quant Regions\",\n\t                \"R Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\\\/\",\n\t            \"name\": \"How To Make A Kalman Filter in R for Pairs Trading | Quant Blog\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"datePublished\": \"2019-09-26T15:20:11+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:44:17+00:00\",\n\t            \"description\": \"Kris Longmore shows how to implement the Kalman filter framework in a trading example. He\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/79c2a2775a70a4da1accf0068d731933\",\n\t            \"name\": \"Kris Longmore\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/krislongmore\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How To Make A Kalman Filter in R for Pairs Trading","description":"Kris Longmore shows how to implement the Kalman filter framework in a trading example. He","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/19580\/","og_locale":"en_US","og_type":"article","og_title":"How To Make A Kalman Filter in R for Pairs Trading | Quant Blog","og_description":"Kris Longmore shows how to implement the Kalman filter framework in a trading example. He","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\/","og_site_name":"IBKR Campus US","article_published_time":"2019-09-26T15:20:11+00:00","article_modified_time":"2022-11-21T14:44:17+00:00","author":"Kris Longmore","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kris Longmore","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\/"},"author":{"name":"Kris Longmore","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/79c2a2775a70a4da1accf0068d731933"},"headline":"How To Make A Kalman Filter in R for Pairs Trading","datePublished":"2019-09-26T15:20:11+00:00","dateModified":"2022-11-21T14:44:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\/"},"wordCount":320,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"keywords":["Kalman filter","R","State Space Model"],"articleSection":["Programming Languages","Quant","Quant Asia Pacific","Quant Development","Quant Regions","R Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\/","name":"How To Make A Kalman Filter in R for Pairs Trading | Quant Blog","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"datePublished":"2019-09-26T15:20:11+00:00","dateModified":"2022-11-21T14:44:17+00:00","description":"Kris Longmore shows how to implement the Kalman filter framework in a trading example. He","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-make-a-kalman-filter-in-r-for-pairs-trading\/"]}]},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/79c2a2775a70a4da1accf0068d731933","name":"Kris Longmore","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/krislongmore\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/19580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/271"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=19580"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/19580\/revisions"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=19580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=19580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=19580"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=19580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}