2

我已经尝试在 C++中实现大量时间(哈哈)Anderson-Darling正态性测试。这是我的代码。我知道这里有类似的话题但不幸的是它并没有解决我的问题。

方差计算得当,我猜也是均匀的标准化分布。问题是 b 和 c 为我的样本数据 1、2....10 提供了 NAN。

您知道公式中的错误在哪里吗? Anderson_Darling() 请参见下面的代码?

为了更清晰,代码从类中删除。我没有在这里放明显的方法,比如 mean() 等。

double variance()
{
    double var_sum = 0.0;
    for(int i = 0; i < int(size); i++)            //(size is taken from a class)
        var_sum += pow(data.at(i)-mean(),2);
    return var_sum / (int(size)-1);
}

double phi(double x)
{
    double res =0.5 * erfc(-x * M_SQRT1_2);
    return res;
}

vector<double> tostdnormal()
{
    vector<double> Y (size);
    for(int i = 0; i < int(size); i++)
        Y.at(i) = (data.at(i) - mean())/(sqrt(variance()));
    return Y;
}

double Anderson_Darling()
{
    sort(data.begin(),data.end());
    int n = int(size);
    vector<double> Y = tostdnormal();

   double S = 0; double a = 0; double b = 0; double c = 0;
    for(int i = 0; i < n; i++)
    {
        a = 2.0 * (i+1) - 1;
        b = log(Y.at(i));
        c = log(1-Y.at(n-i-1));
        S += a * (b + c);
     }
return -n - S / n;


 }

更新- 我将 b 和 c 更改为此,我得到了我期望的输出。

b = log(phi(Y.at(i)));
c = log(1-phi(Y.at(n - i - 1)));
4

0 回答 0