"""Lesson 11 starter: LAD grid search.

Exercise goal:
Replace the outlier 20 used in class by 15, run the same grid search,
and interpret the fitted parameter vector.
"""
import numpy as np

x = np.array([0., 1., 2., 3., 4.])

# TODO 1: confirm that the outlier has been changed from 20 to 15.
y = np.array([0., 1., 2., 3., 15.])

def model(x, w):
    return w[0] + w[1] * x

def lad_cost(w):
    residuals = model(x, w) - y
    return np.mean(np.abs(residuals))

best_cost = np.inf
best_w = None

for w0 in np.linspace(-2, 2, 81):
    for w1 in np.linspace(0, 2, 81):
        w = np.array([w0, w1])
        c = lad_cost(w)
        if c < best_cost:
            best_cost = c
            best_w = w.copy()

print("best w =", best_w)
print("best LAD cost =", best_cost)

# TODO 2: explain in words what the fitted line says about the outlier.
