简体   繁体   English

JavaScript 中变量误差的非线性回归

[英]Non-linear regression with errors in variables in JavaScript

I need a robust curve-fitting algorithm that would work in a browser.我需要一个可以在浏览器中运行的强大的曲线拟合算法。 Namely I need it to be able to fit polynomial and trigonometric (and ideally all custom) functions, and it also has to account for errors in both variables.也就是说,我需要它能够拟合多项式和三角函数(最好是所有自定义函数),并且它还必须考虑两个变量中的错误。

I would like to use an existing library or rewrite an implementation written in a different but understandable language (pseudocode, Python, C#, C without much memory magic, etc.) .我想使用现有的库或重写用不同但可以理解的语言编写的实现(伪代码,Python、C#、C,没有太多 memory 魔法等) Alternatively I could use a transpliter to JavaScript if it were possible.或者,如果可能的话,我可以使用转译器到 JavaScript。 However I've searched for hours and haven't found any suitable JavaScript library, nor a straightforward implementation that I could crib.但是,我已经搜索了几个小时,但没有找到任何合适的 JavaScript 库,也没有找到我可以抄袭的直接实现。

I have found two pieces of software that can do what I want.找到了两个可以做我想做的软件。

  • The first one is Gnuplot which is a utility written in C. It's open-source, but I found the code somewhat convoluted and the curve-fitting part was quite inter-dependent with other parts of the program, so I didn't manage to port it to JavaScript.第一个是 Gnuplot,它是一个写在 C 的实用程序。它是开源的,但我发现代码有点复杂,曲线拟合部分与程序的其他部分相互依赖,所以我没有设法将其移植到 JavaScript。
  • The second one is SciPy, a math library for Python. That would be an easy victory if the relevant part were actually written in Python. Which, sadly, is not the case, as instead it's a piece of old Fortran code modified, so that it can communicate with Python. The code was too difficult and archaic for me and Fortran-to-Javascript transpliters didn't work because of the Python-specific stuff in the code.第二个是 SciPy,Python 的数学库。如果相关部分实际上是在 Python 中编写的,那将是一个轻松的胜利。不幸的是,情况并非如此,因为它是一段修改过的旧 Fortran 代码,因此它可以与 Python 通信。代码对我来说太难和陈旧了,而且 Fortran 到 Javascript 的转译器不起作用,因为代码中有 Python 特定的东西。

Do you know any project I could use?你知道我可以使用的任何项目吗? I know it's not going to be a “solve-all answer” but I will appreciate anything that will get me closer to the finish.我知道这不会是一个“解决所有问题的答案”,但我会感激任何能让我更接近完成的东西。

gnuplot can be transcoded via Emscripten to run as javascript in a browser. gnuplot 可以通过 Emscripten 进行转码,以在浏览器中作为 javascript 运行。 See live demonstration site gnuplot + emscripten .请参阅现场演示站点gnuplot + emscripten The resulting javascript variant is not currently supported by the gnuplot project but the proof-of-principle demonstration is impressive. gnuplot 项目目前不支持由此产生的 javascript 变体,但原理验证演示令人印象深刻。

Alglib.js will allow you to fit data data to an arbitrary function. Alglib.js 将允许您将数据数据拟合到任意 function。

Go here for a complete example https://pterodactylus.github.io/Alglib.js/curve_fitting.html Go 这里有一个完整的例子https://pterodactylus.github.io/Alglib.js/curve_fitting.html

<script type="module">
    import {Alglib} from 'https://cdn.jsdelivr.net/gh/Pterodactylus/Alglib.js@master/Alglib-v1.1.0.js'
    //import {Alglib} from '../Alglib-v1.1.0.js'

    var f = function(a_n, x){
        return a_n[3]*Math.pow(x, 3)+a_n[2]*Math.pow(x, 2)+a_n[1]*Math.pow(x, 1)+a_n[0];
    }

    let data = [[-3, 8], [1,3], [5,3], [9,8], [10,16]]
    var fn1 = function(a){
        let sum = 0
        for (let i = 0; i < data.length; ++i) {
            sum = sum + Math.pow(data[i][1] - f(a, data[i][0]), 2)
        }
        let sse = Math.sqrt(sum)
        return sse
    }
    
    let solver = new Alglib()
    solver.add_function(fn1) //Add the first equation to the solver.
    
    solver.promise.then(function(result) { 
        var x_guess = [1,1,1,1] //Guess the initial values of the solution.
        var s = solver.solve("min", x_guess) //Solve the equation
        let x = solver.get_report()
        solver.remove() //required to free the memory in C++
    })
</script>

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

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