server/include/psvt/statistics.cpp
//  Fasano-Franceschini two-dimensional test

Fasano-Franceschini two-dimensional test
Fasano - Franceschini two - dimensional test
unsigned StTest2D(double& KS2D_measure, double& KS2D_estim, double& PearsonCorr, double* unifP, double* unifQ, unsigned NSample) {
    unsigned wtmp[5], maxloc(0);
    double MeanP(0.0), MeanQ(0.0), cov_sum[3], tmpval; 
    const double InvN(1.0 / NSample), TINY(1.E-20);
    KS2D_measure = 0.0;
    memset(cov_sum, 0, 3 * sizeof(double));
    for (unsigned ii = 0; ii < NSample; ii++) {
        memset(wtmp, 0, 5 * sizeof(unsigned));
        for (unsigned jj = 0; jj < NSample; jj++) {
            if (ii < 1) {
                MeanP += unifP[jj];
                MeanQ += unifQ[jj];
            }
            if (unifQ[jj] > unifQ[ii]) {
                unifP[jj] > unifP[ii] ? ++wtmp[1] : ++wtmp[2];
            } else {
                unifP[jj] > unifP[ii] ? ++wtmp[4] : ++wtmp[3];
            }
        }
        for (unsigned kk = 1; kk < 5; kk++) {
            tmpval = fabs(wtmp[kk] * InvN - std::fabs(((((kk + 1) * (kk + 2)) / 2) % 2 - unifP[ii]) *
                                                      (((kk * (kk + 1)) / 2) % 2 - unifQ[ii])));
            if (tmpval > KS2D_measure) {
                maxloc       = ii;
                KS2D_measure = tmpval;
            }
            KS2D_measure = tmpval > KS2D_measure ? tmpval : KS2D_measure;
        }
        if (ii < 1) {
            MeanP *= InvN;
            MeanQ *= InvN;
        }
        cov_sum[0] += (unifP[ii] - MeanP) * (unifQ[ii] - MeanQ);
        cov_sum[1] += (unifP[ii] - MeanP) * (unifP[ii] - MeanP);
        cov_sum[2] += (unifQ[ii] - MeanQ) * (unifQ[ii] - MeanQ);
    }
    PearsonCorr = cov_sum[0] / (TINY + std::sqrt(cov_sum[1] * cov_sum[2]));
    KS2D_estim  = Kolmogorov_D(KS2D_measure * std::sqrt(static_cast<double>(NSample)) /
                              (1.0 + std::sqrt(1.0 - PearsonCorr * PearsonCorr) * (0.25 - 0.75 * std::sqrt(InvN))));
    return (maxloc);
}
unsigned StTest2D ( double & KS2D_measure , double & KS2D_estim , double & PearsonCorr , double * unifP , double * unifQ , unsigned NSample ) { unsigned wtmp [ 5 ] , maxloc ( 0 ) ; double MeanP ( 0.0 ) , MeanQ ( 0.0 ) , cov_sum [ 3 ] , tmpval ; const double InvN ( 1.0 / NSample ) , TINY ( 1.E-20 ) ; KS2D_measure = 0.0 ; memset ( cov_sum , 0 , 3 * sizeof ( double ) ) ; for ( unsigned ii = 0 ; ii < NSample ; ii ++ ) { memset ( wtmp , 0 , 5 * sizeof ( unsigned ) ) ; for ( unsigned jj = 0 ; jj < NSample ; jj ++ ) { if ( ii < 1 ) { MeanP += unifP [ jj ] ; MeanQ += unifQ [ jj ] ; } if ( unifQ [ jj ] > unifQ [ ii ] ) { unifP [ jj ] > unifP [ ii ] ? ++ wtmp [ 1 ] : ++ wtmp [ 2 ] ; } else { unifP [ jj ] > unifP [ ii ] ? ++ wtmp [ 4 ] : ++ wtmp [ 3 ] ; } } for ( unsigned kk = 1 ; kk < 5 ; kk ++ ) { tmpval = fabs ( wtmp [ kk ] * InvN - std :: fabs ( ( ( ( ( kk + 1 ) * ( kk + 2 ) ) / 2 ) % 2 - unifP [ ii ] ) * ( ( ( kk * ( kk + 1 ) ) / 2 ) % 2 - unifQ [ ii ] ) ) ) ; if ( tmpval > KS2D_measure ) { maxloc = ii ; KS2D_measure = tmpval ; } KS2D_measure = tmpval > KS2D_measure ? tmpval : KS2D_measure ; } if ( ii < 1 ) { MeanP *= InvN ; MeanQ *= InvN ; } cov_sum [ 0 ] += ( unifP [ ii ] - MeanP ) * ( unifQ [ ii ] - MeanQ ) ; cov_sum [ 1 ] += ( unifP [ ii ] - MeanP ) * ( unifP [ ii ] - MeanP ) ; cov_sum [ 2 ] += ( unifQ [ ii ] - MeanQ ) * ( unifQ [ ii ] - MeanQ ) ; } PearsonCorr = cov_sum [ 0 ] / ( TINY + std :: sqrt ( cov_sum [ 1 ] * cov_sum [ 2 ] ) ) ; KS2D_estim = Kolmogorov_D ( KS2D_measure * std :: sqrt ( static_cast < double > ( NSample ) ) / ( 1.0 + std :: sqrt ( 1.0 - PearsonCorr * PearsonCorr ) * ( 0.25 - 0.75 * std :: sqrt ( InvN ) ) ) ) ; return ( maxloc ) ; }
Fasano-Franceschini two-dimensional test
Fasano - Franceschini two - dimensional test
// 0 for max,1,2,3,4 -- numbers in corresp quadrants
