Construir funções para gerar amostras das distribuições binomial e bernoulli conforme apresentado em sala.
bern <- function(n,p){
u <- runif(n)
x <- (u > 1-p) *1
return(x)
}
#Testando a função
bern(n=100, p = 0.8) #ou px(100)
## [1] 0 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 0 1 1 1
## [36] 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 1
## [71] 1 1 0 1 1 0 1 1 0 1 0 0 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 0 1 0
#Freq. Relativa
table(bern(100, 0.8))/100
##
## 0 1
## 0.17 0.83
#Gráfico da Freq. Relativa
barplot(table (bern(100, 0.8)), col = rainbow(3))
bin <- function(n,k,p) #n: Tamanho da amostra; k: número de repetições; p: prob. de sucesso
{
X<-c(1:n)
for (i in 1:k)
{
X[i]<-sum(bern(n,p))
}
X
}
Test_bin <- bin (20, 5, 0.7)
barplot(table(Test_bin), col = rainbow(20))