Example 3: Maximum Likelihood Estimation

The MLEs for the normal distribution can be derived analytically and are well known to be the sample mean and the sample standard deviation (biased version). This example compares the numerical solution to the analytical solution.


import com.imsl.math.*;
import com.imsl.stat.Random;
import com.imsl.stat.distributions.*;

public class MaximumLikelihoodEstimationEx3 {

    public static void main(String[] args) throws Exception {
        int n = 100;
        double[] sample = new double[n];
        double mean = 25;
        double stdev = 1.5;
        Random rand = new Random(123457);

        for (int i = 0; i < n; i++) {
            sample[i] = stdev * rand.nextNormal() + mean;
        }

        NormalPD norm = new NormalPD();
        double[] exactMLEs = norm.getMLEs(sample);

        MaximumLikelihoodEstimation mle
                = new MaximumLikelihoodEstimation(sample, norm, 2, 1);

        mle.compute();

        double[] parameterEstimates = mle.getEstimates();
        double[][] hessian = mle.getHessian();
        double[] stdErrs = mle.getStandardErrors();
        double mLogLikelihood = mle.getMinusLogLikelihood();

        PrintMatrixFormat pmf = new PrintMatrixFormat();
        pmf.setNumberFormat(new java.text.DecimalFormat("0.0000000"));

        PrintMatrix pm = new PrintMatrix("Analytic MLEs");
        pm.print(pmf, exactMLEs);

        pm.setTitle("Numerical MLEs");
        pm.print(pmf, parameterEstimates);

        pm.setTitle("hessian");
        pm.print(pmf, hessian);

        pm.setTitle("standard errors");
        pm.print(pmf, stdErrs);

        System.out.printf("Minus log likelihood: %5.2f\n", mLogLikelihood);
    }
}

Output

 Analytic MLEs
       0       
0  25.0062530  
1   1.6472313  

Numerical MLEs
       0       
0  25.0062531  
1   1.6472315  

           hessian
        0            1       
0  -36.8545110    0.0000055  
1    0.0000055  -73.7091163  

standard errors
       0      
0  0.1647232  
1  0.1164768  

Minus log likelihood: 191.80
Link to Java source.