Question

In: Statistics and Probability

3. Only using the runif function with default settings, generate: (a) n = 1e4 iid realizations...

3. Only using the runif function with default settings, generate:

(a) n = 1e4 iid realizations from a Bernoulli distribution with probability of success parameter, θ = 0.7.

(b) n = 1e4 iid realizations from a Binomial distribution with probability of success parameter, θ = 0.7 and number of trials= 20.

(c) n = 1e4 iid realizations from an Exponential distribution with mean µ = 7 (hint: if X ~ Exp(µ) then Var(X) = µ2 ).

For each case comment on the sample mean and sample variance of these realizations and if match what you expect.

Solutions

Expert Solution

(a) R-code-Generate random number 1e4 with Bernoulli distribution.

rm(list=ls())
rbernoulli <- function(n, p) {
x <- c()
for (i in 1:n) {
u <- runif(1,0,1)
if (u <= p)
x <- c(x, 1)
else
x <- c(x, 0)
}
return (x)
}
x <- rbernoulli(10000, 0.7)

mean(x)
var(x)

output-

> mean(x)
[1] 0.6941
> var(x)
[1] 0.2123464
>

(c) Generate random number 1e4 with expoential distribution.

rm(list=ls(all=T))
ls(all=T)
u=runif(1)
f<-function(u,a){
(-1/a)*log(1-u)
}
rm(list=ls(all=T))
ls(all=T)
u=runif(1)
f<-function(u,a){
(-1/a)*log(1-u)
}
x<-c(0)
u<-c(0)
for(i in 1:1000){
u[i]=runif(1)
x[i]=(-1/2)*log(1-u[i])
}

mean(x)
var(x)

output-

> mean(x)
[1] 0.5061411
> var(x)
[1] 0.2372458
>

(B)Generate random number 1e4 with Binomial distribution.

rbinomial <- function(n,size, p) {
x <- c()
for (i in 1:n) {
u <- runif(1,0,1)
if (u <= p)
x <- c(x, 1)
else
x <- c(x, 0)
}
return (x)
}
x <- rbinomial(10000,20, 0.7)

output

> mean(x)
[1] 0.7059
> var(x)
[1] 0.207626
>


Related Solutions

( C language only )Generate a function  called randomnum(n,m,neg); where n and m are the lower and...
( C language only )Generate a function  called randomnum(n,m,neg); where n and m are the lower and upper bounds for the random number; the generated number is negative if a variable named neg is true .These numbers should be non-zero with a maximum absolute value of 15 . You must use bitwise arithmetic to calculate the modulus of the random numbers
Using, for example, the command “=IF(RAND()<0,4;1;0)” in Excel, simulate 40 different realizations with n = 60...
Using, for example, the command “=IF(RAND()<0,4;1;0)” in Excel, simulate 40 different realizations with n = 60 and the true supporting rate to be 40%. In order to “freeze” the output generated by Excel, select the cells with all 0s and 1s values that have been randomly generated using the above mentioned command, copy them and paste them (using the paste special option) as values back on top of the cells. Then in each of the 40 different simulated “datasets” calculate...
Using, for example, the command “=IF(RAND()<0,4;1;0)” in Excel, simulate 40 different realizations with n = 60...
Using, for example, the command “=IF(RAND()<0,4;1;0)” in Excel, simulate 40 different realizations with n = 60 and the true supporting rate to be 40%. In order to “freeze” the output generated by Excel, select the cells with all 0s and 1s values that have been randomly generated using the above mentioned command, copy them and paste them (using the paste special option) as values back on top of the cells. Then in each of the 40 different simulated “datasets” calculate...
( C language only and all values should be float )Generate a function called randomnum(n,m,neg); where...
( C language only and all values should be float )Generate a function called randomnum(n,m,neg); where n and m are the lower and upper bounds for the random number; the generated number is negative if a variable named neg is true .These numbers should be non-zero with a maximum absolute value of 15 . You must use bitwise arithmetic to calculate the modulus of the random number
Generate a scatter plot of this data, and include a smoothed function using smooth.spline function in...
Generate a scatter plot of this data, and include a smoothed function using smooth.spline function in R. Use predict function in R to calculate the 95% bounds (confidence band) for the mean, and plot them on the same scatterplot (use lty=2, and col=3). How would I go about writing this code in R?
3. Let X1...Xn be N(μX,σ) and Y1...Yn be iid N(μy,σ) with the two samples X1...Xn, and...
3. Let X1...Xn be N(μX,σ) and Y1...Yn be iid N(μy,σ) with the two samples X1...Xn, and Y1...Xn independent of each other. Assume that the common population SD σ is known but the two means are not. Consider testing the hypothesis null: μx = μy vs alternative: μx ≠ μy. d. Assume σ=1 and n=20. How large must δ be for the size 0.01 test to have power at least 0.99? e. Assume σ=1and δ=0.2. How large must n be for...
3. Let X1...Xn be N(μX,σ) and Y1...Yn be iid N(μy,σ) with the two samples X1...Xn, and...
3. Let X1...Xn be N(μX,σ) and Y1...Yn be iid N(μy,σ) with the two samples X1...Xn, and Y1...Xn independent of each other. Assume that the common population SD σ is known but the two means are not. Consider testing the hypothesis null: μx = μy vs alternative: μx ≠ μy. a. Find the likelihood ratio test statistic Λ. Specify which MLEs you are using and how you plug them in.
Find the order of growth for the following function ((n^3) − (60n^2) − 5)(nlog(n) + 3^n...
Find the order of growth for the following function ((n^3) − (60n^2) − 5)(nlog(n) + 3^n )
a) A k-out-of-n system is one that will function if and only if at least k...
a) A k-out-of-n system is one that will function if and only if at least k of the n individual components in the system function. If individual components function independently of one another, each with probability 0.8, what is the probability that a 4-out-of-6 system functions? b) Obtain ?(?(?−2)) where ? ~???????(?) c) Service calls arrive at a maintenance center according to a Poisson process, with average 3.1 calls per minute. (i) Obtain the probability that no more than 4...
Using MS Excel and the random number generator function, generate values for 30 observations for the...
Using MS Excel and the random number generator function, generate values for 30 observations for the following columns with average daily: Body weight with random values between 100 and 250lbs Calories intake with random values between 1000 and 3000 calories Workout duration with random values between 0 and 60 minutes Sleep duration with random values between 2 and 12 hours Work duration with random values between 0 and 12 hours Assuming that the values are averages over 1 year, conduct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT