Linear Least Squares Methods with R: An Algebraic Approach - Part I
Algebraic Approach Principles
The least-squares method is one of the most well-known linear optimization methods because of its flexibility. Furthermore, it gives a reasonable approximation of a given function. Among the diverse applications that it can be used for is Regression in statistical learning, Direct Linear Transformation methods in projective geometry, and so on. We will demonstrate the principles of least squares methods and implement examples in R in this article.
Consider the above figure of a simple linear system where the
which in a more explicit notation from
We want to find and estimation of the variable
We then define a minimization criterion. In this case we use a quadratic minimization criterion, since we guarantee to locate global minima, that is,
In order to estimate the output of
Using the criterion from Eq.
also, expanding the Eq.
To minimize the resulting expression in Eq.
that is
to obtain from Eq.
where
Example 1: Worked Example with R
Consider the following model for data generation specified as
# Data generator functions
fdata <- function(t) {
-2.50*t^2 + 1.75*t + 232.50
}
we use
xxxxxxxxxx
# Generate and plot observed data
x <- 1:10
ydata <- fdata(x)
plot(x, ydata, pch=19, panel.first = grid(),
col='red', xlab = 't in seconds',
ylab = 'Observed data in Meters')
curve(fdata, from = 1, to = 10, col = 'darkred',
lty=5, add = T)
The above code, results in the following plot which simulates some particle moving a certain amount of meter through the time.
given the observed output data
t | y |
---|---|
1 | 231.75 |
2 | 226 |
3 | 215.25 |
4 | 199.5 |
5 | 178.75 |
6 | 153 |
7 | 122.25 |
8 | 86.5 |
9 | 45.75 |
10 | 0 |
We suspect the model better fit is quadratic, that is
xxxxxxxxxx
# Fit Model
fmodel <- function(t) {
c(t^2, t, 1)
}
Also we define support functions to compute pseudo inverse as
xxxxxxxxxx
# Pseudo Inverse compute function
pseudo.inv <- function(M){
solve(t(M)%*%M)%*%t(M)
}
Now, we compute the measurement matrix
xxxxxxxxxx
# Coefficients estimation
x.hat <- pseudo.inv(A)%*%matrix(ydata)
to obtain the same values from the original model
xxxxxxxxxx
[1,] -2.50
[2,] 1.75
[3,] 232.50
Under ideal conditions, if we observed a phenomenon, modeled it, and estimated its inputs, we would obtain its exact value, but this is not the case in reality. Consider now, the original model with additive noise as follows
xxxxxxxxxx
# Additive noise to original observed data
y.noise_obs <- ydata + rnorm(10,1,10)
A <- do.call(rbind, lapply(x, fmodel))
then to estimate the equation as in Eq.
xxxxxxxxxx
# Coefficients estimation
x.hat <- pseudo.inv(A)%*%matrix(y.noise_obs)
y.hat <- function(xhat, t) xhat[1]*t^2 + xhat[2]*t + xhat[3]
where we use the following snippet to view the results
xxxxxxxxxx
# View results
curve(fdata, from = 1, to = 10, col = 'darkblue', lty=5, panel.first = grid(),xlab = 't in seconds',
ylab = 'Observed data in Meters')
curve(y.hat(x.hat, x), from = 1, to = 10,
col = 'darkred', add = T)
points(x, y.noise_obs, pch=19, col='blue')
Where the red line is the estimated model and the blue dashed line is the original and without noise model.
Conclusion
In many fields, such as reverse problems, identification systems for control problems, statistical learning models, and computer vision, least squares is a widely used method for estimating variables. This article provides a brief overview of the theoretical foundations and a working example. In our next article, we will examine the situation where it is necessary to estimate
Comments
Post a Comment