Questão 01

theta <- seq(0.01,0.99,0.02)
var.est <- rep(0,length(theta))
n <- 10
licr <- ((1-2*theta)^2)*theta*(1-theta)/n
na <- 50000
for (i in 1:length(theta)) {
vec <- apply(matrix(rbinom(na*n,1,theta[i]),na,n),1,mean)
estim <- n*vec*(1-vec)/(n-1)
var.est[i] <- var(estim)
}
plot(theta,licr,type="l",ylim=c(0,0.007))
lines(theta,var.est, lty=2)

Questão 03

n<-1000 ; m<-10 ; p<-0.4
x<-rbinom(n,m,p)

licr<-function(p,n,m){ p*(1-p)/(n*m)}
varian<-licr

library(ggplot2)
ggplot()+
  xlim(0.1,0.90)+
  geom_function(fun = licr, args = list(n=1000,m=10))+
  labs(title="Limite Inferior de Cramér-Rao",  x="p",  y="LICR")+
  theme(plot.title = element_text(h=0.5))

Questão 04

licr<-function(n,lambda){log(2)^2/(n*lambda^2)}

ggplot()+
  xlim(0.1,1)+
  geom_function( fun = licr, args = list(n=10),colour= "red")+
  labs(title="Lmite Inferior de Cramér-Rao", 
       x=bquote(lambda ~ (n==10)), y="LICR")+
  theme(plot.title = element_text(size=12,hjust=0.5))

Questão 05

## 1o Modo ( R base)
theta <- seq(-3,3,0.01)
n <- 10
var.est <- pnorm(theta,0,1)*(1-pnorm(theta,0,1))/n
licr <- exp(-theta^2)/(2*pi*n)
plot(theta,licr,type="l",ylim=c(0,0.03))
lines(theta,var.est,lty=2)

## 2o Modo ( Com ggplot do R )
library(ggplot2)
ggplot() + xlim(-3,3) +
  geom_function(fun = ~ exp(- (.x)^2 )/(2*pi*n), args=list(n=10),aes(colour = "LICR") ) +
  geom_function(fun = ~ pnorm(.x)*(1-pnorm(.x))/n, args=list(n=10),aes(colour = "Var") ) +
  labs(title="Variância X LICR", x=bquote(theta), y="Valores") +
  theme(plot.title = element_text(h=0.5))

Questão 06

licr<-function(n,lambda){ lambda*exp(-2*lambda)/n}

var.est.T<-licr  # Prof., acho que a variancia do est. T atinge o LICR
var.emv<-function(n,lambda){exp(n*lambda*(exp(-2*n)-1)) - exp(2*n*lambda*(exp(-n)-1))}


## Variancia de exp(- X.bar), via metodo Monte Carlo
N=1000 ; lambda<-10
res<-numeric(N) ; res2<-numeric(N)

for (i in 1:N){
        x<-rpois(10,2)
         x.bar<-mean(x)
        res[i]<-exp(-x.bar)
}

var.sim<- (N/(N-1))*var(res)

## Problema: a varincia do emv da lista esta apresentando valores menores que LICR.
setNames( c(var.sim,var.emv(10,10),var.est.T(10,10)) , 
          c("Var sim. de exp(-X.bar) "," Var do EMV ", " Var do Est.T") )
## Var sim. de exp(-X.bar)               Var do EMV              Var do Est.T 
##             4.706798e-03             3.720077e-44             2.061154e-09
ggplot()+
  xlim(0.1,1)+
  geom_function( fun = licr, args = list(n=10), colour= "red")+
  geom_function( fun = var.emv, args = list(n=10), colour = "blue")+
  labs(title="LICR X Variância", 
       x=bquote(lambda ~ (n==10)), y="Var")+
  theme(plot.title = element_text(size=12,hjust=0.5))

Questão 13

## 1o Modo
theta <- 2
n <- 10
x <- rbeta(n,theta,1)
theta.vec <- seq(0.01,5,0.01)
veros <- rep(0,length(theta.vec))
for (i in 1:length(theta.vec))
veros[i] <- prod(dbeta(x,theta.vec[i],1))
plot(theta.vec,veros,type="l")
lambda <- 2
r.par <- 2
priori <- dgamma(theta.vec,r.par,rate=lambda)
lines(theta.vec,priori,lty=2)
posteriori <- dgamma(theta.vec,(n+r.par),rate=(lambda-sum(log(x))))
lines(theta.vec,posteriori,lty=3)
legend(3,2,legend=c("Veross.","priori","posteriori"),lty=c(1,2,3))

## 2o Modo