Skip to content

Commit 6a5fa41

Browse files
committed
correctly normalize the asymptotic vcov
1 parent f23b150 commit 6a5fa41

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

R/mledist.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ mledist <- function (data, distr, start=NULL, fix.arg=NULL, optim.method="defaul
404404
if(calcvcov && !is.null(opt$hessian))
405405
{
406406
#see R/util-mledist-vcov.R
407-
varcovar <- mle.vcov(opt$hessian)
407+
varcovar <- mle.vcov(opt$hessian, data.size)
408408
#add names
409409
if(!is.null(varcovar))
410410
colnames(varcovar) <- rownames(varcovar) <- names(opt$par)
@@ -445,7 +445,7 @@ mledist <- function (data, distr, start=NULL, fix.arg=NULL, optim.method="defaul
445445
names(opt$par) <- names(vstart)
446446
if(calcvcov && !is.null(opt$hessian))
447447
{
448-
varcovar <- mle.vcov(opt$hessian)
448+
varcovar <- mle.vcov(opt$hessian, data.size)
449449
#add names
450450
if(!is.null(varcovar))
451451
colnames(varcovar) <- rownames(varcovar) <- names(opt$par)

R/util-mledist-vcov.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
###
2424

2525

26-
#compute H^{-1}
27-
mle.vcov <- function(myhessian)
26+
#compute H^{-1}/n
27+
mle.vcov <- function(myhessian, nsample)
2828
{
2929
if(all(!is.na(myhessian)) && qr(myhessian)$rank == NCOL(myhessian))
3030
{
31-
res <- solve(myhessian)
31+
res <- solve(myhessian)/nsample
3232
}else
3333
{
3434
res <- NULL

R/util-mmedist-vcov.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
###
2525

2626

27-
#compute J^{-1} A J^{-T}, see below
27+
#compute J^{-1} A J^{-T}/n, see below
2828
mme.vcov <- function(par, fix.arg, order, obs, mdistnam, memp, weights,
2929
epsilon = sqrt(.Machine$double.eps), echo=FALSE)
3030
{
@@ -39,7 +39,7 @@ mme.vcov <- function(par, fix.arg, order, obs, mdistnam, memp, weights,
3939
{
4040
Jinv <- solve(Jmat)
4141
Amat <- mme.Ahat(par, fix.arg, order, obs, mdistnam, memp, weights)
42-
res <- Jinv %*% Amat %*% t(Jinv)
42+
res <- Jinv %*% Amat %*% t(Jinv) / length(obs)
4343
}else
4444
res <- NULL
4545
if(echo)

tests/t-mledist-asymptotic-vcov.R

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
11
require("fitdistrplus")
2-
nsample <- 1e6
2+
nsample <- 1e3
33
nsample <- 10
44

55
#### (1) Gamma example ####
66

7-
truetheta <- c("alpha"=3, "beta"=1/2)
7+
truetheta <- c("alpha"=0.19, "beta"=5.18)
88
x <- rgamma(nsample, truetheta["alpha"], truetheta["beta"])
99
f1 <- mledist(x, "gamma", calcvcov = TRUE)
1010
f1$vcov
11+
f1$estimate
12+
infoFisher <- function(alpha, beta)
13+
{
14+
cbind(c(trigamma(alpha), -1/beta),
15+
c(-1/beta, alpha/beta))
16+
}
17+
solve(infoFisher(0.19, 5.18))/nsample
18+
19+
20+
if(FALSE)
21+
{
22+
#check with MASS::fitdistr()
23+
24+
mledist(rgamma(1e2, truetheta["alpha"], truetheta["beta"]), "gamma", calcvcov = TRUE)$vcov
25+
mledist(rgamma(1e3, truetheta["alpha"], truetheta["beta"]), "gamma", calcvcov = TRUE)$vcov
26+
mledist(rgamma(1e4, truetheta["alpha"], truetheta["beta"]), "gamma", calcvcov = TRUE)$vcov
27+
28+
29+
MASS::fitdistr(rgamma(1e2, truetheta["alpha"], truetheta["beta"]), "gamma")$vcov
30+
MASS::fitdistr(rgamma(1e3, truetheta["alpha"], truetheta["beta"]), "gamma")$vcov
31+
MASS::fitdistr(rgamma(1e4, truetheta["alpha"], truetheta["beta"]), "gamma")$vcov
32+
33+
34+
}
35+
1136

1237
# (2) fit a Pareto distribution
1338
#

tests/t-mmedist-asymptotic-vcov.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ if(FALSE)
1515
fitdistrplus:::mme.vcov(as.numeric(truetheta), fix.arg=NULL, order=1:2, obs=x, mdistnam=mgamma, memp, weights=NULL)
1616
}
1717

18+
if(FALSE)
19+
{
20+
mmedist(rgamma(1e2, truetheta["alpha"], truetheta["beta"]), "gamma", order=1:2, calcvcov = TRUE)$vcov
21+
mmedist(rgamma(1e3, truetheta["alpha"], truetheta["beta"]), "gamma", order=1:2, calcvcov = TRUE)$vcov
22+
mmedist(rgamma(1e4, truetheta["alpha"], truetheta["beta"]), "gamma", order=1:2, calcvcov = TRUE)$vcov
23+
}
24+
1825

1926
# (2) fit a Pareto distribution
2027
#

0 commit comments

Comments
 (0)