英文:
Fitting a Globally Monotonically Increasing Polynomial to Data in Python
问题
我有一个数据集,我想以最小二乘的方式拟合一个n次单变量多项式。得到的多项式应该在所有实数上都是单调递增的,而不仅仅是数据范围内。我正在使用Python。
我尝试使用cvxpy创建一个问题,在其中添加了导数在特定点处应为非负的约束。这样我可以创建一个在数据范围内单调递增的多项式,但不能保证多项式在全局范围内都单调递增。
英文:
I have a dataset to which I want to fit a nth degree univariate polynomial in a least squares fashion. The resulting polynomial should be monotonically increasing for all real numbers, not just the range of the data. I am using Python.
I've tried using cvxpy to create a problem where I added constraints that the derivative should be non-negative at specific points. This way I can create a polynomial that is monotonically increasing in the range of data, but it won't guarantee that the polynomial is monotonically increasing globally.
答案1
得分: 1
我明白了。首先,多项式的次数必须是奇数。
以下是我解决一个五次多项式的情况的方法:
如果多项式的导数是一个完全平方数,那么多项式将在全局范围内单调增加。在这种情况下,导数的次数是4。一个四次完全平方的形式为(ax**2 + bx + c)**2。我们可以通过展开这个表达式然后计算其积分来得到我们五次多项式的定义。
我们从参数a、b、c和d开始。d用作偏置参数。然后,我们可以像这样确定原始多项式的系数(多项式的形式将是A*x**5 + B*x**4 + C*x**3 + D*x**2 + E*x + F
):
A = a**2 / 5
B = a * b / 2
C = (2 * a * c + b**2) / 3
D = b * c
E = c**2
F = d
有了这些系数定义,我们可以直接拟合多项式,无需使用任何约束。
英文:
I figured it out. First of all, the degree of the polynomial must be odd.
Here's how I solved a 5th degree polynomial case:
A polynomial will be monotonically increasing globally if its derivative is a perfect square. In this case the degree of the derivative is 4. A fourth degree perfect square has the form (ax**2 + bx + c)**2. We can get a definition for our 5th degree polynomial if we expand this expression and then calculate its integral.
We start with parameters a, b, c, and d. d is serves as a bias parameter. We can then determine the coefficients of the original polynomial like this (the polynomial will be of form A*x**5 + B*x**4 + C*x**3 + D*x**2 + E*x + F
):
A = a**2 / 5
B = a * b / 2
C = (2 * a * c + b**2) / 3
D = b * c
E = c**2
F = d
With these coefficient definitions we can directly fit the polynomial without needing to use any constraints.
答案2
得分: 0
一个类似的问题在这里有讨论,其中讨论了用R语言实现这一功能的软件包(MonoPoly)。我不知道有Python的实现,但R语言的实现细节可以在这篇论文中找到。
英文:
A similar question is addressed here, where a package in R to do exactly this is discussed (MonoPoly). I don't know of a python implementation, but the R implementation details are available in this paper.
Depending on your problem, you might also be able to brute force it and compute the derivative everywhere.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论