{"id":403,"date":"2021-11-15T13:53:47","date_gmt":"2021-11-15T05:53:47","guid":{"rendered":"https:\/\/fanyuzhao.com\/?p=403"},"modified":"2022-10-17T21:06:07","modified_gmt":"2022-10-17T13:06:07","slug":"kalman-filter","status":"publish","type":"post","link":"https:\/\/fanyuzhao.com\/?p=403","title":{"rendered":"Kalman Filter"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Definition<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>In statistics and control theory, Kalman filtering, also known as linear quadratic estimation (LQE), is an algorithm that uses a series of measurements observed over time, including statistical noise and other inaccuracies, and produces estimates of unknown variables that tend to be more accurate than those based on a single measurement alone, by estimating a joint probability distribution over the variables for each timeframe. The filter is named after Rudolf E. K\u00e1lm\u00e1n, who was one of the primary developers of its theory.<\/p><cite>Wikipedia<\/cite><\/blockquote>\n\n\n\n<p>During my study in Cambridge, Professor Oliver Linton introduced the Kalman Filter in Time Series analysis, but I did not get it at that time. So, here is a revisit.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">My Thinking of Kalman Filter<\/h4>\n\n\n\n<p>Kalman Filter is an algorithm that estimates optimal results from uncertain observation (e.g. Time Series Data. We know only the sample, but never know the true distribution of data or never know the true value when there are no errors).<\/p>\n\n\n\n<p>Consider the case, I need to know my weight, but the bodyweight scale cannot give me the true value. How can I know my true weight?<\/p>\n\n\n\n<p>Assume the bodyweight scale gives me error of 2, and my own estimate gives me error of 1. Or in another word, a weight scale is 1\/3 accurate, and my own estimation is 2\/3 accurate. Then, the optimal weight should be,<\/p>\n\n\n\n<p>$$ Optimal Result = \\frac{1}{3}\\times Measurement + \\frac{2}{3}\\times Estimate  $$<\/p>\n\n\n\n<p>, where \\( Measurement\\) means the measurement value, and \\(Estimate\\) means the estimated value. We conduct the following transformation.<\/p>\n\n\n\n<p>$$ Optimal Result = \\frac{1}{3}\\times Measurement +Esimate- \\frac{1}{3}\\times Estimate   $$<\/p>\n\n\n\n<p> <span class=\"katex math multi-line\">Optimal Result = Esimate+\\frac{1}{3}\\times Measurement &#8211; \\frac{1}{3}\\times Estimate<\/span> <\/p>\n\n\n\n<p> <span class=\"katex math multi-line\">Optimal Result = Esimate+\\frac{1}{3}\\times (Measurement &#8211;  Estimate)<\/span>  <\/p>\n\n\n\n<p>Therefore, we can get<\/p>\n\n\n\n<p>  <span class=\"katex math multi-line\">Optimal Result = Esimate+\\frac{p}{p+r}\\times (Measurement &#8211;  Estimate)<\/span>   <\/p>\n\n\n\n<p>, where \\(p\\) is the estimation error and \\(r\\) is the measurement error.<\/p>\n\n\n\n<p>For example, if the estimation error is zero, then the fraction is equal to zero. Thus, the optimal result is just the estimate.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Applying Time Series Data<\/h4>\n\n\n\n<p>$$ Optimal Result_n=\\frac{1}{n}\\times (meas_1+meas_2+meas_3+&#8230;+meas_{n}) $$<\/p>\n\n\n\n<p> <span class=\"katex math multi-line\">Optimal Result_n=\\frac{1}{n}\\times (meas_1+meas_2+meas_3+&#8230;+meas_{n-1})\\\\ +\\frac{1}{n}\\times meas_n<\/span> <\/p>\n\n\n\n<p>  <span class=\"katex math multi-line\">Optimal Result_n=\\frac{n-1}{n}\\times \\frac{1}{n-1}\\times (meas_1+&#8230;+meas_{n-1})\\\\ +\\frac{1}{n}\\times meas_n<\/span>  <\/p>\n\n\n\n<p>Iterating the first term because\\(  \\frac{1}{n-1}\\times (meas_1+&#8230;+meas_{n-1}) = Optimal Result_{n-1} \\),<\/p>\n\n\n\n<p>   <span class=\"katex math multi-line\">Optimal Result_n=\\frac{n-1}{n}\\times Optimal Result_{n-1}\\\\ +\\frac{1}{n}\\times meas_n<\/span>   <\/p>\n\n\n\n<p>   <span class=\"katex math multi-line\">Optimal Result_n=Optimal Result_{n-1}\\\\ -\\frac{1}{n}\\times Optimal Result_{n-1} +\\frac{1}{n}\\times meas_n<\/span>   <\/p>\n\n\n\n<p>   <span class=\"katex math multi-line\">OResult_n=OResult_{n-1}+\\frac{1}{n}\\times (meas_n-OResult_{n-1})<\/span>   <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Kalman Filter Equation<\/h4>\n\n\n\n<p>$$ \\hat{x}_{n,n}=\\hat{x}_{n,n-1}+K_n(z_n-\\hat{x}_{n,n-1}) $$<\/p>\n\n\n\n<p>$$ K_n=\\frac{p_{n,n-1}}{p_{n.n-1}+r_n}  $$<\/p>\n\n\n\n<p>, where \\(p_{n,n-1}\\) is Uncertainty in Estimate, \\(r_n\\) is Uncertainty in Measurement, \\(\\hat{x}_{n,n}\\) is the Optimal Estimate at \\(n\\), and \\(z_n\\) is the Measurement Value at \\(n\\).<\/p>\n\n\n\n<p>The Optimal Estimate is updated by the estimate uncertainty through a Covariance Update Equation,<\/p>\n\n\n\n<p>$$ p_{n,n}=(1-K_n)p_{n,n-1} $$<\/p>\n\n\n\n<p class=\"has-text-color has-normal-font-size\" style=\"color:#808080\">In a more intuitive way (1),<\/p>\n\n\n\n<p class=\"has-text-color has-normal-font-size\" style=\"color:#808080\">$$ OEstimate_n=OEstimate_{n-1}+K_n (meas_n-OEstimate_{n-1})$$<\/p>\n\n\n\n<p class=\"has-text-color has-normal-font-size\" style=\"color:#808080\">$$ K_n=\\frac{OEstimateError_{n-1}}{OEstimateError_{n-1}+MeausreError_n}$$<\/p>\n\n\n\n<p class=\"has-text-color has-normal-font-size\" style=\"color:#808080\">$$OEstimateError_{n-1}=(1-K_{n-1})\\times OEstimateError_{n-2}$$<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/fanyuzhao.com\/wp-content\/uploads\/2021\/11\/Kalman-Filter.xlsx\">Kalman-Filter<\/a><a href=\"https:\/\/fanyuzhao.com\/wp-content\/uploads\/2021\/11\/Kalman-Filter.xlsx\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>num<\/td><td>Meas<\/td><td>MeasError<\/td><td>K<\/td><td>OEstimate<\/td><td>OEstimateError<\/td><\/tr><tr><td>0<\/td><td><\/td><td><\/td><td><\/td><td>75<\/td><td>5<\/td><\/tr><tr><td>1<\/td><td>81<\/td><td>3<\/td><td>0.625<\/td><td>78.75<\/td><td>1.875<\/td><\/tr><tr><td>2<\/td><td>83<\/td><td>3<\/td><td>0.384615<\/td><td>80.38462<\/td><td>1.153846<\/td><\/tr><tr><td>3<\/td><td>79<\/td><td>3<\/td><td>0.277778<\/td><td>80<\/td><td>0.833333<\/td><\/tr><tr><td>4<\/td><td>78<\/td><td>3<\/td><td>0.217391<\/td><td>79.56522<\/td><td>0.652174<\/td><\/tr><tr><td>5<\/td><td>81<\/td><td>3<\/td><td>0.178571<\/td><td>79.82143<\/td><td>0.535714<\/td><\/tr><tr><td>6<\/td><td>79<\/td><td>3<\/td><td>0.151515<\/td><td>79.69697<\/td><td>0.454545<\/td><\/tr><tr><td>7<\/td><td>80<\/td><td>3<\/td><td>0.131579<\/td><td>79.73684<\/td><td>0.394737<\/td><\/tr><tr><td>8<\/td><td>78<\/td><td>3<\/td><td>0.116279<\/td><td>79.53488<\/td><td>0.348837<\/td><\/tr><tr><td>9<\/td><td>81<\/td><td>3<\/td><td>0.104167<\/td><td>79.6875<\/td><td>0.3125<\/td><\/tr><tr><td>10<\/td><td>79<\/td><td>3<\/td><td>0.09434<\/td><td>79.62264<\/td><td>0.283019<\/td><\/tr><tr><td>11<\/td><td>80<\/td><td>3<\/td><td>0.086207<\/td><td>79.65517<\/td><td>0.258621<\/td><\/tr><tr><td>12<\/td><td>78<\/td><td>3<\/td><td>0.079365<\/td><td>79.52381<\/td><td>0.238095<\/td><\/tr><tr><td>13<\/td><td>81<\/td><td>3<\/td><td>0.073529<\/td><td>79.63235<\/td><td>0.220588<\/td><\/tr><tr><td>14<\/td><td>79<\/td><td>3<\/td><td>0.068493<\/td><td>79.58904<\/td><td>0.205479<\/td><\/tr><tr><td>15<\/td><td>82<\/td><td>3<\/td><td>0.064103<\/td><td>79.74359<\/td><td>0.192308<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"481\" height=\"288\" src=\"https:\/\/fanyuzhao.com\/wp-content\/uploads\/2021\/11\/image-4.png\" alt=\"\" class=\"wp-image-493\" srcset=\"http:\/\/fanyuzhao.com\/wp-content\/uploads\/2021\/11\/image-4.png 481w, http:\/\/fanyuzhao.com\/wp-content\/uploads\/2021\/11\/image-4-300x180.png 300w\" sizes=\"(max-width: 481px) 100vw, 481px\" \/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">A Senior Study<\/h4>\n\n\n\n<p><strong>Estimation Equation:<\/strong><\/p>\n\n\n\n<p>$$ \\hat{x}_k^-=A\\hat{x}_{k-1}+Bu_k $$<\/p>\n\n\n\n<p>$$ P_k^-=AP_{k-1}A^T+Q$$<\/p>\n\n\n\n<p><strong>Update Equation (same as the one I just introduced in (1)):<\/strong><\/p>\n\n\n\n<p>$$K_k=\\frac{P_k^- C^T}{CP_k^-C^T+R}$$<\/p>\n\n\n\n<p>$$ \\hat{x}_k^-=A\\hat{x}_{k-1}+K_k(y_k-C\\hat{x}_k^-) $$<\/p>\n\n\n\n<p>$$ P_k=(1-K_kC)P_k^-$$<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"586\" height=\"349\" src=\"https:\/\/fanyuzhao.com\/wp-content\/uploads\/2021\/11\/image-5.png\" alt=\"\" class=\"wp-image-505\" srcset=\"http:\/\/fanyuzhao.com\/wp-content\/uploads\/2021\/11\/image-5.png 586w, http:\/\/fanyuzhao.com\/wp-content\/uploads\/2021\/11\/image-5-300x179.png 300w\" sizes=\"(max-width: 586px) 100vw, 586px\" \/><\/figure>\n\n\n\n<p>Intuitively, I need \\( \\hat{x}_{k-1}\\) (, which is the weight last week) to calculate the optimal estimate weight this week \\(\\hat{x}_k\\). Firstly, I estimate the weights this week \\(\\hat{x}_k^-\\) and measure the weight this week \\(y_k\\). Then, combine them to get the optimal estimate weights this week.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Reading<\/h4>\n\n\n\n<p>The application of the Kalman Filter could be found in the following reading. Also, I will continue in my further study.<\/p>\n\n\n\n<p><a href=\"https:\/\/towardsdatascience.com\/state-space-model-and-kalman-filter-for-time-series-prediction-basic-structural-dynamic-linear-2421d7b49fa6\">https:\/\/towardsdatascience.com\/state-space-model-and-kalman-filter-for-time-series-prediction-basic-structural-dynamic-linear-2421d7b49fa6<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Reference<\/h4>\n\n\n\n<p><a href=\"https:\/\/www.kalmanfilter.net\/kalman1d.html\">https:\/\/www.kalmanfilter.net\/kalman1d.html<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.bilibili.com\/video\/BV1aS4y197bT?share_source=copy_web\">https:\/\/www.bilibili.com\/video\/BV1aS4y197bT?share_source=copy_web<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Definition In statistics and control theory, Kalman filtering, also known as linear quadratic estimation (LQE), is an algorithm that uses a series of measurements observed over time, including statistical noise and other inaccuracies, and produces estimates of unknown variables that tend to be more accurate than those based on a single measurement alone, by estimating &hellip; <a href=\"https:\/\/fanyuzhao.com\/?p=403\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Kalman Filter<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,6,8,18],"tags":[],"_links":{"self":[{"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=\/wp\/v2\/posts\/403"}],"collection":[{"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=403"}],"version-history":[{"count":113,"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=\/wp\/v2\/posts\/403\/revisions"}],"predecessor-version":[{"id":520,"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=\/wp\/v2\/posts\/403\/revisions\/520"}],"wp:attachment":[{"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}