11  Notebook: Toy Straight-Line Fit

A minimal placeholder for the fitting chapter: a noisy linear model.

11.1 Toy data and fit

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rng = np.random.default_rng(7)
x = np.linspace(0.0, 5.0, 16)
y = 1.2 + 0.9 * x + rng.normal(scale=0.45, size=len(x))
coeff = np.polyfit(x, y, deg=1)

pd.DataFrame({
    "parameter": ["intercept", "slope"],
    "estimate": [coeff[1], coeff[0]],
})
parameter estimate
0 intercept 1.077535
1 slope 0.933960

11.2 Best-fit line

fig, ax = plt.subplots(figsize=(6.2, 4.0))
ax.scatter(x, y, color="#1f5f8b")
ax.plot(x, np.polyval(coeff, x), color="#c44536", linewidth=2)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title("Linear toy fit")
plt.show()