简体   繁体   English

如何使正态分布的累积分布函数适合数据点?

[英]How do I fit cumulative distribution function of normal distribution to data points?

I have data which needs to be fitted to the formula 我有需要拟合公式的数据

y = a*CDF[NormalDistribution[m, s], x]

Where I need to find a,m and s 我需要在哪里找到a,m和s

I have tested the fit with Mathematica, and it finds the fit fairly fast and it fits the data nicely. 我已经使用Mathematica测试了拟合度,它发现拟合度相当快,并且很好地拟合了数据。

However, I need to implement this is c#. 但是,我需要实现的是c#。

Currently I've implemented a way to estimate params by going down the gradient hill, but my implementation is very slow (around .5s per estimation) 目前,我已经实现了一种通过沿坡度下降来估算参数的方法,但是我的实现速度非常慢(每次估算约为0.5s)

What is the best way to go about it? 最好的方法是什么?

There is an implementation by John Cook: https://www.johndcook.com/blog/csharp_phi/ 约翰·库克(John Cook)有一个实现: https : //www.johndcook.com/blog/csharp_phi/

and I will replicate it below: 我将在下面复制它:

static double Phi(double x)
{
    // constants
    double a1 = 0.254829592;
    double a2 = -0.284496736;
    double a3 = 1.421413741;
    double a4 = -1.453152027;
    double a5 = 1.061405429;
    double p = 0.3275911;

    // Save the sign of x
    int sign = 1;
    if (x < 0)
        sign = -1;
    x = Math.Abs(x) / Math.Sqrt(2.0);

    // A&S formula 7.1.26
    double t = 1.0 / (1.0 + p*x);
    double y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t * Math.Exp(-x*x);

    return 0.5 * (1.0 + sign*y);
}

static void TestPhi()
{
    // Select a few input values
    double[] x = 
    {
        -3, 
        -1, 
        0.0, 
        0.5, 
        2.1 
    };

    // Output computed by Mathematica
    // y = Phi[x]
    double[] y = 
    { 
        0.00134989803163, 
        0.158655253931, 
        0.5, 
        0.691462461274, 
        0.982135579437 
    };

    double maxError = 0.0;
    for (int i = 0; i < x.Length; ++i)
    {
        double error = Math.Abs(y[i] - Phi(x[i]));
        if (error > maxError)
            maxError = error;
    }

        Console.WriteLine("Maximum error: {0}", maxError);
}

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

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