Spring 2024 Quiz 5

← return to practice.dsc10.com


This quiz was administered in-person. It was closed-book and closed-note; students were not allowed to use the DSC 10 Reference Sheet. Students had 20 minutes to work on the quiz.

This quiz covered Lectures 20-23 of the Spring 2024 offering of DSC 10.

⚠️ PDF version available here .


That's the end of the quiz.

The DataFrame bikes contains a sample of 500 bikes for sale locally. Columns are:


Problem 1

Lecture 22

You want to know if there is a significant difference in the sale prices of "road" and "hybrid" bikes using a permutation test. The hypotheses are:


Problem 1.1

Using the bikes DataFrame and the difference in group means (in the order "road" minus "hybrid") as your test statistic, fill in the blanks so the code below generates 10,000 simulated statistics for the permutation test.

def find_diff(df):
    group_means = df.get(["shuffled", "price"]).groupby("shuffled").mean().get("price")
    return group_means.loc["road"] - group_means.loc["hybrid"]

some_bikes = __(x)__
diffs = np.array([])
for i in np.arange(10000):
    shuffled_df = some_bikes.assign(shuffled = __(y)__)  
    diffs = np.append(diffs, find_diff(shuffled_df))

Answer:

(x): bikes[(bikes.get("style") == "road") | (bikes.get("style") == "hybrid")]
(y): np.random.permutation(some_bikes.get("style"))

  1. filters bikes down to only the “road” and “hybrid” rows, since the test compares just those two groups. (y) shuffles the “style” labels with np.random.permutation, which breaks any link between style and price (simulating the null that both come from the same distribution) while keeping the group sizes the same.

Difficulty: ⭐️⭐️⭐️

The average score on this problem was 54%.


Problem 1.2

Do large values of the observed statistic make us lean towards the null or alternative hypothesis?

Answer: Null Hypothesis

The test statistic is road minus hybrid, but the alternative says hybrid bikes are more expensive. If hybrids cost more, road minus hybrid becomes negative, so the alternative is supported by very small (negative) values. A large positive value means road bikes cost more than hybrids, which is the opposite of the alternative and fits the null.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 63%.


Problem 1.3

Suppose the p-value for this test evaluates to 0.04. What can you conclude based on this? Select all that apply.

Answer: Fail to reject the null hypothesis at a significance level of 0.01, Reject the null hypothesis at a significance level of 0.05.

Since 0.04 > 0.01, the p-value isn’t small enough to reject the null at the 0.01 level, so we fail to reject there. But 0.04 < 0.05, so we do reject the null at the 0.05 level, making those two options correct.


Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 48%.



Problem 2

Lecture 21

You want to determine if the bikes for sale locally have the following distribution of "style": "electric" (15%), "mountain" (20%), "road" (40%), "hybrid" (20%), and "recumbent" (5%). You want to use the 500 rows of bikes to test the following hypotheses:


Problem 2.1

Suppose that in bikes, the "style" column is distributed as follows: "electric" (20%), "mountain" (20%), "road" (30%), "hybrid" (20%), and "recumbent" (10%).

Let’s do a hypothesis test with total variation distance (TVD) as the test statistic. Fill in the blanks below to complete a simulation that calculates 10,000 TVDs under the null hypothesis.

def total_variation_distance(distr1, distr2):
    return __(a)__

proposed = np.array([0.15, 0.20, 0.40, 0.20, 0.05])
observed = np.array([0.20, 0.20, 0.30, 0.20, 0.10])
tvds = np.array([])
for i in np.arange(10000):
    simulated = np.random.multinomial(__(b)__, __(c)__) / 500
    new_tvd = total_variation_distance(__(d)__, __(e)__)
    tvds = np.append(tvds, new_tvd)

Answer:
(a): np.abs((distr1 - distr2)).sum() / 2
(b): 500
(c): proposed
(d): simulated
(e): proposed

  1. TVD is the sum of the absolute differences between two distributions, divided by 2: np.abs(distr1 - distr2).sum() / 2.

(b), (c) Under the null, each simulated sample draws 500 bikes from the proposed distribution, so np.random.multinomial(500, proposed), and dividing by 500 turns the counts into proportions.

(d), (e) Each simulated distribution is then compared to proposed (the null distribution) to get one simulated TVD, which gets appended to tvds.


Difficulty: ⭐️⭐️

The average score on this problem was 78%.


Problem 2.2

Fill in the blanks below so that observed_tvd corresponds to the observed value of the test statistic for this hypothesis test.

observed_tvd = total_variation_distance(__(f)__, __(g)__)

Answer:
(f): observed
(g): proposed

observed_tvd is the TVD between the real data’s style proportions (observed) and the null distribution (proposed). It has to be computed the same way as the simulated TVDs in 2.1, which also compared against proposed.


Difficulty: ⭐️⭐️

The average score on this problem was 83%.


Problem 2.3

Which of the following correctly calculates the p-value for this hypothesis test?

Answer: p_value = np.count_nonzero(tvds >= observed_tvd) / len(tvds)

The p-value is the proportion of simulated TVDs that are at least as large as the observed TVD, so we use >= (large TVDs mean the data is far from the null). We divide by len(tvds), the number of simulations (10,000), since that’s the total we’re taking the proportion of, not len(observed), which is just the 5 style categories.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 73%.



👋 Feedback: Find an error? Still confused? Have a suggestion? Let us know here.