How to resolve the algorithm Polynomial regression step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Polynomial regression step by step in the C++ programming language

Table of Contents

Problem Statement

Find an approximating polynomial of known degree for a given data. Example: For input data: The approximating polynomial is: Here, the polynomial's coefficients are (3, 2, 1). This task is intended as a subtask for Measure relative performance of sorting algorithms implementations.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Polynomial regression step by step in the C++ programming language

This C++ code implements a polynomial regression model to find the best-fit polynomial of degree 2 that approximates a given set of data points. Here's a step-by-step explanation of the code:

  1. Input Data:

    • Two vectors, x and y, store the input data points. In this case, x represents the independent variable (x-values), and y represents the dependent variable (y-values).
  2. Calculating Mean Values:

    • The code calculates the mean values for x, y, x^2, x^3, and x^4. These mean values are denoted as xm, ym, x2m, x3m, and x4m, respectively.
  3. Calculating Covariance Terms:

    • The covariance terms, xym and x2ym, are computed using a function that multiplies and sums corresponding elements from x and y vectors. These covariance terms measure the linear and quadratic relationships between x and y.
  4. Calculating Coefficients for the Polynomial:

    • The code calculates the coefficients a, b, and c for the polynomial equation y = a + bx + cx^2. These coefficients are determined by solving a system of linear equations formed from the covariance terms and mean values.
  5. Printing the Polynomial Equation:

    • The estimated polynomial equation is printed in the format: y = a + bx + cx^2.
  6. Printing a Table of Input and Approximated Values:

    • The code iterates through the x and y vectors and prints a table that shows the input data points alongside the approximated y-values calculated using the estimated polynomial equation. This table allows for a visual comparison of the actual and estimated values.

In summary, this code performs polynomial regression to find the best-fit polynomial curve that approximates the given data points. It calculates the necessary mean and covariance terms and uses them to determine the coefficients of the polynomial. Finally, it prints the polynomial equation and a table comparing the input and approximated y-values.

Source code in the cpp programming language

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

void polyRegression(const std::vector<int>& x, const std::vector<int>& y) {
    int n = x.size();
    std::vector<int> r(n);
    std::iota(r.begin(), r.end(), 0);
    double xm = std::accumulate(x.begin(), x.end(), 0.0) / x.size();
    double ym = std::accumulate(y.begin(), y.end(), 0.0) / y.size();
    double x2m = std::transform_reduce(r.begin(), r.end(), 0.0, std::plus<double>{}, [](double a) {return a * a; }) / r.size();
    double x3m = std::transform_reduce(r.begin(), r.end(), 0.0, std::plus<double>{}, [](double a) {return a * a * a; }) / r.size();
    double x4m = std::transform_reduce(r.begin(), r.end(), 0.0, std::plus<double>{}, [](double a) {return a * a * a * a; }) / r.size();

    double xym = std::transform_reduce(x.begin(), x.end(), y.begin(), 0.0, std::plus<double>{}, std::multiplies<double>{});
    xym /= fmin(x.size(), y.size());

    double x2ym = std::transform_reduce(x.begin(), x.end(), y.begin(), 0.0, std::plus<double>{}, [](double a, double b) { return a * a * b; });
    x2ym /= fmin(x.size(), y.size());

    double sxx = x2m - xm * xm;
    double sxy = xym - xm * ym;
    double sxx2 = x3m - xm * x2m;
    double sx2x2 = x4m - x2m * x2m;
    double sx2y = x2ym - x2m * ym;

    double b = (sxy * sx2x2 - sx2y * sxx2) / (sxx * sx2x2 - sxx2 * sxx2);
    double c = (sx2y * sxx - sxy * sxx2) / (sxx * sx2x2 - sxx2 * sxx2);
    double a = ym - b * xm - c * x2m;

    auto abc = [a, b, c](int xx) {
        return a + b * xx + c * xx*xx;
    };

    std::cout << "y = " << a << " + " << b << "x + " << c << "x^2" << std::endl;
    std::cout << " Input  Approximation" << std::endl;
    std::cout << " x   y     y1" << std::endl;

    auto xit = x.cbegin();
    auto xend = x.cend();
    auto yit = y.cbegin();
    auto yend = y.cend();
    while (xit != xend && yit != yend) {
        printf("%2d %3d  %5.1f\n", *xit, *yit, abc(*xit));
        xit = std::next(xit);
        yit = std::next(yit);
    }
}

int main() {
    using namespace std;

    vector<int> x(11);
    iota(x.begin(), x.end(), 0);

    vector<int> y{ 1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321 };

    polyRegression(x, y);

    return 0;
}


  

You may also check:How to resolve the algorithm Numbers with equal rises and falls step by step in the BASIC programming language
You may also check:How to resolve the algorithm Disarium numbers step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the eC programming language
You may also check:How to resolve the algorithm Polynomial regression step by step in the TI-89 BASIC programming language
You may also check:How to resolve the algorithm OpenWebNet password step by step in the C++ programming language