Test de Kolmogorov-Smirnov
generate_GCL <- function(n, X0, a = 125, c = 1, m = 2^12) {
X <- numeric(n)
X[1] <- X0
for (i in 2:n) {
X[i] <- (a * X[i - 1] + c) %% m
}
return(X / m) }
kolmogorov_smirnov_uniformity_GCL <- function(n, alpha, X0) {
R <- sort(generate_GCL(n, X0))
D_plus <- max((1:n) / n - R)
D_minus <- max(R - (0:(n-1)) / n)
D <- max(D_plus, D_minus)
KS_table <- data.frame(
n = 1:15,
alpha_0.2 = c(0.900, 0.684, 0.565, 0.494, 0.446, 0.410, 0.380,
0.358, 0.322, 0.302, 0.284, 0.274, 0.266, 0.257, 0.247),
alpha_0.15 = c(0.925, 0.726, 0.597, 0.525, 0.470, 0.436, 0.403,
0.388, 0.342, 0.322, 0.304, 0.293, 0.283, 0.273, 0.264),
alpha_0.1 = c(0.950, 0.776, 0.642, 0.564, 0.506, 0.470, 0.438,
0.411, 0.368, 0.348, 0.325, 0.314, 0.304, 0.294, 0.285),
alpha_0.05 = c(0.975, 0.842, 0.708, 0.624, 0.564, 0.510, 0.474,
0.457, 0.408, 0.388, 0.364, 0.349, 0.338, 0.327, 0.318),
alpha_0.01 = c(0.995, 0.929, 0.828, 0.733, 0.669, 0.618, 0.577,
0.543, 0.468, 0.448, 0.418, 0.404, 0.394, 0.382, 0.374)
)
col_name <- paste0("alpha_", alpha)
if (n <= 15) {
D_alpha <- KS_table[[col_name]][n]
} else {
stop("Taille d' é chantillon sup é rieure à 15 : utiliser une
approximation asymptotique.")
}
if (D <= D_alpha) {
result <- paste("Les donn é es suivent une distribution uniforme
avec alpha =", alpha)
} else {
result <- paste("Les donn é es NE suivent PAS une distribution
uniforme avec alpha =", alpha)
}
list(D = D, D_alpha = D_alpha, result = result)
}
n <- 10
alpha <- 0.05
X0 <- 1
test_result <- kolmogorov_smirnov_uniformity_GCL(n, alpha, X0)
cat("D calculé :", test_result$D, "\n")
cat("D_alpha :", test_result$D_alpha, "\n")
cat("Résultat :", test_result$result, "\n")
Générateur congruentiel linéaire (GCL)
La fonction generate_GCL génère des nombres pseudo-aléatoires entre 0 et 1 :
o Entrées :
n : Taille de l'échantillon.
X0 : Graine initiale (valeur de départ).
a, c, m : Paramètres classiques du GCL.
o Sortie : Un vecteur de taille n contenant les valeurs pseudo-aléatoires.
Test de Kolmogorov-Smirnov
La fonction kolmogorov_smirnov_uniformity_GCL teste si les données suivent
une distribution uniforme U(0,1)U(0, 1)U(0,1) :
o Génère les nombres aléatoires avec generate_GCL et les trie.
o Calcule les statistiques D+D^+D+, D−D^-D− et DDD :
o Compare DDD avec la valeur critique Dα(n)D_\alpha(n)Dα(n) issue d'une table
pour différents niveaux de signification (α\alphaα).
o Retourne si les données suivent ou non une distribution uniforme.
Tableau des valeurs critiques
Définit un tableau des valeurs critiques Dα(n)D_\alpha(n)Dα(n) pour n∈[1,15]n \in [1,
15]n∈[1,15] et différents α\alphaα (0.2, 0.15, 0.1, 0.05, 0.01).
Limite : Si n>15, la fonction ne gère pas les cas asymptotiques (cependant, cela est
mentionné avec un message d'erreur).
Résultats
Retourne les valeurs de DDD, DαD_\alphaDα, et la conclusion du test.
Test de Chi2
chi2_table <- data.frame(
df = 1:30,
p_0.90 = c(0.0158, 0.211, 0.584, 1.064, 1.610, 2.204, 2.833, 3.490,
4.168, 4.865,
5.578, 6.304, 7.042, 7.790, 8.547, 9.312, 10.085, 10.865,
11.651, 12.443,
13.240, 14.041, 14.848, 15.659, 16.473, 17.292, 18.114,
18.939, 19.768, 20.599),
p_0.05 = c(3.841, 5.991, 7.815, 9.488, 11.070, 12.592, 14.067,
15.507, 16.919, 18.307,
19.675, 21.026, 22.362, 23.685, 24.996, 26.296, 27.587,
28.869, 30.144, 31.410,
32.671, 33.924, 35.172, 36.415, 37.652, 38.885, 40.113,
41.337, 42.557, 43.773),
p_0.01 = c(6.635, 9.210, 11.345, 13.277, 15.086, 16.812, 18.475,
20.090, 21.666, 23.209,
24.725, 26.217, 27.688, 29.141, 30.578, 32.000, 33.409,
34.805, 36.191, 37.566,
38.932, 40.289, 41.638, 42.980, 44.314, 45.642, 46.963,
48.278, 49.588, 50.892)
)
GCL <- function(n, X0 = 1, a = 125, c = 1, m = 2^12) {
x <- numeric(n)
x[1] <- X0
for (i in 2:n) {
x[i] <- (a * x[i - 1] + c) %% m
}
return(x / m)
}
n <- 100
k <- 10
alpha <- 0.05
random_numbers <- GCL(n)
breaks <- seq(0, 1, length.out = k + 1)
observed_counts <- hist(random_numbers, breaks = breaks, plot =
FALSE)$counts
expected_count <- n / k
chi2_stat <- sum((observed_counts - expected_count)^2 /
expected_count)
df <- k - 1
col_name <- paste0("p_", format(alpha, scientific = FALSE)) # Ex :
"p_0.05"
if (col_name %in% names(chi2_table) && df <= 30) {
critical_value <- chi2_table[[col_name]][chi2_table$df == df]
} else {
critical_value <- NA
warning("Valeurs critiques non disponibles pour ces paramètres. Vé
rifiez df et alpha.")
}
cat("Statistique Chi-deux calculée :", chi2_stat, "\n")
cat("Valeur critique à alpha =", alpha, ":", critical_value, "\n")
if (!is.na(critical_value) && chi2_stat > critical_value) {
cat("Hypothèse nulle rejetée : les données ne suivent pas une loi
uniforme.\n")
} else {
cat("Hypoth è se nulle non rejet é e : les donn é es suivent une loi
uniforme.\n")
}
Le test compare les fréquences observées des nombres aléatoires dans k intervalles égaux
avec les fréquences théoriques.
Étapes clés :
1. Découpage en classes :
Divise l’intervalle [0,1] en k sous-intervalles égaux.
Compte le nombre d’observations dans chaque intervalle
(observed_counts).
2. Calcul de la statistique Chi-deux (χ2) :
Théorie : Si les données suivent une loi uniforme, le nombre attendu par
intervalle est expected_count=n/k
Formule de χ2
3. Valeur critique :
Issue du tableau de Chi-deux pour α=0.05 et df=k−1 degrés de liberté.
4. Comparaison avec la statistique calculée :
Si χ2>critical_value, rejette H0H_0H0 (non uniforme).
Test de Runs above and below the mean
GCL <- function(n, X0 = 1, a = 125, c = 1, m = 2^12) {
x <- numeric(n)
x[1] <- X0
for (i in 2:n) {
x[i] <- (a * x[i - 1] + c) %% m
}
return(x / m)
}
n <- 100
alpha <- 0.05
R_mean <- mean(random_numbers)
binary_sequence <- ifelse(random_numbers > R_mean, 1, 0)
runs <- 1
for (i in 2:length(binary_sequence)) {
if (binary_sequence[i] != binary_sequence[i - 1]) {
runs <- runs + 1
}
}
n1 <- sum(binary_sequence == 1)
n2 <- sum(binary_sequence == 0)
E_R <- (2 * n1 * n2) / n + 1
V_R <- (2 * n1 * n2 * (2 * n1 * n2 - n)) / (n^2 * (n - 1))
z <- (runs - E_R) / sqrt(V_R)
z_critical <- qnorm(1 - alpha / 2)
cat("Statistique z calculée :", z, "\n")
cat("Valeur critique :", z_critical, "\n")
if (abs(z) > z_critical) {
cat("Hypothèse nulle rejetée : les nombres ne sont pas
indépendants.\n")
} else {
cat("Hypothèse nulle non rejetée : les nombres sont
indépendants.\n")
}
Process
o Initializes a vector x to store the generated numbers.
o Sets the first element x[1] to the seed value X0.
o For each subsequent element i from 2 to n:
Calculates the next number using the GCL formula: x[i] = (a * x[i-1]
+ c) mod m.
Divides the result by m to obtain a number between 0 and 1.
Returns the generated sequence of random numbers.
2. Runs Test for Randomness:
Purpose:
o Tests the null hypothesis that the sequence of numbers is random.
o Specifically, it checks if the number of runs (alternating sequences of 1s and
0s) in the binary sequence derived from the random numbers is consistent
with randomness.
Process:
o Calculates the mean of the random numbers.
o Converts the random numbers to a binary sequence: 1 if the number is greater
than the mean, 0 otherwise.
o Counts the number of runs in the binary sequence.
o Calculates the expected number of runs E_R and the variance V_R under the
null hypothesis.
o Computes the test statistic z using the standard normal distribution.
o Compares the calculated z value with the critical value z_critical at the
specified significance level alpha.
o If |z| > z_critical, the null hypothesis is rejected, indicating that the sequence
is not random. Otherwise, the null hypothesis is not rejected.