Gaussian fit in C#

怎甘沉沦 提交于 2019-12-21 05:38:12

问题


In a project I'm working on I need to obtain a Gaussian fit from a set of points - needing mean and variance for some processing, and possibly an error degree (or accuracy level) to let me figure out if the set of points really have a normal distribution.

I've found this question

but it is limited to 3 points only - whereas I need a fit that can work with any number of points.

What I need is similar to the labview Gaussian Peak Fit

I have looked at mathdotnet and aforge.net (using both in the same project), but I haven't found anything.

Does anybody know any C# or (easily convertible) C/C++ or Java solutions?

Alternatively, I've been told that an iterative algorithm should be used - I could implement it by myself (if not too much math-complicated). Any idea about what I can use? I've read a lot of articles (on Wikipedia and others found via Google) but I haven't found any clear indication of a solution.


回答1:


Just calculate the mean and standard deviation of your sample, those are the only two parameters of a Gaussian distribution.

For "goodness of fit", you can do something like mean-square error of the CDF.




回答2:


I've found a good implementation in ImageJ, a public domain image processing program, whose source code is available here

Converted to C# and adapted to my needs.

Thanks to you guys for your answers... not strictly related to the solution I found, but somehow I got there with your help :)




回答3:


In Math.Net, you can do:

        var real_σ = 0.5;
        var real_μ = 0;

        //Define gaussian function
        var gaussian = new Func<double, double, double, double>((σ, μ, x) =>
        {
            return Normal.PDF(μ, σ, x);
        });

        //Generate sample gaussian data
        var data = Enumerable.Range(0, 41).Select(x => -2 + x * 0.1)
            .Select(x => new { x = x, y = gaussian(real_σ, real_μ, x) });



        var xs = data.Select(d => d.x).ToArray();
        var ys = data.Select(d => d.y).ToArray();
        var initialGuess_σ = 1;
        var initialGuess_μ = 0;

        var fit = Fit.Curve(xs, ys, gaussian, initialGuess_σ, initialGuess_μ);
        //fit.Item1 = σ, fit.Item2 = μ


来源:https://stackoverflow.com/questions/7741863/gaussian-fit-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!