<jupyter_start><jupyter_text>MLE with exponential distribution<jupyter_code>import numpy as np
import matplotlib.pylab as plt
import seaborn as sns

np.set_printoptions(precision=4, suppress=True)
sns.set_context('notebook')

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Draw uniform density $f\left(y,\theta\right)=\begin{cases}0, & y\lt a,\\\frac{1}{b-a}, & a\leq y\leq b,\\0, & y\gt b.\end{cases}$<jupyter_code>theta = [[1., 2], [.5, 2.5], [.25, 2.75]]

def f(x, a, b):
    if x < a or x > b:
        return 0
    else:
        return 1/(b-a)

y = np.linspace(0, 3, 1e3)
for t in theta:
    a, b = t[0], t[1]
    ff = [f(x, a, b) for x in y]
    plt.plot(y, ff, lw = 4)

plt.show()<jupyter_output><empty_output><jupyter_text>Simulate data and draw histogram<jupyter_code>n = 100
a, b = 1., 2.
# simulate data
y = np.random.uniform(a, b, n)

# plot data
plt.hist(y, bins=10, normed=True, lw=0)
plt.xlabel('$y_i$')
plt.ylabel('$\hat{f}$')
plt.show()<jupyter_output><empty_output><jupyter_text>Simulate data and estimate model parameter by MLE MLE estimator is$$\hat{a}=\min\left(y_{i}\right),\quad\hat{b}=\max\left(y_{i}\right)$$<jupyter_code># sample size
n = int(1e2)
# true parameter value
a, b = 1., 2.
# simulate data
y = np.sort(np.random.uniform(a, b, n))
# MLE estimator
a_hat, b_hat = np.min(y), np.max(y)
print('Estimates are: a = ', a_hat, ' b = ', b_hat)
# function of exponential density
ff = lambda y, a, b: [f(x, a, b) for x in y]

# plot results
plt.hist(y, bins=10, normed=True, alpha=.2, lw=0)
plt.plot(y, ff(y, a, b), c='black', lw=4)
plt.plot(y, ff(y, a_hat, b_hat), c='red', lw=4)
plt.xlabel(r'$y_i$')
plt.ylabel(r'$\hat{f}$')
plt.legend(('True', 'Fitted','Histogram'))
plt.show()<jupyter_output>Estimates are: a =  1.01122954194  b =  1.99109715887
{'fraction_non_alphanumeric': 0.19574944071588368, 'fraction_numerical': 0.036353467561521254, 'mean_word_length': 4.470948012232416, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '365458', 'license': 'MIT', 'n_tokens_mistral': 851, 'n_tokens_neox': 789, 'n_words': 184, 'path': 'notebooks/mle_uniform.ipynb', 'repo_name': 'khrapovs/metrix', 'size': '1788'}