How can I build a maximum likelihood score in Python

I draw a few examples from exponential distribution . In my first experiment, I draw 1,000 samples, and for the second I draw 10,000 samples from this distribution. (with numpy.random.exponential)

I would like to visually compare the difference in assessing the maximum likelihood of my two experiments. (since this is an exponential distribution, MLE will be just a sample of the mean, so with my second experiment, MLE should be closer to the true density).

How can I make such a comparison in Python? I know how to draw graphics in matplotlib, but here I do not know what type of graphics I should use.

+5
source share
1 answer

Given the comments in the comments, I think the following: this is what you are looking for:

import numpy as np
import matplotlib.pyplot as plt

def plot_exponential_density(mu, xmax, fmt, label):
        x = np.arange(0, xmax, 0.1)
        y = 1/mu * np.exp(-x/mu)
        plt.plot(x, y, fmt, label=label)

def sample_and_plot(N, color):
        # first sample N valus
        samples = np.zeros( (N,1) )
        for i in range(0,N):
                samples[i] = np.random.exponential()

        # determine the mean
        mu = np.mean(samples)
        print("N = %d  ==> mu = %f" % (N, mu))

        # plot a histogram of the samples
        (n, bins) = np.histogram(samples, bins=int(np.sqrt(N)), density=True)
        plt.step(bins[:-1], n, color=color, label="samples N = %d" % N)

        xmax = max(bins)

        # plot the density according to the estimated mean
        plot_exponential_density(mu, xmax, color + "--", label="estimated density N = %d" % N)

        return xmax


# sample 100 values, draw a histogram, and the density according to
# the estimated mean
xmax1 = sample_and_plot(100, 'r')
# do the same for 1000 samples
xmax2 = sample_and_plot(10000, 'b')

# finally plot the true density
plot_exponential_density(1, max(xmax1, xmax2), 'k', "true density")

# add a legend
plt.legend()

# and show the plot
plt.show()

enter image description here

I used 100 and 10,000 samples, since with 1000 samples the rating is already very good. But still, having only 100 samples, I am somewhat surprised how good the estimate of the average and, consequently, the density. Given only a histogram without knowing that the samples are taken from the exponential distribution, I'm not sure that I would recognize the exponential distribution here ...

+4
source

All Articles