Skip to content
Merged
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
85 changes: 85 additions & 0 deletions Algorithms.Tests/MachineLearning/LinearRegressionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Algorithms.MachineLearning;

namespace Algorithms.Tests.MachineLearning;

/// <summary>
/// Unit tests for the LinearRegression class.
/// </summary>
public class LinearRegressionTests
{
[Test]
public void Fit_ThrowsException_WhenInputIsNull()
{
var lr = new LinearRegression();
Assert.Throws<ArgumentException>(() => lr.Fit(null!, new List<double> { 1 }));
Assert.Throws<ArgumentException>(() => lr.Fit(new List<double> { 1 }, null!));
}

[Test]
public void Fit_ThrowsException_WhenInputIsEmpty()
{
var lr = new LinearRegression();
Assert.Throws<ArgumentException>(() => lr.Fit(new List<double>(), new List<double>()));
}

[Test]
public void Fit_ThrowsException_WhenInputLengthsDiffer()
{
var lr = new LinearRegression();
Assert.Throws<ArgumentException>(() => lr.Fit(new List<double> { 1 }, new List<double> { 2, 3 }));
}

[Test]
public void Fit_ThrowsException_WhenXVarianceIsZero()
{
var lr = new LinearRegression();
Assert.Throws<ArgumentException>(() => lr.Fit(new List<double> { 1, 1, 1 }, new List<double> { 2, 3, 4 }));
}

[Test]
public void Predict_ThrowsException_IfNotFitted()
{
var lr = new LinearRegression();
Assert.Throws<InvalidOperationException>(() => lr.Predict(1.0));
Assert.Throws<InvalidOperationException>(() => lr.Predict(new List<double> { 1.0 }));
}

[Test]
public void FitAndPredict_WorksForSimpleData()
{
// y = 2x + 1
var x = new List<double> { 1, 2, 3, 4 };
var y = new List<double> { 3, 5, 7, 9 };
var lr = new LinearRegression();
lr.Fit(x, y);
Assert.That(lr.IsFitted, Is.True);
Assert.That(lr.Intercept, Is.EqualTo(1.0).Within(1e-6));
Assert.That(lr.Slope, Is.EqualTo(2.0).Within(1e-6));
Assert.That(lr.Predict(5), Is.EqualTo(11.0).Within(1e-6));
}

[Test]
public void FitAndPredict_WorksForNegativeSlope()
{
// y = -3x + 4
var x = new List<double> { 0, 1, 2 };
var y = new List<double> { 4, 1, -2 };
var lr = new LinearRegression();
lr.Fit(x, y);
Assert.That(lr.Intercept, Is.EqualTo(4.0).Within(1e-6));
Assert.That(lr.Slope, Is.EqualTo(-3.0).Within(1e-6));
Assert.That(lr.Predict(3), Is.EqualTo(-5.0).Within(1e-6));
}

[Test]
public void Predict_List_WorksCorrectly()
{
var x = new List<double> { 1, 2, 3 };
var y = new List<double> { 2, 4, 6 };
var lr = new LinearRegression();
lr.Fit(x, y); // y = 2x
var predictions = lr.Predict(new List<double> { 4, 5 });
Assert.That(predictions[0], Is.EqualTo(8.0).Within(1e-6));
Assert.That(predictions[1], Is.EqualTo(10.0).Within(1e-6));
}
}
100 changes: 100 additions & 0 deletions Algorithms/MachineLearning/LinearRegression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Algorithms.MachineLearning;

/// <summary>
/// Implements simple linear regression for one independent variable (univariate).
/// Linear regression is a supervised learning algorithm used to model the relationship
/// between a scalar dependent variable (Y) and an independent variable (X).
/// The model fits a line: Y = a + bX, where 'a' is the intercept and 'b' is the slope.
/// </summary>
public class LinearRegression
{
// Intercept (a) and slope (b) of the fitted line
public double Intercept { get; private set; }

public double Slope { get; private set; }

public bool IsFitted { get; private set; }

/// <summary>
/// Fits the linear regression model to the provided data.
/// </summary>
/// <param name="x">List of independent variable values.</param>
/// <param name="y">List of dependent variable values.</param>
/// <exception cref="ArgumentException">Thrown if input lists are null, empty, or of different lengths.</exception>
public void Fit(IList<double> x, IList<double> y)
{
if (x == null || y == null)
{
throw new ArgumentException("Input data cannot be null.");
}

if (x.Count == 0 || y.Count == 0)
{
throw new ArgumentException("Input data cannot be empty.");
}

if (x.Count != y.Count)
{
throw new ArgumentException("Input lists must have the same length.");
}

// Calculate means
double xMean = x.Average();
double yMean = y.Average();

// Calculate slope (b) and intercept (a)
double numerator = 0.0;
double denominator = 0.0;
for (int i = 0; i < x.Count; i++)
{
numerator += (x[i] - xMean) * (y[i] - yMean);
denominator += (x[i] - xMean) * (x[i] - xMean);
}

const double epsilon = 1e-12;
if (Math.Abs(denominator) < epsilon)
{
throw new ArgumentException("Variance of X must not be zero.");
}

Slope = numerator / denominator;
Intercept = yMean - Slope * xMean;
IsFitted = true;
}

/// <summary>
/// Predicts the output value for a given input using the fitted model.
/// </summary>
/// <param name="x">Input value.</param>
/// <returns>Predicted output value.</returns>
/// <exception cref="InvalidOperationException">Thrown if the model is not fitted.</exception>
public double Predict(double x)
{
if (!IsFitted)
{
throw new InvalidOperationException("Model must be fitted before prediction.");
}

return Intercept + Slope * x;
}

/// <summary>
/// Predicts output values for a list of inputs using the fitted model.
/// </summary>
/// <param name="xValues">List of input values.</param>
/// <returns>List of predicted output values.</returns>
/// <exception cref="InvalidOperationException">Thrown if the model is not fitted.</exception>
public IList<double> Predict(IList<double> xValues)
{
if (!IsFitted)
{
throw new InvalidOperationException("Model must be fitted before prediction.");
}

return xValues.Select(Predict).ToList();
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ find more than one implementation for the same objective but using different alg
* [SoftMax Function](./Algorithms/Numeric/SoftMax.cs)
* [RecommenderSystem](./Algorithms/RecommenderSystem)
* [CollaborativeFiltering](./Algorithms/RecommenderSystem/CollaborativeFiltering)
* [Machine Learning](./Algorithms/MachineLearning)
* [Linear Regression](./Algorithms/MachineLearning/LinearRegression.cs)
* [Searches](./Algorithms/Search)
* [A-Star](./Algorithms/Search/AStar/)
* [Binary Search](./Algorithms/Search/BinarySearcher.cs)
Expand Down