Skip to content
Snippets Groups Projects
Commit e9e9ea22 authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Add input offset

parent 7d640401
No related branches found
No related tags found
No related merge requests found
......@@ -7,8 +7,24 @@ namespace simox::math
{
LinearRegression3D
LinearRegression3D::Fit(const std::vector<double>& xs, const std::vector<Eigen::Vector3d>& ys)
LinearRegression3D::Fit(
const std::vector<double>& xs,
const std::vector<Eigen::Vector3d>& ys,
bool offsetInput)
{
if (offsetInput and xs.at(0) != 0)
{
double offset = - xs.at(0); // Move x_0 to 0.
std::vector<double> virtualXs = xs;
for (double& x : virtualXs)
{
x = x + offset;
}
LinearRegression3D r = LinearRegression3D::Fit(virtualXs, ys, false);
r.inputOffset = offset;
return r;
}
Eigen::Matrix3Xd ysMatrix(3, ys.size());
for (long col = 0; col < ysMatrix.cols(); ++col)
{
......@@ -43,7 +59,7 @@ namespace simox::math
LinearRegression3D::predict(double x) const
{
Eigen::Vector2d input;
input << 1.0, x;
input << 1.0, x + inputOffset;
return coefficients * input;
}
......
......@@ -35,15 +35,21 @@ namespace simox::math
*/
CoefficientsMatrix coefficients = CoefficientsMatrix::Zero();
/// The input offset, so the virtual input x' = x + offset.
double inputOffset = 0;
/**
* @brief Fit a linear regression model to the given data.
* @param xs The input variables.
* @param ys The output variables.
* @param offsetInput If true, the inputs are offset to x' = x - x_0.
* @return The regression model.
*/
static LinearRegression3D
Fit(const std::vector<double>& xs, const std::vector<Eigen::Vector3d>& ys);
Fit(const std::vector<double>& xs,
const std::vector<Eigen::Vector3d>& ys,
bool offsetInput = false);
/**
* @brief Predict the output variable of the given input variable.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment