Example #1
0
int main (void)
{
   unif01_Gen *gen1, *gen2, *gen3;
   long I[3] = { 3, 7, 9 };
   int i, n = 20;
   double x;

   gen1 = utaus_CreateTaus (31, 3, 12, 12345);
   for (i = 0; i < n; i++)
      printf ("%f\n", unif01_StripD (gen1, 0));
   utaus_DeleteGen (gen1);
   printf ("\n");

   gen1 = utaus_CreateTaus (31, 3, 12, 12345);
   gen2 = unif01_CreateLacGen (gen1, 3, I);
   for (i = 0; i < n; i++)
      printf ("%f\n", unif01_StripD (gen2, 0));

   gen3 = unif01_CreateDoubleGen (gen2, 24);
   for (i = 0; i < n; i++)
      x = unif01_StripD (gen3, 0);
   unif01_DeleteDoubleGen (gen3);
   unif01_DeleteLacGen (gen2);

   gen2 = utaus_CreateTaus (28, 7, 14, 12345);
   gen3 = unif01_CreateCombXor2 (gen1, gen2, "A Combined Tausworthe Gener.");
   for (i = 0; i < n; i++)
      x = unif01_StripD (gen3, 0);
   unif01_DeleteCombGen (gen3);
   utaus_DeleteGen (gen2);
   utaus_DeleteGen (gen1);
   return 0;
}
Example #2
0
static void PutPointsGnu (
   unif01_Gen *gen,
   FILE *f,              /* Output file for the points in gnuplot format */
   int Prec              /* Write the points with Prec decimals */
   )
{
   int j;
   long Npoints;                  /* Number of points */

   sprintf (S, "%%%1d", Prec + 5);
   sprintf (str, ".%1df", Prec);
   strcat (S, str);
   for (j = 1; j <= scatter_t; j++)
      V[j] = unif01_StripD (gen, 0);
   Npoints = 0;
   scatter_Nkept = 0;
   while (Npoints < scatter_N) {
      ++Npoints;
      if (Retenu ()) {
         ++scatter_Nkept;
         fprintf (f, S, V[scatter_x]);
         fprintf (f, S, V[scatter_y]);
         fprintf (f, "\n");
      }
      if (scatter_Over) {
         for (j = 1; j < scatter_t; j++)
            V[j] = V[j + 1];
         V[scatter_t] = unif01_StripD (gen, 0);
      } else {
         for (j = 1; j <= scatter_t; j++)
            V[j] = unif01_StripD (gen, 0);
      }
   }
}
Example #3
0
void svaria_SampleProd (unif01_Gen * gen, sres_Basic * res,
   long N, long n, int r, int t)
{
   long i;
   int j;
   long Seq;
   double *P;
   double temp;
   double Par[1];
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "svaria_SampleProd test";

   Timer = chrono_Create ();
   if (swrite_Basic) {
      swrite_Head (gen, TestName, N, n, r);
      printf (",   t = %d\n\n", t);
   }

   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateBasic ();
   }
   sres_InitBasic (res, N, "svaria_SampleProd");

   P = util_Calloc ((size_t) n + 1, sizeof (double));
   statcoll_SetDesc (res->sVal1, "SampleProd sVal1:   Uniform [0, 1]");
   Par[0] = t;

   for (Seq = 1; Seq <= N; Seq++) {
      for (i = 1; i <= n; i++) {
         temp = unif01_StripD (gen, r);
         for (j = 2; j <= t; j++)
            temp *= unif01_StripD (gen, r);
         P[i] = temp;
      }
      gofw_ActiveTests1 (P, n, FDistProd, Par, res->sVal2, res->pVal2);
      statcoll_AddObs (res->sVal1, res->pVal2[gofw_AD]);
   }

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

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

   if (swrite_Basic) {
      gofw_WriteActiveTests2 (N, res->sVal2, res->pVal2,
         "Anderson-Darling statistic            :");
      swrite_Final (gen, Timer);
   }
   util_Free (P);
   if (localRes)
      sres_DeleteBasic (res);
   chrono_Delete (Timer);
}
Example #4
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);
}
Example #5
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);
}
Example #6
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);
}
Example #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);
}
Example #8
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);
}
Example #9
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;
}
Example #10
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);
}
Example #11
0
void svaria_SumLogs (unif01_Gen * gen, sres_Chi2 * res,
   long N, long n, int r)
{
   const double Eps = DBL_EPSILON / 2.0;      /* To avoid log(0) */
   const double Epsilon = 1.E-100;            /* To avoid underflow */
   long i;
   long Seq;
   double u;
   double Prod;
   double Sum;
   double V[1];
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "svaria_SumLogs test";
   char chaine[LEN1 + 1] = "";
   char str[LEN2 + 1];

   Timer = chrono_Create ();
   if (swrite_Basic) {
      swrite_Head (gen, TestName, N, n, r);
      printf ("\n\n");
   }
   util_Assert (n < LONG_MAX/2, "2n > LONG_MAX");
   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateChi2 ();
   }
   sres_InitChi2 (res, N, -1, "svaria_SumLogs");

   strncpy (chaine, "SumLogs sVal1:   chi2 with ", (size_t) LEN1);
   sprintf (str, "%ld", 2 * n);
   strncat (chaine, str, (size_t) LEN2);
   strncat (chaine, " degrees of freedom", (size_t) LEN1);
   statcoll_SetDesc (res->sVal1, chaine);
   res->degFree = 2 * n;
   if (res->degFree < 1) {
      util_Warning (TRUE, "Chi-square with 0 degree of freedom.");
      if (localRes)
         sres_DeleteChi2 (res);
      return;
   }

   for (Seq = 1; Seq <= N; Seq++) {
      Prod = 1.0;
      Sum = 0.0;
      for (i = 1; i <= n; i++) {
         u = unif01_StripD (gen, r);
         if (u < Eps)
            u = Eps;
         Prod *= u;
         if (Prod < Epsilon) {
            Sum += log (Prod);
            Prod = 1.0;
         }
      }
      statcoll_AddObs (res->sVal1, -2.0 * (Sum + log (Prod)));

   }
   V[0] = 2 * n;
   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);
}
Example #12
0
void svaria_SampleCorr (unif01_Gen * gen, sres_Basic * res,
   long N, long n, int r, int k)
{
   long i;
   long Seq;
   double U;
   double Sum;
   double *Pre;                   /* Previous k generated numbers */
   int pos;                       /* Circular index to element at lag k */
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "svaria_SampleCorr test";

   Timer = chrono_Create ();
   if (swrite_Basic) {
      swrite_Head (gen, TestName, N, n, r);
      printf (",   k = %d\n\n", k);
   }
   util_Assert (n > 2, "svaria_SampleCorr:   n <= 2");

   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateBasic ();
   }
   sres_InitBasic (res, N, "svaria_SampleCorr");
   statcoll_SetDesc (res->sVal1,
      "SampleCorr sVal1:   asymptotic standard normal");

   Pre = util_Calloc ((size_t) (k + 1), sizeof (double));

   for (Seq = 1; Seq <= N; Seq++) {
      /* Generate first k numbers U and keep them in Pre */
      for (i = 0; i < k; i++)
         Pre[i] = unif01_StripD (gen, r);

      Sum = 0.0;
      pos = 0;
      /* Element Pre[pos] is at lag k from U */
      for (i = k; i < n; i++) {
         U = unif01_StripD (gen, r);
         Sum += Pre[pos] * U - 0.25;
         Pre[pos] = U;
         pos++;
         pos %= k;
      }
      /* Save standardized correlation */
      statcoll_AddObs (res->sVal1, Sum * sqrt (12.0 / (n - k)));
   }

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

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

   if (swrite_Basic) {
      gofw_WriteActiveTests2 (N, res->sVal2, res->pVal2,
         "Normal statistic                      :");
      swrite_NormalSumTest (N, res);
      swrite_Final (gen, Timer);
   }
   util_Free (Pre);
   if (localRes)
      sres_DeleteBasic (res);
   chrono_Delete (Timer);
}
Example #13
0
void svaria_SampleMean (unif01_Gen * gen, sres_Basic * res,
   long N, long n, int r)
{
   long i;
   long Seq;
   double Sum;
   double Coef[SAM_LIM + 1];
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "svaria_SampleMean test";

   Timer = chrono_Create ();
   if (swrite_Basic) {
      swrite_Head (gen, TestName, N, n, r);
      printf ("\n\n");
   }
   util_Assert (n > 1, "svaria_SampleMean:   n < 2");

   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateBasic ();
   }
   sres_InitBasic (res, N, "svaria_SampleMean");
   if (n < SAM_LIM)
      InitFDistMeans (n, Coef);

   if (n < SAM_LIM)
      statcoll_SetDesc (res->sVal1, "SampleMean sVal1:   n*<U>");
   else
      statcoll_SetDesc (res->sVal1, "SampleMean sVal1:   standard normal");

   for (Seq = 1; Seq <= N; Seq++) {
      Sum = 0.0;
      for (i = 1; i <= n; i++)
         Sum += unif01_StripD (gen, r);

      if (n < SAM_LIM)
         statcoll_AddObs (res->sVal1, Sum);
      else
         statcoll_AddObs (res->sVal1, sqrt (12.0 / n) * (Sum - 0.5 * n));
   }

   if (n < SAM_LIM) {
      gofw_ActiveTests2 (res->sVal1->V, res->pVal1->V, N, FDistMeans, Coef,
                         res->sVal2, res->pVal2);
   } else {
      /* Normal approximation */
      gofw_ActiveTests2 (res->sVal1->V, res->pVal1->V, N, wdist_Normal,
         (double *) NULL, res->sVal2, res->pVal2);
   }
   res->pVal1->NObs = N;

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

   if (swrite_Basic) {
      gofw_WriteActiveTests2 (N, res->sVal2, res->pVal2,
         "Statistic value                       :");
      swrite_Final (gen, Timer);
   }
   if (localRes)
      sres_DeleteBasic (res);
   chrono_Delete (Timer);
}
Example #14
0
void sentrop_EntropyDMCirc (unif01_Gen * gen, sres_Basic * res,
   long N, long n, int r, long m)
{
   long i;                        /* Index */
   double I0, x;
   long Seq;                      /* Replication number */
   double Entropy;                /* Value of the statistic H(m, n) */
   double LnEntropy;
   double SumR;                   /* 1 + 1/2 + ... + 1/(2m-1) */
   double nLR = n;
   double Twom;                   /* 2m */
   double *AU;
   lebool localRes = FALSE;
   chrono_Chrono *Timer;
   char *TestName = "sentrop_EntropyDMCirc test";

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

   Twom = 2 * m;
   I0 = Twom - 1.0;
   SumR = 0.0;
   for (i = 2 * m - 1; i >= 1; i--) {
      SumR += 1.0 / I0;
      I0 -= 1.0;
   }

   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateBasic ();
   }
   sres_InitBasic (res, N, "sentrop_EntropyDMCirc");
   AU = util_Calloc ((size_t) (n + 1), sizeof (double));
   statcoll_SetDesc (res->sVal1,
      "The N statistic values (a standard normal)");

   for (Seq = 1; Seq <= N; Seq++) {
      /* Generate the sample and sort */
      for (i = 1; i <= n; i++)
         AU[i] = unif01_StripD (gen, r);
      tables_QuickSortD (AU, 1, n);

      /* Compute empirical entropy */
      Entropy = 1.0;
      LnEntropy = 0.0;
      for (i = 1; i <= n; i++) {
         if (i - m < 1) {
            Entropy *= (AU[i + m] - AU[n + i - m] + 1.0);
         } else if (i + m > n) {
            Entropy *= (AU[i + m - n] - AU[i - m] + 1.0);
         } else
            Entropy *= (AU[i + m] - AU[i - m]);

         if (Entropy < Epsilon) {
            LnEntropy += log (Entropy);
            Entropy = 1.0;
         }
      }

      Entropy = (LnEntropy + log (Entropy)) / nLR + log (nLR / Twom);

      /* Compute standardized statistic */
      x  = sqrt (3.0 * Twom * nLR) * ((Entropy + log (Twom) + Euler) - SumR);
      statcoll_AddObs (res->sVal1, x);
   }

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

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

   if (swrite_Basic) {
      gofw_WriteActiveTests2 (N, res->sVal2, res->pVal2,
         "Normal statistic                      :");
      swrite_NormalSumTest (N, res);
      swrite_Final (gen, Timer);
   }

   util_Free (AU);
   if (localRes)
      sres_DeleteBasic (res);
   chrono_Delete (Timer);
}