Quadratic Regression in C#

Quadratic Regression in C#

In real-world datasets, relationships between variables are not always linear. While Linear Regression assumes a straight-line relationship between input and output variables, many systems behave in a curved or accelerating way. In such cases, a linear model becomes too simplistic and fails to capture the true structure of the data.

Quadratic Regression is a type of polynomial regression that extends Linear Regression by introducing a squared term. This allows the model to fit a curved, parabolic relationship instead of a straight line, making it significantly more flexible for non-linear patterns.

What is Quadratic Regression?

Quadratic Regression is a statistical technique used to model relationships where the dependent variable changes in a curved pattern relative to the independent variable. It fits a second-degree polynomial equation to the data.

y = a x² + b x + c

In this equation, a controls the curvature of the parabola, b controls the slope, and c represents the intercept. By adjusting these parameters, the model can represent upward or downward curves depending on the dataset.

This makes Quadratic Regression particularly useful when the relationship between variables increases or decreases at a changing rate rather than a constant one.

Quadratic Regression is a special case of Polynomial Regression where the highest degree is exactly 2.

Where is Quadratic Regression Used?

Quadratic Regression is widely used in scenarios where data naturally forms a curved pattern. Many physical, financial, and biological systems exhibit quadratic behavior.

  • Projectile motion in physics (ballistics, trajectories)
  • Profit optimization in business models
  • Growth and decay modeling
  • Engineering stress-strain analysis
  • Machine learning feature approximation
  • Computer graphics curve modeling

Why Not Use Linear Regression?

Linear Regression assumes a constant rate of change, which means it cannot adapt to curvature in data. When applied to non-linear datasets, it often underfits the model, leading to inaccurate predictions.

Quadratic Regression solves this limitation by introducing an additional degree of freedom through the squared term. This allows the model to bend and better match the actual shape of the data distribution.

Feature Linear Regression Quadratic Regression
Model Shape Straight Line Parabolic Curve
Complexity Low Moderate
Flexibility Limited Higher
Best for Linear relationships Non-linear curved relationships
Risk of Overfitting Low Medium

How Quadratic Regression Works

Quadratic Regression transforms the original dataset by adding a new feature: x². Once this transformation is applied, the problem becomes similar to Linear Regression but in a higher-dimensional feature space.

The algorithm then finds the best coefficients (a, b, c) that minimize the error between predicted and actual values using methods such as Least Squares Optimization.

Loss = Σ (y_actual - (a x² + b x + c))²

This optimization ensures that the resulting parabola best fits the observed data points.

Implementing Quadratic Regression in C# (From Scratch)

The following example demonstrates a simple least-squares implementation of Quadratic Regression in C#. It calculates coefficients for a second-degree polynomial using matrix-like summations.

using System;

public class QuadraticRegression
{
    public double A { get; private set; }
    public double B { get; private set; }
    public double C { get; private set; }

    public void Fit(double[] x, double[] y)
    {
        int n = x.Length;

        double sumX = 0, sumX2 = 0, sumX3 = 0, sumX4 = 0;
        double sumY = 0, sumXY = 0, sumX2Y = 0;

        for (int i = 0; i < n; i++)
        {
            double xi = x[i];
            double yi = y[i];

            sumX += xi;
            sumX2 += xi * xi;
            sumX3 += xi * xi * xi;
            sumX4 += xi * xi * xi * xi;
            sumY += yi;
            sumXY += xi * yi;
            sumX2Y += xi * xi * yi;
        }

        double[,] matrix = new double[3, 4]
        {
            { sumX4, sumX3, sumX2, sumX2Y },
            { sumX3, sumX2, sumX, sumXY },
            { sumX2, sumX, n, sumY }
        };

        // Gaussian elimination (simplified solving step omitted for brevity)
        // In real implementations, use a linear algebra library
    }

    public double Predict(double x)
    {
        return A * x * x + B * x + C;
    }
}

The manual implementation above demonstrates the mathematical foundation, but in production systems you should use a numerical library for matrix solving and stability.

Using MathNet.Numerics for Polynomial Regression

In real-world applications, polynomial regression is usually implemented using numerical libraries that handle matrix operations efficiently and safely.

MathNet.Numerics provides powerful linear algebra tools that can be used to perform polynomial regression by transforming input features.

using System;
using MathNet.Numerics.LinearAlgebra;

public static double[] MultiDim(double[][] x, double[] y)
{
    return MultipleRegression.NormalEquations(x, y);
}

By expanding the feature space manually, quadratic regression can be reduced to a standard linear regression problem applied to transformed variables.

Common Libraries in .NET

  • ML.NET
  • MathNet.Numerics
  • Accord.NET
  • SharpLearning
  • TensorFlow.NET

Summary

Quadratic Regression extends Linear Regression by introducing a squared term, allowing models to capture curved relationships in data. While linear models are limited to straight-line patterns, quadratic models can represent acceleration, deceleration, and other non-linear behaviors.

Although it is more flexible, Quadratic Regression also introduces a higher risk of overfitting if applied without proper data understanding. For this reason, it is often used as an intermediate step between simple linear models and more complex machine learning algorithms.

Conclusion

Quadratic Regression is a powerful yet interpretable modeling technique that bridges the gap between simple linear models and advanced machine learning systems. Understanding how it works in C# helps developers build a strong foundation in predictive modeling and prepares them for more advanced regression techniques.

Contents related to 'Quadratic Regression in C#'

Linear Regression in C#
Linear Regression in C#