Skip to main content

Chapter 10 Least Squares

This chapter and the next introduce the idea of fitting a model to data. In this context, a model consists of a mathematical description of the relationship between variables -- like a straight line -- and a description of random variation -- like a normal distribution.
When we say that a model fits data, we usually mean that it minimizes errors, which are the distances between the model and the data. Weโ€™ll start with one of the most widely-used ways of fitting a model, minimizing the sum of the squared errors, which is called a least squares fit.
Weโ€™ll also start with models that work with just two variables at a time. The next chapter introduces models that can handle more than two variables.
Listing 10.0.1. Python Code
from os.path import basename, exists


def download(url):
    filename = basename(url)
    if not exists(filename):
        from urllib.request import urlretrieve

        local, _ = urlretrieve(url, filename)
        print("Downloaded " + local)


download("https://github.com/AllenDowney/ThinkStats/raw/v3/nb/thinkstats.py")
Listing 10.0.2. Python Code
try:
    import empiricaldist
except ImportError:
    %pip install empiricaldist
Listing 10.0.3. Python Code
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from thinkstats import decorate