In [1]:
from typing import Union

import numpy as np
import matplotlib.pyplot as plt

get_ipython().run_line_magic('config', "InlineBackend.figure_format = 'retina'")
In [2]:
def logit(p: Union[float, np.ndarray]) -> Union[float, np.ndarray]:
    return np.log(p / (1 - p))


def logistic(log_odds: Union[float, np.ndarray]) -> Union[float, np.ndarray]:
    return 1 / (1 + np.exp(-log_odds))
In [3]:
# ps can't hit 0 or 1 to avoid numerical error.
ps = np.linspace(1e-3, 1 - 1e-3, 100)
log_odds = np.linspace(-4, 5, 100)
In [4]:
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
ax0, ax1 = axes.ravel()

ax0.plot(ps, logit(ps), label="Logit")
ax0.set(
    title="Logit",
    xlabel="Probability ($p$)",
    ylabel="Log_odds",
)

ax1.plot(log_odds, logistic(log_odds))
ax1.set(
    title="Logistic",
    xlabel="Log_odds",
    ylabel="Probability ($p$)",
)

for _ax in [ax0, ax1]:
    _ax.grid()
In [ ]: