diff --git a/DIRECTORY.md b/DIRECTORY.md index 311a7c7c9de8..d6ceb04a62eb 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -715,6 +715,7 @@ * [Mfcc](machine_learning/mfcc.py) * [Mini Batch Gradient Descent](machine_learning/mini_batch_gradient_descent.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) * [Principle Component Analysis](machine_learning/principle_component_analysis.py) * [Q Learning](machine_learning/q_learning.py) diff --git a/machine_learning/ordinary_least_squares_regression.py b/machine_learning/ordinary_least_squares_regression.py new file mode 100644 index 000000000000..4fda6288ea77 --- /dev/null +++ b/machine_learning/ordinary_least_squares_regression.py @@ -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 a 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: The independent variable. + y: 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) + >>> float(a) # Intercept should be 0.0 + 0.0 + >>> float(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)