I try to implement plenty of time already (haha) Anderson-Darling normality test into C++. Here is my code. I know there is similar topic here but unfortunatelly it did not solve my problem.
Variance is calculated properly, uniform standartized distribution too I would guess. The problem is that b and c gives me NAN for my sample data 1, 2....10.
Would you have any idea where is the error in the formula - Anderson_Darling() see code below?
The code is cut from the class for better clarity. I did not put here obvious methods like mean() etc.
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;
}
Update - I changed b and c to this, and I get the output I expected.
b = log(phi(Y.at(i)));
c = log(1-phi(Y.at(n - i - 1)));