double num2_VolumeSphere (double pLR, int k) /* Returns volume of unit sphere in dimension k, norm p */ { const double eps = 2.0 * DBL_EPSILON; int p = (int)pLR; double kLR = (double) k; double Vol; int s; util_Assert (pLR >= 0.0, "num2_VolumeSphere: p < 0"); if (fabs (pLR - p) <= eps) { switch (p) { case 0: return num_TwoExp[k]; break; case 1: return num_TwoExp[k] / num2_Factorial (k); break; case 2: if ((k % 2) == 0) { return pow (num_Pi, kLR / 2.0) / num2_Factorial (k / 2); } else { s = (k + 1) / 2; return pow (num_Pi, (double) (s) - 1.0) * num2_Factorial (s) * num_TwoExp[2 * s] / num2_Factorial (2 * s); } break; default: break; } } Vol = kLR * (num_Ln2 + num2_LnGamma (1.0 + 1.0 / pLR)) - num2_LnGamma (1.0 + kLR / pLR); return exp (Vol); }
static void InitFDistMeans (int n, double Coef[]) /* * Initializes the distribution for svaria_SampleMean by computing the * coefficients. We shall keep the value of n in element Coef[SAM_LIM], * since we shall need it to get the value of the distribution at x. */ { int s; double z; fmass_INFO Q; z = num2_Factorial (n); /* This uses the binomial formulae, but is not the binomial probability distribution since p + q != 1 */ Q = fmass_CreateBinomial (n, -1.0, 1.0); for (s = 0; s <= n; s++) Coef[s] = fmass_BinomialTerm2 (Q, s) / z; fmass_DeleteBinomial (Q); Coef[SAM_LIM] = n; if (swrite_Classes) { printf ("---------------------------------------\n"); for (s = 0; s <= n; s++) { printf (" Coeff[%2d] = %14.6g\n", s, Coef[s]); } printf ("\n"); } }
static double ProbabiliteG (int jmin, int j, double g) /* * Returns the probability that the minimum number of random U(0,1) whose * sum is larger than g is j+1. g cannot be too large because the * calculation here becomes numerically unstable. */ { int s; double temp; const double jLR = j; double signe; /* +1 or -1 */ double somme; signe = 1.0; somme = 0.0; for (s = 0; s <= jmin; s++) { temp = signe * num2_Combination (j + 1, s); temp *= pow (g - s, jLR); somme += temp; signe = -signe; } somme = (jLR + 1.0 - g) * somme / num2_Factorial (j + 1); return somme; }
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); }