Repository navigation
Expand file tree
/
Copy path06-output-feedback.Rmd
More file actions
1962 lines (1702 loc) · 103 KB
/
Copy path06-output-feedback.Rmd
File metadata and controls
1962 lines (1702 loc) · 103 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Output Feedback {#output-feedback}
We have presented many algorithms for optimal control of dynamical systems. These algorithms, however, have a strict assumption, i.e., the state of the system is directly available. These controllers are called _state-feedback_ controllers.
In many applications, the state of the system is not readily available but measured via sensors. Therefore, a more realistic model of the dynamical system is
\begin{equation}
\begin{split}
\dot{x} &= f(x,u) + w \\
y &= h(x,u) + v
\end{split}
(\#eq:output-feedback-system)
\end{equation}
where $x(t) \in \mathbb{X} \subseteq \mathbb{R}^n$ the state of the system, $u(t) \in \mathbb{U} \subseteq \mathbb{R}^m$ the control (or input), $y(t) \in \mathbb{Y} \subseteq \mathbb{R}^{d}$ the output (i.e., measurement) of the state and control, and $f,g$ the evolution and measurement functions (which are sufficiently smooth). $w(t)$ and $v(t)$ denote disturbance and measurement noise, respectively. The discrete-time analog of \@ref(eq:output-feedback-system) is
\begin{equation}
\begin{split}
x_{k+1} &= f(x_k,u_k) + w_k \\
y_k &= h(x_k,u_k) + v_k
\end{split},
(\#eq:output-feedback-system-discrete-time)
\end{equation}
with $k$ denoting the discrete timestep. When faced with systems like \@ref(eq:output-feedback-system) and \@ref(eq:output-feedback-system-discrete-time), the controller typically needs to use the history of all measurements, also known as _output feedback_
$$
u(t) = \pi(\{y(\tau)\}_{\tau=0}^t), \quad u_k = \pi (\{y_0,\dots,y_k\}).
$$
## Least-Squares Estimation
Suppose we are given two random variables $x \in \mathbb{R}^n$ and $y \in \mathbb{R}^m$, where $y$ is a measurement that provides some information about the model $x$. We are interested in finding an _estimator_, i.e., a function $x(y)$, that minimizes
$$
\mathbb{E}_{x,y} \left\{ \Vert x - x(y) \Vert^2 \right\}.
$$
The optimal estimator, $x^\star(y)$, is called a _least-squares estimator_:
$$
x^\star(y) = \arg\min_{x(\cdot)} \mathbb{E}_{x,y} \left\{ \Vert x - x(y) \Vert^2 \right\}.
$$
Note that $x^\star(y)$ is a function with input $y$.
Since
\begin{equation}
\mathbb{E}_{x,y}\left\{ \Vert x - x(y) \Vert^2 \right\} = \mathbb{E}_y \left\{ \mathbb{E}_x \left\{ \Vert x - x(y) \Vert^2 \mid y\right\} \right\},
(\#eq:expand-conditional-expectation)
\end{equation}
it is clear that $x^\star(y)$ is a least-squares estimator if $x^\star(y)$ minimizes the conditional expectation in \@ref(eq:expand-conditional-expectation) for every $y \in \mathbb{R}^m$, that is,
\begin{equation}
x^\star(y) = \arg\min_{z \in \mathbb{R}^n} \mathbb{E}_x \left\{ \Vert x - z \Vert^2 \mid y \right\}, \quad \forall y \in \mathbb{R}^m.
(\#eq:rewrite-least-squares-conditional)
\end{equation}
With this observation, we have the following proposition.
::: {.theorembox}
::: {.proposition #least-squares-estimator name="Least Squares Estimator"}
The least squares estimator $x^\star(y)$ is given by
\begin{equation}
x^\star(y) = \mathbb{E}_x \left\{ x \mid y \right\}, \quad \forall y \in \mathbb{R}^m,
(\#eq:conditional-mean)
\end{equation}
i.e., $x^\star(y)$ returns the conditional mean of $x$ given $y$.
:::
:::
::: {.proofbox}
::: {.proof}
We expand the objective in \@ref(eq:rewrite-least-squares-conditional)
$$
\mathbb{E}_x \left\{ \Vert x - z \Vert^2 \mid y \right\} = \mathbb{E}_x \left\{ \Vert x \Vert^2 \mid y \right\} - 2 z^T \mathbb{E}_x \left\{x \mid y \right\} + \Vert z \Vert^2.
$$
Observe that problem \@ref(eq:rewrite-least-squares-conditional) is an unconstrained quadratic optimization, whose optimal solution can be obtained by setting the gradient of objective (w.r.t. $z$) to zero:
$$
z = \mathbb{E}_x \left\{ x \mid y \right\},
$$
concluding the proof.
:::
:::
Depending on the joint distribution of $(x,y)$, the least-squares estimator can be very complicated (due to $\mathbb{E}_x\left\{ x \mid y\right\}$ being complicated) and in general does not admit a clean solution. We now turn to the family of linear estimators.
### Linear Least-Squares Estimation
A linear estimator $x(y)$ takes the following form
$$
x(y) = Ay + b, \quad A \in \mathbb{R}^{n \times m}, b \in \mathbb{R}^n.
$$
The optimal linear estimator
\begin{equation}
(A^\star, b^\star) = \arg\min_{A,b} \mathbb{E}_{x,y} \left\{ \Vert x - Ay - b \Vert^2 \right\}
(\#eq:linear-least-squares-estimator)
\end{equation}
is called a _linear least-squares estimator_.
In the special case where $x$ and $y$ are jointly Gaussian random variables, the conditional expectation $\mathbb{E}_{x} \{x \mid y \}$ is indeed linear, and hence a linear least-squares estimator is also the true least-squares estimator.
::: {.theorembox}
::: {.proposition #linear-least-squares-estimator name="Linear Least-Squares Estimator"}
If the random variables $(x,y)$ are jointly Gaussian, then the conditional expectation $\mathbb{E}_x \{x \mid y \}$ is linear in $y$. Therefore, the linear least-squares estimator \@ref(eq:linear-least-squares-estimator) is also the least-squares estimator.
:::
:::
Now let us turn to characterize the linear least-squares estimator.
::: {.theorembox}
::: {.proposition #linear-least-squares-solution name="Solution of Linear Least-Squares Estimator"}
Let $x,y$ be random variables in $\mathbb{R}^n$ and $\mathbb{R}^m$ drawn from some underlying distribution. The means and covariances of $x$ and $y$ are given by
\begin{equation}
\begin{split}
\mathbb{E}\{ x\} = \bar{x}, \quad \mathbb{E} \{ y \} = \bar{y}. \\
\mathbb{E}\{ (x - \bar{x})(x - \bar{x})^T \} = \Sigma_{xx}, \quad \mathbb{E}\{ (y - \bar{y})(y-\bar{y})^T \} = \Sigma_{yy}, \\
\mathbb{E}\{ (x - \bar{x})(y - \bar{y})^T \} = \Sigma_{xy}, \quad \mathbb{E}\{ (y - \bar{y})(x - \bar{x})^T \} = \Sigma^T_{xy},
\end{split}
\end{equation}
and assume $\Sigma_{yy}$ is invertible (positive definite). Then the linear least-squares estimator $x^\star(y)$, i.e., solution of \@ref(eq:linear-least-squares-estimator), is given by
\begin{equation}
x^\star(y) = \bar{x} + \Sigma_{xy} \Sigma^{-1}_{yy} (y - \bar{y}).
(\#eq:linear-least-squares-solution)
\end{equation}
The corresponding error covariance matrix is given by
\begin{equation}
\mathbb{E}_{x,y} \left\{ (x - x^\star(y))(x - x^\star(y))^T \right\} = \Sigma_{xx} - \Sigma_{xy} \Sigma^{-1}_{yy} \Sigma^T_{xy}.
(\#eq:linear-least-squares-solution-covariance)
\end{equation}
:::
:::
::: {.proofbox}
::: {.proof}
Problem \@ref(eq:linear-least-squares-estimator) is an unconstrained quadratic optimization in $(A,b)$. Therefore, its optimal solution can be computed by taking the derivative of the objective, i.e., $f(A,b) = \mathbb{E}_{x,y} \{ \Vert x - Ay - b \Vert^2 \}$ , w.r.t. $(A,b)$ and setting them to zero
\begin{equation}
\begin{split}
0 = \frac{\partial f}{\partial A} = 2 \mathbb{E}_{x,y} \{ (b + Ay -x) y^T \}, \\
0 = \frac{\partial f}{\partial b} = 2 \mathbb{E}_{x,y} \{ b + Ay - x \}.
\end{split}
(\#eq:linear-least-squares-derivative-zero)
\end{equation}
The second equation gives us the solution of $b^\star$
\begin{equation}
b^\star = \bar{x} - A \bar{y}.
(\#eq:linear-least-squares-proof-solution-b)
\end{equation}
Plug the solution \@ref(eq:linear-least-squares-proof-solution-b) back into the first equation in \@ref(eq:linear-least-squares-derivative-zero), we obtain
\begin{equation}
\mathbb{E}_{x,y} \{ (A(y - \bar{y}) - (x - \bar{x})) y^T\} = 0.
(\#eq:linear-least-squares-proof-simplify-first-equation)
\end{equation}
On the other hand, we have
\begin{equation}
\left( \mathbb{E}_{x,y} \{ A (y - \bar{y}) - (x - \bar{x})\} \right) \bar{y}^T = 0.
(\#eq:linear-least-squares-proof-simplify-first-equation-other)
\end{equation}
Subtracting \@ref(eq:linear-least-squares-proof-simplify-first-equation-other) from \@ref(eq:linear-least-squares-proof-simplify-first-equation), we have
$$
\mathbb{E}_{x,y} \left\{ (A(y-\bar{y}) - (x - \bar{x})) (y - \bar{y})^T \right\} = 0,
$$
which is exactly
$$
A \Sigma_{yy} - \Sigma_{xy} = 0.
$$
Therefore, we obtain the optimal $A$
\begin{equation}
A^\star = \Sigma_{xy} \Sigma^{-1}_{yy}.
(\#eq:linear-least-squares-proof-solution-A)
\end{equation}
Combining \@ref(eq:linear-least-squares-proof-solution-A) and \@ref(eq:linear-least-squares-proof-solution-b), we get the optimal linear least-squares estimator
$$
x^\star(y) = \Sigma_{xy} \Sigma^{-1}_{yy} y + \left( \bar{x} - \Sigma_{xy} \Sigma^{-1}_{yy} \bar{y} \right) = \bar{x} + \Sigma_{xy} \Sigma^{-1}_{yy} (y - \bar{y}).
$$
The error covariance \@ref(eq:linear-least-squares-solution-covariance) follows by direct computation.
:::
:::
We next list a few useful properties of the linear least-squares estimator.
::: {.theorembox}
::: {.corollary #properties-linear-least-squares-estimator name="Properties of the Linear Least-Squares Estimator"}
The linear least-squares estimator $x^\star(y)$ in \@ref(eq:linear-least-squares-solution) is unbiased, i.e.,
$$
\mathbb{E}_y \{ x^\star(y) \} = \bar{x}.
$$
The estimation error $x - x^\star(y)$ is uncorrelated with both $y$ and $x^\star(y)$, i.e.,
\begin{equation}
\begin{split}
\mathbb{E}_{x,y} \{ (x - x^\star(y))y^T \} = 0. \\
\mathbb{E}_{x,y} \{ (x - x^\star(y)) (x^\star(y))^T \} = 0.
\end{split}
(\#eq:linear-least-squares-orthogonal-projection)
\end{equation}
:::
:::
Equation \@ref(eq:linear-least-squares-orthogonal-projection) is known as the _orthogonal projection principle_.
The next corollary considers the linear least-squares estimator of a new variable $z$ that is a linear function of $x$.
::: {.theorembox}
::: {.corollary #linear-least-squares-z}
Consider in addition to $x$ and $y$, the random vector $z$ defined by
$$
z = Cx,
$$
where $C \in \mathbb{R}^{p \times n}$ is given. Then the linear least-squares estimator of $z$ given $y$ is
$$
z^\star(y) = C x^\star(y),
$$
and the corresponding error covariance matrix is
$$
\mathbb{E}_{z,y}\left\{ (z - z^\star(y))(z - z^\star(y))^T \right\} = C \mathbb{E}_{x,y} \left\{ (x - x^\star(y))(x - x^\star(y))^T \right\} C^T,
$$
where $\mathbb{E}_{x,y} \left\{ (x - x^\star(y))(x - x^\star(y))^T \right\}$ is given in \@ref(eq:linear-least-squares-solution-covariance).
:::
:::
::: {.proofbox}
::: {.proof}
Since $z = Cx$, we have $\mathbb{E} \{ z \} = C \bar{x} = \bar{z}$, and
$$
\Sigma_{zz} = \mathbb{E} \{ (z - \bar{z})(z - \bar{z})^T \} = C \Sigma_{xx} C^T, \\
\Sigma_{zy} = \mathbb{E} \{ (z - \bar{z})(y - \bar{y})^T \} = C \Sigma_{xy}, \\
\Sigma_{yz} = \mathbb{E} \{ (y - \bar{y}) (z - \bar{z})^T \} = \Sigma^T_{xy} C^T.
$$
The result then follows by applying Proposition \@ref(prp:linear-least-squares-solution) to $z$ and $y$.
:::
:::
The next corollary considers the linear least-squares estimator of $x$ given $z$, a random vector that is a linear function of $y$.
::: {.theorembox}
::: {.corollary #linear-least-squares-x-z}
Consider in addition to $x$ and $y$, an additional random variable $z$
$$
z = C y + u,
$$
where $C \in \mathbb{R}^{p \times m}$, with rank $p$, and $u \in \mathbb{R}^p$ are given. Then the linear least-squares estimator of $x$ given $z$ is
$$
x^\star(z) = \bar{x} + \Sigma_{xy} C^T (C \Sigma_{yy} C^T)^{-1} (z - C \bar{y} - u),
$$
and the corresponding error covariance matrix is
$$
\mathbb{E}_{x,z}\left\{ (x - x^\star(z))(x - x^\star(z))^T \right\} = \Sigma_{xx} - \Sigma_{xy} C^T (C \Sigma_{yy} C^T)^{-1} C \Sigma_{xy}^T.
$$
:::
:::
::: {.proofbox}
::: {.proof}
Since $z = Cy + u$, we have $\bar{z} = \mathbb{E}\{ z \} = C \bar{y} + u$, and
$$
\Sigma_{zz} = \mathbb{E}\{ (z - \bar{z})(z - \bar{z})^T \} = C \Sigma_{yy} C^T, \\
\Sigma_{zx} = \mathbb{E}\{ (z - \bar{z})(x - \bar{x})^T \} = C \Sigma_{xy}^T, \\
\Sigma_{xz} = \mathbb{E}\{ (x - \bar{x})(z - \bar{z})^T \} = \Sigma_{xy}C^T.
$$
The result then follows by applying Proposition \@ref(prp:linear-least-squares-solution).
:::
:::
Frequently, we want to estimate a vector of parameters $x \in \mathbb{R}^n$ given a measurement vector $z \in \mathbb{R}^m$ of the form $z = C x + v$ with $C \in \mathbb{R}^{m \times n}$ a given matrix and $v \in \mathbb{R}^m$ a noise vector. The following corollary gives the linear least squares estimator $x^\star(z)$ and its error covariance.
::: {.theorembox}
::: {.corollary #linear-least-squares-noise name="Linear Least-Squares Estimator with Noise"}
Let
$$
z = C x + v,
$$
where $C \in \mathbb{R}^{m \times n}$ is a given matrix, and the random variable $x$ and $v$ are uncorrelated. Denote
$$
\mathbb{E}\{x \} = \bar{x}, \quad \mathbb{E}\{ (x - \bar{x})(x - \bar{x})^T \} = \Sigma_{xx}, \\
\mathbb{E}\{ v\} = \bar{v}, \quad \mathbb{E}\{ (v - \bar{v})(v - \bar{v})^T \} = \Sigma_{vv},
$$
and assume $\Sigma_{vv}$ is positive definite. Then the linear least-squares estimator of $x$ given $z$ is
$$
x^\star(z) = \bar{x} + \Sigma_{xx} C^T (C \Sigma_{xx} C^T + \Sigma_{vv})^{-1} (z - C \bar{x} - \bar{v}),
$$
and the corresponding error covariance is
$$
\mathbb{E}_{x,v} \{ (x - x^\star(z))(x - x^\star(z))^T \} = \Sigma_{xx} - \Sigma_{xx}C^T (C \Sigma_{xx} C^T + \Sigma_{vv})^{-1} C \Sigma_{xx}.
$$
:::
:::
::: {.proofbox}
::: {.proof}
Denote
$$
y = \begin{bmatrix} x \\ v \end{bmatrix}, \quad \bar{y} = \begin{bmatrix} \bar{x} \\ \bar{v} \end{bmatrix},
$$
then clearly we have
$$
x = \underbrace{ \begin{bmatrix} I & 0 \end{bmatrix}}_{C_x} y,
$$
and
$$
z = \underbrace{\begin{bmatrix} C & I \end{bmatrix}}_{\tilde{C}} y.
$$
Using Corollary \@ref(cor:linear-least-squares-z), we have
$$
x^\star(z) = C_x y^\star(z) \\
\mathbb{E}\{ (x - x^\star(z))(x - x^\star(z))^T \} = C_x \mathbb{E}\{(y - y^\star(z))(y - y^\star(z))^T \} C_x^T,
$$
where $y^\star(z)$ is the linear least-sqaures estimator of $y$ given $z$.
To obtain $y^\star(z)$, we can apply Corollary \@ref(cor:linear-least-squares-x-z) with $u=0$ and $x=y$, leading to
$$
y^\star(z) = \bar{y} + \Sigma_{yy} \tilde{C}^T (\tilde{C} \Sigma_{yy} \tilde{C}^T)^{-1} (z - \tilde{C}\bar{y}),
$$
and the error covariance
$$
\mathbb{E}\{(y - y^\star(z))(y - y^\star(z))^T \} = \Sigma_{yy} - \Sigma_{yy} \tilde{C}^T (\tilde{C}\Sigma_{yy}\tilde{C}^T)^{-1} \tilde{C} \Sigma_{yy}.
$$
The result then follows by noting
$$
\Sigma_{yy} = \begin{bmatrix} \Sigma_{xx} & 0 \\ 0 & \Sigma_{vv} \end{bmatrix}
$$
because $x$ and $v$ are uncorrelated.
:::
:::
The next two corollaries deal with least-squares estimators involving multiple measurements arriving sequentially. In particular, the corollaries show how to modify an existing least-squares estimate $x^\star(y)$ to obtain $x^\star(y,z)$ once an additional measurement $z$ becomes available. This is a central operation in Kalman filtering.
::: {.theorembox}
::: {.corollary #linear-least-squares-correction-uncorrelated name="Linear Least-Squares Estimator of Uncorrelated Measurements"}
Consider in addition to $x$ and $y$, an additional random vector $z \in \mathbb{R}^p$ that is uncorrelated with $y$. Then the linear least-squares estimator $x^\star(y,z)$ of $x$ given both $y$ and $z$ has the form
$$
x^\star(y,z) = x^\star(y) + x^\star(z) - \bar{x},
$$
where $x^\star(y)$ and $x^\star(z)$ are the linear least-squares estimates of $x$ given $y$ and given $z$, respectively. Furthermore, the error covariance matrix is
$$
\mathbb{E}_{x,y,z} \{ (x - x^\star(y,z))(x - x^\star(y,z))^T \} = \Sigma_{xx} - \Sigma_{xy} \Sigma^{-1}_{yy} \Sigma^T_{xy} - \Sigma_{xz} \Sigma^{-1}_{zz} \Sigma_{xz}^T,
$$
where
$$
\Sigma_{xz} = \mathbb{E}_{x,z}\{ (x - \bar{x})(z - \bar{z})^T \}, \quad \Sigma_{zz} = \mathbb{E}_{z} \{ (z - \bar{z})(z - \bar{z})^T \}, \quad \bar{z} = \mathbb{E}_z \{ z \},
$$
and it is assumed that $\Sigma_{zz}$ and $\Sigma_{yy}$ are both invertible.
:::
:::
::: {.proofbox}
::: {.proof}
Let
$$
w = \begin{bmatrix} y \\ z \end{bmatrix}, \quad \bar{w} = \begin{bmatrix} \bar{y} \\ \bar{z} \end{bmatrix}.
$$
Then we can apply Proposition \@ref(prp:linear-least-squares-solution) to compute the linear least-squares estimator of $x$ given $w$
$$
x^\star(w) = \bar{x} + \Sigma_{xw} \Sigma^{-1}_{ww} (w - \bar{w}).
$$
On the other hand, we have
$$
\Sigma_{xw} = \mathbb{E}\{ (x - \bar{x}) \begin{bmatrix} (y - \bar{y})^T & ( z - \bar{z} )^T \end{bmatrix} \} = \begin{bmatrix} \Sigma_{xy} & \Sigma_{xz} \end{bmatrix},
$$
and since $y$ and $z$ are uncorrelated, we have
$$
\Sigma_{ww} = \begin{bmatrix} \Sigma_{yy} & 0 \\ 0 & \Sigma_{zz} \end{bmatrix}.
$$
The result then follows by direct computation.
:::
:::
The next corollary generalizes the previous result to the case where $y$ and $z$ are correlated.
::: {.theorembox}
::: {.corollary #linear-least-squares-correction-correlated name="Linear Least-Squares Estimator of Correlated Measurements"}
Consider the situation in Corollary \@ref(cor:linear-least-squares-correction-uncorrelated), but with $z$ and $y$ being correlated
$$
\Sigma_{yz} = \mathbb{E}_{y,z} \{ (y - \bar{y})(z - \bar{z})^T \} \neq 0.
$$
Then $x^\star(y,z)$ has the following form
$$
x^\star(y,z) = x^\star(y) + x^\star(z - z^\star(y)) - \bar{x},
$$
where $x^\star(z - z^\star(y))$ denotes the linear least-squares estimate of $x$ given the random vector $z - z^\star(y)$ and $z^\star(y)$ is the linear least-squares estimate of $z$ given $y$. Furthermore, the error covariance matrix is
$$
\mathbb{E}_{x,y,z} \{ (x - x^\star(y,z))(x - x^\star(y,z))^T \} = \Sigma_{xx} - \Sigma_{xy} \Sigma^{-1}_{yy} \Sigma^T_{xy} - \hat{\Sigma}_{xz} \hat{\Sigma}^{-1}_{zz} \hat{\Sigma}^T_{xz},
$$
where
$$
\hat{\Sigma}_{xz} = \mathbb{E}_{x,y,z} \{ (x - \bar{x})(z - z^\star(y))^T \}, \quad \hat{\Sigma}_{zz} = \mathbb{E}_{y,z} \{ (z - z^\star(y))(z - z^\star(y))^T \}.
$$
:::
:::
::: {.proofbox}
::: {.proof}
By Proposition \@ref(prp:linear-least-squares-solution), we know $z^\star(y)$ is linear in $y$ as
$$
z^\star(y) = \bar{z} + \Sigma_{zy} \Sigma^{-1}_{yy} (y - \bar{y}) := B y + c,
$$
where $B = \Sigma_{zy} \Sigma^{-1}_{yy}$ and $c = \bar{z} - \Sigma_{zy} \Sigma^{-1}_{yy} \bar{y}$. The linear least-squares estimate of $x$ given $(y,z)$ has the form
\begin{equation}
x = \begin{bmatrix} A_y^\star & A_z^\star \end{bmatrix} \begin{bmatrix} y \\ z \end{bmatrix} + b^\star,
(\#eq:linear-least-squares-solution-A-b)
\end{equation}
where $(A_y^\star, A_z^\star, b^\star)$ is solution to
\begin{equation}
(A_y^\star, A_z^\star, b^\star) = \arg\min_{A_y,A_z,b} \mathbb{E}_{x,y,z} \left\{ \Vert x - A_y y - A_z z - b \Vert^2 \right\}.
(\#eq:correlated-A-b)
\end{equation}
The linear least-squares estimate of $x$ given $(y, z - z^\star(y))$ has the form
\begin{equation}
x = \begin{bmatrix} G_y^\star & G_z^\star \end{bmatrix} \begin{bmatrix} y \\ z - By - c \end{bmatrix} + h^\star,
(\#eq:linear-least-squares-solution-G-h)
\end{equation}
where $(G_y^\star,G_z^\star,h^\star)$ is solution to
\begin{equation}
(G_y^\star,G_z^\star,h^\star) = \arg\min_{G_y,G_z,h} \mathbb{E}_{x,y,z} \left\{ \Vert x - G_y y - G_z (z - By - c) - h \Vert^2 \right\}.
(\#eq:correlated-G-h)
\end{equation}
Observe that the objective in \@ref(eq:correlated-G-h) can be rewritten as
$$
\mathbb{E}_{x,y,z} \left\{ \Vert x - (G_y - G_z B) y - G_z z - (h - G_z c) \Vert^2 \right\},
$$
when matched with the objective of \@ref(eq:correlated-A-b) we have
$$
A_y^\star = G_y^\star - G_z^\star B, \quad A_z^\star = G_z^\star, \quad b^\star = h^\star - G_z^\star c.
$$
As a result, the linear least-squares estimator in \@ref(eq:linear-least-squares-solution-G-h) becomes
$$
x = (A_y^\star + A_z^\star B) y + A_z^\star (z - By - c) + b^\star + A_z^\star c = A^\star_y y + A^\star_z z + b^\star,
$$
which is the same as \@ref(eq:linear-least-squares-solution-A-b). Therefore, we have
$$
x^\star(y,z) = x^\star(y, z - z^\star(y)).
$$
From Corollary \@ref(cor:properties-linear-least-squares-estimator), we know $y$ and $z - z^\star(y)$ is uncorrelated. Consequently, we reduce to the uncorrelated setup in Corollary \@ref(cor:linear-least-squares-correction-uncorrelated) and the result follows.
:::
:::
## Kalman Filter {#kalman-filter}
Consider now a linear dynamical system without control
\begin{equation}
\begin{cases}
x_{k+1} = A_k x_k + w_k \\
y_k = C_k x_k + v_k
\end{cases}, k=0,1,\dots,N-1,
(\#eq:kalman-filter-linear-system-discrete)
\end{equation}
where $x_k \in \mathbb{R}^n$ the state, $w_k \in \mathbb{R}^n$ the random disturbance, $y_k \in \mathbb{R}^d$ the measurement (output), and $v_k \in \mathbb{R}^d$ the measurement noise.
We assume $x_0,w_0,\dots,w_{N-1},v_0,\dots,v_{N-1}$ are independent random vectors with means
$$
\mathbb{E}\{ w_k \} = \mathbb{E} \{ v_k \} = 0, \quad k=0,\dots,N-1,
$$
and covariances
$$
\mathbb{E} \{ w_k w_k^T \} = M_k, \quad \mathbb{E} \{ v_k v_k^T \} = N_k, \quad k=0,\dots,N-1,
$$
with $N_k$ positive definite for all $k$. We assume the initial state $x_0$ has mean $\mathbb{E}\{ x_0 \}$ and covariance
$$
S = \mathbb{E} \{ (x_0 - \mathbb{E}\{x_0 \} ) (x_0 - \mathbb{E}\{x_0 \} )^T \}.
$$
Let
$$
Y_k = (y_0,\dots,y_k)
$$
be the set of measurements up to time $k$, our goal is to obtain the linear least-squares estimate of $x_k$ given $Y_k$, denoted as
$$
x^\star_{k \mid k} := x^\star_k (Y_k),
$$
and its error covariance matrix
$$
\Sigma_{k \mid k} := \mathbb{E} \{ (x_k - x^\star_{k \mid k}) (x_k - x^\star_{k \mid k})^T\}.
$$
By definition, we have
\begin{equation}
x^\star_{0 \mid -1} = \mathbb{E}\{ x_0 \}, \quad \Sigma_{0 \mid -1} = S.
(\#eq:kalman-filter-initial-condition)
\end{equation}
The Kalman filter [@kalman60new] will derive a recursion to update $x^\star_{k \mid k}$ and $\Sigma_{k \mid k}$.
**Road map**. Towards this, let us assume we have computed $x^\star_{k \mid k-1}$ and its error covariance $\Sigma_{k \mid k-1}$ (the initial condition is given by \@ref(eq:kalman-filter-initial-condition)). Our goal is to compute $x^\star_{k+1 \mid k}$ and its error covariance $\Sigma_{k+1\mid k}$. We will see that, to compute $x^\star_{k+1 \mid k}$, we compute $x^\star_{k \mid k}$ first, and to compute $\Sigma_{k+1 \mid k}$, we compute $\Sigma_{k\mid k}$ first, as illustrated below.
\begin{equation}
\begin{split}
\text{Update estimate:}\quad x^\star_{k \mid k-1} \longrightarrow x^\star_{k \mid k} \longrightarrow x^\star_{k+1 \mid k}. \\
\text{Update covariance:}\quad \Sigma_{k \mid k-1} \longrightarrow \Sigma_{k \mid k} \longrightarrow \Sigma_{k+1 \mid k}.
\end{split}
\end{equation}
**Using the measurement**. At time $k$, we receive a new measurement
$$
y_k = C_k x_k + v_k.
$$
We have
$$
x^\star_{k\mid k} = x^\star_k (Y_k) = x^\star_k(Y_{k-1},y_k),
$$
by Corollary \@ref(cor:linear-least-squares-correction-correlated), we have
\begin{equation}
x^\star_{k \mid k} = x^\star_{k \mid k-1} + x^\star_k (y_k - y_k^\star(Y_{k-1})) - \mathbb{E}\{ x_k \},
(\#eq:kalman-filter-derivation-1)
\end{equation}
where $y_k^\star(Y_{k-1})$ is the linear least-squares estimate of $y_k$ given $Y_{k-1}$.
**Simplify \@ref(eq:kalman-filter-derivation-1)**. First note that by Corollary \@ref(cor:linear-least-squares-z) and independence of $v_k$, we have
$$
y_k^\star(Y_{k-1}) = C_k x^\star_{k \mid k-1}.
$$
We now need to compute $x_k^\star(y_k - y_k^\star(Y_{k-1}))$. We do so by Proposition \@ref(prp:linear-least-squares-solution). The covariance matrix of $y_k - y_k^\star(Y_{k-1})$ is, by Corollary \@ref(cor:linear-least-squares-z),
$$
\mathbb{E} \{ (y_k - y_k^\star(Y_{k-1}))(y_k - y_k^\star(Y_{k-1}))^T \} = C_k \Sigma_{k \mid k-1} C_k^T + N_k.
$$
The correlation between $x_k$ and $y_k - y_k^\star(Y_{k-1})$ is
\begin{equation}
\begin{split}
\mathbb{E}\{ x_k (y_k - y_k^\star(Y_{k-1}))^T \} = \mathbb{E} \{ x_k(C_k (x_k - x^\star_{k\mid k-1}))^T \} + \underbrace{ \mathbb{E}\{ x_k v_k^T \} }_{=0 \text{ by independence of } v_k} \\
= \mathbb{E}\{(x_k - x^\star_{k \mid k-1}) (x_k - x^\star_{k \mid k-1})^T \}C_k^T + \underbrace{\mathbb{E}\{ x^\star_{k \mid k-1}(x_k - x^\star_{k \mid k-1})^T \}}_{=0 \text{ by the orthogonal projection principle}}C_k^T \\
= \Sigma_{k \mid k-1} C_k^T.
\end{split}
\end{equation}
As a result, by Proposition \@ref(prp:linear-least-squares-solution), we have
$$
x_k^\star(y_k - y_k^\star(Y_{k-1})) = \mathbb{E}\{x_k \} + \Sigma_{k \mid k-1} C_k^T (C_k \Sigma_{k \mid k-1} C_k^T + N_k)^{-1} (y_k - C_k x^\star_{k \mid k-1}).
$$
**Update the estimate**. Thus, \@ref(eq:kalman-filter-derivation-1) is simplified as
\begin{equation}
x^\star_{k\mid k} = x^\star_{k \mid k-1} + \Sigma_{k \mid k-1} C_k^T (C_k \Sigma_{k \mid k-1} C_k^T + N_k)^{-1} (y_k - C_k x^\star_{k \mid k-1}),
(\#eq:kalman-filter-update-1)
\end{equation}
which describes the update from $x^\star_{k \mid k-1}$ to $x^\star_{k \mid k}$.
To update $x^\star_{k+1 \mid k}$ from $x^\star_{k \mid k}$, by Corollary \@ref(cor:linear-least-squares-z), we have
\begin{equation}
x^\star_{k+1 \mid k} = A_k x^\star_{k \mid k}.
(\#eq:kalman-filter-update-2)
\end{equation}
**Update the covariance**. The error covariance $\Sigma_{k \mid k}$, by Corollary \@ref(cor:linear-least-squares-correction-correlated), can be written as
\begin{equation}
\Sigma_{k \mid k} = \Sigma_{k \mid k-1} - \Sigma_{k\mid k-1} C_k^T (C_k \Sigma_{k \mid k-1} C_k^T + N_k)^{-1} C_k \Sigma_{k \mid k-1}.
(\#eq:kalman-filter-update-3)
\end{equation}
(This requires just a little bit thinking.) To update $\Sigma_{k+1 \mid k}$ from $\Sigma_{k \mid k}$, we can use Corollary \@ref(cor:linear-least-squares-z) and the independence of $w_k$ to obtain
\begin{equation}
\Sigma_{k+1 \mid k} = A_k \Sigma_{k \mid k} A_k^T + M_k.
(\#eq:kalman-filter-update-4)
\end{equation}
Equations \@ref(eq:kalman-filter-update-1)-\@ref(eq:kalman-filter-update-4) forms the Kalman Filter.
**Including controls**. When the dynamics includes control
$$
x_{k+1} = A_k x_k + B_k u_k + w_k,
$$
the only change that needs to be made to the Kalman Filter is that \@ref(eq:kalman-filter-update-2) should be replaced by
\begin{equation}
x^\star_{k+1 \mid k} = A_k x^\star_{k \mid k} + B_k u_k.
(\#eq:kalman-filter-update-5)
\end{equation}
### Steady-State Kalman Filter
Combining the two updates on covariance matrices \@ref(eq:kalman-filter-update-3) and \@ref(eq:kalman-filter-update-4), we obtain
\begin{equation}
\Sigma_{k+1 \mid k} = A_k (\Sigma_{k \mid k-1} - \Sigma_{k \mid k-1} C_k^T (C_k \Sigma_{k \mid k-1} C_k^T + N_k)^{-1} C_k \Sigma_{k \mid k-1} ) A_k^T + M_k,
(\#eq:kalman-filter-covariance-recursion)
\end{equation}
which is the same as the discrete-time Riccati equation \@ref(eq:finite-discrete-lqr-riccati) when we study the solution of the finite-horizon LQR problem in Proposition \@ref(prp:discretetimefinitehorizonlqrsolution).
When $A_k, C_k, M_k, N_k$ are constant matrices
$$
A_k = A, \quad C_k = C, \quad, M_k = M, \quad N_k = N,
$$
the recursion \@ref(eq:kalman-filter-covariance-recursion) tends to the algebraic Riccati equation
\begin{equation}
\Sigma = A(\Sigma - \Sigma C^T(C \Sigma C^T + N)^{-1}C \Sigma)A^T + M,
(\#eq:kalman-filter-algebraic-riccati-equation)
\end{equation}
for which a unique positive definite solution $\Sigma$ exists if $(A,C)$ is observable and $(A,D)$ is controllable with $M = D D^T$. Then, using equation \@ref(eq:kalman-filter-update-3), we have that $\Sigma_{k \mid k}$ tends to
$$
\bar{\Sigma} = \Sigma - \Sigma C^T (C \Sigma C^T + N)^{-1} C \Sigma.
$$
This leads to the simple steady-state Kalman filter
\begin{equation}
x^\star_{k \mid k} = \underbrace{A x^\star_{k-1 \mid k-1}}_{\text{prediction}} + \underbrace{\Sigma C^T(C \Sigma C^T + N)^{-1} (y_k - CA x_{k-1 \mid k-1})}_{\text{feedback correction}}.
(\#eq:steady-state-kalman-filter)
\end{equation}
### Continuous-time Kalman Filter
Consider the continuous-time analog of the linear system in \@ref(eq:kalman-filter-linear-system-discrete)
\begin{equation}
\begin{cases}
\dot{x} = A x + Bu + w \\
y = C x + v
\end{cases}
(\#eq:kalman-filter-linear-system-continuous)
\end{equation}
where $x \in \mathbb{R}^n$ the state, $u \in \mathbb{R}^m$ the control, $y \in \mathbb{R}^d$ the measurement, and $w,v$ are white noises with
$$
\mathbb{E}\{w\}=0, \quad \mathbb{E}\{ w w^T \} = M, \quad \mathbb{E}\{v\} = 0, \quad \mathbb{E}\{ v v^T\} = N.
$$
Suppose the initial state $x(0)$ satisfies
$$
\mathbb{E}\{ x(0) \} = \hat{x}_0, \quad \mathbb{E}\{ (x(0) - \hat{x}_0) (x(0) - \hat{x}_0)^T \} = \Sigma_0.
$$
Then the continuous-time Kalman Filter updates the state estimate as
\begin{equation}
\dot{\hat{x}} = \underbrace{A \hat{x} + Bu}_{\text{prediction}} + \underbrace{K (y - C \hat{x})}_{\text{feedback correction}},
(\#eq:continuous-time-kalman-filter-state)
\end{equation}
where $K$ is a feedback gain computed by
$$
K = \Sigma C^T N^{-1},
$$
with $\Sigma$ satisfying the following differential equation
\begin{equation}
\dot{\Sigma} = A \Sigma + \Sigma A^T + M - \Sigma C^T N^{-1} C \Sigma.
(\#eq:continuous-time-kalman-filter-covariance)
\end{equation}
Similarly, at steady state, the differential equation \@ref(eq:continuous-time-kalman-filter-covariance) tends to the continuous-time algebraic Riccati equation
$$
0 = A \Sigma + \Sigma A^T + M - \Sigma C^T N^{-1} C \Sigma.
$$
## Linear Quadratic Gaussian Control
With our knowledge about the Kalman filter, we are ready to solve a particular instance of output-feedback control, known as the Linear Quadratic Gaussian (LQG) control.
Consider the discrete-time linear dynamical system:
\begin{equation}
\begin{cases}
x_{k+1} = A_k x_k + B_k u_k + w_k \\
y_k = C_k x_k + v_k
\end{cases}, \quad k=0,\dots,N-1,
(\#eq:lqg-linear-dynamical-system)
\end{equation}
where $w_k$ and $v_k$ are white noises with
$$
\mathbb{E}\{ w_k \} = 0, \mathbb{E}\{ v_k \} = 0, \quad k=0,\dots,N-1, \\
\mathbb{E} \{ w_k w_k^T \} = M_k, \mathbb{E} \{ v_k v_k^T \} = N_k, \quad k=0,\dots,N-1.
$$
Assume the initial state $x_0$ follows a Gaussian distribution $x_0 \sim \mathcal{N}(\bar{x}_0, S)$. This is the same setup as Section \@ref(kalman-filter) Kalman filter.
The goal of the LQG control is
\begin{equation}
\min_{u_k,k=0,\dots,N-1} \mathbb{E} \left\{ x_N^T Q_N x_N + \sum_{k=0}^{N-1} (x_k^T Q_k x_k + u_k^T R_k u_k) \right\},
(\#eq:lqg-cost-to-go)
\end{equation}
where the expectation is taken over the randomness of both $w_k$ and $v_k$. The LQG cost function \@ref(eq:lqg-cost-to-go) reads exactly the same as the LQR problem in \@ref(eq:lqr-formulation). However, the key difference between the LQG problem \@ref(eq:lqg-cost-to-go) and the LQR problem \@ref(eq:lqr-formulation) is that in LQR the controller has full access to the state $x_k$ of the system, while in LQG the controller only has access to a sequence of measurements
$$
Y_k = \{y_0,\dots,y_k \}.
$$
We will now show that the Kalman filter brings us back to the setup of LQR.
Let us rewrite the cost
$$
\mathbb{E}\{x_k^T Q_k x_k \} = \mathbb{E} \left\{ \mathbb{E} \{ x_k^T Q_k x_k \mid Y_k \} \right\} = \mathbb{E} \left\{ \mathbb{E} \{ (x_k - x^\star_{k \mid k} + x^\star_{k \mid k})^T Q_k (x_k - x^\star_{k \mid k} + x^\star_{k \mid k}) \mid Y_k \} \right\} \\
= \mathbb{E}\left\{ \mathbb{E}\{ (x_k - x^\star_{k \mid k})^T Q_k (x_k - x^\star_{k \mid k}) + 2 (x^\star_{k \mid k})^T Q_k (x_k - x^\star_{k \mid k}) + (x^\star_{k \mid k})^T Q_k x^\star_{k \mid k} \mid Y_k\} \right\} \\
= \mathbb{E}\{ (x^\star_{k \mid k})^T Q_k x^\star_{k \mid k} \} +
\underbrace{\mathbb{E}\{ \mathbb{E}\{ 2 (x^\star_{k \mid k})^T Q_k (x_k - x^\star_{k \mid k}) \mid Y_k \} \}}_{=0 \text{ due to orthogonal projection principle}}
+ \underbrace{\mathbb{E}\{ (x_k - x^\star_{k \mid k})^T Q_k (x_k - x^\star_{k \mid k}) \}}_{=\text{tr}(Q_k \Sigma_{k \mid k})},
$$
where $x^\star_{k\mid k}$ is the Kalman filter estimate of the state (i.e., the linear least squares estimate, or the conditional expectation). Note that from the Kalman filter we know the error covariance $\Sigma_{k \mid k}$ (and hence $\text{tr}(Q_k \Sigma_{k \mid k})$) is independent from the control. Therefore, the LQG problem \@ref(eq:lqg-cost-to-go) is equivalent to the following LQR problem
\begin{equation}
\min_{u_k, k=0,\dots,N-1} \mathbb{E} \left\{ (x^\star_{N\mid N})^T Q_N x^\star_{N\mid N} + \sum_{k=0}^{N-1} (x^\star_{k \mid k})^T Q_k x^\star_{k \mid k} + u_k^T R_k u_k \right\},
(\#eq:lqg-to-lqr)
\end{equation}
for which we know the optimal control is (according to Proposition \@ref(prp:discretetimefinitehorizonlqrsolution))
\begin{equation}
u_k^\star = - \underbrace{(R_k + B_k^T S_{k+1} B_k)^{-1} B_k^T S_{k+1} A_k}_{K_k} x^\star_{k \mid k},
(\#eq:lqg-lqr-solution-u)
\end{equation}
with $S_k$ computed backwards as $S_N = Q_N$,
\begin{equation}
S_k = Q_k + A_k^T \left[ S_{k+1} - S_{k+1} B_k (R_k + B_k^T S_{k+1} B_k)^{-1} B_k^T S_{k+1} \right] A_k.
(\#eq:lqg-lqr-solution-S)
\end{equation}
In summary, to implement the optimal controller for LQR problem, we need to
- Compute $S_k$ backwards in time according to \@ref(eq:lqg-lqr-solution-S)
- Run the Kalman filter \@ref(eq:kalman-filter-update-1) and \@ref(eq:kalman-filter-update-2) to obtain $x^\star_{k \mid k}$
- Compute control according to \@ref(eq:lqg-lqr-solution-u)
**Separation theorem**. Our derivation above shows that the optimal output-feedback control for LQG consists of (i) an optimal estimator, the Kalman filter, and (ii) an optimal state-feedback controller, the LQR controller.
### Steady-state LQG
In steady-state LQG, we consider the dynamical system
\begin{equation}
\begin{cases}
x_{k+1} = A x_k + B u_k + w_k \\
y_k = C x_k + v_k
\end{cases}, \quad k=0,\dots
\end{equation}
where $\mathbb{E}\{ w_k\} =0, \mathbb{E}\{ v_k\} = 0$, and
$$
\mathbb{E}\{w_k w_k^T \} = M, \quad \mathbb{E}\{v_k v_k^T \} = N.
$$
The LQG control problem is
\begin{equation}
\min \mathbb{E} \left\{ \sum_{k=0}^{\infty} (x_k^T Q_k x_k + u_k^T R_k u_k) \right\}.
\end{equation}
The optimal controller is
$$
u^\star_k = - \left[ (R + B^T S B)^{-1} B^T S A \right]x^\star_{k \mid k},
$$
where $S$ is the solution to the discrete-time algebraic Riccati equation
$$
S = Q + A^T \left[ S - SB (R + B^T S B)^{-1} B^T S \right]A,
$$
and $x^\star_{k\mid k}$ comes from the steady-state Kalman filter
$$
x_{k \mid k}^\star = A x^\star_{k-1\mid k-1} + B u_{k-1} + \Sigma C^T (C \Sigma C^T + N)^{-1} (y_k - C(A x^\star_{k-1\mid k-1} + B u_{k-1})),
$$
with $\Sigma$ the solution to another discrete-time algebraic Riccati equation
$$
\Sigma = M + A\left[ \Sigma - \Sigma C^T (N + C \Sigma C^T)^{-1} C \Sigma \right]A^T.
$$
::: {.examplebox}
::: {.example #lqg-control-pendulum name="LQG Stabilization of Simple Pendulum"}
Consider the pendulm dynamics that has already been shifted such that $x=[\theta,\dot{\theta}]=0$ represents the upright position that we want to stabilize (cf. Example \@ref(exm:lqr-pendulum-stabilization))
$$
\dot{x} = \begin{bmatrix}
x_2 \\
\frac{1}{ml^2} (u - b x_2 + mgl \sin x_1)
\end{bmatrix} + \begin{bmatrix} 0 \\ w \end{bmatrix},
$$
where $w$ is a random white noise.
Linearizing the dynamics around $x=0$ we have
$$
\dot{x} \approx \underbrace{\begin{bmatrix} 0 & 1 \\ \frac{g}{l} & - \frac{b}{ml^2} \end{bmatrix}}_{A_c} x + \underbrace{\begin{bmatrix} 0 \\ \frac{1}{ml^2} \end{bmatrix}}_{B_c} u + \begin{bmatrix} 0 \\ w \end{bmatrix}.
$$
We convert the continuous-time dynamics to discrete time with a fixed discretization $h$
$$
x_{k+1} = \dot{x}_k \cdot h + x_k = (h \cdot A_c + I) x_k + (h\cdot B_c) u_k + h \begin{bmatrix} 0 \\ w \end{bmatrix} \\
= A x_k + B u_k + w_k.
$$
We assume we can only observe the angular position of the pendulum
$$
y_k = \underbrace{\begin{bmatrix} 1 & 0 \end{bmatrix}}_{C} x_k + v_k,
$$
with $v_k$ some white noise.
We start with the true initial state $x_0 = [2;-2]$, but with a noisy estimate $x^\star_{0 \mid 0} = [0;0]$. Implementing the steady-state LQG controller produces the following result.
```{r pendulum-lqg, out.width='80%', fig.show='hold', fig.cap='LQG Stabilization of Simple Pendulum.', fig.align='center', echo=FALSE}
knitr::include_graphics('images/pendulum_lqg.png')
```
You can find code for this example [here](https://github.com/ComputationalRobotics/OptimalControlEstimation-Examples/blob/main/pendulum_stabilization_lqg.m).
:::
:::
**Closed-loop analysis**. Now that we have the Kalman filter and LQR controller implemented, let us take a look at the closed-loop response of the system. To ease our notation, let us denote $\hat{x}_k := x^\star_{k \mid k}$ as the Kalman estimate of state. Then we can write the LQG controller as
\begin{equation}
\begin{cases}
\hat{x}_{k+1} = A \hat{x}_k + B u_k + L (y_{k+1} - C(A \hat{x}_k + B u_k) ) & \text{Kalman filter}\\
x_{k+1} = A x_k + B u_k + w_k & \text{Original dynamics}
\end{cases},
(\#eq:lqg-closed-loop-1)
\end{equation}
where $L$ is the Kalman gain. The first equation can be simplified as
$$
\hat{x}_{k+1} = A \hat{x}_k + Bu_k + L( C\underbrace{(A x_k + B u_k + w_k)}_{x_{k+1}} + v_{k+1} - CA\hat{x}_k - CB u_k )\\
= A \hat{x}_k + Bu_k + L (CA x_k - CA \hat{x}_k + C w_k + v_{k+1})
$$
Let us denote the estimation error as $e_k = x_k - \hat{x}_k$, the above equation becomes
$$
\hat{x}_{k+1} = A \hat{x}_k + B u_k + LCA e_k + LC w_k + L v_{k+1}
$$
Subtracting the equation above from the second equation of \@ref(eq:lqg-closed-loop-1), we have
\begin{equation}
e_{k+1} = (A - LCA) e_k + (I - LC) w_k - L v_{k+1},
(\#eq:lqg-closed-loop-2)
\end{equation}
which describes the evolution of the error dynamics. Now with
$$
u_k = - K \hat{x}_k = -K (x_k - e_k) = K e_k - K x_k
$$
inserted into the second equation of \@ref(eq:lqg-closed-loop-1), we have
\begin{equation}
x_{k+1} = (A - BK) x_k + BK e_k + w_k,
(\#eq:lqg-closed-loop-3)
\end{equation}
which describes the evolution of the true state dynamics.
Combining \@ref(eq:lqg-closed-loop-2) and \@ref(eq:lqg-closed-loop-3), we have the closed-loop system
\begin{equation}
\begin{bmatrix} x_{k+1} \\ e_{k+1} \end{bmatrix} = \underbrace{\begin{bmatrix}
A - BK & BK \\
0 & A - LCA \end{bmatrix}}_{A_{\mathrm{cl}}} \begin{bmatrix} x_k \\ e_k \end{bmatrix} + \begin{bmatrix} w_k \\ (I - LC) w_k - L v_{k+1} \end{bmatrix}.
(\#eq:lqg-closed-loop-4)
\end{equation}
Now observe that if the Kalman gain $L$ is stable, then $e_{k}$ tends to zero with some noise. In this case, if the LQR gain $K$ is stable, then the state $x_k$ also tends to zero with some noise. In fact, since $A_{\mathrm{cl}}$ is block-diagonal, we know the eigenvalues of $A_{\mathrm{cl}}$ contains those of $A - BK$ and those of $A - LC A$.
### Continuous-time LQG
## Nonlinear Filtering
Kalman filter is the optimal filter for linear dynamics, linear measurements, and Gaussian noises. However, interesting real-world systems are often nonlinear
\begin{equation}
\begin{cases}
x_{k+1} = f(x_k) + w_k, \\
y_k = h(x_k) + v_k
\end{cases},
(\#eq:nolinear-filter-system)
\end{equation}
where $w_k$ is white disturbance $\mathbb{E}\{ w_k\} =0, \mathbb{E}\{ w_k w_k^T\} = M_k$, $v_k$ is white measurement noise $\mathbb{E}\{ v_k\} = 0, \mathbb{E} \{v_k v_k^T \} = N_k$, and $f,g$ are assumed to be differentiable nonlinear functions.
A nonlinear filter follows the general strategy of a Kalman filter
\begin{equation}
\begin{split}
\text{Update estimate:}\quad \hat{x}_{k \mid k-1} \overset{(3)}{\longrightarrow} \hat{x}_{k \mid k} \overset{(1)}{\longrightarrow} \hat{x}_{k+1 \mid k}. \\
\text{Update covariance:}\quad \Sigma_{k \mid k-1} \overset{(4)}{\longrightarrow} \Sigma_{k \mid k} \overset{(2)}{\longrightarrow} \Sigma_{k+1 \mid k}.
\end{split}
\end{equation}
where I have replaced $x^\star$ with $\hat{x}$ because the estimator is often not optimal in the case of nonlinear functions.
### Extended Kalman Filter
The key idea of extended Kalman filter (EKF) is to linearize the nonlinear functions $f$ and $h$.
**Prediction (1) and (2)**. To perform prediction update according to the dynamics, we linearize $f$ around $\hat{x}_{k\mid k}$
$$
x_{k+1} = f(x_k) + w_k = f(\hat{x}_{k\mid k}) + J_f(\hat{x}_{k \mid k})(x_k - \hat{x}_{k \mid k}) + w_k + \text{h.o.t.},
$$
where $\text{h.o.t.}$ standards for high-order terms, and $J_f$ is the Jacobian of the vector function $f$:
$$
J_f = \begin{bmatrix} \frac{\partial f_1}{x_1} & \cdots & \frac{\partial f_1}{\partial x_n} \\
\vdots & \ddots & \vdots \\
\frac{\partial f_n}{\partial x_1} & \cdots & \frac{\partial f_n}{\partial x_n}
\end{bmatrix}.
$$
With this linearization, update (1) of EKF is simply
\begin{equation}
\hat{x}_{k+1 \mid k} = f(\hat{x}_{k \mid k}),
(\#eq:ekf-update-1)
\end{equation}
and covariance update (2) is
\begin{equation}
\Sigma_{k+1\mid k} = M_k + J_f(\hat{x}_{k \mid k}) \Sigma_{k \mid k} J_f^T (\hat{x}_{k \mid k}).
(\#eq:ekf-update-2)
\end{equation}
**Correction (3) and (4)**. To perform measurement correction from $y_k$, we linearize $h$ around $\hat{x}_{k \mid k-1}$
$$
y_{k} = h(x_k) + v_k = h(\hat{x}_{k \mid k-1}) + J_h(\hat{x}_{k \mid k-1}) (x_k - \hat{x}_{k \mid k-1}) + v_k + \text{h.o.t.}.
$$
With this linearization, update (3) of EKF is
\begin{equation}
\hspace{-10mm} \hat{x}_{k \mid k} = \hat{x}_{k \mid k-1} + \Sigma_{k\mid k-1}J_h^T(\hat{x}_{k\mid k-1}) ( J_h(\hat{x}_{k\mid k-1})\Sigma_{k \mid k-1} J_h^T(\hat{x}_{k\mid k-1}) + N_k )^{-1}(y_k - h(\hat{x}_{k\mid k-1})),
(\#eq:ekf-update-3)
\end{equation}
and update (4) of EKF is
\begin{equation}
\hspace{-10mm}\Sigma_{k \mid k-1} - \Sigma_{k \mid k-1} \Sigma_{k\mid k-1}J_h^T(\hat{x}_{k\mid k-1}) ( J_h(\hat{x}_{k\mid k-1})\Sigma_{k \mid k-1} J_h^T(\hat{x}_{k\mid k-1}) + N_k )^{-1} J_h(\hat{x}_{k \mid k-1}) \Sigma_{k \mid k-1}.
(\#eq:ekf-update-4)
\end{equation}
Compared with the original Kalman filter in \@ref(eq:kalman-filter-update-1)-\@ref(eq:kalman-filter-update-4), we can see that EKF \@ref(eq:ekf-update-1)-\@ref(eq:ekf-update-4) (i) replaces $A_k$ with the Jacobian $J_f(\hat{x}_{k \mid k})$, (ii) replaces $C_k$ with the Jacobian $J_h(\hat{x}_{k \mid k-1})$, and (iii) replaces $C_k \hat{x}_{k \mid k-1}$ with $h(\hat{x}_{k \mid k-1})$ (those are the expected measurements).
**When does EKF perform well?** Since EKF is based on linearization, it performs well when (i) the functions $f,g$ are locally linear around the linearization points, and (ii) there is less uncertainty in the covariance matrices $\Sigma_{k \mid k-1},\Sigma_{k \mid k}$. Good illustrations can be found in Fig. 3.5 and Fig. 3.6 of [@thrun05book-probabilistic].
**Guarantees**. Under the technical conditions in [@reif99tac-stochastic], the estimation error of EKF can be guaranteed to be bounded.
### Unscented Kalman Filter
In nonlinear filtering, usually the distribution of the true state is never a Gaussian distribution. The key idea of unscented Kalman filter is to use a set of so-called "_Sigma points_" to capture the most important statistical properties of the true distribution, notably the first and second-order moments (i.e., mean and covariance).
**Sigma points**. Given a distribution with mean and covariance $(\mu, \Sigma)$ (assume the random variable has dimension $n$), it can be shown that $(2n+1)$ Sigma points can be chosen to match the mean and covariance [@van04-sigma]
\begin{equation}
\begin{split}
x^0 = \mu, \\
x^i = \mu + \left[\sqrt{(n+\lambda)\Sigma} \right]_{i}, \quad i = 1,\dots,n, \\
x^{i} = \mu - \left[\sqrt{(n+\lambda)\Sigma} \right]_{i-n}, \quad i=n+1,\dots,2n,
\end{split}
\end{equation}
where $\sqrt{(n+\lambda)\Sigma}$ denotes the square root of the matrix $(n+\lambda)\Sigma$, $[\cdot]_i$ denotes the $i$-th column of the matrix, and
$$
\lambda = \alpha^2(n+\kappa) - n,
$$
is a scaling parameter determined by hyperparameters $\alpha$ and $\kappa$ ($\alpha$ usually set between $10^{-2}$ and $1$, and $\kappa$ is usually set to either $0$ or $3-n$). Each of the Sigma point also comes with two weights as
$$
w^0_m = \frac{\lambda}{n+\lambda}, w^0_c = \frac{\lambda}{n+\lambda} + (1 - \alpha^2 + \beta), \\
w^i_m = w^i_c = \frac{1}{2(L + \lambda)}, \quad i=1,\dots,2n,
$$
where $\beta$ is an extra hyperparameter (for Gaussian $\beta=2$).
Now we are ready to use the idea of Sigma points to present the unscented Kalman filter (UKF).
**Prediction (1) and (2)**. To perform the prediction step, we generate $2n+1$ Sigma points from the distribution $(\hat{x}_{k \mid k}, \Sigma_{k \mid k})$:
$$
\bar{x}^0_{k \mid k}, \dots, \bar{x}^{2n}_{k \mid k}.
$$
The estimate $\hat{x}_{k+1\mid k}$ is updated as the sample mean of evaluating $f$ on all Sigma points
\begin{equation}
\hat{x}_{k+1 \mid k} = \sum_{i=0}^{2n} w^i_m f(\bar{x}^{i}_{k \mid k}),
(\#eq:ukf-update-1)
\end{equation}
and the covariance estimate is updated as the sample covariance
\begin{equation}
\Sigma_{k+1 \mid k} = M_k + \sum_{i=0}^{2n} w^i_c (f(\bar{x}^{i}_{k \mid k}) - \hat{x}_{k+1 \mid k})(f(\bar{x}^{i}_{k \mid k}) - \hat{x}_{k+1 \mid k})^T,
(\#eq:ukf-update-2)
\end{equation}
where $M_k$ comes from the disturbance $w_k$ in the dynamics.
**Correction (3) and (4)**. To understand the UKF measurement correction, we need to present a key observation of the original Kalman filter. In \@ref(eq:kalman-filter-update-1) and \@ref(eq:kalman-filter-update-3), the matrix
$$
C_k \Sigma_{k \mid k-1} C_k^T + N_k
$$
can be interpreted as the covariance of the measurement $y_k$ (conditioned on all past measurements), i.e., $\Sigma_{yy,k}$, due to the measurement function $y_k = C_k x_k + v_k$, while the matrix
$$
\Sigma_{k \mid k-1} C_k^T
$$
can be interpreted as the correlation between $x_k$ and $y_k$, i.e., $\Sigma_{xy,k}$.
Now that we have Sigma points in UKF, we can use them to approximate $\Sigma_{yy,k}$ and $\Sigma_{xy,k}$. To do so, we first generate $(2n+1)$ Sigma points from the distribution $(\hat{x}_{k \mid k-1}, \Sigma_{k \mid k-1})$
$$
\bar{x}^0_{k \mid k-1}, \dots, \bar{x}^{2n}_{k \mid k-1}.
$$
We then compute the expected measurements of these Sigma points
$$
\bar{y}^i_{k \mid k-1} = h(\bar{x}^i_{k \mid k-1}), i=0,\dots,2n.
$$
The sample mean of these measurements is
$$
\hat{y}_{k \mid k-1} = \sum_{i=0}^{2n} w^i_m \bar{y}^i_{k \mid k-1},
$$
and the sample covariance is
$$
\Sigma_{yy,k} = N_k + \sum_{i=0}^{2n} w^i_c (\bar{y}^i_{k \mid k-1} - \hat{y}_{k \mid k-1})(\bar{y}^i_{k \mid k-1} - \hat{y}_{k \mid k-1})^T.
$$
The sample correlation between $x$ and $y$ can be computed as
$$
\Sigma_{xy,k} = \sum_{i=0}^{2n} w^i_c (\bar{x}^i_{k \mid k-1} - \hat{x}_{k \mid k-1}) (\bar{y}^i_{k \mid k-1} - \hat{y}_{k \mid k-1})^T.
$$
As a result, the Kalman gain matrix is
$$
L_k = \Sigma_{xy,k} \Sigma^{-1}_{yy,k}.
$$
The UKF update (3) is therefore
\begin{equation}
\hat{x}_{k \mid k} = \hat{x}_{k \mid k-1} + L_k (y_k - \hat{y}_{k \mid k-1}),
(\#eq:ukf-update-3)
\end{equation}
and the update (4) is
\begin{equation}
\Sigma_{k \mid k} = \Sigma_{k \mid k-1} - L_k \Sigma_{yy,k} L_k^T.
(\#eq:ukf-update-4)
\end{equation}
**Applications of EKF and UKF**. Perhaps one of the most of well-known applications of EKF and UKF is simultaneous location and mapping (SLAM) in mobile robotics. Matlab has a [nice demo](https://www.mathworks.com/help/nav/ug/ekf-based-landmark-slam.html) that I recommend you to play with.
### Particle Filter
Kalman filter, EKF, and UKF all represent the uncertainty of the state as Gaussian distributions, specifically as means and covariances. However, as mentioned before, the true distribution of the state is rarely a Gaussian distribution. How can we faithfully represent non-Gaussian distributions?
The UKF does provide us with a possible solution. UKF represents the Gaussian distribution by $2n+1$ Sigma points. What if we generalize this idea by allowing more points, or in other words, represent a distribution by random samples?
This is the basic idea of a _particle filter_, which is arguably one of the most high-impact nonlinear filtering techniques. The idea of a particle filter is simple to state. Consider the dynamical system \@ref(eq:nolinear-filter-system), but this time assume we know the distribution of $w_k$ and $v_k$. For example, in the case of a Gaussian distribution $\mathcal{N}(\mu,\Sigma)$ (with dimension $n$), we know its probability density function is
\begin{equation}
\mathbb{P}(x = \xi) = \frac{\exp\left( -\frac{1}{2} (\xi- \mu)^T \Sigma^{-1} (\xi - \mu) \right)}{\sqrt{(2\pi)^n \det \Sigma}}.
(\#eq:Gaussian-pdf)
\end{equation}
The goal of a nonlinear filter is to estimate the conditional distribution (or sometimes called the _belief_) of $x_k$ given all previous measurements $Y_k = (y_0,\dots,y_k)$
$$
p(x_k \mid Y_k).
$$
Since the conditional distribution generally cannot be written analytically, a particle filter will represent the conditional distribution as a set of samples, i.e.,
$$
\mathcal{X}_{k\mid k} = \{x_{k \mid k}^1,\dots,x_{k\mid k}^M \} \sim p(x_k \mid Y_k),
$$
where $M$ is the total number of samples. The particle filter will then update the set of samples, using a similar prediction-correction two-step approach:
$$
\mathcal{X}_{k \mid k} \overset{\text{prediction}}{\longrightarrow} \mathcal{X}_{k+1 \mid k} \overset{\text{correction}}{\longrightarrow} \mathcal{X}_{k+1 \mid k+1},
$$
where the prediction step leverages the dynamics, and the correction step leverages the measurement function.
**Prediction**. The prediction step is straightforward
$$
\mathcal{X}_{k+1\mid k} = \{x^i_{k+1\mid k} \}_{i=1}^M, \quad x_{k+1\mid k}^i = f(x^i_{k\mid k}) + w_k^i, i=1,\dots,M,
$$
which basically passes each sample in $\mathcal{X}_{k\mid k}$ through the nonlinear dynamics $f$ and then add a random disturbance. Note that $w_k^i$ follows the known distribution of the noise that we know how to sample from, for example it can be the Gaussian distribution in \@ref(eq:Gaussian-pdf).
**Correction**. The correction step will leverage the new measurement, $y_{k+1}$, to adjust the set of samples. Particularly, we will evaluate the probability of observing $y_{k+1}$ for each of the samples in the set $\mathcal{X}_{k+1 \mid k}$, that is
$$
\alpha_{k+1}^i = p(y_{k+1} \mid x_{k+1\mid k}^i), i=1,\dots,M.
$$
This is simply the probability of
$$
\alpha_{k+1}^i = \mathbb{P}(v_{k+1} = y_{k+1} - h(x_{k+1\mid k}^i)),
$$
which can be computed since we know the distribution of $v_{k+1}$. For example, if the distribution of $v_{k+1}$ is Gaussian, then we can evaluate the probability using \@ref(eq:Gaussian-pdf). Intuitively, the higher $\alpha_{k+1}^i$ is, the more likely it is for the true state to be $x_{k+1\mid k}^i$. Therefore, effectively, we have obtained a set of samples together with their probabilities
$$
\{ (x_{k+1\mid k}^i, \alpha_{k+1}^i) \}_{i=1}^M.
$$
The next step is to perform _importance sampling_. In particular, we will generate a new set of $M$ samples by sampling (with replacement) from the population $\mathcal{X}_{k+1 \mid k}$ with the probability of sampling the $i$-th sample being $\alpha_{k+1}^i$. This leads to the new sample set $\mathcal{X}_{k+1 \mid k+1}$.
Hopefully you see that the particle filter is very easy to implement. Let us work on a simple example.
::: {.examplebox}
::: {.example #nonlinear-filter-robot-localization name="Robot Localization by Nonlinear Filtering"}
Consider a 2D robot with state $s = (x,y,\theta)$ where $(x,y)$ is the position and $\theta$ is the heading. The robot has two controls, the first one being the linear velocity $l$, and the second one being the angular velocity (steering) $u$. Therefore, the dynamics of the robot is
$$
\begin{bmatrix}