Least Square Method | Definition Graph and Formula
Last Updated :
03 May, 2025
The Least Square method is a popular mathematical approach used in data fitting, regression analysis, and predictive modeling. It helps find the best-fit line or curve that minimizes the sum of squared differences between the observed data points and the predicted values. This technique is widely used in statistics, machine learning, and engineering applications.

Least Squares Method is used to derive a generalized linear equation between two variables. When the value of the dependent and independent variables they are represented as x and y coordinates in a 2D Cartesian coordinate system. Initially, known values are marked on a plot. The plot obtained at this point is called a scatter plot.
Then, we try to represent all the marked points as a straight line or a linear equation. The equation of such a line is obtained with the help of the Least Square method. This is done to get the value of the dependent variable for an independent variable for which the value was initially unknown. This helps us to make predictions for the value of a dependent variable.
Why use the Least Square Method?
In statistics, when the data can be represented on a Cartesian plane by using the independent and dependent variables as the x and y coordinates, it is called scatter data. This data might not be useful in making interpretations or predicting the values of the dependent variable for the independent variable. So, we try to get an equation of a line that fits best to the given data points with the help of the Least Square Method.
The Least Square Method formula finds the best-fitting line through a set of data points. For a simple linear regression, which is a line of the form y=mx+c, where y is the dependent variable, x is the independent variable, a is the slope of the line, and b is the y-intercept, the formulas to calculate the slope (m) and intercept (c) of the line are derived from the following equations:
- Slope (m) Formula: m = n(∑xy)−(∑x)(∑y) / n(∑x2)−(∑x)2​
- Intercept (c) Formula: c = (∑y)−a(∑x) / n​
Where:
- n is the number of data points,
- ∑xy is the sum of the product of each pair of x and y values,
- ∑x is the sum of all x values,
- ∑y is the sum of all y values,
- ∑x2 is the sum of the squares of the x values.
Steps to find the line of Best Fit by using the Least Squares Method :
- Step 1: Denote the independent variable values as xi and the dependent ones as yi.
- Step 2: Calculate the average values of xi and yi as X and Y.
- Step 3: Presume the equation of the line of best fit as y = mx + c, where m is the slope of the line and c represents the intercept of the line on the Y-axis.
- Step 4: The slope m can be calculated from the following formula:
m = [Σ (X - xi)×(Y - yi)] / Σ(X - xi)2
- Step 5: The intercept c is calculated from the following formula:
c = Y - mX
Thus, we obtain the line of best fit as y = mx + c, where values of m and c can be calculated from the formulae defined above.
These formulas are used to calculate the parameters of the line that best fits the data according to the criterion of the least squares, minimizing the sum of the squared differences between the observed values and the values predicted by the linear model.
Least Square Method Graph
Let us have a look at how the data points and the line of best fit obtained from the Least Square method look when plotted on a graph.

The red points in the above plot represent the data points for the sample data available. Independent variables are plotted as x-coordinates, and dependent ones are plotted as y-coordinates. The equation of the line of best fit obtained from the Least Square method is plotted as the red line in the graph.
We can conclude from the above graph how the Least Square method helps us to find a line that best fits the given data points and hence can be used to make further predictions about the value of the dependent variable where it is not known initially.
Limitations of the Least Square Method
The Least Squares method assumes that the data is evenly distributed and doesn't contain any outliers for deriving a line of best fit. However, this method doesn't provide accurate results for unevenly distributed data or data containing outliers.
Check: Least Square Regression Line
How Do You Calculate Least Square?
To calculate the least squares solution, you typically need to:
- Determine the equation of the line you believe best fits the data.
- Calculate the residuals (differences) between the observed values and the values predicted by your model.
- Square each of these residuals and sum them up.
- Adjust the model to minimize this sum.
Least Square Method Solved Examples
Problem 1: Find the line of best fit for the following data points using the Least Square method: (x,y) = (1,3), (2,4), (4,8), (6,10), (8,15).
Solution:
Here, we have x as the independent variable and y as the dependent variable. First, we calculate the means of x and y values denoted by X and Y respectively.
X = (1+2+4+6+8)/5 = 4.2
Y = (3+4+8+10+15)/5 = 8
| xi | yi | X - xi | Y - yi | (X-xi)*(Y-yi) | (X - xi)2 |
---|
1 | 3 | 3.2 | 5 | 16 | 10.24 |
---|
2 | 4 | 2.2 | 4 | 8.8 | 4.84 |
---|
4 | 8 | 0.2 | 0 | 0 | 0.04 |
---|
6 | 10 | -1.8 | -2 | 3.6 | 3.24 |
---|
8 | 15 | -3.8 | -7 | 26.6 | 14.44 |
---|
Sum (Σ) | | | 0 | 0 | 55 | 32.8 |
---|
The slope of the line of best fit can be calculated from the formula as follows:
m = (Σ (X - xi)*(Y - yi)) /Σ(X - xi)2
m = 55/32.8 = 1.68 (rounded upto 2 decimal places)
Now, the intercept will be calculated from the formula as follows:
c = Y - mX
c = 8 - 1.68*4.2 = 0.94
Thus, the equation of the line of best fit becomes, y = 1.68x + 0.94.
Problem 2: Find the line of best fit for the following data of heights and weights of students of a school using the Least Squares method:
- Height (in centimeters): [160, 162, 164, 166, 168]
- Weight (in kilograms): [52, 55, 57, 60, 61]
Solution:
Here, we denote Height as x (independent variable) and Weight as y (dependent variable). Now, we calculate the means of x and y values denoted by X and Y respectively.
X = (160 + 162 + 164 + 166 + 168 ) / 5 = 164
Y = (52 + 55 + 57 + 60 + 61) / 5 = 57
| xi | yi | X - xi | Y - yi | (X-xi)*(Y-yi) | (X - xi)2 |
---|
160 | 52 | 4 | 5 | 20 | 16 |
---|
162 | 55 | 2 | 2 | 4 | 4 |
---|
164 | 57 | 0 | 0 | 0 | 0 |
---|
166 | 60 | -2 | -3 | 6 | 4 |
---|
168 | 61 | -4 | -4 | 16 | 16 |
---|
Sum ( Σ ) | | | 0 | 0 | 46 | 40 |
---|
Now, the slope of the line of best fit can be calculated from the formula as follows:
m = (Σ (X - xi)✕(Y - yi)) / Σ(X - xi)2
m = 46/40 = 1.15
Now, the intercept will be calculated from the formula as follows:
c = Y - mX
c = 57 - 1.15*164 = -131.6
Thus, the equation of the line of best fit becomes, y = 1.15x - 131.6
Real-Life Application of the Least Squares Method
- Predicting housing prices using linear regression.
- Estimating trends in the financial market.
- Fitting Curves to experimental data in physics and chemistry.
- Calibrating sensors in engineering.
Similar Reads
Graph of an Equation Formula
In mathematics, there are different types of graphs studied or plotted according to the collected data provided. These graphs are the pictorial representation of the information. There are several types of graphs studied under mathematics that are listed below:Pie chartBar graphHistogramDot plotScat
5 min read
Completing the Square: Method, Formula and Examples
Completing the square is a method used to solve quadratic equations, simplify expressions, and rewrite quadratic equations in a way that makes it easier to solve or analyze. It involves turning a quadratic expression into a perfect square trinomial.Imagine we have a simple expression like x2 + bx. H
7 min read
Equation in Maths | Definition , Types, Uses and Examples
Equations in Maths are statements that equates two terms involving variables and constants. Equations are a form of statement that shows that the two expressions are equal. There are various types of equations - Linear, Quadratic, Polynomial, Logarithmic, Trigonometric as well as some other shape eq
15+ min read
Graph definition & meaning in DSA
A Graph is a non-linear data structure consisting of vertices and edges where two vertices are connected by an edge. Example of GraphProperties of a Graph:Vertices (nodes): The points where edges meet in a graph are known as vertices or nodes. A vertex can represent a physical object, concept, or ab
4 min read
First Derivative: Definition, Formulas, and Examples
First Derivative Test is the test in calculus to find whether a function has a maximum or minimum value in the given interval. As the name suggests, the first derivative is used in this test to find the critical point and then further conditions are used to check each critical point for extrema. Man
13 min read
What is Linear Graph? Definition, Equation, Examples
Linear Graph: A Linear Graph is a graphical representation that discusses the relationship between two or more quantities or variables. In the case of any two variables, the relation between these two variables can be drawn by constructing the table of values if the rule for that relation is mention
9 min read
Zero Slop- Definition, Types, Graph, Equation, Examples
In mathematics, a zero slope refers to the flatness of a line where there is no inclination or rise. Zero slope represents a particular case that holds significance in various mathematical contexts. Zero slope indicates that the line is perfectly horizontal. In this article, we will learn about zero
8 min read
How to Find Slope From a Graph?
Answer: To find the slope from a graph, divide the change in the y-coordinate (rise) by the change in the x-coordinate (run) between two points on the line.The slope of a line on a graph represents how steep the line is, and it's calculated as the ratio of the vertical change (rise) to the horizonta
2 min read
Domain and Range for Graph
Domain and Range are fundamental in understanding the behavior of functions when they are represented graphically. Domain of a function refers to all the possible input values (typically represented on the x-axis) that the function can accept without resulting in any undefined behavior. On the other
11 min read
What is mean method formula?
Statistics is a mathematical branch that is carried out by the collection and summarization of data. It is concerned with collecting, analyzing, interpreting, presenting a set of data. Statistics has its role in the field of data collection especially data used by the government like a population ce
3 min read