1 #ifndef POLYNOMIAL_REGRESSION_HPP
2 #define POLYNOMIAL_REGRESSION_HPP
27 : degree_(degree), lambda_(regularizationParameter) {
29 throw std::invalid_argument(
"Degree of the polynomial must be non-negative.");
32 throw std::invalid_argument(
"Regularization parameter must be non-negative.");
41 void train(
const std::vector<double>& x,
const std::vector<double>& y) {
42 if (x.size() != y.size()) {
43 throw std::invalid_argument(
"Feature and target vectors must have the same length.");
46 throw std::invalid_argument(
"Input vectors must not be empty.");
48 if (x.size() <=
static_cast<size_t>(degree_)) {
49 throw std::invalid_argument(
"Number of data points must be greater than the polynomial degree.");
51 computeCoefficients(x, y);
61 double result = coefficients_.back();
62 for (
int i = coefficients_.size() - 2; i >= 0; --i) {
63 result = result * x + coefficients_[i];
79 std::vector<double> coefficients_;
86 void computeCoefficients(
const std::vector<double>& x,
const std::vector<double>& y) {
91 std::vector<std::vector<double>> X(n, std::vector<double>(m));
92 for (
size_t i = 0; i < n; ++i) {
94 for (
int j = 1; j < m; ++j) {
95 X[i][j] = X[i][j - 1] * x[i];
100 std::vector<std::vector<double>> XtX(m, std::vector<double>(m, 0.0));
101 std::vector<double> Xty(m, 0.0);
103 for (
int i = 0; i < m; ++i) {
104 for (
size_t k = 0; k < n; ++k) {
105 Xty[i] += X[k][i] * y[k];
107 for (
int j = i; j < m; ++j) {
108 for (
size_t k = 0; k < n; ++k) {
109 XtX[i][j] += X[k][i] * X[k][j];
111 XtX[j][i] = XtX[i][j];
115 XtX[i][i] += lambda_;
120 coefficients_ = solveLinearSystem(XtX, Xty);
129 std::vector<double> solveLinearSystem(
const std::vector<std::vector<double>>& A,
const std::vector<double>& b) {
131 std::vector<std::vector<double>> L(n, std::vector<double>(n, 0.0));
134 for (
int i = 0; i < n; ++i) {
135 for (
int k = 0; k <= i; ++k) {
137 for (
int j = 0; j < k; ++j) {
138 sum += L[i][j] * L[k][j];
141 double val = A[i][i] - sum;
143 throw std::runtime_error(
"Matrix is not positive-definite.");
145 L[i][k] = std::sqrt(val);
147 L[i][k] = (A[i][k] - sum) / L[k][k];
153 std::vector<double> y(n);
154 for (
int i = 0; i < n; ++i) {
156 for (
int k = 0; k < i; ++k) {
157 sum += L[i][k] * y[k];
159 y[i] = (b[i] - sum) / L[i][i];
163 std::vector<double> x(n);
164 for (
int i = n - 1; i >= 0; --i) {
166 for (
int k = i + 1; k < n; ++k) {
167 sum += L[k][i] * x[k];
169 x[i] = (y[i] - sum) / L[i][i];
Polynomial Regression model for fitting polynomial curves.
Definition: PolynomialRegression.hpp:19
std::vector< double > getCoefficients() const
Get the coefficients of the fitted polynomial.
Definition: PolynomialRegression.hpp:72
PolynomialRegression(int degree, double regularizationParameter=0.0)
Constructor initializing the polynomial degree.
Definition: PolynomialRegression.hpp:26
void train(const std::vector< double > &x, const std::vector< double > &y)
Train the model using features and target values.
Definition: PolynomialRegression.hpp:41
double predict(double x) const
Predicts the output for a given input value.
Definition: PolynomialRegression.hpp:59