import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import mean_absolute_error, mean_squared_error
"figure.facecolor"] = (1, 1, 1, 0) # RGBA tuple with alpha=0
plt.rcParams["axes.facecolor"] = (1, 1, 1, 0) # RGBA tuple with alpha=0 plt.rcParams[
Loss Functions
Mean Absolute Error (L1 Loss)
def calc_loss_one_pred(loss_function, actuals, pred):
= np.full(actuals.shape, pred)
preds_array = loss_function(actuals, preds_array)
loss return loss
def plot_loss(loss_function, actuals, possible_preds) -> None:
= [calc_loss_one_pred(loss_function, actuals, pred) for pred in possible_preds]
loss
= plt.subplots(figsize=(5, 3))
fig, ax
ax.plot(possible_preds, loss)
0, possible_preds.max())
ax.set_xlim(0, possible_preds.max() + 1, 1))
ax.set_xticks(np.arange("Prediction")
ax.set_xlabel(
0, max(loss) + 1)
ax.set_ylim("Loss")
ax.set_ylabel(
__name__.replace("_", " ").title())
ax.set_title(loss_function.True)
ax.grid(
plt.show()
= np.array([1, 8, 9])
actuals = np.arange(0, 12, 0.1) possible_preds
plot_loss(mean_absolute_error, actuals, possible_preds)
def utility_function(inventory, demand):
if demand >= inventory:
return -3 * (demand - inventory)
else:
return demand - inventory
print(f"Loss from stocking 6 when demand is 1: {utility_function(6, 1)}")
print(f"Loss from stocking 6 when demand is 8: {utility_function(6, 8)}")
print(f"Loss from stocking 6 when demand is 9: {utility_function(6, 9)}")
Loss from stocking 6 when demand is 1: -5
Loss from stocking 6 when demand is 8: -6
Loss from stocking 6 when demand is 9: -9
Mean Squared Error
plot_loss(mean_squared_error, actuals, possible_preds)
One way in which mean absolute and mean squared error differ is that MAE leads your model to predict the median of the distribution while MSE leads your model to predict the mean of the distribution.
TODO: proof of this