Skip to main content

Linear Least Squares Methods with R: An Algebraic Approach - Part I

Linear Least-Squares Method with R - Part I

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 x variable is an input variable, A is a measurements matrix, and y is the output variable. That is, the model for the equation is given by

(1)y=Ax,

which in a more explicit notation from (1) can be expressed as

(2)[y1y2yi]=[a1,1a1,2a1,ia2,1a2,2a2,iaj,1aj,2aj,i][x1x2xi]

We want to find and estimation of the variable x given the matrix measurement A and the output y. To do that, we consider the equivalent estimated model input and output given by

(3)y^=Ax^.

We then define a minimization criterion. In this case we use a quadratic minimization criterion, since we guarantee to locate global minima, that is,

(4)minxyy^2=minxyAx^2.

In order to estimate the output of y^ as closely as possible to y, we define an error measurement using Eq.(3) as follows

(5)err=yy^=yAx^.

Using the criterion from Eq. (4) in matrix notation we have

(6)err2=(yAx^)T(yAx^),

also, expanding the Eq. (6) we obtain the following procedure

(7)(yAx^)T(yAx^)=yTy(Ax^)TyyT(Ax^)+(Ax^)T(Ax^)=yTyx^TATyx^TATy+x^TATAx^=yTy2x^TATy+x^TATAx^.

To minimize the resulting expression in Eq. (7) we perform

(8)minx^{yTy2x^TATy+x^TATAx^},

that is

(9)x^{yTy2x^TATy+x^TATAx^}=0,2ATy+2ATAx^=0,ATAx^=ATyx^=(ATA)1ATyx^=Ay.

to obtain from Eq. (9) the well know general expression of Linear Least Squares

(10)x^=(ATA)1ATyx^=Ay,

where x^ is the estimated input variable and A is the so-called Pseudo-Inverse matrix of A.

Example 1: Worked Example with R

Consider the following model for data generation specified as

we use t within range from 1 to 10 and generate simulated data using

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 y, we wan to estimate the estimated input x^.

ty
1231.75
2226
3215.25
4199.5
5178.75
6153
7122.25
886.5
945.75
100

We suspect the model better fit is quadratic, that is

Also we define support functions to compute pseudo inverse as

Now, we compute the measurement matrix A as

to obtain the same values from the original model

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

then to estimate the equation as in Eq. (3) we perform

where we use the following snippet to view the results

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 β coefficients in linear models for ordinary least squares regression problems.

 

 

Comments

Popular posts from this blog

Solid Principles in Object Oriented Programming

SOLID Principles in Object Oriented Programming Solid Principles in Object Oriented Programming Introduction SOLID is an acronym that represents five principles of object-oriented programming and design, aimed at making software systems more maintainable and scalable. The SOLID principles are: Single Responsibility Principle (SRP) - A class should have only one reason to change, meaning that a class should have only one responsibility. Open-Closed Principle (OCP) - Software entities should be open for extension but closed for modification, meaning that a class should be easily extendable without modifying its existing code. Liskov Substitution Principle (LSP) - Subtypes should be substitutable for their base types, meaning that objects of a derived class should be able to replace objects of the base class without affecting the correctness of the program. Interface Segregation Principle (ISP) - Clients should not be forced to depend on interfaces they do not use, meaning tha...

Variance concepts in the context of parametric programming with Java

Variance concepts in the context of parametric programming with Java By Obed Rios (5/7/2023 ) Revision 1.0 Abstract In Java, the concepts of variance are related to how the type parameters of a class or interface are related to each other when the class or interface is sub-typed or implemented. The key difference between invariant and covariant in the context of Java generics is how they handle sub-typing relationships. Invariant types do not allow assignments between different type parameters, while covariant types can accept a specified type or any of its sub-types. In addition contravariance enables you to use a more general type (super type) in a generic type or method that would normally require a more specific type (sub-type). In this work, we show explicitly the concepts of variance in the context of Java Generics. Introduction Parametric variance refers to the relationship between the type parameters of a class or interface and their subtypes. It defines how subtyping is i...