SLAM Least Squares Notes
SLAM Least Squares Notes
Shoudong Huang
March 21, 2017
Abstract
This notes describes least squares optimisation techniques using simple
examples. It starts from one dimensional linear least squares (LLS). Fol-
lowed by multi-dimensional LLS, and then nonlinear least squares (NLS).
The relation between LLS and Kalman filter (KF) is also discussed.
Contents
1 One dimensional linear least squares 1
1.1 Least squares and weighted least squares . . . . . . . . . . . . . . 2
1.2 1D Gaussian distribution and its information . . . . . . . . . . . 3
1.3 Weighted least squares with Gaussian measurement noises . . . . 3
1.4 The uncertainty of weighted least squares eatimate . . . . . . . . 4
5 Applications 12
1
1.1 Least squares and weighted least squares
Suppose x is the scalar parameter to be estimated (e.g. the height of a building).
If we have two measurements about x, say z1 = 100m and z2 = 110m, then we
can use least squares method to obtain an estimate of x.
Intuitively, we want z1 − x to be close to 0, and we also want z2 − x to be
close to 0. Thus we formulate the problem as:
Least Squares Problem: Find x to minimise
To solve this problem, we compute the stationary point (letting the derivative
equal to 0)
f 0 (x) = −2(z1 − x) − 2(z2 − x) = 0
and the unique solution is
1 1
x∗ = z1 + z2 . (2)
2 2
This solution is simply the average of z1 = 100m and z2 = 110m, that is,
x∗ = 105m. This answer matches our intuition, averaging the two measure-
ments.
In the case when one measurement is more accurate than the other, we need
to use different weights w1 > 0 and w2 > 0 for the two terms in the least squares
formulation.
Weighted Least Squares Problem: Find x to minimise
2
1D Gaussian Distribution 1D Gaussian Distribution
0.09
0.08 0.02
0.07
0.06 0.015
0.05
0.04 0.01
0.03
0.02 0.005
0.01
0 0
0 50 100 150 200 0 50 100 150 200
N(100,25) N(100,400)
(a) One dimensional Gaussian distribution (b) One dimensional Gaussian distribution
with smaller variance σ 2 = 25 (information with larger variance σ 2 = 400 (information
= 1/25). = 1/400).
x ∼ N (m, σ 2 ) (6)
3
Weighted Least Squares Problem with Gaussian Measurement Noises:
Find x to minimise
1 1
f (x) = (z1 − x)2 + 2 (z2 − x)2 . (10)
σ12 σ2
1 1
σ12 σ22
= 1
+ 1 z1 + 1
+ 1 z2 (11)
σ12 σ22 σ12 σ22
σ22 σ12
= z
σ12 +σ22 1
+ z .
σ12 +σ22 2
1 1 σ2 + σ2
I = I1 + I2 = 2 + 2 = 1 2 22. (12)
σ1 σ2 σ1 σ2
σ12 σ22
σ 2 = I −1 = . (13)
σ12 + σ22
Equation (12) means the total information is the sum of the information
contained in z1 and the information contained in z2 . Equation (13) means the
variance is the inverse of the information.
Figure 2 illustrates the result of both least squares estimate and its variance.
This is actually the same as the update step of 1D Kalman filter [1].
4
Least Squares Estimate
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
90 95 100 105 110 115 120 125
N(100,1) (z1) + N(110,4) (z2) ==> N(102,0.8)
Figure 2: One dimensional least squares estimate (dot line represents z1 and its
uncertainty, dashed line represents z2 and its uncertainty, solid line repersents
the least squares estimate and its uncertainty). I1 = 1, I2 = 41 , I = I1 + I2 = 45 ;
variance = I1 = 0.8; least squares estimate = II1 × 100 + II2 × 110 = 102.
5
2 Two dimensional linear least squares
This section considers the least squares problems when there are two parameters
to be estimated, x1 and x2 .
∂f
∂x1 = −2a11 [b1 − (a11 x1 + a12 x2 )] − 2a21 [b2 − (a21 x1 + a22 x2 )] − 2a31 [b3 − (a31 x1 + a32 x2 )] = 0;
∂f
∂x2 = −2a12 [b1 − (a11 x1 + a12 x2 )] − 2a22 [b2 − (a21 x1 + a22 x2 )] − 2a32 [b3 − (a31 x1 + a32 x2 )] = 0;
(16)
which is
a11 (a11 x1 + a12 x2 ) + a21 (a21 x1 + a22 x2 ) + a31 (a31 x1 + a32 x2 ) = a11 b1 + a21 b2 + a31 b3 ;
a12 (a11 x1 + a12 x2 ) + a22 (a21 x1 + a22 x2 ) + a32 (a31 x1 + a32 x2 ) = a12 b1 + a22 b2 + a32 b3 ;
(17)
or
a11 a11 + a21 a21 + a31 a31 a11 a12 + a21 a22 + a31 a32 x1a11 b1 + a21 b2 + a31 b3
= .
a12 a11 + a22 a21 + a32 a31 a12 a12 + a22 + a22 + a32 a32 x2a12 b1 + a22 b2 + a32 b3
(18)
Solving this linear equations we can obtain x∗1 and x∗2 , the solution to the least
squares problem (15).
6
In this case, we can formulate the weighted LLS problem as:
Weighted LLS Problem: Find x1 , x2 to minimise
1 1 1
f (x1 , x2 ) = [z1 −(a11 x1 +a12 x2 )]2 + 2 [z2 −(a21 x1 +a22 x2 )]2 + 2 [z3 −(a31 x1 +a32 x2 )]2 .
σ12 σ2 σ3
(20)
This problem can also be solved by letting the partial derivatives equal to
zero.
a211
1 a11 1 a11 a12
I1 = σ2 [a11 a12 ] = σ2
1 a12 1 a12 a11 a212
a221
1 a21 1 a21 a22
I2 = [a21 a22 ] = (21)
σ22 a22 σ22 a22 a21 a222
a231
1 a31 1 a31 a32
I3 = [a31 a32 ] =
σ32 a32 σ32 a32 a31 a232
Now the covariance matrix of the weighted least squares estimate can be
obtained by
1
σ12 0 0
a11 a12
a11 a21 a31 0 1
0 a21 a22 , (22)
I = I1 + I2 + I3 =
a12 a22 a32
σ22
0 0 σ12 a31 a32
3
P LS = I −1 . (23)
Again, the first equation means the total information is the sum of the infor-
mation containted in each of the measurements, the second equation means the
covariance matrix is the inverse of the information matrix.
Note that (19) can be written as
z1 a11 a12 n1
z2 = a21 a22 x1 + n2 (24)
x2
z3 a31 a32 n3
1 For a measurement given by z = AX + n , n ∼ N (0, σ 2 ), the information about vector X
z z z
contained in z is given by σ12 AT A. For a measurement vector given by Z = AX + NZ , NZ ∼
z
N (0, PZ ), the information about X contained in Z is given by AT PZ−1 A. See equation (3.4.2.-
6) and the sentence below it in [2].
7
Also, (18) can also be written as
a a12 b1
a11 a21 a31 11
x1 a11 a21 a31
a21 a22 = b2 . (25)
a12 a22 a32 x2 a12 a22 a32
a31 a32 b3
When writing everything in the matrix/vector format, we can get the general
linear least squares formulation and solution in a more concise manner. The next
section will provide the details.
where
b1 a11 a12 ··· a1n x1
b2 a21 a22 ··· a2n x2
b= , A = , X = , m ≥ n. (26)
.. .. .. .. .. ..
. . . . . .
bm am1 am2 ··· amn xn
Why? Because
X = (AT A)−1 AT b.
Z = AX + NZ , NZ ∼ N (0, PZ ), (28)
8
where A and X are defined in (26), then we can formulate the following weighted
LLS problem.
Weighted LLS Problem: Find X to minimise
xk+1 = xk + uk + wk (32)
σk2
xk x̂k 1 0 0
X= , Z= , A= , PZ = . (35)
xk+1 −uk 1 −1 0 σu2
9
Now we can formulate the weighted LLS problem as in (29). Since A is full
rank, the covariance matrix can be computed by (31) as
" 1
# !−1
0
1 1 2 1 0
P LS = (AT PZ−1 A)−1 = σk
1
0−1 0 1 −1
2
σu (36)
σk2 σk2 σk2
1 0 0 1 1
= = ,
1 −1 0 σu2 0 −1 σk2 σk + σu2
2
10
where
z1 f1 (X) f1 (x1 , x2 , · · · , xn )
z2 f2 (X) f2 (x1 , x2 , · · · , xn )
Z= , F (X) = = , m ≥ n. (39)
.. .. ..
. . .
zm fm (X) fm (x1 , x2 , · · · , xn )
Suppose X is close to X0 , by Taylor expansion (linearisation),
evaluated at X0 .
Thus
Z − F (X) ≈ Z − F (X0 ) + JF (X0 )X0 − JF (X0 )X.
Solution: Let
A = JF (X0 ), b = Z − F (X0 ) + JF (X0 )X0 ,
using the linear least square solution X = (AT A)−1 AT b, we get
X1 = [JFT (X0 )JF (X0 )]−1 JFT (X0 )[Z − F (X0 ) + JF (X0 )X0 ].
In general,
Xk+1 = [JFT (Xk )JF (Xk )]−1 JFT (Xk )[Z − F (Xk ) + JF (Xk )Xk ].
Iterating this until Xk converge we can get the solution.
This is called Gauss-Newton iteration.
11
5 Applications
LLS and NLS have many applications. Simultaneous Localisation and Mapping
(SLAM) is one of them. For 1D SLAM problem, LLS can be applied to solve it
and the solution is the same as the solution obtained by KF [5]. For 2D or 3D
SLAM, NLS can be used [6] [7] [8] and the solution is better than that obtained
using EKF [9] [10].
References
[1] S. Huang, Understanding Extended Kalman Filter – Part I: One Di-
mensional Kalman Filter. Faculty of Engineering and IT, Univeristy of
Technology Sydney. 2010.
[2] Y. Bar-Shalom, X. R. Li, and T. Kirubarajan, Estimation with Appli-
cations to Tracking and Navigation: Theory Algorithms and Software.
John Wiley & Sons. 2001. (Electronic Version)
[3] S. Huang, Understanding Extended Kalman Filter – Part II: Multi-
dimensional Kalman Filter. Faculty of Engineering and IT, Univeristy
of Technology Sydney. 2010.
[4] S. Huang, Understanding Extended Kalman Filter – Part III: Extended
Kalman Filter. Faculty of Engineering and IT, Univeristy of Technology
Sydney. 2010.
[5] S. Huang, Notes on 1D SLAM. Faculty of Engineering and IT, Univeristy
of Technology Sydney. 2016.
[6] F. Dellaert and M. Kaess, Square root SAM: Simultaneous localization
and mapping via square root information smoothing. International Jour-
nal of Robotics Research, 25(12):1181-1203, 2006.
[7] R. Kummerle, G, Grisetti, H. Strasdat, K. Konolige, and W. Burgard,
g2o: A general framework for graph optimization. IEEE International
Conference on Robotics and Automation, pp. 3607-3613, 2011.
[8] H. Wang, S. Huang, U. Frese, and G. Dissanayake. The nonlinearity
structure of point feature SLAM problems with spherical covariance ma-
trices. Automatica, 49, no. 10 (2013): 3112-3119.
[9] G. Dissanayake, P. Newman, S. Clark, H. Durrant-Whyte, and M.
Csobra, A solution to the simultaneous localization and map build-
ing (SLAM) problem. IEEE Transactions on Robotics and Automation,
17(3):229-241, 2001.
[10] S. Huang, G. Dissanayake, Convergence and consistency analysis for Ex-
tended Kalman Filter based SLAM, IEEE Transactions on Robotics,
23(5):1036-1049, 2007.
12