Skip to content

Add: Ordinary Least Squares Regression Algorithm #10800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@
* [Loss Functions](machine_learning/loss_functions.py)
* [Mfcc](machine_learning/mfcc.py)
* [Multilayer Perceptron Classifier](machine_learning/multilayer_perceptron_classifier.py)
* [Ordinary Least Squares Regression](machine_learning/ordinary_least_squares_regression.py)
* [Polynomial Regression](machine_learning/polynomial_regression.py)
* [Scoring Functions](machine_learning/scoring_functions.py)
* [Self Organizing Map](machine_learning/self_organizing_map.py)
Expand Down
80 changes: 80 additions & 0 deletions machine_learning/ordinary_least_squares_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""
Ordinary Least Squares Regression (OLSR):

Ordinary Least Squares Regression (OLSR) is a statistical method for
estimating the parameters of a linear regression model.
It is the most commonly used regression method,
and it is based on the principle of minimizing
the sum of the squared residuals.

Below is simple implementation of OLSR
without using any external libraries.

WIKI: https://en.wikipedia.org/wiki/Ordinary_least_squares
"""

import numpy as np


def ols_regression(x_point: np.ndarray, y_point: np.ndarray) -> tuple:
"""
Performs Ordinary Least Squares Regression (OLSR) on the given data.

Args:
x (numpy.ndarray): The independent variable.
y (numpy.ndarray): The dependent variable.

Returns:
a (float): The intercept of the regression line.
b (float): The slope of the regression line.

Examples:
>>> x = np.array([1, 2, 3, 4, 5])
>>> y = np.array([2, 4, 6, 8, 10])
>>> a, b = ols_regression(x, y)
>>> a # Intercept should be 0.0
0.0
>>> round(b, 2) # Slope should be 2.0
2.0
"""

# Calculate the mean of the independent variable and
# the dependent variable.
x_mean = np.mean(x_point)
y_mean = np.mean(y_point)

# Calculate the slope of the regression line.
slope = np.sum((x_point - x_mean) * (y_point - y_mean)) / np.sum(
(x_point - x_mean) ** 2
)

# Calculate the intercept of the regression line.
intercept = y_mean - slope * x_mean

return intercept, slope


if __name__ == "__main__":
import doctest

doctest.testmod()

# Load the data
x_points = np.array([1, 2, 3, 4, 5])
y_points = np.array([2, 4, 6, 8, 10])

# Perform OLS regression
intercept, slope = ols_regression(x_points, y_points)

# Intercept (a) and slope (b) of the regression line
print("Intercept:", intercept)
print("Slope:", slope)

# Predict the target variable for a new data point with
# an independent variable value of 6
x_new = 6

# Make a prediction
y_pred = intercept + slope * x_new

print("Prediction:", y_pred)