C++ 中的 Anderson Darling 测试 [英] Anderson Darling Test in C++

查看:23
本文介绍了C++ 中的 Anderson Darling 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算在 此处找到的 Anderson-Darling 测试.我按照维基百科上的步骤进行操作,并确保在计算数据的平均值和标准偏差时,我使用 MATLAB 来测试表示为 X 的数据.另外,我使用了一个名为 phi 的函数来计算标准的普通 CDF,我还测试了这个函数以确保它是正确的.现在我在实际计算 A 平方时似乎遇到了问题(在维基百科中表示,我在 C++ 中将其表示为 A).

I am trying to compute the Anderson-Darling test found here. I followed the steps on Wikipedia and made sure that when I calculate the average and standard deviation of the data I am testing denoted X by using MATLAB. Also, I used a function called phi for computing the standard normal CDF, I have also tested this function to make sure it is correct which it is. Now I seem to have a problem when I actually compute the A-squared (denoted in Wikipedia, I denote it as A in C++).

这是我为 Anderson-Darling 测试所做的函数:

Here is my function I made for Anderson-Darling Test:

void Anderson_Darling(int n, double X[]){
    sort(X,X + n);
    // Find the mean of X
    double X_avg = 0.0;
    double sum = 0.0;
    for(int i = 0; i < n; i++){
        sum += X[i];
    }
    X_avg = ((double)sum)/n;


    // Find the variance of X
    double X_sig = 0.0;
    for(int i = 0; i < n; i++){
        X_sig += (X[i] - X_avg)*(X[i] - X_avg);
    }
    X_sig /= n;


    // The values X_i are standardized to create new values Y_i
    double Y[n];
    for(int i = 0; i < n; i++){
        Y[i] = (X[i] - X_avg)/(sqrt(X_sig));
        //cout << Y[i] << endl;
    }

    // With a standard normal CDF, we calculate the Anderson_Darling Statistic
    double A = 0.0;
    for(int i = 0; i < n; i++){
        A += -n - 1/n *(2*(i) - 1)*(log(phi(Y[i])) + log(1 - phi(Y[n+1 - i])));
    }
    cout << A << endl;
} 

注意,我知道 Anderson-Darling(A 平方)的公式从 i = 1 开始到 i = n,尽管当我将索引更改为使它在 C++ 中工作,我仍然得到相同的结果而不更改索引.

Note, I know that the formula for Anderson-Darling (A-squared) starts with i = 1 to i = n, although when I changed the index to make it work in C++, I still get the same result without changing the index.

我在 C++ 中得到的价值是:

The value I get in C++ is:

-4e+006

我应该得到的值,在 MATLAB 中收到的是:

The value I should get, received in MATLAB is:

0.2330

非常感谢任何建议.

这是我的全部代码:

#include <iostream>
#include <math.h>
#include <cmath>
#include <random>
#include <algorithm>
#include <chrono>

using namespace std;

double *Box_Muller(int n, double u[]);
double *Beasley_Springer_Moro(int n, double u[]);
void Anderson_Darling(int n, double X[]);
double phi(double x);

int main(){
    int n = 2000;
    double Mersenne[n];
    random_device rd;
    mt19937 e2(1);
    uniform_real_distribution<double> dist(0, 1);
    for(int i = 0; i < n; i++){
        Mersenne[i] = dist(e2);
    }

    // Print Anderson Statistic for Mersenne 6a
    double *result = new double[n];
    result = Box_Muller(n,Mersenne);
    Anderson_Darling(n,result);




    return 0;
}

double *Box_Muller(int n, double u[]){
    double *X = new double[n];
    double Y[n];
    double R_2[n];
    double theta[n];
    for(int i = 0; i < n; i++){
        R_2[i] = -2.0*log(u[i]);
        theta[i] = 2.0*M_PI*u[i+1];
    }
    for(int i = 0; i < n; i++){
        X[i] = sqrt(-2.0*log(u[i]))*cos(2.0*M_PI*u[i+1]);
        Y[i] = sqrt(-2.0*log(u[i]))*sin(2.0*M_PI*u[i+1]);
    }
    return X;
}

double *Beasley_Springer_Moro(int n, double u[]){
    double y[n];
    double r[n+1];
    double *x = new double(n);
    // Constants needed for algo
    double a_0 = 2.50662823884;     double b_0 = -8.47351093090;
    double a_1 = -18.61500062529;   double b_1 = 23.08336743743;
    double a_2 = 41.39119773534;    double b_2 = -21.06224101826;
    double a_3 = -25.44106049637;   double b_3 = 3.13082909833;

    double c_0 = 0.3374754822726147; double c_5 = 0.0003951896511919;
    double c_1 = 0.9761690190917186; double c_6 = 0.0000321767881768;
    double c_2 = 0.1607979714918209; double c_7 = 0.0000002888167364;
    double c_3 = 0.0276438810333863; double c_8 = 0.0000003960315187;
    double c_4 = 0.0038405729373609;

    // Set r and x to empty for now
    for(int i = 0; i <= n; i++){
        r[i] = 0.0;
        x[i] = 0.0;
    }
    for(int i = 1; i <= n; i++){
        y[i] = u[i] - 0.5;
        if(fabs(y[i]) < 0.42){
            r[i] = pow(y[i],2.0);
            x[i] = y[i]*(((a_3*r[i] + a_2)*r[i] + a_1)*r[i] + a_0)/((((b_3*r[i] + b_2)*r[i] + b_1)*r[i] + b_0)*r[i] + 1);
        }else{
            r[i] = u[i];
            if(y[i] > 0.0){
                r[i] = 1.0 - u[i];
                r[i] = log(-log(r[i]));
                x[i] = c_0 + r[i]*(c_1 + r[i]*(c_2 + r[i]*(c_3 + r[i]*(c_4 + r[i]*(c_5 + r[i]*(c_6 + r[i]*(c_7 + r[i]*c_8)))))));
            }
            if(y[i] < 0){
                x[i] = -x[i];
            }
        }
    }
    return x;
}

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


void Anderson_Darling(int n, double X[]){
    sort(X,X + n);
    // Find the mean of X
    double X_avg = 0.0;
    double sum = 0.0;
    for(int i = 0; i < n; i++){
        sum += X[i];
    }
    X_avg = ((double)sum)/n;


    // Find the variance of X
    double X_sig = 0.0;
    for(int i = 0; i < n; i++){
        X_sig += (X[i] - X_avg)*(X[i] - X_avg);
    }
    X_sig /= (n-1);


    // The values X_i are standardized to create new values Y_i
    double Y[n];
    for(int i = 0; i < n; i++){
        Y[i] = (X[i] - X_avg)/(sqrt(X_sig));
        //cout << Y[i] << endl;
    }

    // With a standard normal CDF, we calculate the Anderson_Darling Statistic
    double A = -n;
    for(int i = 0; i < n; i++){
        A +=  -1.0/(double)n *(2*(i+1) - 1)*(log(phi(Y[i])) + log(1 - phi(Y[n - i])));
    }
    cout << A << endl;
}

推荐答案

让我猜猜,你的 n 是 2000.对吧?这里的主要问题是当您在最后一个表达式中执行 1/n 时.1 是一个 int 而 ao 是 n.当您将 1 除以 n 时,它执行整数除法.现在 1 除以任意数 > 1 在整数除法下是 0(认为它是否只保留商的整数部分.您需要做的是通过写 1/(double)n 将 n 转换为 double.

Let me guess, your n was 2000. Right? The major issue here is when you do 1/n in the last expression. 1 is an int and ao is n. When you divide 1 by n it performs integer division. Now 1 divided by any number > 1 is 0 under integer division (think if it as only keeping only integer part of the quotient. What you need to do is cast n as double by writing 1/(double)n.

其余的都应该可以正常工作.

Rest all should work fine.

讨论总结 -

  1. Y[] 的索引应分别为 i 和 n-1-i.
  2. n 不应添加到循环中,而只能添加一次.

小修正,例如在计算方差时将除数更改为 n 而不是 n-1.

Minor fixes like changing divisor to n instead of n-1 while calculating Variance.

这篇关于C++ 中的 Anderson Darling 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆