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):
samples = np.zeros( (N,1) )
for i in range(0,N):
samples[i] = np.random.exponential()
mu = np.mean(samples)
print("N = %d ==> mu = %f" % (N, mu))
(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_exponential_density(mu, xmax, color + "--", label="estimated density N = %d" % N)
return xmax
xmax1 = sample_and_plot(100, 'r')
xmax2 = sample_and_plot(10000, 'b')
plot_exponential_density(1, max(xmax1, xmax2), 'k', "true density")
plt.legend()
plt.show()

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 ...
ahans source
share