MATH 314 Quiz 18

Given on 2025-12-09

Please submit a jupyter notebook to the Quiz 18 GitHub repository by 1.55pm.

There are exactly 5 things wrong in each code chuck of each of the problems below. Wrong can be either

Please cross out the wrong characters and write an ordered, comma separated list of replacement characters after the comment #. If you don't know the correct Python syntax for the replacement characters you want, make something not unreasonable up for partial credit.

You should assume the following code precedes each code chunk in each question. Otherwise, the code chunks are independent across problems; no code in one question's code chunk informs code in another question's code chunk.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
import scipy.stats as st
url = "https://raw.githubusercontent.com/roualdes/data/refs/heads/master/finches.csv"
df = pd.read_csv(url)
  1. Write a generic bootstap function that returns a dictionary of both the R bootstrap estimates and the confidence interval, the latter of which is specified by the keyword argument conflevel.
def bootstrap(T, data, R = 1_000, conflevel = 0.95, **kwargs):
    shp = np.shape(T(data, **kwargs))
    rng = np.random.default_rng()
    Ts = np.zeros((R) + shp)
    N = np.shape(data)[1]
    a = (1 - conflevel) / 2
    for r in range(N):
        idx = rng.integers(N, size = N)
        Ts[r] = T(data[idx], **kwargs)
    ci = np.quantile(Ts, [a, a], axis = 0)
    return (Ts, ci)
  1. Use linear regression to predict beakwidth using lowerbeaklength. Then predict beakwidth using a lowerbeaklength of 6.5.
model = smf.ols("body_mass_g  bill_depth_mm", data = df)
model.data
model.predict(df)