简体   繁体   English

在Java中查找二次回归曲线的最佳方法

[英]Best way to find Quadratic Regression Curve in Java

I've three sets of data such as: 我有三组数据,例如:

x   y
4   0
6   60
8   0

Does anyone know any (efficient) Java codes that can give me back the values of a, b, and c (the coefficients)? 有谁知道任何(有效的)Java代码可以回馈a,b和c(系数)的值?

I assume you want the formula in this form: 我假设你想要这种形式的公式:

y = a * x^2 + b*x + c

If you have only three points you can describe the quadratic curve that goes through all three points with the formula: 如果您只有三个点,则可以使用以下公式描述通过所有三个点的二次曲线:

y = ((x-x2) * (x-x3)) / ((x1-x2) * (x1-x3)) * y1 +
    ((x-x1) * (x-x3)) / ((x2-x1) * (x2-x3)) * y2 +
    ((x-x1) * (x-x2)) / ((x3-x1) * (x3-x2)) * y3

In your example: 在你的例子中:

x1 = 4, y1 = 0, x2 = 6, y2 = 60, x3 = 8, y3 = 0

To get the coefficients a, b, c in terms of x1, x2, x3, y1, y2 and y3 you just need to multiply the formula out and then collect the terms. 要根据x1,x2,x3,y1,y2和y3得到系数a,b,c,您只需要将公式相乘,然后收集项。 It's not difficult, and it will run very fast but it will be quite a lot of code to type in. It would probably be better to look for a package that already does this for you, but if you want to do it yourself, this is how you could do it. 这并不难,而且运行速度非常快,但输入的代码相当多。最好找一个已经为你做的包,但是如果你想自己做,这个你是怎么做到的。

The fact that two of the y terms are zero in your example makes the formula a lot simpler, and you might be able to take advantage of that. 在您的示例中,y个项中的两个为零的事实使得公式更简单,并且您可能能够利用它。 But if that was just a coincidence and not a general rule, then you need the full formula. 但如果这只是巧合而不是一般规则,那么你需要完整的公式。

The LaGrange interpolation is probably the most 'efficient' (how do you measure that?) solution you are going to find. LaGrange插值可能是您将要找到的最“有效”(如何衡量?)解决方案。 So I'll suggest a completely general code. 所以我会建议一个完全通用的代码。 You did want code, right? 你确实想要代码,对吗? This code can go linear, quadratic, cubic, .... for any number of points. 对于任意数量的点, 此代码可以是线性的,二次的,立方的....

I didn't actually try to compile it, so I don't if the source code is up to date. 我实际上并没有尝试编译它,所以如果源代码是最新的,我不会。 You know how online demos go. 你知道在线演示是如何进行的。 Yet the applet from the associated web page is fully functional. 然而,来自相关网页的小程序功能齐全。 The jar file will run standalone. jar文件将独立运行。 With a resizable window, you really don't need to customize it. 使用可调整大小的窗口,您实际上不需要自定义它。

It depends on exactly what you are looking for: Are you looking for the unique polynomial which is defined by those three points, or are you looking for a library which will generate a polynomial which passes through all points? 它取决于您正在寻找的内容:您是在寻找由这三个点定义的唯一多项式,还是在寻找能够生成通过所有点的多项式的库?

If you are looking at the first, the best technique is to construct the coefficient matrix(That is, the set of three linear equations which uniquely constrain this quadratic equation)and apply Gaussian Elimination to get your result. 如果你正在看第一个,最好的技术是构造系数矩阵(即,三个线性方程的集合,它唯一地约束这个二次方程)并应用高斯消元法来得到你的结果。 This can be done by hand the most efficiently, but you can also use The Apache Commons Math Library's Real Matrix solve methods. 这可以通过最有效的手工完成,但您也可以使用Apache Commons Math Library的Real Matrix solve方法。 ( EDIT Thanks for the correction--I speak before I think sometimes ;) 编辑感谢您的纠正 - 我有时候会说话;)

If you are looking at the second, this is specific case of a general class of problems called Interpolation by Polynomials , and there are several ways of solving--Splines are my personal favorite, but all have their strengths and weaknesses. 如果你正在研究第二个问题,这就是一个一般类问题的特殊情况,称为多项式插值 ,有几种解决方法 - Splines是我个人的最爱,但都有自己的优点和缺点。 Luckily, Apache Commons Math implements several such methods. 幸运的是, Apache Commons Math实现了几种这样的方法。 I would look at the SplineInterpolator class. 我会看一下SplineInterpolator类。 Splines use cubics instead of quadratics, but they tend to be very good approximations. 样条曲线使用立方体而不是正方形,但它们往往是非常好的近似值。 They also don't fail if one point is a linear multiple of another. 如果一个点是另一个点的线性倍数,它们也不会失败。

For just three points, both methods should be about equal in performance characteristics. 仅仅三点,两种方法在性能特征上应该大致相同。 If you are doing more than three points, however, I would strongly recommend using interpolation, as using Guassian Elimination scales incredibly poorly( O(n^3)), and Splines(Or another interpolation technique) are less likely to fail. 但是,如果你做了三个以上的点,我强烈建议使用插值,因为使用Guassian Elimination难以置信地缩放(O(n ^ 3)),Splines(或另一种插值技术)不太可能失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM