Linear Least Square Method#

Author : Mustafa Sadeghi
E-mail : mustafasadeghi@mail.um.ac.ir
Project Description#
This code provides an interactive visualization of different methods to compute the Least Squares Regression Line using vertical, horizontal, and perpendicular residuals. The goal of this visualization is to allow users to manually adjust the slope (
The code calculates the Sum of Squared Distances (SSD) for both the user-defined line and the automatically computed least squares line. The user can interactively visualize the effect of different slope and intercept values on the regression line’s fit, using sliders for adjustments.
Mathematical Insights:#
Linear Regression with Different Residuals:
In standard Least Squares Regression, the goal is to minimize the vertical distance between the data points and the regression line. This approach uses the formula:
where:
is the slope of the line. is the intercept of the line (the value of when ). represents the residual for the -th data point, which is the difference between the actual value and the predicted value .
The goal is to find values for
and that minimize the sum of the squared residuals (SSR):Note: We are using this as the cost function to find the best fitted line.
Three Types of Residuals:
This code allows users to visualize three different types of residuals:
Vertical Residuals: The vertical difference between the data point and the regression line, commonly used in standard Least Squares Regression.
Horizontal Residuals: The horizontal distance between the data point and the regression line. This method measures the deviation along the
-axis.where
is found by solving for when , leading to:Perpendicular Residuals: The shortest (perpendicular) distance from the data point to the regression line, computed using geometric methods. This approach provides a more accurate geometric fit but is not typically used in standard regression.
Derivation of the Perpendicular Residual Formula
The perpendicular residual quantifies the distance from a point
to a regression line given by:where:
is the slope, is the intercept.
The perpendicular residual, unlike the vertical residual, is measured perpendicularly from the point to the regression line.
1. General Formula for the Distance from a Point to a Line
In 2D geometry, the formula for the perpendicular distance from a point
to a line of the form is:2. Rearranging the Regression Line Equation
To apply this formula to our regression line, we first rewrite the line equation
in the form . Rearranging the terms, we get:Here, we can identify:
, , .
3. Substituting into the Perpendicular Distance Formula
Now, using the point
in the distance formula:The denominator simplifies to
, giving us:4. Removing the Absolute Value for Residuals
In regression analysis, residuals are typically signed, indicating whether the data point lies above or below the line. Thus, instead of using the absolute value, we keep the sign:
This formula expresses the perpendicular residual, which measures the signed perpendicular distance from the point
to the regression line.

Sum of Squared Distances (SSD):
In this code, the Sum of Squared Distances (SSD) is computed dynamically for both the user-defined line and the least squares line for each type of residual.
The formula for SSD is similar to the sum of squared residuals:
The goal is to minimize the SSD by adjusting
and . The code visualizes how different residual types affect the computed SSD.
Key Features:#
Interactive Plot:
The plot displays the data points along with both the user-defined regression line and the least squares regression line. The user can manually adjust the slope (
) and intercept ( ) via sliders, and the plot updates in real-time to show the new lines.
Residual Visualization:
The distances (residuals) between each data point and the regression line are visualized as lines on the plot. The user can switch between three types of residuals (vertical, horizontal, and perpendicular) using a dropdown menu.
Sum of Squared Distances (SSD):
The SSD for both the user-defined line and the least squares line is displayed on the plot. The SSD is recalculated as the user adjusts the slope and intercept, helping users understand the effect of different residual types on the overall error.
Mathematical Insight:
The code offers a deep understanding of the least squares method by allowing users to explore different types of residuals and see their impact on the regression line and SSD. Users can learn why vertical residuals are typically used in the classic least squares method and how alternative methods (horizontal and perpendicular) affect the fit.
Minimizing Vertical Residuals (Least square Method) :#
In standard linear regression, we aim to find the best-fitting straight line through a set of data points by minimizing the sum of squared vertical residuals. This method is known as the Ordinary Least Squares (OLS) regression. The residuals are the vertical distances (errors) between the observed values and the values predicted by the linear model.
Problem Definition#
Given a set of data points
where
Our objective is to find
Minimization Process#
To find the values of
Step 1: Compute Partial Derivatives#
a. Partial Derivative with respect to #
Compute
Simplify:
b. Partial Derivative with respect to #
Compute
Simplify:
Step 2: Set Partial Derivatives to Zero#
a. Setting #
Divide both sides by
b. Setting #
Divide both sides by
Step 3: Derive the Normal Equations#
a. First Normal Equation (from derivative w.r.t. )#
We have:
Simplify the summation:
Rewriting:
b. Second Normal Equation (from derivative w.r.t. )#
We have:
Simplify the summation:
Rewriting:
Step 4: Solve the System of Equations#
We have two normal equations:
Let’s denote:
Then the normal equations become:
Solving for and #
From the first equation:
Substitute
Simplify:
Multiply both sides by
Group terms involving
Factor out
Rewriting:
Thus, the solution for
Once
Step 5: Express in Terms of Means#
Let’s define the sample means:
Also define:
Expressing
Similarly,
Summary of Results#
Slope (
):Intercept (
):
These formulas provide the least squares estimates of the slope and intercept that minimize the sum of squared vertical residuals.
Key Points#
Ordinary Least Squares (OLS) minimizes the sum of squared vertical residuals between observed and predicted values.
The normal equations derived from setting the partial derivatives to zero provide a system of linear equations to solve for
and .The final formulas for
and are expressed in terms of the sums of the data and their means.
Conclusion#
By following this detailed derivation, we have obtained explicit formulas for the regression coefficients
Exercise : Minimizing Vertical Residuals (Least square Method)#
Given the data points:
We aim to find the regression line of the form:
$
Step 1: Organize the Data#
First, let’s organize the given data points and compute the necessary sums.
Data Point |
||||
---|---|---|---|---|
1 |
0 |
1 |
0 |
0 |
2 |
2 |
1 |
4 |
2 |
3 |
3 |
4 |
9 |
12 |
Total |
5 |
6 |
13 |
14 |
Calculations:
Number of data points,
Sum of
:Sum of
:Sum of
:Sum of
:
Step 2: Compute the Means#
Calculate the mean of
Step 3: Apply the OLS Formulas#
The OLS estimates for the slope (
Calculate :#
Substitute the known values into the formula for
Simplify the Numerator and Denominator:
Numerator: $
$Denominator: $
$
Compute
Calculate :#
Compute the Product:
$
Subtract from
Step 4: Formulate the Regression Line#
Using the calculated values of
Step 5: Interpretation#
The regression line
Verification#
Let’s verify the residuals for each data point:
For
: $ $For
: $ $For
: $ $
Sum of Squared Residuals:
$
This confirms that the chosen line minimizes the sum of squared vertical residuals for the given data points.
By applying the Ordinary Least Squares method, we derived the regression line:
$
Minimizing Horizental Residuals :#
In regression analysis, we typically minimize the vertical residuals, which are the differences between the observed
This derivation provides a step-by-step explanation of how to minimize the sum of squared horizontal residuals to find the regression parameters
Problem Definition#
Given a set of data points
that minimize the Sum of Squared Horizontal Residuals (SSH):
where
Expressing Horizontal Residuals#
For each data point
Predicted
-value ( ):From the regression equation:
Solving for
:Horizontal Residual (
):
Objective Function#
Our goal is to minimize the Sum of Squared Horizontal Residuals (SSH):
Minimization Process#
To find the values of
Step 1: Compute Partial Derivatives#
a. Partial Derivative with respect to #
Compute
First, write
Let
Compute the partial derivative:
b. Partial Derivative with respect to #
Compute
Compute the partial derivative:
Step 2: Set Partial Derivatives to Zero#
a. Setting #
This is Equation (1).
b. Setting #
This is Equation (2).
Step 3: Express in Terms of Known Quantities#
Recall that:
Simplify:
Step 4: Substitute into the Equations#
Equation (2):#
Simplify:
Multiply both sides by
Rewriting:
So,
Equation (1):#
Simplify the terms:
Let’s denote:
Now rewrite Equation (1):
Simplify:
Combine like terms:
Multiply both sides by
Now, recall that from earlier:
Substitute
This process becomes very algebraically intensive and leads to a nonlinear equation in
Step 5: Conclusion#
The minimization of the sum of squared horizontal residuals leads to a nonlinear equation in
Therefore, to find
Numerical Solution Approach#
Given the complexity of the equations, the typical steps to find
Initialize
and :Start with initial guesses for
and , possibly using the OLS estimates.Iterative Optimization:
Use an optimization algorithm to adjust
and to minimize .Gradient Descent:
Update parameters using the gradients computed from the partial derivatives.
where
is the learning rate.Newton-Raphson Method:
Update parameters using second-order derivatives (Hessian matrix).
Optimization Libraries:
Use built-in optimization functions from statistical software or programming libraries.
Convergence Check:
Iterate until the changes in
and are below a predefined threshold, or until stops decreasing significantly.Solution:
The values of
and at convergence are the estimates that minimize the sum of squared horizontal residuals.
Key Points#
Nonlinear Optimization:
Minimizing
results in nonlinear equations without closed-form solutions.Numerical Methods:
Practical implementation requires numerical optimization techniques.
Comparison with Vertical Residuals:
Unlike vertical residual minimization, which yields analytical solutions, horizontal residual minimization is more computationally intensive
Exercise : Minimizing Horizontal Residuals#
Given the data points:
We aim to find the regression line of the form:
$
Step 1: Organize the Data#
First, let’s organize the given data points and compute the necessary sums.
Data Point |
||||
---|---|---|---|---|
1 |
0 |
1 |
0 |
0 |
2 |
2 |
1 |
4 |
2 |
3 |
3 |
4 |
9 |
12 |
Total |
5 |
6 |
13 |
14 |
Calculations:
Number of data points,
Sum of
:Sum of
:Sum of
:Sum of
:
Step 2: Compute the Means#
Calculate the mean of
Step 3: Formulate the Objective Function#
When minimizing horizontal residuals, we aim to minimize the sum of squared differences between the observed
For each data point
The horizontal residual (
The Sum of Squared Horizontal Residuals (SSH) is:
$
Step 4: Minimize the Sum of Squared Horizontal Residuals#
To find the values of
Partial Derivatives#
a. Partial Derivative with Respect to #
b. Partial Derivative with Respect to #
Setting Partial Derivatives to Zero#
a. Setting #
b. Setting #
Solving the Equations#
Equation 2:#
Substituting the residuals:
$
\frac{1 - \beta_0}{\beta_1} + 2 - \frac{1 - \beta_0}{\beta_1} + 3 - \frac{4 - \beta_0}{\beta_1} = 0 $
$\frac{6 - 3\beta_0}{\beta_1} + 5 = 0 $
$
Equation 1:#
Substituting the residuals:
$
\frac{(1 - \beta_0)^2}{\beta_1} + \left(2(1 - \beta_0) - \frac{(1 - \beta_0)^2}{\beta_1} \right) + \left(3(4 - \beta_0) - \frac{(4 - \beta_0)^2}{\beta_1} \right) = 0 $
$\frac{2(1 - \beta_0)^2}{\beta_1} + 2(1 - \beta_0) + 12 - 3\beta_0 - \frac{(4 - \beta_0)^2}{\beta_1} = 0 $
$\frac{2(1 - \beta_0)^2 + (4 - \beta_0)^2}{\beta_1} + 2(1 - \beta_0) + 12 - 3\beta_0 = 0 $$
Multiply both sides by
Expand and simplify:
$
Now, substitute
Substitute
Simplify each term:
First Term:
Second Term: $
$Third Term: $
(6 - 5\beta_1)^2 = 36 - 60\beta_1 + 25\beta_1^2 $
\frac{36 - 60\beta_1 + 25\beta_1^2}{3} = -12 + 20\beta_1 - \frac{25}{3}\beta_1^2 $$
Fourth Term:
Fifth Term: $
$
Combine all terms:
$
Combine like terms:
Constants:
terms: terms:
Thus, the equation simplifies to:
$
Calculate :#
Using Equation A:
$
Step 4: Formulate the Regression Line#
Using the calculated values of
Step 5: Interpretation#
The regression line
Verification#
Let’s verify the residuals for each data point:
For
: $ $For
: $ $For
: $ $
Sum of Squared Horizontal Residuals:
$
This confirms that the chosen line minimizes the sum of squared horizontal residuals for the given data points.
Conclusion#
By applying the method of minimizing horizontal residuals, we derived the regression line:
$
Minimizing Perpendicular Residuals#
Introduction#
In linear regression analysis, the Ordinary Least Squares (OLS) method is widely used to determine the best-fitting line by minimizing the vertical residuals, which are the differences between the observed
This derivation provides a comprehensive, step-by-step explanation of how to minimize the sum of squared perpendicular residuals to find the regression parameters
Problem Definition#
Given a set of data points
that minimize the Sum of Squared Perpendicular Residuals (SSPR):
where
Expressing Perpendicular Residuals#
For each data point
1. General Formula for Distance from a Point to a Line#
In 2D geometry, the distance
2. Rearranging the Regression Line Equation#
The regression line equation
Here, the coefficients are:
3. Substituting into the Distance Formula#
Using the point
Since residuals in regression can be positive or negative (indicating direction), we often omit the absolute value to preserve the sign:
Objective Function#
Our objective is to minimize the Sum of Squared Perpendicular Residuals (SSPR):
Simplifying:
Minimization Process#
To find the values of
Compute Partial Derivatives of
with Respect to andSet the Partial Derivatives to Zero to Obtain Normal Equations
Solve the System of Equations to Find
and
Step 1: Compute Partial Derivatives#
a. Partial Derivative with Respect to #
Compute
Apply the quotient rule and chain rule:
Simplify by factoring out common terms:
b. Partial Derivative with Respect to #
Compute
Apply the chain rule:
Step 2: Set Partial Derivatives to Zero#
To find the minima, set the partial derivatives equal to zero:
a. Setting #
Since
b. Setting #
Step 3: Derive the Normal Equations#
We now have a system of two equations:
Equation (1):
Equation (2):
Simplifying Equation (2):#
Expand the summation:
Solve for
Substituting into Equation (1):#
First, substitute
This substitution leads to a complex, nonlinear equation in
Step 4: Solving the System of Equations#
Due to the complexity of the equations derived, especially Equation (1), an analytical solution for
a. Total Least Squares (TLS) Approach#
Total Least Squares minimizes the sum of squared perpendicular residuals by considering errors in both
Steps to Compute TLS:#
Center the Data:
Subtract the mean of
and from each data point to center the data around the origin.where:
Form the Data Matrix:
Create a matrix
where each row represents a centered data point:Perform Singular Value Decomposition (SVD):
Decompose matrix
using SVD: is an orthogonal matrix. is an diagonal matrix with singular values. is a orthogonal matrix whose columns are the right singular vectors.
Determine the Best-Fit Line:
The best-fit line is determined by the right singular vector corresponding to the smallest singular value in
. Let this vector be .The slope
is:The intercept
is then:
Rationale:#
The right singular vector corresponding to the smallest singular value indicates the direction of least variance, which aligns with minimizing the perpendicular distances from the data points to the regression line.
b. Numerical Optimization Approach#
Alternatively, numerical optimization techniques can be employed to minimize
Steps to Perform Numerical Optimization:#
Define the Objective Function:
The objective function to minimize is
:Simplify:
Choose Initial Estimates:
Start with initial guesses for
and . These can be the OLS estimates or any reasonable approximation.Select an Optimization Algorithm:
Utilize algorithms such as:
Gradient Descent
Newton-Raphson Method
Quasi-Newton Methods (e.g., BFGS)
Conjugate Gradient Method
Implement the Optimization:
Use optimization techniques to iteratively adjust
and to minimize .Iterate Until Convergence:
Continue updating
and until the changes in or the parameters themselves are below a predefined threshold.Obtain the Optimal Parameters:
The values of
and at convergence are the estimates that minimize the sum of squared perpendicular residuals.
Step 5: Practical Implementation Example#
While numerical methods and SVD provide robust solutions for minimizing perpendicular residuals, the focus here is on understanding the mathematical derivation rather than implementation. However, it’s essential to recognize that these methods require computational tools to handle the complexity of the equations involved.
Conclusion#
Minimizing the sum of squared perpendicular residuals provides a more geometrically accurate fit, especially in scenarios where both
Key Differences Between OLS and TLS:#
Objective:
OLS: Minimizes the sum of squared vertical residuals.
TLS: Minimizes the sum of squared perpendicular residuals.
Assumptions:
OLS: Assumes errors are only in the
-direction.TLS: Accounts for errors in both
and -directions.
Solution:
OLS: Provides analytical solutions for
and .TLS: Requires numerical methods or SVD for solutions.
Understanding the distinction between these methods is crucial for selecting the appropriate regression technique based on the nature of the data and the underlying assumptions about measurement errors.
Exercise : Minimizing Perpendicular Residuals#
Given the data points:
We aim to find the regression line of the form:
$
Step 1: Define the Perpendicular Distance#
The perpendicular distance
Step 2: Expand the SSPR Expression#
For our data points
Step 3: Set Up the Minimization Problem#
To minimize
a. Partial Derivative with Respect to #
b. Partial Derivative with Respect to #
Simplifying:
$
Step 4: Solve the System of Equations#
Given the complexity of the partial derivatives, especially with ( \beta_1 ), an analytical solution can be intricate. However, with only three data points, we can proceed by making reasonable substitutions.
a. From Equation 1:#
b. Substitute ( \beta_0 ) into the Partial Derivative with Respect to ( \beta_1 )#
Substituting
Step 5: Numerical Solution Approach#
Given the complexity of the equations, we’ll use the following numerical approach to approximate the values of ( \beta_1 ) and ( \beta_0 ).
a. Choose an Initial Estimate#
Start with an initial guess for
From OLS, the slope
b. Iterative Optimization#
Using the OLS estimates as starting points:
$
Objective: Minimize
Procedure:
Calculate
for the current estimates.Compute the partial derivatives
and .Update the estimates using a suitable optimization algorithm (e.g., Gradient Descent).
Repeat until convergence is achieved (i.e., changes in
and are below a predefined threshold).
Given the small size of the dataset, convergence can be achieved quickly.
c. Example Iteration#
For illustrative purposes, let’s perform one iteration using the Newton-Raphson method.
Newton-Raphson Update Rules:
$
Note: Calculating second-order derivatives is beyond the scope of this step-by-step guide. In practice, software tools or numerical libraries handle these computations.
d. Convergence#
Repeat the iterative updates until
Step 6: Final Regression Line#
After performing the iterative optimization (steps not fully detailed here), suppose we obtain the final estimates:
$
Step 7: Verification#
To verify the accuracy of the regression line, calculate the perpendicular residuals for each data point.
a. For :#
b. For :#
c. For :#
Sum of Squared Perpendicular Residuals:
$
This confirms that the chosen line minimizes the sum of squared perpendicular residuals for the given data points.
Conclusion#
By minimizing the sum of squared perpendicular residuals, we derived the regression line:
$
Note: For precise calculations and multiple iterations required for convergence, it’s recommended to use numerical optimization tools or statistical software.
Implement 3 types of residuals and a regression line in Python#
Note : This version of the code is implemented using Plotly and may not work in a static Jupyter Book. Please download this Jupyter Notebook and run it on your local system.
1. Import necessary libraries#
Explanation of Libraries Used in this code#
NumPy: Provides support for numerical computations and data manipulation. Used for generating data points and performing mathematical operations.
Plotly: A graphing library that creates interactive visualizations, used here for plotting the scatter plot, regression line, and residuals.
ipywidgets: Allows creation of interactive sliders and dropdowns for real-time updates to the plot as the user adjusts slope, intercept, and distance type.
IPython Display: Embeds interactive elements like widgets and plots within the Jupyter Notebook.
time: Measures the execution time of the program.
import numpy as np
import plotly.graph_objs as go
from ipywidgets import FloatSlider, Dropdown, Layout, HBox, VBox, interactive_output, HTML
from IPython.display import display
import time
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[1], line 2
1 import numpy as np
----> 2 import plotly.graph_objs as go
3 from ipywidgets import FloatSlider, Dropdown, Layout, HBox, VBox, interactive_output, HTML
4 from IPython.display import display
ModuleNotFoundError: No module named 'plotly'
start_time = time.time()
2. Generate random linear data#
This block generates random linear data for x
and y
.
x: A sequence of 50 evenly spaced values between -5 and 5.
y: A linear function of
x
with added random noise to simulate real-world variations.
np.random.seed(20)
x = np.linspace(-5, 5, 50)
y = 0.5 * x + np.random.normal(size=x.size)
3. Define the function for perpendicular projection#
This function calculates the perpendicular projection of a point (x0
, y0
) onto a line defined by its slope and intercept. The function returns the projected point on the line (x_proj
, y_proj
).
def perpendicular_projection(x0, y0, slope, intercept):
x_proj = (x0 + slope * (y0 - intercept)) / (slope**2 + 1)
y_proj = slope * x_proj + intercept
return x_proj, y_proj
4. Define the function to plot regression and residuals#
This function creates an interactive plot showing the data points, a regression line, and the residual distances between the data points and the line. The residuals can be calculated using:
Vertical Distance: The vertical distance between the data point and the line.
Horizontal Distance: The horizontal distance between the data point and the line.
Perpendicular Distance: The shortest distance between the data point and the line.
The plot also displays the Sum of Squared Distances (SSD), a measure of the model’s total error, which is updated dynamically as the slope and intercept change.
def plot_regression_plotly(slope=1.0, intercept=0.0, distance_type="vertical"):
# Compute the fitted regression line
y_pred = slope * x + intercept
# Initialize traces for the plot
data = []
# Trace for the data points
data.append(go.Scatter(x=x, y=y, mode='markers', name='Data points', marker=dict(color='black')))
# Trace for the fitted regression line
line_x = np.linspace(-6, 6, 100)
line_y = slope * line_x + intercept
data.append(go.Scatter(x=line_x, y=line_y, mode='lines', name=f'Fitted line: y = {slope:.2f}x + {intercept:.2f}', line=dict(color='red')))
# Add residual lines and calculate SSD
ssd = 0
for i in range(len(x)):
if distance_type == "vertical":
# Vertical distance (difference in y)
data.append(go.Scatter(x=[x[i], x[i]], y=[y[i], y_pred[i]], mode='lines', line=dict(color='pink', dash='dash')))
ssd += (y[i] - y_pred[i]) ** 2
elif distance_type == "horizontal":
# Horizontal distance (difference in x)
x_proj = (y[i] - intercept) / slope
data.append(go.Scatter(x=[x[i], x_proj], y=[y[i], y[i]], mode='lines', line=dict(color='green', dash='dash')))
ssd += (x[i] - x_proj) ** 2
elif distance_type == "perpendicular":
# Perpendicular distance
x_proj, y_proj = perpendicular_projection(x[i], y[i], slope, intercept)
data.append(go.Scatter(x=[x[i], x_proj], y=[y[i], y_proj], mode='lines', line=dict(color='blue', dash='dash')))
perp_dist = np.sqrt((x[i] - x_proj)**2 + (y[i] - y_proj)**2)
ssd += perp_dist ** 2
# Create the layout for the plot with larger size
layout = go.Layout(
title=f'Sum of squared distances ({distance_type}): {ssd:.2f}',
xaxis=dict(title='x', range=[-6, 6]),
yaxis=dict(title='y', range=[-6, 6]),
showlegend=True,
width=900,
height=600,
margin=dict(l=40, r=40, t=40, b=40)
)
# Create the figure and display it
fig = go.Figure(data=data, layout=layout)
fig.show()
5. Create interactive widgets#
This block creates interactive widgets using ipywidgets
:
Slope Slider: Allows the user to adjust the slope of the regression line.
Intercept Slider: Allows the user to adjust the intercept of the regression line.
Distance Type Dropdown: Lets the user choose how the distances (residuals) are calculated—either vertically, horizontally, or perpendicularly.
slope_slider = FloatSlider(value=1.0, min=-3.0, max=3.0, step=0.1, layout=Layout(width='300px'))
intercept_slider = FloatSlider(value=0.0, min=-5.0, max=5.0, step=0.1, layout=Layout(width='300px'))
distance_type_dropdown = Dropdown(options=["vertical", "horizontal", "perpendicular"], layout=Layout(width='300px'))
slope_label = HTML(value=f"<b>Slope:</b> {slope_slider.value}")
intercept_label = HTML(value=f"<b>Intercept:</b> {intercept_slider.value}")
distance_type_label = HTML(value=f"<b>Distance type:</b> {distance_type_dropdown.value}")
6. Update labels dynamically#
This function updates the text labels for slope, intercept, and distance type dynamically as the user interacts with the sliders and dropdown menu. It ensures the displayed labels always reflect the current settings.
# Function to update the labels dynamically
def update_labels(change):
slope_label.value = f"<b>Slope:</b> {slope_slider.value:.2f}"
intercept_label.value = f"<b>Intercept:</b> {intercept_slider.value:.2f}"
distance_type_label.value = f"<b>Distance type:</b> {distance_type_dropdown.value}"
7. Attach the update function to widgets#
In this block, the update_labels
function is attached to the slope and intercept sliders and the distance type dropdown. This ensures that every time the user modifies a value, the corresponding labels update.
slope_slider.observe(update_labels, names='value')
intercept_slider.observe(update_labels, names='value')
distance_type_dropdown.observe(update_labels, names='value')
8. Arrange widgets in a horizontal layout#
This block arranges the sliders and dropdown widgets in a horizontal box (HBox
) for a clean and organized layout within the notebook. Each control (slope, intercept, distance type) is placed side by side.
controls = HBox([VBox([slope_label, slope_slider]), VBox([intercept_label, intercept_slider]), VBox([distance_type_label, distance_type_dropdown])])
9. Define the function to update the plot#
This function updates the plot based on the current values of the slope, intercept, and selected distance type. Every time the user interacts with the widgets, this function recalculates the residuals and updates the plot accordingly.
def update_plot(slope, intercept, distance_type):
plot_regression_plotly(slope, intercept, distance_type)
10. Display the interactive plot and controls#
This block combines the interactive controls (sliders and dropdown) with the plot output. It uses interactive_output
to link the plot to the widgets, so the plot updates dynamically when the user changes any value.
output = interactive_output(update_plot, {'slope': slope_slider, 'intercept': intercept_slider, 'distance_type': distance_type_dropdown})
# Display the controls and the plot
display(controls, output)
end_time = time.time()
Print the execution time#
execution_time = end_time - start_time
print(f"Program execution time: {execution_time:.4f} seconds")
Program execution time: 0.2698 seconds
Visit the online and local app using Streamlit.#
onilne app on streamlit :
I programmed another version of this app using Streamlit and uploaded it to Streamlit Cloud. If you want to visit it : Clik hereNote : If you get a 403 error when clicking on this link, you will need to use a VPN.
Run the app locally on your computer: If you cannot reach the app online, you can run it locally on your computer.
Download the streamlit_app.py from this repository.
Install Streamlit via the command line: pip install streamlit
Run the file using the following command: streamlit run “path_to_the_file”
Useful Tool for a better understanding#
For a better understanding of the Least Squares Method, please visit this link : chasereynolds