Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Assigment 2_AGS

Andres Gonzalez
2024-03-01

# By using the file NormalDist in R, research on how to use the Kolmogorov Smirnoff goodness of
fit procedure to test if a set of data accomplishes a normal distribution or not. You will need
to create an empirical data set through a random variate generator, like the one covered in clas
s using polar coordinates.

# Function to generate two independet standard normal random variables using the polar method
set.seed(42)
n <- 100
generate_normal_polar <- function() {
repeat {
U1 <- runif(1, 0, 1)
U2 <- runif(1, 0, 1)
V1 <- 2 * U1 - 1
V2 <- 2 * U2 - 1
S <- V1^2 + V2^2

if (S <= 1) {
break
}
}

X1 <- V1 * sqrt(-2 * log(S) / S)


X2 <- V2 * sqrt(-2 * log(S) / S)

return(c(X1, X2))
}

# Function to generate a sample of standard normal random variables


set.seed(42)
generate_normal_sample <- function(n) {
sample <- numeric(n)
for (i in seq(1, n, by = 2)) {
normal_pair <- generate_normal_polar()
sample[i] <- normal_pair[1]
if (i + 1 <= n) {
sample[i + 1] <- normal_pair[2]
}
}
return(sample)
}
norm <- generate_normal_sample(100)

norm[1:20]
## [1] -0.53150173 0.82125270 2.21744970 0.29873471 0.40485862 -0.62517195
## [7] 0.98817644 1.29076523 -0.34018681 1.76389569 0.08708504 -0.04899897
## [13] -0.06016687 0.70210235 -1.09241065 2.63601766 -0.84708286 0.02883056
## [19] -0.21764884 0.80429165

set.seed(42)
library(stats)

ks_test <- ks.test(norm, rnorm(n))


ks_test

##
## Asymptotic two-sample Kolmogorov-Smirnov test
##
## data: norm and rnorm(n)
## D = 0.11, p-value = 0.5806
## alternative hypothesis: two-sided

# P es nivel de significancia estadístico

You might also like