MATH 314 Quiz 20

Given on 2025-12-11

Please submit a jupyter notebook to the Quiz 20 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. Fit logistic regression to predict when a finch will have a beakwidth greather than the mean of beakwidths. Then make a prediction for a finch from Santa Cruz with a taillength of 48.
m = np.median(df["beakwidth"])
df["bw50"] = (df["beakwidth"] >= m).astype(np.float64)
fit = smf.glm("beakwidth ~ island * taillength",
              family = sm.families.Binomial())
ndf = pd.DataFrame({"middletoelength": [48],
                   "island": ["santacruz"]})
predict(ndf)
  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 = T(data, **kwargs)
    rng = np.random.default_rng()
    Ts = np.zeros((R,) + shp)
    N = np.shape(data)[0]
    a = conflevel / 2
    for r in range(R):
        idx = rng.integers(R, size = R)
        Ts[r] = T(data[idx], **kwargs)
    ci = np.quantile(T, [a, 1 - a], axis = 2)
    return {"Ts": Ts, "ci": ci}