Code
import polars as pl
from polars import col
import seaborn as sns
from sklearn.model_selection import KFold, LeaveOneOut
# Plotting style
sns.set_style("whitegrid")
# Reproducibility for cross-validation
RNG_SEED = 42Site Updates: Week 10 Slides
Eshin Jolly
January 28, 2026
Authors: Jane Yang, Eshin Jolly
Date: 01/28/26
Deadline: Midnight Monday Feb 1st, 2026 This homework builds on your HW1 exploration of the cat name recognition dataset. You’ll apply the three resampling methods you learned in lab — bootstrap, simulation, and cross-validation — to make statistical inferences about the data. Reference the Week 4 lab notebooks to help you out (also available on the course website).
Your Instructors will grade the last git push you submit by the deadline. However, after we review the HW together during lab, you can push additional changes to improve your grade.
We’ll be looking for:
scikit-learn cross-validation documentationRemember to ping your instructors on Github/Slack if you get stuck!
In HW1 you explored the cat name recognition dataset using polars and seaborn. You computed summary statistics, created visualizations, and observed patterns in looking times across conditions, settings, and ages.
Now we’ll use resampling methods to move beyond description to inference: quantifying uncertainty, checking assumptions, and evaluating predictions.
Quick data recap:
Each section below focuses on one resampling technique applied to one question. For each, you’ll write code, create a visualization, and write an interpretation.
Let’s the libraries you’ll need. We’ve listed the key ones below.
Load the dataset and drop any rows with missing looking times. Then create a cat-level summary DataFrame that you’ll reuse throughout the assignment.
Your cat_summary should have one row per cat with the following columns:
cat_id, age, settingmean_lt: mean looking time across all of that cat’s trialscongruent: mean looking time for congruent trialsincongruent: mean looking time for incongruent trialsdiff_score: congruent minus incongruentUse pl.read_csv() to load the data, then .drop_nulls(subset=["looking_time"]) to remove missing values. For the cat-level summary, you can:
.group_by("cat_id").agg(...).group_by("cat_id", "condition").agg(...) followed by .pivot().join() the two tables on cat_id.with_columns() to compute diff_score5 points total
In HW1 you may have noticed a trend when plotting age against looking time. But how confident should we be in the strength of that relationship?
Use bootstrapping to quantify the uncertainty of the Pearson correlation between age and mean looking time at the cat level.
Calculate the Pearson correlation between age and mean_lt from your cat-level summary using pl.corr
Use the manual bootstrap pattern from lab to compute a 95% confidence interval for the Pearson correlation between age and mean looking time. Use 5,000 resamples.
Use the manual bootstrap pattern from lab:
nboot times (e.g., 5000)cat_summary with replacement using .sample(n=n_cats, with_replacement=True)pl.corr("age", "mean_lt")For the 95% CI, use the percentile method: the 2.5th and 97.5th percentiles of the bootstrap distribution.
Create a histogram of the bootstrapped correlation coefficients. Add:
Label your axes and add a title.
Write 3–4 sentences interpreting the bootstrap results in context:
Your response here…
5 points total
Many classical statistical tests assume that the data (or a summary statistic like the mean) follows a normal distribution. But is that assumption justified for our cat looking time data?
Use Monte Carlo simulation to compare what we’d expect under a normal assumption to what we would expect under a t-distribution assumption.
Simulate 5,000 hypothetical experiments under the assumption that cat-level mean looking times are either normally distributed or t-distributed using the observed mean and standard deviation from the dataset
For each simulated experiment:
This gives you 2 simulated sampling distribution of the mean under normal and t-distribution assumptions. Compute the standard deviation of each distribution and compare it the the observed standard error
First use polars to compute the observed mean and standard deviation from cat_summary. Use the lab pattern for generating random samples with the normal() function. The cell below sets up the imported functions for you. For the t-distribution we’ve setup the function for you and you you can use it in the same way as normal() except for 1 difference:
The first argument is a degrees-of-freedom value, which for simplicity you can set to 46 (48 cats - 2)
Create a single figure with overlapping histograms (or KDE plots) of:
Use different colors and add a legend
Write 3–4 sentences comparing the two sampling distributions:
your response here…
Finally, let’s think about prediction. We have two candidate strategies for predicting a cat’s looking time on a given trial:
Use K-fold cross-validation to compare these strategies. The key constraint: hold out entire cats (not individual trials) to avoid data leakage, since trials from the same cat are not independent.
Use scikit-learn’s KFold to create 5 folds over the unique cat IDs (not over rows). For each fold:
df to get training and test trialscat_ids = cat_summary["cat_id"].to_list()
kf = KFold(n_splits=5, shuffle=True, random_state=RNG_SEED)
for train_idx, test_idx in kf.split(cat_ids):
train_cats = [cat_ids[i] for i in train_idx]
test_cats = [cat_ids[i] for i in test_idx]
# Filter df for train/test cats using .filter(col("cat_id").is_in(...))RMSE formula using polars (from lab):
For Model B predictions, try using .replace_strict() to map condition labels to their training-set means.
Create a visualization comparing RMSE across folds for the two models. This could be a grouped bar chart, paired dot plot, or box plot — choose whatever makes the comparison clearest. Also report the mean RMSE for each model.
Write 3–4 sentences interpreting the cross-validation results:
your response here…
3 pts total
Write a short paragraph (4–6 sentences) reflecting on how the three techniques you used address different types of questions:
In your reflection, discuss which technique was most informative for understanding the cat name recognition experiment, and why.
your response here…
Before submitting, make sure you have:
Good luck!