In [1]:
USE_COLAB = False
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
if not USE_COLAB:
    plt.rc("text", usetex=True)
import scipy.special as spspec

In [7]:
x = np.linspace(-3, 3, 10000)
f = np.abs(x)
logsum_exp = np.log(np.exp(x) + np.exp(-x))

def huber(x_range, lam):
    h = np.zeros_like(x_range)
    for i, x in enumerate(x_range):
        if np.abs(x) <= lam:
            h[i] = x**2 / (2 * lam)
        else:
            h[i] = np.abs(x) - lam / 2
    return h

lam = 0.5
huber_f = huber(x, lam)
mu = 0.5
sqrt_f = np.sqrt(x**2 + mu**2) - mu

In [8]:
plt.figure(figsize=(14, 10))
plt.plot(x, f, label="$|x|$")
plt.plot(x, logsum_exp, label="$\log(e^{x} + e^{-x})$")
plt.plot(x, huber_f, label=r"Huber $(\lambda = {})$".format(lam))
plt.plot(x, sqrt_f, label="$\sqrt{{x^2 + \mu^2}} - \mu \; (\mu = {})$".format(mu))
plt.grid(True)
plt.xticks(fontsize=28)
plt.yticks(fontsize=28)
plt.legend(fontsize=28)


Out[8]:
<matplotlib.legend.Legend at 0x7fb5f3c9cd68>

In [ ]: