Skip to content

Commit

Permalink
correctly normalize the asymptotic vcov
Browse files Browse the repository at this point in the history
  • Loading branch information
dutangc committed Dec 5, 2024
1 parent f23b150 commit 6a5fa41
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
4 changes: 2 additions & 2 deletions R/mledist.R
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ mledist <- function (data, distr, start=NULL, fix.arg=NULL, optim.method="defaul
if(calcvcov && !is.null(opt$hessian))
{
#see R/util-mledist-vcov.R
varcovar <- mle.vcov(opt$hessian)
varcovar <- mle.vcov(opt$hessian, data.size)
#add names
if(!is.null(varcovar))
colnames(varcovar) <- rownames(varcovar) <- names(opt$par)
Expand Down Expand Up @@ -445,7 +445,7 @@ mledist <- function (data, distr, start=NULL, fix.arg=NULL, optim.method="defaul
names(opt$par) <- names(vstart)
if(calcvcov && !is.null(opt$hessian))
{
varcovar <- mle.vcov(opt$hessian)
varcovar <- mle.vcov(opt$hessian, data.size)
#add names
if(!is.null(varcovar))
colnames(varcovar) <- rownames(varcovar) <- names(opt$par)
Expand Down
6 changes: 3 additions & 3 deletions R/util-mledist-vcov.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
###


#compute H^{-1}
mle.vcov <- function(myhessian)
#compute H^{-1}/n
mle.vcov <- function(myhessian, nsample)
{
if(all(!is.na(myhessian)) && qr(myhessian)$rank == NCOL(myhessian))
{
res <- solve(myhessian)
res <- solve(myhessian)/nsample
}else
{
res <- NULL
Expand Down
4 changes: 2 additions & 2 deletions R/util-mmedist-vcov.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
###


#compute J^{-1} A J^{-T}, see below
#compute J^{-1} A J^{-T}/n, see below
mme.vcov <- function(par, fix.arg, order, obs, mdistnam, memp, weights,
epsilon = sqrt(.Machine$double.eps), echo=FALSE)
{
Expand All @@ -39,7 +39,7 @@ mme.vcov <- function(par, fix.arg, order, obs, mdistnam, memp, weights,
{
Jinv <- solve(Jmat)
Amat <- mme.Ahat(par, fix.arg, order, obs, mdistnam, memp, weights)
res <- Jinv %*% Amat %*% t(Jinv)
res <- Jinv %*% Amat %*% t(Jinv) / length(obs)
}else
res <- NULL
if(echo)
Expand Down
29 changes: 27 additions & 2 deletions tests/t-mledist-asymptotic-vcov.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
require("fitdistrplus")
nsample <- 1e6
nsample <- 1e3
nsample <- 10

#### (1) Gamma example ####

truetheta <- c("alpha"=3, "beta"=1/2)
truetheta <- c("alpha"=0.19, "beta"=5.18)
x <- rgamma(nsample, truetheta["alpha"], truetheta["beta"])
f1 <- mledist(x, "gamma", calcvcov = TRUE)
f1$vcov
f1$estimate
infoFisher <- function(alpha, beta)
{
cbind(c(trigamma(alpha), -1/beta),
c(-1/beta, alpha/beta))
}
solve(infoFisher(0.19, 5.18))/nsample


if(FALSE)
{
#check with MASS::fitdistr()

mledist(rgamma(1e2, truetheta["alpha"], truetheta["beta"]), "gamma", calcvcov = TRUE)$vcov
mledist(rgamma(1e3, truetheta["alpha"], truetheta["beta"]), "gamma", calcvcov = TRUE)$vcov
mledist(rgamma(1e4, truetheta["alpha"], truetheta["beta"]), "gamma", calcvcov = TRUE)$vcov


MASS::fitdistr(rgamma(1e2, truetheta["alpha"], truetheta["beta"]), "gamma")$vcov
MASS::fitdistr(rgamma(1e3, truetheta["alpha"], truetheta["beta"]), "gamma")$vcov
MASS::fitdistr(rgamma(1e4, truetheta["alpha"], truetheta["beta"]), "gamma")$vcov


}


# (2) fit a Pareto distribution
#
Expand Down
7 changes: 7 additions & 0 deletions tests/t-mmedist-asymptotic-vcov.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ if(FALSE)
fitdistrplus:::mme.vcov(as.numeric(truetheta), fix.arg=NULL, order=1:2, obs=x, mdistnam=mgamma, memp, weights=NULL)
}

if(FALSE)
{
mmedist(rgamma(1e2, truetheta["alpha"], truetheta["beta"]), "gamma", order=1:2, calcvcov = TRUE)$vcov
mmedist(rgamma(1e3, truetheta["alpha"], truetheta["beta"]), "gamma", order=1:2, calcvcov = TRUE)$vcov
mmedist(rgamma(1e4, truetheta["alpha"], truetheta["beta"]), "gamma", order=1:2, calcvcov = TRUE)$vcov
}


# (2) fit a Pareto distribution
#
Expand Down

0 comments on commit 6a5fa41

Please sign in to comment.