Professional Documents
Culture Documents
Week 3, Solution: Exercise 4.1
Week 3, Solution: Exercise 4.1
Exercise 4.1
n2 <- c(42860,89213,47819)
n6 <- c(1096,6233,15700,22221,17332,7908,1579)
(N2 <- sum(n2))
## [1] 179892
## [1] 72069
a)
pnk k (2)
or
n n n
0 = ∑ nk (k − nθ ) = ∑ nk k − nθ ∑ nk (8)
k=0 k=0 k=0
n
= ∑ nk k − nθ N (9)
k=0
## [1] 0.5137833
## [1] 0.5148723
## Likelihood function
nll <- function(theta,nk){
n <- length(nk) - 1
-(sum(nk * ((0:n) * log(theta) + (n:0)* log(1-theta))))
}
## Optimize
optimize(nll,c(0.01,0.99),nk=n2)$minimum ## Family size = 2
2
## [1] 0.5137949
## [1] 0.5148818
b)
l(θ ) = l2 (θ ) + l6 (θ ) (11)
where n amd m are used for families of size 2 and 6 respectively. Setting the
derivative equal zero we get
n m
0 = ∑ nk (k − nθ ) + ∑ mk (k − mθ ) (13)
k=0 k=0
! !
n m
= ∑ nk k − nN2 θ + ∑ mk k − mN6 θ (14)
k=0 k=0
or
∑nk=0 nk k + ∑m
k=0 mk k
θ̂ = (15)
nN2 + mN6
## [1] 0.5143777
## joint likelihood
jnll <- function(theta,n1,n2){
nll(theta,n1) + nll(theta,n2)
3
}
## MLE
opt <- optimize(jnll, c(0.01,0.99), n1 = n2, n2 = n6)
(theta.hat <- opt$minimum)
## [1] 0.5143882
library(numDeriv)
## [1] 0.00056153
plot(theta,exp(-jointnll+opt$objective),type="l")
## Compare with estimates based on the two datasets
lines(theta1 * c(1,1), c(0,1), col = 2)
lines(theta2 * c(1,1), c(0,1), col = 2)
## cut off to define profile likellihood CI
lines(range(theta),exp(-qchisq(0.95,df=1)/2)*c(1,1),lty=2,col=2)
## Compare with wald CI
rug(theta.hat+1.96*sd.theta*c(-1,1),col=3,lwd=2)
4
1.0
0.8
exp(−jointnll + opt$objective)
0.6
0.4
0.2
0.0
theta
c)
In order to answer the question we need the goodness of fit we calculate the
expected number of observations under the model
5
chisq.stat <- sum(r2^2) + sum(r6^2)
chisq.stat
## [1] 123.633
pchisq(chisq.stat,df=10-1,lower.tail=FALSE)
## [1] 2.407376e-22
round(rbind(n2,e2,r2),digits=1)
round(rbind(n6,e6,r6),digits=1)
We see that we have too many observations in the tail of the distribution
(in particular for families of size 6). One explanation could be that the
probability is not constant between families.
Exercise 4.12
6
## negative log likelihood
nll <- function(theta,nk){
k <- 0:(length(nk)-1)
- sum(nk * dpois(k, lambda = theta, log = TRUE))
}
## Estimate
nk <- c(109,65,22,3,1,0)
opt <- optimize(nll,c(0,100),nk=nk)
opt$minimum
## [1] 0.6099907
## Expected number
e <- c(dpois(0:3,lambda = opt$minimum), 1 - ppois(3,lambda = opt$minimum)) *
sum(nk)
e
## chi.sq statistics
sum((e-obs)^2/e)
## [1] 0.3235224
## Compared with chisq with 3 df, i.e. cannot reject that data follow
## a poisson dist.
Exercise 4.26
We have
7
y <- as.vector(t(read.table("../IALscript/LKPACK/michel.dat")))
x <- y[1:20]
y <- y[-c(1:20)]
t.test(x,y,var.equal=TRUE)
##
## Two Sample t-test
##
## data: x and y
## t = 3.8197, df = 98, p-value = 0.0002344
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 33.99336 107.50664
## sample estimates:
## mean of x mean of y
## 909.00 838.25
a)
n 1 n m 1 m
l(θ ) = − log(σ 2 ) − 2 ∑ (xi − µ)2 − log(σ 2 ) − 2 ∑ (yi − µ − δ )2
2 2σ i=1 2 2σ i=1
(19)
n+m 1 n 1 m
=− log(σ 2 ) − 2 ∑ (xi − µ)2 − 2 ∑ (yi − µ − δ )2 (20)
2 2σ i=1 2σ i=1
(21)
i.e
∂ l(θ ) 1 n 1 m
= 2 ∑ (xi − µ) + 2 ∑ (yi − µ − δ ) (22)
∂µ σ i=1 σ i=1
1
= 2 (n(x̄ − µ) + mȳ − m(µ + δ )) (23)
σ
1
= 2 (nx̄ − µ(n + m) + mȳ − mδ ) = 0 (24)
σ
giving
nx̄ + mȳ − mδ
µ̂(δ ) = (25)
n+m
8
solving for σ 2 gives
∂ l(θ ) n+m 1 n 2 1 m
∂σ2
= −
2σ 2
+ ∑
2σ 4 i=1
(x i − µ) + ∑ (yi − µ − δ )2
2σ 2 i=1
(26)
!
1 1 n 1 m
= 2 −(n + m) + 2 ∑ (xi − µ)2 + 2 ∑ (yi − µ − δ )2 (27)
2σ σ i=1 σ i=1
giving
∑ni=1 (xi − µ)2 + ∑m
i=1 (yi − µ − δ )
2
σ̂ 2 (δ , µ) = (28)
n+m
and
∑ni=1 (xi − µ̂(δ ))2 + ∑m
i=1 (yi − µ̂(δ ) − δ )
2
σ̂ 2 (δ ) = (29)
n+m
and we can write the profile likelihood as
lP (δ ) = l([δ , µ̂(δ ), σ̂ 2 (δ )]) (30)
The above is implemented and plotted below
## plot
par(mfrow=c(1,2))
plot(delta,lp.a,type="l")
lines(range(delta),-qchisq(0.95,df=1)/2*c(1,1),col=2,lwd=2,lty=2)
plot(delta,exp(lp.a),type="l")
lines(range(delta),exp(-qchisq(0.95,df=1)/2)*c(1,1),col=2,lwd=2,lty=2)
## Compare with the t-test
rug(-as.numeric(t.test(x,y,var.equal=TRUE)$conf.int),lwd=2)
9
1.0
0
−1
0.8
−2
0.6
−3
exp(lp.a)
lp.a
−4
0.4
−5
0.2
−6
0.0
−7
delta delta
0.0.1 b)
10
and we have
n
σx2
x̄ + σm2 (ȳ − δ )
y
µ̂(δ ) = n (35)
σx2
+ σm2
y
Hence the solution depends on the parameters σx2 and σy2 , and we have to
depend on the numerical solution. The varaince parameter can however
easily be written as a function of µ and δ by
1 n
σ̂x2 (µ, δ ) = ∑ (xi − µ)2 (37)
n i=1
1 m
σ̂y2 (µ, δ ) = ∑ (yi − µ − δ )2 (38)
m i=1
11
## Plot
par(mfrow=c(1,2))
plot(delta,lp.b,type="l")
lines(delta,lp.a,col=2)
plot(delta,exp(lp.b),type="l")
lines(delta,exp(lp.a),col=2)
lines(range(delta),exp(-qchisq(0.95,df=1)/2)*c(1,1),col=2,lwd=2,lty=2)
## Compare with the t-test
rug(-as.numeric(t.test(x,y)$conf.int),lwd=2)
rug(-as.numeric(t.test(x,y,var.equal=TRUE)$conf.int),lwd=2,col=2)
1.0
0
0.8
−1
0.6
exp(lp.b)
lp.b
−2
0.4
−3
0.2
0.0
delta delta
12
c)
σx2
Set β = σy2
, we have
n m
n 1 m
l(θ ) = − log(β σy2 ) − ∑ (x i − µ)2
− log(σy
2
) − f rac12σy
2
∑ (yi − µ − δ )2
2 2β σy2 i=1 2 i=1
(40)
From above we have
nσy2 x̄ + mβ σy2 (ȳ − δ )
µ̂(δ ) = (41)
nσy2 + mβ σy2
nx̄ + mβ (ȳ − δ )
= (42)
n + mβ
Further we have
∂ l(θ ) 1 m
= 2 ∑ (yi − µ − δ ) (43)
∂δ 2σy i=1
1
= 2 (mȳ − mµ − mδ ) (44)
2σy
=0 (50)
with the solution
!
n m
1 1
σ̂y2 (β ) = ∑ (xi − x̄)2 2
+ ∑ (yi − ȳ) (51)
n+m β i=1 i=1
13
lp.beta <- function(beta, x, y){
n <- length(x)
m <- length(y)
delta.hat <- mean(y) - mean(x)
mu.hat <- mean(x)
sigmasq.hat.y <- (sum((x - mu.hat)^2) / beta +
sum((y - mu.hat - delta.hat)^2))/ (n + m)
sum(dnorm(x, mean = mu.hat, sd = sqrt(sigmasq.hat.y * beta),
log =TRUE)) +
sum(dnorm(y, mean = mu.hat + delta.hat,
sd = sqrt(sigmasq.hat.y), log =TRUE))
}
par(mfrow=c(1,2))
plot(beta,lp.c,type="l")
plot(beta,exp(lp.c),type="l")
lines(range(beta),exp(-qchisq(0.95,df=1)/2)*c(1,1),
col=2,lwd=2,lty=2)
## Compare with observed value
lines(var(x)/var(y)*c(1,1),c(0,1),col=4)
14
1.0
0
0.8
−1
0.6
exp(lp.c)
−2
lp.c
0.4
−3
0.2
−4
0.0
1 2 3 4 5 6 7 1 2 3 4 5 6 7
beta beta
Exercise 4.30
we also have
Z
∂
pθ (x)dx = 0 (54)
∂θ
and when the function is weel behaved we can write
Z Z Z
∂ ∂
x − A0 (θ ) pθ (x)dx
pθ (x)dx = pθ (x)dx = (55)
∂θ ∂θ
15
Hence we have
Z Z Z
− A0 (θ )pθ (x)dx + xpθ (x)dx = − A0 (θ ) pθ (x)dx + Eθ [X] (56)
= − A0 (θ ) + Eθ [X] = 0 (57)
16