java - 如何使用 Apache Commons Math 从曲线中获取残差
问题描述
我正在尝试使用 Commons Math 将多项式曲线拟合到数据集。我有:
- 创建了一个多项式曲线拟合器,
- 得到系数,
- 用这些系数创建了一个多项式函数,并且
- 创建了一个由函数生成的值数组。
或多或少手动创建预测。
我的问题是,我怎样才能得到拟合值和原始观测值之间的残差?
我在 commons.math 代码中看到 PolynomialCurveFitter 创建了一个 LeastSquaresProblem,leastSquaresProblem 可以返回一个评估,并且评估有一个返回残差的方法。
但是,PolynomialCurveFitter 中的 getProblem() 方法是受保护的,所以我不能使用它。
有没有办法从 CurveFitter 获得适当的 LeastSquaresProblem 以便我可以获得残差?
或者我应该扩展 Fitter 以使 getProblem() 可访问?
还是完全有另一种方式?
到目前为止,这是我的代码:
public static double[] fitPolynomial(double[] xCol, double[] yCol, int degree) {
Collection<WeightedObservedPoint> points = new ArrayList<>();
for (int i = 0; i < xCol.length; i++) {
points.add(new WeightedObservedPoint(1.0, xCol[i], yCol[i]));
}
PolynomialCurveFitter fitter = PolynomialCurveFitter.create(degree);
double[] coeff = fitter.fit(points);
PolynomialFunction function = new PolynomialFunction(coeff);
double[] fitted = new double[xCol.length];
for (int i = 0; i < xCol.length; i++) {
fitted[i] = function.value(i);
}
return fitted;
}
解决方案
推荐阅读
- java - 如何修复 java.lang.SecurityException:调用者缺少所需的权限 com.google.android.things.permission.USE_PERIPHERAL_IO 错误?
- php - 如果变量为空,则使用另一个值
- vue.js - 使用 vue-cli 构建深度嵌套的 html 需要很长时间
- python - 改进代码以查找素数
- c# - C# 清除定时器中的richTextBox
- vba - Office 365 受保护的视图破坏 Excel Marcos
- scroll - Bootstrap 4手风琴向下滚动行为
- c++ - 如何在将跟踪对象计数的类中声明一个变量 Created c++
- android - AlarmManager setRepeting 不起作用
- c++ - 如何将局部变量初始化为未知值C++