#311 · Machine Learning · Medium
⊣ Solve on deep-ml.comClassify critical points of a multivariable function using the eigenvalues of the Hessian matrix. A critical point is a local minimum if all eigenvalues are positive, a local maximum if all are negative, and a saddle point if eigenvalues have mixed signs.
import numpy as np
def compute_hessian(f, x: np.ndarray, h: float = 1e-5) -> np.ndarray:
n = len(x)
hessian = np.zeros((n, n))
for i in range(n):
for j in range(n):
x_pp = x.copy(); x_pp[i] += h; x_pp[j] += h
x_pm = x.copy(); x_pm[i] += h; x_pm[j] -= h
x_mp = x.copy(); x_mp[i] -= h; x_mp[j] += h
x_mm = x.copy(); x_mm[i] -= h; x_mm[j] -= h
hessian[i, j] = (f(x_pp) - f(x_pm) - f(x_mp) + f(x_mm)) / (4 * h * h)
return hessian
def classify_critical_point(f, x: np.ndarray) -> dict:
hessian = compute_hessian(f, x)
eigenvalues = np.linalg.eigvalsh(hessian)
if np.all(eigenvalues > 0):
classification = "local minimum"
elif np.all(eigenvalues < 0):
classification = "local maximum"
elif np.any(eigenvalues > 0) and np.any(eigenvalues < 0):
classification = "saddle point"
else:
classification = "inconclusive"
return {
"hessian": hessian,
"eigenvalues": eigenvalues.tolist(),
"classification": classification,
}