Gaussian fit in C#

亡梦爱人 提交于 2019-12-03 17:37:55

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.

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 :)

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