Académique Documents
Professionnel Documents
Culture Documents
Problema 1.
Write R code to compare the performances of de Monte Carlo estimators of
2
x
2
e 2 dx
1
U(1,2)
Normal(0
,1)
0.143960
2
0.135507
1
0.315208
8
0.281534
3
sampling
Nsim=10^2
y=rweibull(Nsim,1,5)
con
tres
p<-exp((-y^2)/2)*2*pi*(1+((y1)/2)^2)/sqrt(2*pi)
estint<-cumsum(p)/(1:Nsim)
esterr=sqrt(cumsum((p-estint)^2))/(1:10^4)
plot(estint,xlab="Iterations",ty="l",lwd=2,mai
n="Candidata Cauchy(1,2)",
ylab="",lty=2)
r<-pnorm(2,lower.tail=T)pnorm(1,lower.tail=T)
abline(a=r,b=0,col="red")
mean(p)
p<-(exp((-y^2)/2)/sqrt(2*pi))*(((1/5)*exp(y/5))^(-1))
estint<-cumsum(p)/(1:Nsim)
esterr=sqrt(cumsum((p-estint)^2))/(1:Nsim)
plot(estint,xlab="Iterations",ty="l",lwd=2,mai
n="Candidata weibull(1,5)",
ylab="",lty=2)
r<-pnorm(2,lower.tail=T)pnorm(1,lower.tail=T)
abline(a=r,b=0,col="red")
mean(p)
n
100
10000
n=100
n=10000
f ( x )=
With
e{ x}
( 1+e { x } )
c=
,< x< .
4
2 . The program should allow the user to specify the number of
deviates. The output list should contain the vector of normal deviates and the
proportion of acceptances.
Cdigo R
target<-function(x){exp(-x^2/2)/sqrt(2*pi)} ##es la funcin de inters
propousal<-function(x){(exp(x)/(1+exp(x))^2)*(4/sqrt(2*pi))}
n=100000
sample.x<-rlogis(n)
accept=c()
for(i in 1:length(sample.x)){
U=runif(1,0,1)
if((propousal(sample.x[i])*U)<=target(sample.x[i]))
{
accept[i]="Yes"
}
else if ((propousal(sample.x[i])*U)>target(sample.x[i]))
{
accept[i]="No"
}
}
T=data.frame(sample.x,accept=factor(accept,levels=c("Yes","No")))
hist(T[,1][T$accept=="Yes"],breaks=seq(-5,5,0.3),freq=FALSE,main="Histogram of X",xlab="X")
x<-seq(-5,5,0.01)
lines(x,dnorm(x,0,1))
library(ggplot2)
print(qplot(sample.x,data=T,geom="density",color=accept))
print(qplot(sample.x,data=T,geom="histogram",fill=accept,binwidth=0.01))
colnames(T)<-c('valor','dec')
T
r<-as.matrix(subset(T,dec=='Yes'))
proporcion<-sum(nrow(r))/n
Grficos
Problema 3.
Kennedy abd Gentle(1980) studied the following algorithm to generate
Poisson deviates using the well-known relationship between exponential waiting times and Poisson counts:
1.- Set L = exp{-}, x=0,p=1
2.- Generate u from Unif(0,1) and set p=pu.
3.- If p L, set x = x+1 and repeat step 2.Otherwise, output x.
Explain why this algorithm generates Poisson deviates. Hint: If N Pois(), and
n
N=n X i 1
i=1
n+1
and
X i>1
i=1
Cdigo en R
poiss<- function(n,lambda)
{
xv<- matrix(0,nrow=n,ncol=1)
l=exp(-lambda)
for (i in 1:n)
{
x=0
p=1
u<- runif(1,0,1)
p=p*u
while (p>=l)
{
x=x+1
u<- runif(1,0,1)
p=p*u
}
xv[i]=x
}
par(mfrow=c(1,2))
y<-rpois(n,lambda)
hist(xv)
hist(y)
return(xv)
}
poi<- poiss(10000,4)
Problema 4.
Suppose we want to simulate from the bivariate normal density.
(( ) (
'
( X , Y ) N 0 , 1 0.9
0 0.9 1
))
X y N ( y ,(12 ) ) ,
Y x N ( x ,(1 2) ) .
x , x
xy 2
2(12 )
y x2
2(12 )
e
P
Cdigo en R
gibbs<-function(n,rho)
{
xy<-matrix(nrow=n,nco=2)
x=0
y=0
c=sqrt(1-rho^2)
xy[1,]<-c(x,y)
for (i in 2:n){
x<-rnorm(1,rho*y,c)
y<-rnorm(1,rho*x,c)
xy[i,]<-c(x,y)
}
xy
}
xyn<-gibbs(10000,0.9)
par(mfrow=c(1,1))
plot(xyn,col=1:10000)
plot(xyn,type="l")
plot(ts(xyn[,1]))
plot(ts(xyn[,2]))
hist(xyn[,1],40,main="Distri
bucion condicional x")
hist(xyn[,2],40,,main="Distri
bucion condicional y")
par(mfrow=c(1,1))
f X ( x ) = f ( x , y)dy
K(y,x)f(y)=K(x,y)f(x)
Luego si se supone una cadema de Markov con kernel de transicin K satisface
la condicin de balance detallado como una funcin de densidad de
probabilidad f. Entonces:
La densidad f es una densidad invariante de la cadena.
La cadena es reversible.
b) Show
that
this
conditional
X x N ( 2 x,(1 4 ) )
distribution
simplifies
so
that
Problema 5.
The Michaelis-Menten model is popular for growth-curve data, where Y is the
response, and X, the covariate, is some measure of age or fime:
Y i=
1 X i
,i=1, , n
2 + X i
Formula: Y ~ t1 * X/(t2 + X)
Parameters:
Estimate Std. Error t value Pr(>|t|)
t1 592.5090 25.8936 22.882 < 2e-16 ***
t2 1.9141
0.2147 8.914 1.38e-11 ***
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 34.27 on 46 degrees of freedom
Number of iterations to convergence: 9
Achieved convergence tolerance: 1.108e-06
Cdigo en R
Y<-c(190,199,200,209,213,215,216,230,235,238,257,264,265,272,285,286,
289,295,295,301,301,305,234,310,326,329,340,343,380,381,382,384,
400,401,341,375,383,456,415,436,436,459,470,470,502,451,455,530)
X<-c(rep(1,12),rep(2,10),rep(3,12),rep(4,4),rep(5,4),6,6,6,7,7,10)
n=length(X)
modelo.nl<-nls(Y ~ t1*X/(t2+X),start=list(t1=5,t2=2) )
summary(modelo.nl)
###obtencin de los residuales del modelo
residuales<-resid(modelo.nl)
#bootstrap sampling de los residuales
cboot<-matrix(0,1,1000)
boot.mean<-function(residuales,nboot)
{
n<-length(residuales)
cboot<-numeric(nboot)
s<-matrix(0,nrow=1000,ncol=1)
for(i in 1:nboot)
{
id<-sample(1:n,size=n,replace=T)
cboot[i]<-mean(residuales[id])
s[i]<-var(residuales[id])
}
media.ord<-sort(cboot)
s.ord<-sort(s)
ICI<-media.ord[50]-qt(0.05,n-1,lower.tail=T,log.p=F)*sqrt(s[50])/sqrt(n)
ICS<-media.ord[950]+qt(0.95,n-1,lower.tail=T,log.p=F)*sqrt(s[950])/sqrt(n)
#int<-return(cbind(ICI,ICS,n))
dat<-return(cbind(media.ord,s.ord))
}
test<-boot.mean(residuales,1000)
m<-test[,1]
med<-mean(m)
var<-test[,2]
var<-var
hist(m,main="Medias usando el bootstrap")
#intervalo de confianza al 90%
# Calculo y graficacion de bandas de confianza para la media #
ICA<- sort(m+qt(0.05, n-1,lower.tail = T, log.p = F)*sqrt(var)/sqrt(n)
ICB<- sort(m-qt(0.05, n-1,lower.tail = T, log.p = F)*sqrt(var)/sqrt(n)
###GRAFICANDO
plot(ICA, type='l', lty=1, main='Banda de confianza al 90% para los residuales', ylab='Residual', col=3)
lines(ICB, type='l',lty=1, col=2)
#lines(sort(residuales), type='l',lty=2, col=4)
lines(m, type='l',lty=2, col=4)
Problema 6.
Suppose you have n data points X 1,,Xn that are assumed to come from a
Poisson() distribution. Carry out a Monte Carlo simulation study to find out
about the coverage of the nominal asymptotic 95% confidence interval (
^1.96
s
, ^ +1.96 s / n
), where
n
n
15
30
50
10
Error
estndar
1.154147
0.8154018
0.6312396
0.4474374
Poisson(n,20)
Lmite
Inferior
15.35043
16.83783
17.64403
18.16005
Limite
superior
4.652221
4.156
3.96072
3.667015
Lmite
superior
24.3427
22.92145
22.45158
21.72111
Cdigo en R
poisson<-function(n,lambda)
{
#mu.0<-0
#s.0<-0
mu<-matrix(0,nrow=100000,ncol=1)
s<-matrix(0,nrow=100000,ncol=1)
for(i in 1:100000)
{
x<-rpois(n,lambda)
mu[i]<-mean(x)
s[i]<-sqrt(var(x))
}
mu.0<-sort(mu)
s.0<-sort(s)
ESM<-sqrt(var(mu))
ICI<-mu.0[2500]-1.96*s[2500]/sqrt(n)
ICS<-mu.0[97500]+1.96*s[97500]/sqrt(n)
return(cbind(ICI,ICS,n,ESM))
}
poisson(15,3)
poisson(30,3)
poisson(50,3)
poisson(100,3)
poisson(100,20)
Puede notarse que al incrementar el tamao de muestra el error estndar va
disminuyendo. Y lo que se espera que la cobertura vaya aumentando.
Y=