-
Notifications
You must be signed in to change notification settings - Fork 0
/
simhw4p3.R
executable file
·25 lines (24 loc) · 956 Bytes
/
simhw4p3.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
myfunc <- function(n,p){
gen1 <- function() { # function to generate 1 Poisson RV
geo <- c(0.4650,.3200,.4490,.2480,.8310,.9740,.2750,.7890,.4860,.2670,.1720,.6580,.1420,.6990,.5240,.4290,.1490,.7230,.7870,.0850,.7840,.3220,.2090,.1440,.5310,.9930,.8410,.8170,.1890,.8600,.6340,.6750,.1240,.2420,.0470,.0340,.7960,.7400,.3760,.3400,.8400,.7830,.0830,.7610,.8130,.9730,.9700,.4700,.1630,.7340,.2280,.4570,.0840,.8920,.7500,.6430,.7980,.2120,.3720,.9450,.7790,.5390,.0030,.0150,.7670,.1980,.0810,.8430,.1080,.7980,.5510,.7630,.1850,.5360,.2430,.3200,.5240,.9240,.2290,.349)
i <- 1
y <- 0
tot <- 0
while(y != 1){
s <- sample(geo,1)
if(s < p){
y <- 1
tot <- i
} else{
i <- i + 1
}
}
return(tot)
}
replicate(n,gen1())
}
# compare to R's function rpois()
myrvs <- myfunc(10,0.3)
xbar <- mean(myrvs)
sd(myrvs)
CI <- xbar + c(-1,1)*1.96*(sd(myrvs)/sqrt(10))