[英]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.我找到了两个可以做我想做的软件。
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.