Inference — Maximum-Likelihood Estimation

Below is the content demonstrated in the workshop held on February 25, 2022. It illustrates how one proceeds Maximum-Likelihood Estimation in Python and also helps you understand the concept of confidence intervals.

ToolsIt’s assumed that you’ve already imported the familiar modules i.e. import numpy as np; import matplotlib.pyplot as plt; import scipy.stats as st.

plt.axvline and plt.axhline

Draw vertical and horizontal lines respectively.

plt.axvline(0.5)
plt.axhline(0.7)
plt.show()

opt.minimize(fun, x0, ...)

Minimize fun with initial guess x0.

Note that we import an additional module here by import scipy.optimize as opt.

# Here we minimize y = x**4 + x**2.

def y(x): return x**4 + x**2

import scipy.optimize as opt
res = opt.minimize(y, x0=np.asarray([0.1]))

print('The minimized value of y is', res.fun)
print('The minimum happens at x =', res.x)
The minimized value of y is 1.531597404220429e-13
The minimum happens at x = [-3.91356283e-07]

opt.fsolve(fun, x0, ...)

Find the roots of a function with initial guess x0.

# Here we solve x**4 + x**2 = 1, or equivalently,
# f(x) := x**4 + x**2 - 1 = 0.

def f(x): return x**4 + x**2 - 1

res = opt.fsolve(f, x0=np.asarray([-1., 1.]))

print('The roots found are', res)
The roots found are [-0.78615138  0.78615138]

np.vectorize

You can vectorize any function, i.e. make it able to perform element-wise operation on NumPy array, with np.vectorize.

def myFunction(x):
    if x > 0:
        return x**2
    else:
        return 0

vectorized_myFunction = np.vectorize(myFunction)
vectorized_myFunction(np.arange(-2, 3, 1))
array([0, 0, 0, 1, 4])

Hands-On

Log-likelihood

Point Estimation

Interval Estimation

Verification and Practice