Beispiel #1
0
void sknuth_MaxOft (unif01_Gen * gen, sknuth_Res1 * res,
   long N, long n, int r, int d, int t)
{
   long Seq;                      /* Replication number */
   double tReal = t;
   double dReal = d;
   double NbExp;                  /* Expected number in each class */
   double MaxU;
   double U;
   long Groupe;
   int j, Indice;
   double *P;
   double Par[1];
   double X2;
   double V[1];                   /* Number degrees of freedom for Chi2 */
   char str[LENGTH + 1];
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "sknuth_MaxOft test";
   sres_Basic *Bas;
   sres_Chi2 *Chi;

   Timer = chrono_Create ();
   Par[0] = t;

   NbExp = n / dReal;
   if (swrite_Basic)
      WriteDataMaxOft (gen, TestName, N, n, r, d, t, NbExp);
   util_Assert (NbExp >= gofs_MinExpected,
      "MaxOft:   NbExp < gofs_MinExpected");
   if (res == NULL) {
      localRes = TRUE;
      res = sknuth_CreateRes1 ();
   }
   InitRes1 (res, N, d);
   Bas = res->Bas;
   Chi = res->Chi;
   Chi->jmin = 0;
   Chi->jmax = d - 1;
   for (j = 0; j < d; j++) {
      Chi->Loc[j] = j;
      Chi->NbExp[j] = NbExp;
   }

   sprintf (str, "The N statistic values (a ChiSquare with %1d degrees"
                 " of freedom):", d - 1);
   statcoll_SetDesc (Chi->sVal1, str);
   Chi->degFree = d - 1;
   statcoll_SetDesc (Bas->sVal1,
      "The N statistic values (the Anderson-Darling p-values):");
   P = util_Calloc ((size_t) n + 1, sizeof (double));

   for (Seq = 1; Seq <= N; Seq++) {
      for (Indice = 0; Indice < d; Indice++)
         Chi->Count[Indice] = 0;
      for (Groupe = 1; Groupe <= n; Groupe++) {
         /* Generate a vector and find the max value */
         MaxU = unif01_StripD (gen, r);
         for (j = 1; j < t; j++) {
            U = unif01_StripD (gen, r);
            if (U > MaxU)
               MaxU = U;
         }
         /* For the chi2 */
         Indice = pow (MaxU, tReal) * dReal;
         ++Chi->Count[Indice];

         /* For the Anderson-Darling */
         P[Groupe] = MaxU;
      }
      if (swrite_Counters)
         tables_WriteTabL (Chi->Count, 0, d - 1, 5, 10, "Observed numbers:");

      /* Value of the chi2 statistic */
      X2 = gofs_Chi2Equal (NbExp, Chi->Count, 0, d - 1);
      statcoll_AddObs (Chi->sVal1, X2);

      /* Value of the Anderson-Darling statistic */
      gofw_ActiveTests1 (P, n, FDistMax, Par, Bas->sVal2, Bas->pVal2);
      statcoll_AddObs (Bas->sVal1, Bas->pVal2[gofw_AD]);
   }
   util_Free (P);

   V[0] = d - 1;
   gofw_ActiveTests2 (Chi->sVal1->V, Chi->pVal1->V, N, wdist_ChiSquare, V,
      Chi->sVal2, Chi->pVal2);
   Chi->pVal1->NObs = N;
   sres_GetChi2SumStat (Chi);

   gofw_ActiveTests2 (Bas->sVal1->V, Bas->pVal1->V, N, wdist_Unif,
      (double *) NULL, Bas->sVal2, Bas->pVal2);
   Bas->pVal1->NObs = N;

   if (swrite_Collectors) {
      statcoll_Write (Chi->sVal1, 5, 14, 4, 3);
      statcoll_Write (Bas->sVal1, 5, 14, 4, 3);
   }
   if (swrite_Basic) {
      if (N == 1) {
         swrite_AddStrChi (str, LENGTH, Chi->degFree);
         gofw_WriteActiveTests2 (N, Chi->sVal2, Chi->pVal2, str);
      } else {
         printf ("\n-----------------------------------------------\n");
         printf ("Test results for chi2 with %2ld degrees of freedom:\n",
                 Chi->degFree);
         gofw_WriteActiveTests0 (N, Chi->sVal2, Chi->pVal2);
         swrite_Chi2SumTest (N, Chi);
      }

      if (N == 1) {
         gofw_WriteActiveTests2 (N, Bas->sVal2, Bas->pVal2,
            "Anderson-Darling statistic            :");
      } else {
         printf ("\n-----------------------------------------------\n");
         printf ("Test results for Anderson-Darling:\n");
         gofw_WriteActiveTests0 (N, Bas->sVal2, Bas->pVal2);
      }
      printf ("\n");
      swrite_Final (gen, Timer);
   }
   if (localRes)
      sknuth_DeleteRes1 (res);
   chrono_Delete (Timer);
}
Beispiel #2
0
void sknuth_Run (unif01_Gen * gen, sres_Chi2 * res,
   long N, long n, int r, lebool Up)
{
   long Seq;                      /* Replication number */
   double U;
   double UPrec;                  /* Preceding value of U */
   double nReal = n;
   double A[6][6];
   double B[6];
   double *NbExp;
   long k;
   int j, i;
   long Longueur;                 /* Current length of the sequence */
   double Khi;
   long *Count;
   char str[LENGTH + 1];
   double V[1];                   /* Number degrees of freedom for Chi2 */
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "sknuth_Run test";

   Timer = chrono_Create ();
   if (swrite_Basic)
      WriteDataRun (gen, TestName, N, n, r, Up);

   if (n < 600)
      return;
   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateChi2 ();
   }
   sres_InitChi2 (res, N, 6, "sknuth_Run");
   NbExp = res->NbExp;
   Count = res->Count;
   res->jmin = 1;
   res->jmax = 6;

   A[0][0] =   4529.35365;
   A[0][1] =   9044.90208;
   A[0][2] =  13567.9452;
   A[0][3] =  18091.2672;
   A[0][4] =  22614.7139;
   A[0][5] =  27892.1588;
   A[1][1] =  18097.0254;
   A[1][2] =  27139.4552;
   A[1][3] =  36186.6493;
   A[1][4] =  45233.8198;
   A[1][5] =  55788.8311;
   A[2][2] =  40721.3320;
   A[2][3] =  54281.2656;
   A[2][4] =  67852.0446;
   A[2][5] =  83684.5705;
   A[3][3] =  72413.6082;
   A[3][4] =  90470.0789;
   A[3][5] = 111580.110;
   A[4][4] = 113261.815;
   A[4][5] = 139475.555;
   A[5][5] = 172860.170;

   for (i = 2; i <= 6; i++) {
      for (j = 1; j < i; j++)
         A[i - 1][j - 1] = A[j - 1][i - 1];
   }

   B[0] = 1.0 / 6.0;
   B[1] = 5.0 / 24.0;
   B[2] = 11.0 / 120.0;
   B[3] = 19.0 / 720.0;
   B[4] = 29.0 / 5040.0;
   B[5] = 1.0 / 840.0;
   for (i = 1; i <= 6; i++) {
      NbExp[i] = nReal * B[i - 1];
      res->Loc[i] = i;
   }

   if (swrite_Classes)
      /* gofs_Classes (NbExp, NULL, 1, 6, 0); */
      tables_WriteTabD (NbExp, 1, 6, 1, 20, 2, 1, "Expected numbers:");

   statcoll_SetDesc (res->sVal1,
      "The N statistic values (a ChiSquare with 6 degrees of freedom):");
   res->degFree = 6;

   /* Beginning of test */
   for (Seq = 1; Seq <= N; Seq++) {
      for (i = 1; i <= 6; i++)
         Count[i] = 0;
      Longueur = 1;
      UPrec = unif01_StripD (gen, r);
      /* Generate n numbers */
      for (k = 1; k < n; k++) {
         U = unif01_StripD (gen, r);
         if ((Up && U < UPrec) || (!Up && U > UPrec)) {
            /* The end of a "Run" */
            ++Count[Longueur];
            Longueur = 1;
         } else if (Longueur < 6)
            ++Longueur;
         UPrec = U;
      }
      ++Count[Longueur];

      if (swrite_Counters)
         tables_WriteTabL (Count, 1, 6, 5, 10, "Observed numbers:");

      /* Compute modified Chi2 for a sequence */
      Khi = 0.0;
      for (i = 1; i <= 6; i++) {
	 for (j = 1; j <= 6; j++) {
	    Khi += A[i-1][j-1]*(Count[i] - NbExp[i])*(Count[j] - NbExp[j]);
	 }
      }
      statcoll_AddObs (res->sVal1, Khi / (nReal - 6.0));
   }

   V[0] = 6;
   gofw_ActiveTests2 (res->sVal1->V, res->pVal1->V, N, wdist_ChiSquare, V,
      res->sVal2, res->pVal2);
   res->pVal1->NObs = N;
   sres_GetChi2SumStat (res);

   if (swrite_Collectors)
      statcoll_Write (res->sVal1, 5, 14, 4, 3);

   if (swrite_Basic) {
      swrite_AddStrChi (str, LENGTH, res->degFree);
      gofw_WriteActiveTests2 (N, res->sVal2, res->pVal2, str);
      swrite_Chi2SumTest (N, res);
      swrite_Final (gen, Timer);
   }
   if (localRes)
      sres_DeleteChi2 (res);
   chrono_Delete (Timer);
}
Beispiel #3
0
void sknuth_RunIndep (unif01_Gen * gen, sres_Chi2 * res,
   long N, long n, int r, lebool Up)
{
   long Seq;                      /* Replication number */
   double U;
   double UPrec;                  /* Preceding value of U */
   double X2;
   long Nb;
   long k;
   int i;
   long Longueur;                 /* Current length of the sequence */
   long *Count;
   double *NbExp;
   double Prob[7];
   char str[LENGTH + 1];
   double V[1];                   /* Number degrees of freedom for Chi2 */
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "sknuth_RunIndep test";

   Timer = chrono_Create ();
   if (swrite_Basic)
      WriteDataRun (gen, TestName, N, n, r, Up);

   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateChi2 ();
   }
   sres_InitChi2 (res, N, 6, "sknuth_RunIndep");
   NbExp = res->NbExp;
   Count = res->Count;
   res->jmin = 1;
   res->jmax = 6;
   sprintf (str, "NumExpected[6] < %.1f", gofs_MinExpected);

   for (i = 1; i <= 5; i++) {
      Prob[i] = 1.0 / num2_Factorial (i) - 1.0 / num2_Factorial (i + 1);
   }
   Prob[6] = 1.0 / num2_Factorial (6);

   statcoll_SetDesc (res->sVal1,
      "The N statistic values (a ChiSquare with 5 degrees of freedom):");
   res->degFree = 5;

   for (Seq = 1; Seq <= N; Seq++) {
      for (i = 1; i <= 6; i++)
         Count[i] = 0;
      Longueur = 1;
      UPrec = unif01_StripD (gen, r);
      for (k = 1; k <= n; k++) {
         U = unif01_StripD (gen, r);
         if ((Up && U < UPrec) || (!Up && U > UPrec)) {
            /* The end of a "Run" */
            ++Count[Longueur];
            Longueur = 1;
            U = unif01_StripD (gen, r);
         } else if (Longueur < 6)
            ++Longueur;
         UPrec = U;
      }
      ++Count[Longueur];

      Nb = 0;
      for (i = 1; i <= 6; i++)
         Nb += Count[i];
      for (i = 1; i <= 6; i++)
         NbExp[i] = Nb * Prob[i];

      if (swrite_Counters) {
         tables_WriteTabD (NbExp, 1, 6, 1, 20, 2, 1, "Expected numbers:");
         tables_WriteTabL (Count, 1, 6, 1, 17, "Observed numbers:");
      }
      /*     util_Warning (NbExp[6] < gofs_MinExpected, str); */

      X2 = gofs_Chi2 (NbExp, Count, 1, 6);
      statcoll_AddObs (res->sVal1, X2);
   }

   V[0] = 5;
   gofw_ActiveTests2 (res->sVal1->V, res->pVal1->V, N, wdist_ChiSquare, V,
      res->sVal2, res->pVal2);
   res->pVal1->NObs = N;
   sres_GetChi2SumStat (res);

   if (swrite_Collectors)
      statcoll_Write (res->sVal1, 5, 14, 4, 3);

   if (swrite_Basic) {
      swrite_AddStrChi (str, LENGTH, res->degFree);
      gofw_WriteActiveTests2 (N, res->sVal2, res->pVal2, str);
      swrite_Chi2SumTest (N, res);
      swrite_Final (gen, Timer);
   }
   if (localRes)
      sres_DeleteChi2 (res);
   chrono_Delete (Timer);
}
Beispiel #4
0
void sknuth_SimpPoker (unif01_Gen *gen, sres_Chi2 *res,
                       long N, long n, int r, int d, int k)
{
   long Seq;                      /* Replication number */
   long NbGroups;                 /* Number of classes */
   long jhigh;
   long jlow;
   long Groupe;
   long L;
   int Minkd;
   int s, j;
   double X2;
   double Mult;
   double *NbExp;
   long *Loca;
   long *Nb;
   lebool Occurs[1 + Maxkd];
   double **M;
   double V[1];                   /* Number degrees of freedom for Chi2 */
   char str[LENGTH + 1];
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "sknuth_SimpPoker test";

   Timer = chrono_Create ();
   if (swrite_Basic)
      WriteDataPoker (gen, TestName, N, n, r, d, k);

   util_Assert (d <= Maxkd, "sknuth_SimpPoker:   d > 127");
   util_Assert (k <= Maxkd, "sknuth_SimpPoker:   k > 127");
   util_Assert (d > 1, "sknuth_SimpPoker:   d < 2");
   util_Assert (k > 1, "sknuth_SimpPoker:   k < 2");
   if (k < d)
      Minkd = k;
   else
      Minkd = d;

   num2_CalcMatStirling (&M, Minkd, k);

   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateChi2 ();
   }
   sres_InitChi2 (res, N, Minkd, "sknuth_SimpPoker");
   NbExp = res->NbExp;
   Nb = res->Count;
   Loca = res->Loc;

   /* NbExp[s] = n * d * (d-1) * ... * (d-s+1) * M [s,k] / d^k.  */
   Mult = n * pow ((double) d, -(double) k);
   for (s = 1; s <= Minkd; s++) {
      Mult *= d - s + 1;
      NbExp[s] = Mult * M[s][k];
   }
   jlow = 1;
   jhigh = Minkd;
   if (swrite_Classes)
      gofs_WriteClasses (NbExp, Loca, jlow, jhigh, 0);
   gofs_MergeClasses (NbExp, Loca, &jlow, &jhigh, &NbGroups);
   if (swrite_Classes)
      gofs_WriteClasses (NbExp, Loca, jlow, jhigh, NbGroups);
   res->jmin = jlow;
   res->jmax = jhigh;
   res->degFree = NbGroups - 1;
   if (res->degFree < 1) {
      if (localRes)
         sres_DeleteChi2 (res);
      return;
   }
   sprintf (str, "The N statistic values (a ChiSquare with %1ld degrees"
                 " of freedom):", NbGroups - 1);
   statcoll_SetDesc (res->sVal1, str);

   for (Seq = 1; Seq <= N; Seq++) {
      for (s = 1; s <= Minkd; s++)
         Nb[s] = 0;
      for (Groupe = 1; Groupe <= n; Groupe++) {
         /* Draw one poker hand */
         for (j = 0; j < d; j++)
            Occurs[j] = FALSE;
         s = 0;                   /* s = number of different values */
         for (j = 1; j <= k; j++) {
            L = unif01_StripL (gen, r, d);
            if (!Occurs[L]) {
               Occurs[L] = TRUE;
               ++s;
            }
         }
         ++Nb[Loca[s]];
      }
      if (swrite_Counters)
         tables_WriteTabL (Nb, jlow, jhigh, 5, 10, "Observed numbers:");

      X2 = gofs_Chi2 (NbExp, Nb, jlow, jhigh);
      statcoll_AddObs (res->sVal1, X2);
   }

   V[0] = NbGroups - 1;
   gofw_ActiveTests2 (res->sVal1->V, res->pVal1->V, N, wdist_ChiSquare, V,
      res->sVal2, res->pVal2);
   res->pVal1->NObs = N;
   sres_GetChi2SumStat (res);

   if (swrite_Collectors) {
      statcoll_Write (res->sVal1, 5, 14, 4, 3);
   }
   if (swrite_Basic) {
      swrite_AddStrChi (str, LENGTH, res->degFree);
      gofw_WriteActiveTests2 (N, res->sVal2, res->pVal2, str);
      swrite_Chi2SumTest (N, res);
      swrite_Final (gen, Timer);
   }
   num2_FreeMatStirling (&M, Minkd);
   if (localRes)
      sres_DeleteChi2 (res);
   chrono_Delete (Timer);
}
Beispiel #5
0
void sknuth_CouponCollector (unif01_Gen * gen, sres_Chi2 * res,
   long N, long n, int r, int d)
{
   long Seq;                      /* Replication number */
   long Segm;
   const int t = MAXT;
   long tt = t;
   int dInt = d;
   long dd = d;
   int s, k;
   long NbGroups;
   double Moydes;
   double Mult;
   double dReal = d;
   double **M;
   double *NbExp;
   long *Loca;
   long *Nb;
   lebool Occurs[1 + MAXT];
   double X2;
   double V[1];                   /* Number degrees of freedom for Chi2 */
   char str[LENGTH + 1];
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "sknuth_CouponCollector test";

   Timer = chrono_Create ();
   if (swrite_Basic)
      WriteDataCoupCol (gen, TestName, N, n, r, d);

   util_Assert (d < MAXT, "sknuth_CouponCollector:  d >= 62");
   util_Assert (d > 1, "sknuth_CouponCollector:  d < 2");

   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateChi2 ();
   }
   sres_InitChi2 (res, N, MAXT, "sknuth_CouponCollector");
   NbExp = res->NbExp;
   Nb = res->Count;
   Loca = res->Loc;

   /* Compute the expected number of segments of each length */
   /* NbExp [s] = n * d! * Stirling (d-1, s-1) / d^s for d <= s <= t - 1 */
   /* NbExp [t] = n * (1 - d! * Stirling (d, t-1) / d^{t-1}) */
   dInt = d;
   num2_CalcMatStirling (&M, d, t - 1);
   Mult = n;
   for (s = 1; s <= d; s++) {
      Mult *= s / dReal;
   }
   NbExp[d] = Mult;
   Moydes = d * Mult;
   for (s = d + 1; s < t; s++) {
      Mult /= dReal;
      NbExp[s] = Mult * M[d - 1][s - 1];
      Moydes += s * NbExp[s];
   }
   NbExp[t] = n - Mult * M[d][t - 1];
   Moydes += t * NbExp[t];
   Moydes /= n;
 /* 
   if (swrite_Basic) {
       printf ("   Expected value of s = ");
	   num_WriteD (Moydes, 10, 2, 2);
      printf ("\n\n");
   }
 */
   if (swrite_Classes)
      gofs_WriteClasses (NbExp, Loca, d, t, 0);
   gofs_MergeClasses (NbExp, Loca, &dd, &tt, &NbGroups);
   if (swrite_Classes)
      gofs_WriteClasses (NbExp, Loca, dd, tt, NbGroups);
   res->jmin = dd;
   res->jmax = tt;
   res->degFree = NbGroups - 1;
   if (res->degFree < 1) {
      if (localRes)
         sres_DeleteChi2 (res);
      return;
   }

   sprintf (str, "The N statistic values (a ChiSquare with %1ld degrees"
                 " of freedom):", NbGroups - 1);
   statcoll_SetDesc (res->sVal1, str);

   /* Beginning of test */
   for (Seq = 1; Seq <= N; Seq++) {
      for (s = dInt; s <= MAXT; s++)
         Nb[s] = 0;
      for (Segm = 1; Segm <= n; Segm++) {
         /* One collection of values. */
         for (k = 0; k < dInt; k++)
            Occurs[k] = FALSE;
         ++Nb[Loca[NRepet (gen, dInt, r, Occurs)]];
      }
      if (swrite_Counters)
         tables_WriteTabL (Nb, dd, tt, 5, 10, "Observed numbers:");

      X2 = gofs_Chi2 (NbExp, Nb, dd, tt);
      statcoll_AddObs (res->sVal1, X2);
   }

   V[0] = NbGroups - 1;
   gofw_ActiveTests2 (res->sVal1->V, res->pVal1->V, N, wdist_ChiSquare, V,
      res->sVal2, res->pVal2);
   res->pVal1->NObs = N;
   sres_GetChi2SumStat (res);

   if (swrite_Collectors) {
      statcoll_Write (res->sVal1, 5, 14, 4, 3);
   }
   if (swrite_Basic) {
      swrite_AddStrChi (str, LENGTH, res->degFree);
      gofw_WriteActiveTests2 (N, res->sVal2, res->pVal2, str);
      swrite_Chi2SumTest (N, res);
      swrite_Final (gen, Timer);
   }
   num2_FreeMatStirling (&M, d);
   if (localRes)
      sres_DeleteChi2 (res);
   chrono_Delete (Timer);
}
Beispiel #6
0
void svaria_SumCollector (unif01_Gen * gen, sres_Chi2 * res,
   long N, long n, int r, double g)
{
   const double gmax = 10.0;      /* Maximal value of g */
   const int jmax = 50;           /* Maximal number of classes */
   int j;                         /* Class index */
   long Seq;
   long i;
   double X;
   double Y;
   double Sum;
   long NbClasses;
   long *Loc;
   double V[1];
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "svaria_SumCollector test";
   char chaine[LEN1 + 1] = "";
   char str[LEN2 + 1];

   Timer = chrono_Create ();
   if (swrite_Basic)
      WriteDataSumColl (gen, TestName, N, n, r, g);

   if (g < 1.0 || g > gmax) {
      util_Error ("svaria_SumCollector:   g < 1.0 or g > 10.0");
   }
   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateChi2 ();
   }
   sres_InitChi2 (res, N, jmax, "svaria_SumCollector");
   Loc = res->Loc;

   res->jmin = g;
   res->jmax = jmax;
   Sum = 0.0;
   for (j = res->jmin; j < jmax; j++) {
      res->NbExp[j] = n * ProbabiliteG (res->jmin, j, g);
      Sum += res->NbExp[j];
   }
   res->NbExp[jmax] = util_Max (0.0, n - Sum);

   if (swrite_Classes)
      gofs_WriteClasses (res->NbExp, Loc, res->jmin, res->jmax, 0);
   gofs_MergeClasses (res->NbExp, Loc, &res->jmin, &res->jmax, &NbClasses);
   if (swrite_Classes)
      gofs_WriteClasses (res->NbExp, Loc, res->jmin, res->jmax, NbClasses);

   strncpy (chaine, "SumCollector sVal1:   chi2 with ", (size_t) LEN1);
   sprintf (str, "%ld", NbClasses - 1);
   strncat (chaine, str, (size_t) LEN2);
   strncat (chaine, " degrees of freedom", (size_t) LEN1);
   statcoll_SetDesc (res->sVal1, chaine);
   res->degFree = NbClasses - 1;
   if (res->degFree < 1) {
      if (localRes)
         sres_DeleteChi2 (res);
      return;
   }

   for (Seq = 1; Seq <= N; Seq++) {
      for (j = 1; j <= jmax; j++)
         res->Count[j] = 0;

      for (i = 1; i <= n; i++) {
         X = 0.0;
         j = 0;
         do {
            X += unif01_StripD (gen, r);
            ++j;
         }
         while (X <= g);
         if (j > res->jmax)
            ++res->Count[res->jmax];
         else
            ++res->Count[Loc[j - 1]];
      }
      if (swrite_Counters)
         tables_WriteTabL (res->Count, res->jmin, res->jmax, 5, 10,
                           "Observed numbers:");
      Y = gofs_Chi2 (res->NbExp, res->Count, res->jmin, res->jmax);
      statcoll_AddObs (res->sVal1, Y);
   }

   V[0] = NbClasses - 1;
   gofw_ActiveTests2 (res->sVal1->V, res->pVal1->V, N, wdist_ChiSquare, V,
                      res->sVal2, res->pVal2);
   res->pVal1->NObs = N;
   sres_GetChi2SumStat (res);
   
   if (swrite_Collectors)
      statcoll_Write (res->sVal1, 5, 14, 4, 3);

   if (swrite_Basic) {
      swrite_AddStrChi (str, LEN2, res->degFree);
      gofw_WriteActiveTests2 (N, res->sVal2, res->pVal2, str);
      swrite_Chi2SumTest (N, res);
      swrite_Final (gen, Timer);
   }
   if (localRes)
      sres_DeleteChi2 (res);
   chrono_Delete (Timer);
}
Beispiel #7
0
void sknuth_Gap (unif01_Gen *gen, sres_Chi2 *res,
                 long N, long n, int r, double Alpha, double Beta)
{
   int len;
   int t;
   long m;                        /* Number of observed Gaps */
   long Seq;                      /* Current replication number */
   double p;                      /* Probability of U01 in (Alpha, Beta) */
   double X2;
   double U;
   double Mult;
   double V[1];                   /* Number of degrees of freedom for Chi2 */
   char str[LENGTH + 1];
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "sknuth_Gap test";

   Timer = chrono_Create ();
   p = Beta - Alpha;
   t = (int)(log (gofs_MinExpected / n) / num2_log1p (-p));
   len = (int)(1 + log (gofs_MinExpected / (n*p)) / num2_log1p (-p));
   t = util_Min(t, len);
   t = util_Max(t, 0);

   Mult = p * n;
   if (swrite_Basic)
      WriteDataGap (gen, TestName, N, n, r, Alpha, Beta);

   util_Assert (Alpha >= 0.0 && Alpha <= 1.0,
                "sknuth_Gap:   Alpha outside interval [0..1]");
   util_Assert (Beta <= 1.0 && Beta > Alpha,
                "sknuth_Gap:   Beta outside interval (Alpha..1]");

   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateChi2 ();
   }
   sres_InitChi2 (res, N, t, "sknuth_Gap");

   sprintf (str, "The N statistic values (a ChiSquare with %1d degrees"
                 " of freedom):", t);
   statcoll_SetDesc (res->sVal1, str);
   res->degFree = t;
   if (res->degFree < 1) {
      util_Warning (TRUE, "Chi-square with 0 degree of freedom.");
      if (localRes)
         sres_DeleteChi2 (res);
      chrono_Delete (Timer);
      return;
   }

   /* Compute the probabilities for each gap length */
   res->NbExp[0] = Mult;
   res->Loc[0] = 0;
   for (len = 1; len < t; len++) {
      Mult *= 1.0 - p;
      res->NbExp[len] = Mult;
      res->Loc[len] = len;
   }
   res->NbExp[t] = Mult * (1.0 - p) / p;
   res->Loc[t] = t;
   if (swrite_Classes)
      gofs_WriteClasses (res->NbExp, res->Count, 0, t, 0);

   /* Beginning of test */
   for (Seq = 1; Seq <= N; Seq++) {
      for (len = 0; len <= t; len++)
         res->Count[len] = 0;
      for (m = 1; m <= n; m++) {
         /* Process one gap */
         len = 0;
         U = unif01_StripD (gen, r);
         while ((U < Alpha || U >= Beta) && len < n) {
            ++len;
            U = unif01_StripD (gen, r);
         }
         if (len >= n) {
            util_Warning (TRUE,
   "sknuth_Gap:   one gap of length > n\n*********  Interrupting the test\n");
            printf ("\n\n");
            res->pVal2[gofw_Mean] = res->pVal2[gofw_AD]
                   = res->pVal2[gofw_KSM] = res->pVal2[gofw_KSP] = 0.0;
            if (localRes)
               sres_DeleteChi2 (res);
            chrono_Delete (Timer);
            return;
         }
         if (len >= t)
            ++res->Count[t];
         else
            ++res->Count[len];
      }
      if (swrite_Counters)
         tables_WriteTabL (res->Count, 0, t, 5, 10, "Observed numbers:");

      X2 = gofs_Chi2 (res->NbExp, res->Count, 0, t);
      statcoll_AddObs (res->sVal1, X2);
   }

   V[0] = t;
   gofw_ActiveTests2 (res->sVal1->V, res->pVal1->V, N, wdist_ChiSquare, V,
                      res->sVal2, res->pVal2);
   sres_GetChi2SumStat (res);

   if (swrite_Collectors)
      statcoll_Write (res->sVal1, 5, 14, 4, 3);

   if (swrite_Basic) {
      swrite_AddStrChi (str, LENGTH, res->degFree);
      gofw_WriteActiveTests2 (N, res->sVal2, res->pVal2, str);
      swrite_Chi2SumTest (N, res);
      swrite_Final (gen, Timer);
   }
   if (localRes)
      sres_DeleteChi2 (res);
   chrono_Delete (Timer);
}
Beispiel #8
0
static int svaria_CollisionArgMax_00 (unif01_Gen *gen, sres_Chi2 *res,
   long N, long n, int r, long k, long m)
/*
 * Return 0 if no error, otherwise return != 0.
 */
{
   double X;
   double U;
   double Max;
   long NbColl;
   long Indice = -1;
   long j;
   long i;
   long Rep;
   long Seq;
   long NbClasses;
   long *Loc;
   int *Urne;
   double V[1];
   fmass_INFO Q;
   lebool localRes = FALSE;
   chrono_Chrono *chro, *Timer;
   char *TestName = "svaria_CollisionArgMax test";
   char chaine[LEN1 + 1] = "";
   char str[LEN2 + 1];

   Timer = chrono_Create ();
   if (swrite_Basic)
      WriteDataArgMax (gen, TestName, N, n, r, k, m);

   util_Assert (n <= 4 * k, "svaria_CollisionArgMax:   n > 4k");
   /*   util_Assert (m > 2.0 * gofs_MinExpected,
	"svaria_CollisionArgMax:    m <= 2*gofs_MinExpected"); */

   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateChi2 ();
   }
   sres_InitChi2 (res, N, n, "svaria_CollisionArgMax");
   Loc = res->Loc;
   Urne = util_Calloc ((size_t) k + 1, sizeof (int));

   if (svaria_Timer) {
      printf ("-----------------------------------------------");
      printf ("\nCPU time to initialize the collision distribution:  ");
      chro = chrono_Create ();
   }
   Q = smultin_CreateCollisions (n, (smultin_CellType) k);
   if (svaria_Timer) {
      chrono_Write (chro, chrono_hms);
      printf ("\n\n");
   }

   /* Compute the expected numbers of collisions: m*P(j) */
   for (j = 0; j <= n; j++)
      res->NbExp[j] = m * smultin_CollisionsTerm (Q, j);
   smultin_DeleteCollisions (Q);

   res->jmin = 0;
   res->jmax = n;
   if (swrite_Classes)
      gofs_WriteClasses (res->NbExp, Loc, res->jmin, res->jmax, 0);

   gofs_MergeClasses (res->NbExp, Loc, &res->jmin, &res->jmax, &NbClasses);

   if (swrite_Classes)
      gofs_WriteClasses (res->NbExp, Loc, res->jmin, res->jmax, NbClasses);

   strncpy (chaine, "CollisionArgMax sVal1:   chi2 with ", (size_t) LEN1);
   sprintf (str, "%ld", NbClasses - 1);
   strncat (chaine, str, (size_t) LEN2);
   strncat (chaine, " degrees of freedom", (size_t) LEN1);
   statcoll_SetDesc (res->sVal1, chaine);
   res->degFree = NbClasses - 1;
   if (res->degFree < 1) {
      if (localRes)
         sres_DeleteChi2 (res);
      return 1;
   }

   if (svaria_Timer)
      chrono_Init (chro);

   for (Seq = 1; Seq <= N; Seq++) {
      for (j = 0; j <= n; j++)
         res->Count[j] = 0;

      for (Rep = 1; Rep <= m; Rep++) {
         for (j = 0; j <= k; j++)
            Urne[j] = -1;

         NbColl = 0;
         for (j = 1; j <= n; j++) {
            Max = -1.0;
            for (i = 1; i <= k; i++) {
               U = unif01_StripD (gen, r);
               if (U > Max) {
                  Max = U;
                  Indice = i;
               }
            }
            if (Urne[Indice] < 0)
               Urne[Indice] = 1;
            else
               ++NbColl;
         }
         if (NbColl > res->jmax)
            ++res->Count[res->jmax];
         else
            ++res->Count[Loc[NbColl]];
      }
      if (swrite_Counters)
         tables_WriteTabL (res->Count, res->jmin, res->jmax, 5, 10,
                           "Observed numbers:");
      X = gofs_Chi2 (res->NbExp, res->Count, res->jmin, res->jmax);
      statcoll_AddObs (res->sVal1, X);
   }

   if (svaria_Timer) {
      printf ("\n----------------------------------------------\n"
              "CPU time for the test           :  ");
      chrono_Write (chro, chrono_hms);
      printf ("\n\n");
      chrono_Delete (chro);
   }

   V[0] = NbClasses - 1;
   gofw_ActiveTests2 (res->sVal1->V, res->pVal1->V, N, wdist_ChiSquare, V,
                      res->sVal2, res->pVal2);
   res->pVal1->NObs = N;
   sres_GetChi2SumStat (res);
   
   if (swrite_Collectors)
      statcoll_Write (res->sVal1, 5, 14, 4, 3);

   if (swrite_Basic) {
      swrite_AddStrChi (str, LEN2, res->degFree);
      gofw_WriteActiveTests2 (N, res->sVal2, res->pVal2, str);
      swrite_Chi2SumTest (N, res);
      swrite_Final (gen, Timer);
   }
   util_Free (Urne);
   if (localRes)
      sres_DeleteChi2 (res);
   chrono_Delete (Timer);
   return 0;
}
Beispiel #9
0
void svaria_WeightDistrib (unif01_Gen * gen, sres_Chi2 * res,
   long N, long n, int r, long k, double Alpha, double Beta)
{
   long W;
   long j;
   long i;
   long Seq;
   double X;
   double U;
   double p;
   double nLR = n;
   double V[1];
   long NbClasses;
   long *Loc;
   fmass_INFO Q;
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "svaria_WeightDistrib test";
   char chaine[LEN1 + 1] = "";
   char str[LEN2 + 1];

   Timer = chrono_Create ();
   if (swrite_Basic)
      WriteDataWeight (gen, TestName, N, n, r, k, Alpha, Beta);

   /*   util_Assert (n >= 3.0 * gofs_MinExpected,
	"svaria_WeightDistrib:   n is too small"); */
   util_Assert (Alpha <= 1.0 && Alpha >= 0.0,
      "svaria_WeightDistrib:    Alpha must be in [0, 1]");
   util_Assert (Beta <= 1.0 && Beta >= 0.0,
      "svaria_WeightDistrib:    Beta must be in [0, 1]");
   p = Beta - Alpha;

   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateChi2 ();
   }
   sres_InitChi2 (res, N, k, "svaria_WeightDistrib");
   Loc = res->Loc;

   /* Compute binomial probabilities and multiply by n */
   Q = fmass_CreateBinomial (k, p, 1.0 - p);
   for (i = 0; i <= k; i++)
      res->NbExp[i] = nLR * fmass_BinomialTerm2 (Q, i);
   fmass_DeleteBinomial (Q);

   res->jmin = 0;
   res->jmax = k;
   if (swrite_Classes)
      gofs_WriteClasses (res->NbExp, Loc, res->jmin, res->jmax, 0);

   /* Merge classes for the chi-square */
   gofs_MergeClasses (res->NbExp, Loc, &res->jmin, &res->jmax, &NbClasses);

   if (swrite_Classes)
      gofs_WriteClasses (res->NbExp, Loc, res->jmin, res->jmax, NbClasses);

   strncpy (chaine, "WeightDistrib sVal1:   chi2 with ", (size_t) LEN1);
   sprintf (str, "%ld", NbClasses - 1);
   strncat (chaine, str, (size_t) LEN2);
   strncat (chaine, " degrees of freedom", (size_t) LEN1);
   statcoll_SetDesc (res->sVal1, chaine);
   res->degFree = NbClasses - 1;
   if (res->degFree < 1) {
      if (localRes)
         sres_DeleteChi2 (res);
      return;
   }

   for (Seq = 1; Seq <= N; Seq++) {
      for (i = 0; i <= k; i++)
         res->Count[i] = 0;
      for (i = 1; i <= n; i++) {
         W = 0;
         for (j = 1; j <= k; j++) {
            U = unif01_StripD (gen, r);
            if (U >= Alpha && U < Beta)
               ++W;
         }
         if (W > res->jmax)
            ++res->Count[res->jmax];
         else
            ++res->Count[Loc[W]];
      }
      if (swrite_Counters)
         tables_WriteTabL (res->Count, res->jmin, res->jmax, 5, 10,
                           "Observed numbers:");

      X = gofs_Chi2 (res->NbExp, res->Count, res->jmin, res->jmax);
      statcoll_AddObs (res->sVal1, X);
   }

   V[0] = NbClasses - 1;
   gofw_ActiveTests2 (res->sVal1->V, res->pVal1->V, N, wdist_ChiSquare, V,
                      res->sVal2, res->pVal2);
   res->pVal1->NObs = N;
   sres_GetChi2SumStat (res);

   if (swrite_Collectors)
      statcoll_Write (res->sVal1, 5, 14, 4, 3);

   if (swrite_Basic) {
      swrite_AddStrChi (str, LEN2, res->degFree);
      gofw_WriteActiveTests2 (N, res->sVal2, res->pVal2, str);
      swrite_Chi2SumTest (N, res);
      swrite_Final (gen, Timer);
   }
   if (localRes)
      sres_DeleteChi2 (res);
   chrono_Delete (Timer);
}
Beispiel #10
0
static void EntropyDisc00 (unif01_Gen * gen, sentrop_Res * res,
   long N, long n, int r, int s, int L)
/*
 * Test based on the discrete entropy, proposed by Compagner and L'Ecuyer
 */
{
   long Seq;
   long j;
   long i;
   double EntropyNorm;            /* Normalized entropy */
   double Entropy;                /* Value of entropy S */
   double EntropyPrev;            /* Previous value of entropy */
   double SumSq;                  /* To compute the covariance */
   double Sigma, Mu;              /* Parameters of the normal law */
   double tem;
   double nLR = n;
   long d;                        /* 2^s */
   long C;                        /* 2^L */
   long LSurs;                    /* L / s */
   long sSurL;                    /* s / L */
   long nLSurs;                   /* nL / s */
   double xLgx[NLIM + 1];         /* = -i/n * Lg (i/n) */
   unsigned long Block;
   unsigned long Number;
   unsigned int LL = L;
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "sentrop_EntropyDisc test";

   Timer = chrono_Create ();
   if (s <= L && L % s) {
      util_Error ("EntropyDisc00:   s <= L and L % s != 0");
   }
   if (s > L && s % L) {
      util_Error ("EntropyDisc00:   s > L and s % L != 0");
   }

   d = num_TwoExp[s];
   C = num_TwoExp[L];

   if (s <= L)
      LSurs = L / s;
   else {
      sSurL = s / L;
      nLSurs = n / sSurL;
      if (n % sSurL)
         ++nLSurs;
   }

   util_Assert (n / num_TwoExp[L] < NLIM,
      "sentrop_EntropyDisc:    n/2^L is too large");
   smultin_MultinomMuSigma (n, num_TwoExp[L], 0.0, (double) n,
        FoncMNEntropie, &Mu, &Sigma);

   if (swrite_Basic)
      WriteDataDisc (gen, TestName, N, n, r, s, L, Mu, Sigma);

   if (res == NULL) {
      localRes = TRUE;
      res = sentrop_CreateRes ();
   }
   InitRes (res, N, C - 1, "sentrop_EntropyDisc");
   CalcLgx (xLgx, n);

   statcoll_SetDesc (res->Bas->sVal1, "EntropyDisc sVal1");
   statcoll_SetDesc (res->Bas->pVal1, "EntropyDisc pVal1");
   SumSq = EntropyPrev = 0.0;

   for (Seq = 1; Seq <= N; Seq++) {
      for (i = 0; i < C; i++)
         res->Count[i] = 0;

      if (s <= L) {
         for (i = 1; i <= n; i++) {
            Block = unif01_StripB (gen, r, s);
            for (j = 2; j <= LSurs; j++)
               Block = Block * d + unif01_StripB (gen, r, s);
            ++res->Count[Block];
         }

      } else {                    /* s > L */
         for (i = 1; i <= nLSurs; i++) {
            Number = unif01_StripB (gen, r, s);
            for (j = 1; j <= sSurL; j++) {
               Block = Number % C;
               ++res->Count[Block];
               Number >>= LL;
            }
         }
      }

      /* Compute entropy */
      Entropy = 0.0;
      for (i = 0; i < C; i++) {
         if (res->Count[i] > NLIM) {
            tem = res->Count[i] / nLR;
            tem *= -num_Log2 (tem);
            Entropy += tem;
         } else if (res->Count[i] > 0) {
            Entropy += xLgx[res->Count[i]];
         }
      }
      EntropyNorm = (Entropy - Mu) / Sigma;
      statcoll_AddObs (res->Bas->sVal1, EntropyNorm);
      SumSq += EntropyNorm * EntropyPrev;
      EntropyPrev = EntropyNorm;

      if (swrite_Counters)
         tables_WriteTabL (res->Count, 0, C - 1, 5, 10, "Counters:");

      if (swrite_Collectors) {
         printf ("Entropy = ");
         num_WriteD (Entropy, 15, 6, 1);
         printf ("\n");
      }
   }

   gofw_ActiveTests2 (res->Bas->sVal1->V, res->Bas->pVal1->V, N, wdist_Normal,
      (double *) NULL, res->Bas->sVal2, res->Bas->pVal2);
   res->Bas->pVal1->NObs = N;
   sres_GetNormalSumStat (res->Bas);

   /* We now test the correlation between successive values of the entropy.
      The next SumSq should have mean 0 and variance 1. */
   if (N > 1) {
      res->Bas->sVal2[gofw_Cor] = SumSq / sqrt ((double) N);
      res->Bas->pVal2[gofw_Cor] = fbar_Normal1 (res->Bas->sVal2[gofw_Cor]);
   }
   if (swrite_Collectors) {
      statcoll_Write (res->Bas->sVal1, 5, 14, 4, 3);
   }
   if (swrite_Basic) {
      WriteResultsDisc (N, res->Bas->sVal2, res->Bas->pVal2, res->Bas);
      swrite_Final (gen, Timer);
   }

   if (localRes)
      sentrop_DeleteRes (res);
   chrono_Delete (Timer);
}
Beispiel #11
0
void sentrop_EntropyDiscOver2 (unif01_Gen * gen, sentrop_Res * res,
   long N, long n, int r, int s, int L)
{
   long i, j;                     /* Indices */
   unsigned long B2, B1, B0;      /* Blocks of bits */
   long Seq;                      /* Replication number */
   double Entropy;                /* Value of the entropy S */
   double tempPrev;               /* Previous value of the entropy */
   double SumSq;                  /* To compute the covariance */
   double Corr;                   /* Empirical correlation */
   double Var;                    /* Empirical variance */
   double Mean;                   /* Empirical mean */
   double Sigma, Mu;              /* Parameters of the normal law */
   double Sum2, Sum;              /* Temporary variables */
   unsigned long d;               /* 2^s */
   long C;                        /* 2^L */
   unsigned long CLC;             /* 2^L */
   long m0;                       /* m0 = ceil (L/s) */
   long m;                        /* m = n/s */
   double xLgx[NLIM + 1];         /* = -i/n * Lg (i/n) */
   double NLR = N;
   double temp, E1;
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "sentrop_EntropyDiscOver2 test";

   Timer = chrono_Create ();
   InitExactOver (n, L, &Mu, &Sigma);
   if (swrite_Basic)
      WriteDataDisc (gen, TestName, N, n, r, s, L, Mu, Sigma);

   util_Assert (L <= n, "sentrop_EntropyDiscOver2:   L > n");
   util_Assert (L <= 15, "sentrop_EntropyDiscOver2:   L > 15");
   util_Assert (r <= 31, "sentrop_EntropyDiscOver2:   r > 31");
   util_Assert (s <= 31, "sentrop_EntropyDiscOver2:   s > 31");
   util_Assert (L + s <= 31, "sentrop_EntropyDiscOver2:   L+s > 31");
   util_Assert (n % s == 0, "sentrop_EntropyDiscOver2:   n % s != 0");

   d = num_TwoExp[s];
   m = n / s;
   m0 = L / s;
   if (m0 * s < L)
      ++m0;
   /* B0 must not be larger than LONG_MAX (31 bits) */
   util_Assert (m0 * s <= 31, "sentrop_EntropyDiscOver2:   m0 * s > 31");
   C = num_TwoExp[L];
   CLC = num_TwoExp[L];

   if (res == NULL) {
      localRes = TRUE;
      res = sentrop_CreateRes ();
   }
   InitRes (res, N, C - 1, "sentrop_EntropyDiscOver2");
   tempPrev = SumSq = Sum2 = Sum = 0.0;
   CalcLgx (xLgx, n);

   for (Seq = 1; Seq <= N; Seq++) {
      for (i = 0; i < C; i++)
         res->Count[i] = 0;

      B0 = unif01_StripB (gen, r, s);
      for (j = 2; j <= m0; j++)
         B0 = B0 * d + unif01_StripB (gen, r, s);

      /* B0 now contains the bits 0,...,0,b_1,...,b_{m0*s} */
      B2 = B0;

      /* Count the blocks of L bits in b_1,...,b_{m0*s} */
      for (i = 0; i <= m0 * s - L; i++) {
         ++res->Count[B2 % CLC];
         B2 >>= 1;
      }
      B1 = B0 % CLC;
      B0 = B2 % CLC;
      /* B1 contains 0,...,0,b_{m0*s-L+1},...,b_{m0*s} */
      /* B0 contains 0,...,0,b_1,...,b_{L-1} */
      for (j = 1; j <= m - m0; j++) {
         B1 = B1 * d + unif01_StripB (gen, r, s);
         B2 = B1;
         B1 %= CLC;
         /* B1 and B2 contain L bits and L+s bits, resp. */
         for (i = 1; i <= s; i++) {
            ++res->Count[B2 % CLC];
            B2 >>= 1;
         }
      }

      /* B1 contains 0,...,0,b_{m*s-L+1},...,b_{m*s}. */
      /* Her we must have 2 * L <= 31. */
      B2 = B0 + B1 * (CLC / 2);
      /* B2 contains 0,..,0,b_{m*s-L+1},..,b_{m*s},b_1,...,b_{L-1}. */
      /* Now count blocks with overlap. */
      for (i = 1; i < L; i++) {
         ++res->Count[B2 % CLC];
         B2 >>= 1;
      }

      /* Compute entropy */
      Entropy = 0.0;
      for (i = 0; i < C; i++) {
         util_Assert (res->Count[i] <= NLIM,
            "sentrop_EntropyDiscOver2:   NLIM is too small");
         Entropy += xLgx[res->Count[i]];
      }

#ifdef STABLE
      /* Ideally, we should use the moving average for numerical stability.
         But we shall use the first observed value of instead; it should be
         typical and will prevent loss of precision (unless it is 0). */

      if (1 == Seq)
         E1 = Entropy;
      temp = Entropy - E1;
      Sum += temp;
      Sum2 += temp * temp;
      SumSq += temp * tempPrev;
      tempPrev = temp;

#else
      /* The naive unstable method */
      Sum += Entropy;
      Sum2 += Entropy * Entropy;
      SumSq += Entropy * tempPrev;
      tempPrev = Entropy;
#endif

      if (swrite_Counters)
         tables_WriteTabL (res->Count, 0, C - 1, 5, 10, "Counters:");

      if (swrite_Collectors) {
         printf ("Entropy = ");
         num_WriteD (Entropy, 15, 6, 1);
         printf ("\n");
      }
   }

   /* We now test the correlation between successive values of the */
   /* entropy. Corr should have mean 0 and variance 1. */

#ifdef STABLE
   Mean = Sum / NLR + E1;
   Var = Sum2 / NLR - (E1 - Mean) * (E1 - Mean);
   Var *= NLR / (NLR - 1.0);
   temp = (Entropy + E1 * NLR - 2.0 * NLR * Mean) * E1 / (NLR - 1.0);
   Corr = SumSq / (NLR - 1.0) - temp - Mean * Mean;
   if (Var <= 0.0) {
      Corr = 1.0e100;
      util_Warning (TRUE,
         "Empirical variance <= 0.   Correlation set to 1e100.");
   } else
      Corr /= Var;

#else
   /* Naive calculations. Here, there could be huge losses of precision
      because Mean*Mean, Sum2/NLR, and SumSq/(NLR - 1.0) may be very close. */
   Mean = Sum / NLR;
   Var = (Sum2 / NLR - Mean * Mean) * NLR / (NLR - 1.0);
   Corr = (SumSq / (NLR - 1.0) - Mean * Mean) / Var;
#endif

   if (Sigma > 0.0) {
      /* We know the true values of Mu and Sigma */
      res->Bas->sVal2[gofw_Mean] = (Mean - Mu) * sqrt (NLR) / Sigma;
      res->Bas->pVal2[gofw_Mean] = fbar_Normal1 (res->Bas->sVal2[gofw_Mean]);
   } else
      res->Bas->sVal2[gofw_Mean] = -1.0;

   res->Bas->sVal2[gofw_Cor] = Corr * sqrt (NLR);
   res->Bas->pVal2[gofw_Cor] = fbar_Normal1 (res->Bas->sVal2[gofw_Cor]);

   if (swrite_Basic) {
      WriteResultsDiscOver (res, NLR, Sum2, SumSq, Mu, Sigma, Mean, Var,
         Corr);
      swrite_Final (gen, Timer);
   }
   if (localRes)
      sentrop_DeleteRes (res);
   chrono_Delete (Timer);
}
Beispiel #12
0
void sentrop_EntropyDiscOver (unif01_Gen * gen, sentrop_Res * res,
   long N, long n, int r, int s, int L)
{

   long i;                        /* Index */
   unsigned long Block1, Block0;  /* Blocks of bits */
   long Seq;                      /* Replication number */
   double Entropy;                /* Value of the entropy S */
   double tempPrev;               /* Previous value of entropy */
   double SumSq;                  /* To compute the covariance */
   double Corr;                   /* Empirical correlation */
   double Var;                    /* Empirical variance */
   double Mean;                   /* Empirical mean */
   double Sigma, Mu;              /* Parameters of the normal law */
   double Sum2, Sum;              /* Temporary variables */
   long d;                        /* 2^s */
   long C;                        /* 2^L */
   long nSurs;                    /* n / s */
   double xLgx[NLIM + 1];         /* = -i/n * Lg (i/n) */
   double NLR = N;
   double temp, E1;
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "sentrop_EntropyDiscOver test";

   Timer = chrono_Create ();
   InitExactOver (n, L, &Mu, &Sigma);
   if (swrite_Basic)
      WriteDataDisc (gen, TestName, N, n, r, s, L, Mu, Sigma);

   util_Assert (L <= n - L, "sentrop_EntropyDiscOver:   L > n-L");
   util_Assert (n <= 31, "sentrop_EntropyDiscOver:   n > 31");
   util_Assert (r <= 31, "sentrop_EntropyDiscOver:   r > 31");
   util_Assert (s <= 31, "sentrop_EntropyDiscOver:   s > 31");
   util_Assert (n % s == 0, "sentrop_EntropyDiscOver:   n % s != 0");
   util_Assert (N > 1, "sentrop_EntropyDiscOver:   N <= 1");

   d = num_TwoExp[s];
   C = num_TwoExp[L];
   nSurs = n / s;

   if (res == NULL) {
      localRes = TRUE;
      res = sentrop_CreateRes ();
   }
   InitRes (res, N, C - 1, "sentrop_EntropyDiscOver");
   CalcLgx (xLgx, n);
   tempPrev = SumSq = Sum2 = Sum = 0.0;

   for (Seq = 1; Seq <= N; Seq++) {
      for (i = 0; i < C; i++)
         res->Count[i] = 0;

      Block0 = unif01_StripB (gen, r, s);
      for (i = 2; i <= nSurs; i++)
         Block0 = Block0 * d + unif01_StripB (gen, r, s);

      /* Compute entropy of the block of n bits = Block0. */
      /* This block has less than 31 bits. */
      Block1 = Block0;
      for (i = 0; i <= n - L - 1; i++) {
         ++res->Count[Block1 % C];
         Block1 >>= 1;
      }
      Block1 = (Block1 % C) + C * (Block0 % C);
      for (i = n - L; i < n; i++) {
         ++res->Count[Block1 % C];
         Block1 >>= 1;
      }

      Entropy = 0.0;
      for (i = 0; i < C; i++) {
         util_Assert (res->Count[i] <= NLIM,
            "sentrop_EntropyDiscOver:   NLIM is too small");
         Entropy += xLgx[res->Count[i]];
      }

#ifdef STABLE
      /* Ideally, we should use the moving average for numerical stability.
         But we shall use the first observed value instead; it should be
         typical and will prevent loss of precision (unless it is 0). */
      if (1 == Seq)
         E1 = Entropy;
      temp = Entropy - E1;
      Sum += temp;
      Sum2 += temp * temp;
      SumSq += temp * tempPrev;
      tempPrev = temp;

#else
      /* The naive method: it is simple but numerically unstable. It can be
         used for debugging and testing the more stable calculation in the
         case of small samples. */
      Sum += Entropy;
      Sum2 += Entropy * Entropy;
      SumSq += Entropy * tempPrev;
      tempPrev = Entropy;
#endif

      if (swrite_Counters)
         tables_WriteTabL (res->Count, 0, C - 1, 5, 10, "Counters:");

      if (swrite_Collectors) {
         printf ("Entropy = ");
         num_WriteD (Entropy, 15, 6, 1);
         printf ("\n");
      }
   }

   /* We now test the correlation between successive values of the entropy.
      Corr should have mean 0 and variance 1. We use a numerically stable
      calculation. */

#ifdef STABLE
   Mean = Sum / NLR + E1;
   Var = Sum2 / NLR - (E1 - Mean) * (E1 - Mean);
   Var *= NLR / (NLR - 1.0);
   temp = (Entropy + E1 * NLR - 2.0 * NLR * Mean) * E1 / (NLR - 1.0);
   Corr = SumSq / (NLR - 1.0) - temp - Mean * Mean;
   if (Var <= 0.0) {
      Corr = 1.0e100;
      util_Warning (TRUE,
         "Empirical variance <= 0.   Correlation set to 1e100.");
   } else
      Corr /= Var;

#else
   /* Naive calculations. Here, there could be huge losses of precision
      because Mean*Mean, Sum2/NLR, and SumSq/(NLR - 1.0) may be very close. */
   Mean = Sum / NLR;
   Var = (Sum2 / NLR - Mean * Mean) * NLR / (NLR - 1.0);
   Corr = (SumSq / (NLR - 1.0) - Mean * Mean) / Var;

#endif


   if (Sigma > 0.0) {
      /* We know the true values of Mu and Sigma */
      res->Bas->sVal2[gofw_Mean] = (Mean - Mu) * sqrt (NLR) / Sigma;
      res->Bas->pVal2[gofw_Mean] = fbar_Normal1 (res->Bas->sVal2[gofw_Mean]);
   } else
      res->Bas->pVal2[gofw_Mean] = -1.0;

   res->Bas->sVal2[gofw_Cor] = Corr * sqrt (NLR);
   res->Bas->pVal2[gofw_Cor] = fbar_Normal1 (res->Bas->sVal2[gofw_Cor]);

   if (swrite_Basic) {
      WriteResultsDiscOver (res, NLR, Sum2, SumSq, Mu, Sigma, Mean, Var,
         Corr);
      swrite_Final (gen, Timer);
   }
   if (localRes)
      sentrop_DeleteRes (res);
   chrono_Delete (Timer);
}