# Normal Random Numbers using Acceptance Rejection Sampling
set.seed(24)
# Specify the number of random numbers to be generated
size=10
#First randon numbers are generated from Uniform distribution to generate #g(y). Here as some samples will be rejected in AR method we generate 3 times
u=runif(3*size)
# the random numbers from the covering distribution is generated
exp=-log(u)
#Second set of random numbers to check the condition is generated
u2=runif(3*size)
#function to evalauate the upper limit for uniform random number
const=function(y){
return(exp(-1*(y-1)^2/2))
}
n=1
i=1
#modz represents the list of ABSOLUTE standard normal ramdon numbers
modz=c()
while (n<=size){
if(u2[i]<=const(exp[i])){
modz=append(modz,exp[i])
n=n+1
}
i=i+1
}
#Generate binary random number which assumes -1 0r +1 with equal probability
u3=runif(size)
#s is the random numbers of the random variable assuming -1 0r +1 with equal probability
s=c()
for (i in 1:size){
if (u3[i]<=0.5){
s=append(s,-1)
}
else{
s=append(s,1)
}
}
#multiply absolute standard normal random number with -1 0r +1
z=modz*s
z
## [1] -1.22903780 -1.49213880 -0.35066028 -0.65604959 0.08289935 -1.27391050
## [7] 0.26942252 0.22110736 0.50271054 -0.99226790
set.seed(24)
# Specify the number of random numbers to be generated
size=10
#First randon numbers are generated from Uniform distribution to generate #g(y). Here as some samples will be rejected in AR method we generate 3 times
u=runif(3*size)
# the random numbers from the covering distribution is generated
exp=-log(u)
#Second set of random numbers to check the condition is generated
u2=runif(3*size)
#function to evalauate the upper limit for uniform random number
const=function(y){
return(exp(-1*(y-1)^2/2))
}
n=1
i=1
#modz represents the list of ABSOLUTE standard normal ramdon numbers
modz=c()
while (n<=size){
if(u2[i]<=const(exp[i])){
modz=append(modz,exp[i])
n=n+1
}
i=i+1
}
#Generate binary random number which assumes -1 0r +1 with equal probability
u3=runif(size)
#s is the random numbers of the random variable assuming -1 0r +1 with equal probability
s=c()
for (i in 1:size){
if (u3[i]<=0.5){
s=append(s,-1)
}
else{
s=append(s,1)
}
}
#multiply absolute standard normal random number with -1 0r +1
z=modz*s
Now we shall consider the p-p plot of the generated random numbers to confirm whether the distribution of the random number is normal.
par(mfrow=c(2,1))
library(stats)
ProbDist=pnorm(sort(z))
plot(ppoints(length(z)), ProbDist,xlab = 'Observed Probability',main = 'P P Plot',ylab = 'Expected Probability',lwd=1,type = 'l',ylim =c(0,1))
abline(0,1)
hist(z,freq = FALSE)
ProbDist=pnorm(sort(z))
plot(ppoints(length(z)), ProbDist,xlab = 'Observed Probability',main = 'P P Plot',ylab = 'Expected Probability',lwd=1,type = 'l',ylim =c(0,1))
abline(0,1)
x=seq(-3,3,0.05)
par(mar = c(3, 3, 1, 1))
plot(x,pnorm(x),type='l')
Testing of goodness of fit is used to evaluate whether the assumed distribution fits the given data.
# Observed frequencies
observed <- c(20, 30, 15, 25)
# Expected probabilities (equal proportions)
expected <- rep(1/4, 4)
# Perform the chi-square goodness of fit test
result <- chisq.test(observed, p = expected)
# Print the test result
print(result)
##
## Chi-squared test for given probabilities
##
## data: observed
## X-squared = 5.5556, df = 3, p-value = 0.1354
Since p-value is greater than \(0.05\), we cannot reject the null hypothesis.
##
## 0 1 2 3 4 5
## 6 20 24 42 24 12
Fit a Binomial distribution. Also test the goodness of fit.
Answer
To fit a binomial distribution first we have to estimate the parameters of the distribution. To estimate the parameter \(n\), we assign the maximum value taken by the random variable. Hence \(\hat{n}=6\) here. Now we shall estimate \(p\) using method of moments. Hence we equate sample mean to population. On simplification we get \(\hat{p}=\dfrac{\bar{x}}{\hat{n}}\)
values=seq(0,5)
frequ=c(6,20,24,42,24,12)
nhat=max(values)
nhat
## [1] 5
# Now we shall find the sample mean
mean_x=weighted.mean(values,frequ)
mean_x
## [1] 2.734375
phat=mean_x/nhat
phat
## [1] 0.546875
Now we will evaluate thee expected probabilities using dbinom() function.
ExpProb=c()
for (i in 0:5){
ExpProb=append(ExpProb,dbinom(i,nhat,phat))
}
ExpProb
## [1] 0.01910250 0.11527368 0.27824682 0.33581513 0.20264706 0.04891481
chisq.test(frequ,p=ExpProb)
## Warning in chisq.test(frequ, p = ExpProb): Chi-squared approximation may be
## incorrect
##
## Chi-squared test for given probabilities
##
## data: frequ
## X-squared = 16.249, df = 5, p-value = 0.006169