Does anyone know how to modify this function to get a 4th degree polynomial regression? So that I will get the values for 'd' and 'e'. Do I need to calculate 'xm' up to x8m? Then x3ym and x4ym? I'm not sure what to do after.
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;
}
Comments
Post a Comment