# Maximum Likelihood versus Goodness of Fit

Thursday, I got an interesting question from a colleague of mine (JP). I mean, the way I understood the question turned out to be a nice puzzle (but I have to confess I might have misunderstood). The question is the following : consider a i.i.d. sample $\{X_1,\cdots,X_n\}$ of continuous variables. We would like to choose between two (parametric) families for the distribution, $\mathcal{F}=\{F_{\boldsymbol \alpha};\boldsymbol \alpha\in\mathcal{A}\}$ and $\mathcal{G}=\{G_{\boldsymbol \beta};\boldsymbol \beta\in\mathcal{B}\}$. If we use maximum likelihood techniques, we get two estimators, one for each family, $\widehat{\boldsymbol \alpha}$ and $\widehat{\boldsymbol \beta}$. Clearly, $F_{\widehat{\boldsymbol \alpha}}(\cdot)$ is a much better than $G_{\widehat{\boldsymbol \beta}}(\cdot)$, in the sense of a standard goodness of fit test (e.g. Kolmogorov-Smirnov since the sample is assumed to be obtained from a continuous variable). Does that mean that family is $\mathcal{F}$ (somehow) better than family $\mathcal{G}$ ?

This is my interpretation of the question, and I found it amusing. So I will try to show (using simulated samples) that some odd situations can easily be obtained.

Consider a sample from a mixture of log-normal distributions,

>  set.seed(228)
>  X=exp(c(rnorm(50,1,1),rnorm(50,2,1.2)))

Consider two standard families for positive random variables: a Gamma distribution and a lognormal distribution.

> library(MASS)
> ab=fitdistr(X,"gamma")
> ms=fitdistr(X,"lognormal")

If we want to visualized those two distributions, let us use

> vab=pgamma(u,ab$estimate[1],ab$estimate[2])
> vms=plnorm(u,ms$estimate[1],ms$estimate[2])
> plot(ecdf(X))
> lines(u,vab,col="red")
> lines(u,vms,col="blue")

Here, we get

What else can we say ? actually, we can also compute Kolmogorov-Smirnov statistic,

$D_n=\sup_x |\widehat F_n(x)-F_\star(x)|$where

$\widehat F_n(x)={1 \over n}\sum_{i=1}^n \boldsymbol{1}_{X_i\leq x}$

This can be done using

> ks.test(X,"plnorm",ms$estimate[1],ms$estimate[2])

One-sample Kolmogorov-Smirnov test

data:  X
D = 0.0693, p-value = 0.7231
alternative hypothesis: two-sided

> ks.test(X,"pgamma",ab$estimate[1],ab$estimate[2])

One-sample Kolmogorov-Smirnov test

data:  X
D = 0.148, p-value = 0.02507
alternative hypothesis: two-sided

From a theoretical point of view, we should not look at the p-values, since the null-distribution is based on a fixed distribution, not a fitted one (see the Lilliefors tests for normal samples). But still. The Gamma distribution seems to be very far away from the true distribution. The statistics is twice the one we have with our lognormal distribution. And one p-value is 72%, while the other one is 2.5%. Here, we should prefer this lognormal distribution to that Gamma one. But here, we did consider only one distribution in each family. Does that mean that we cannot find one Gamma distribution that will be better than all possible lognormal distributions ? Better, for instance, according to Kolmogorov-Smirnov statistics…

Well, it is possible to use another strategy to find appropriate parameters. We can minimize this statistic actually. Define

> ks1=function(ms) {m=ms[1];s=ms[2];ks.test(X,"plnorm",m,s)$statistic} > ks2=function(ab) {a=ab[1];b=ab[2];ks.test(X,"pgamma",a,b)$statistic}

and compute

> n1=nlm(ks1,c(ms$estimate[1],ms$estimate[2]))
> n1
$minimum [1] 0.05252692$estimate
[1] 1.547437 1.121864
> n2=nlm(ks2,c(ab$estimate[1],ab$estimate[2]))
> n2
$minimum [1] 0.04737725$estimate
[1] 1.1449041 0.167041

So here, it is possible to find a distribution much closer to the empirical sample, within the Gamma family actually.

>  vab=pgamma(u,n2$estimate[1],n2$estimate[2])
>  vms=plnorm(u,n1$estimate[1],n1$estimate[2])
>  lines(u,vab,col="red",lwd=2)
>  lines(u,vms,col="blue",lwd=2)

What would be the point here? Maybe just the idea that the maximum likelihood estimator is only one estimator among a lot of them. And if it has interesting asymptotic properties, on small samples, it might not be the best estimator to consider…

And to be completely honest, I’ve been cheating here… I mean, not really cheating (not more than any researcher using a statistical test to validate the findings). But here, I did fix the seed of the random number generator. Actually, such example does not occur that frequently. Here, out of 1000 samples, I got this odd conclusion almost 15 times. And the smaller the sample, the more likely we can observe that story, where the maximum likelihood estimator can be far away from the best fit. Here is the proportion of opposite conclusions, as a function of the sample size,

> SIM=function(ns=1000,n=100){
+ t=0
+ for(s in 1:ns){
+  set.seed(s)
+  X=exp(c(rnorm(n/2,1,1),rnorm(n/2,2,1.2)))
+  ks1=function(ms) {m=ms[1];s=ms[2];ks.test(X,"plnorm",m,s)$statistic} + ks2=function(ab) {a=ab[1];b=ab[2];ks.test(X,"pgamma",a,b)$statistic}
+  library(MASS)
+  ab=fitdistr(X,"gamma")
+  ms=fitdistr(X,"lognormal")
+  n1=nlm(ks1,c(ms$estimate[1],ms$estimate[2]))
+  n2=nlm(ks2,c(ab$estimate[1],ab$estimate[2]))
+  if( (ks.test(X,"plnorm",ms$estimate[1],ms$estimate[2])$statistic- + ks.test(X,"pgamma",ab$estimate[1],ab$estimate[2])$statistic)
+ *(n1$minimum-n2$minimum)<=0 ) t=t+1
+ }
+ return(t/ns)}

> VM=rep(NA,20)
> VS=seq(10,200,by=10)
> for(i in 1:20){VM[i]=SIM(n=VS[i],ns=1000)}
> plot(VS,VM,type="p")

So to provide a more complete answer to JP’s question, with a very large sample, I guess that your intuition should be valid…. but clearly not on a small sample.

## 8 thoughts on “Maximum Likelihood versus Goodness of Fit”

1. szczepan says:

Hey, Am I missing something or are you testing the fit to the distribution with the parameters estimated from the data? Would one-sided KS test be OK to use in that case, without reading the test statistics from the table?

2. tis says:

i want to ask about gof in likelihood…
why the biggest value in goodness of fit of likelihood be the better than another value?
thank you for attention…

3. Even before going to the simulations, there’s the question of the dimensions of the parameter spaces of the two families. One can imagine that the better fitting family has an excess of degrees of freedom and the MLE for that family is fitting noise.

I’d be interested to see the MSE of the MLE and the MGoFE at these small sample sizes. (For the log-normal model, one can consider the estimand to be the asymptotic limit of the MLE.) Maybe the MGoFE has a better bias-variance tradeoff?

1. Muaz says:

MGoFE stands for?

1. Corey says:

maximum goodness-of-fit estimator

4. George says:

Hi Arthur,

You appear to have left out the code to create the “u” vector of quantiles for input to the pgamma and plnorm distrubition function.

Perhaps the rest of the post is error free.

1. indeed, that’s one of the problem with posting code within comments (or the reverse), things might be missing… but as I keep saying, the goal is to giv the core of the algorithm, so usually, I leave typos (I do that for my students, just to avoid too simple copy & paste, and to make sure my students actually understand the code)

This site uses Akismet to reduce spam. Learn how your comment data is processed.