Asymptotic distribution of MLEs

Asymptotic distribution of MLEs

In class, we have seen that the asymptotic distribution of a maximum likelihood estimator \(\hat{\theta}_{MLE}\) for a parameter \(\theta\) is \(\hat{\theta}_{MLE} \sim N(\theta, CRLB)\). In class, we have shown that the maximum likelihood estimator \(\hat{\theta}_{MLE}\) for the scale parameter \(\theta\) of Gamma distribution, when the shape parameter \(\kappa\) is known is: \(\hat{\theta}_{MLE}=\frac{\bar{X}}{\kappa}\), where its asymptotic distribution is normal distribution with mean \(\theta\) and variance \(\frac{\theta^{2}}{n\kappa}\). Let’s investigate how the asymptotic distribution of \(\hat{\theta}_{MLE}\) changes with respec to sample size \(n\), when \(\kappa=1\).

# import required libraries
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widgets
from IPython.display import display
# define a function
def mle_gamma_dist_theta(teta = 1, n = 100):

    np.random.seed(123) # set a random seed
    #n = 100  #sample size default = 100
    N = 100000  #number of repeatitions (repeated sampling)
    k = 1  #true population shape parameter, fixed over the simulation
    # when k = 1, thetahat = xbar
    #theta = 1  #true population scale parameter, default = 1
    #generate true population under these settings
    pop_data = np.random.gamma(shape = k, scale = teta, size = N)
    #generate N different samples with size of n under these settings
    data = np.random.choice(pop_data, size = (n, N))
    
    xbar = data.mean(axis = 0)  #calculates xbar for each N sample
    grand_avg = xbar.mean() #find the mean of N xbar estimates
    grand_var = xbar.var(ddof=1)  #find the the sample variance of N xbar estimates 
    
    fig, ax = plt.subplots(figsize = (10, 10))
    
    # Plot the histogram of true population
    plt.subplot(1, 2, 1)
    plt.hist(pop_data, bins=25, density=True, alpha=0.5, color='b')
    plt.title(r'True population: Gamma dist. with $\theta=%.1f, \kappa=%.1f$' % (teta,k))
    plt.xlabel(r'$x$')
    
    # Plot the histogram of sample means
    plt.subplot(1, 2, 2)
    plt.hist(xbar, bins=25, density=True, alpha=0.5, color='b')
    plt.title(r"Asymptotic distribution of $\hat{\theta}_{MLE}=\frac{\bar{X}}{\kappa}$")
    plt.xlabel(r'$\frac{\bar{x}}{\kappa}$')
    #plt.ylim(0, 2) ######i need to work on upper ylim and coordinates of plt.text later######
    #add a text
    plt.text(1.5,1.3,"Mean = %.4f \n Variance = %.4f" % (grand_avg, grand_var))      
    
    #side-by-side
    fig.tight_layout()
    plt.show()
#mle_gamma_dist_theta(theta = 1, n = 10)
theta_wid = widgets.IntSlider(min = 1, max = 10, step=1, value=1, description = r'$\theta$')
sample_size_wid = widgets.IntSlider(min = 0, max = 100, step=1, value=10, description = "Sample size")
widgets.interact(mle_gamma_dist_theta, teta = theta_wid, n = sample_size_wid);