Problem Solving — CS & DS

Lecture 26 — Counting and Probability in Data

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

The Survey Skew: weighted averages and representativeness

cs-100

Sample: 8 first-years (avg mood 6), 2 third-years (avg mood 8). Overall average mood - naive or weighted, and which is right?

m1, n1 = 6.0, 8
m2, n2 = 8.0, 2
pooled = (n1 * m1 + n2 * m2) / (n1 + n2)
weighted = (m1 + m2) / 2
print(round(pooled, 2), round(weighted, 2))

Expected: pooled mean 6.4 (per respondent); cohort-balanced 7.0 (per cohort)

Cost: O(1) - the difficulty is conceptual, not computational.

Worked example

The Late Bus Frequency: histogram as a hypothesis test

cs-101

Minutes-late over 14 days: 2, 0, 5, 3, 0, 7, 4, 1, 2, 6, 3, 0, 9, 2. Build the frequency table. Is 'usually less than 3 minutes late' a fair claim?

delays = [2, 0, 5, 3, 0, 7, 4, 1, 2, 6, 3, 0, 9, 2]
freq = {}
for d in delays:
    freq[d] = freq.get(d, 0) + 1
under3 = sum(c for d, c in freq.items() if d < 3)
print(under3, len(delays), f"{under3 / len(delays):.0%}")

Expected: 7 of 14 days under 3 minutes - 50%, not 'usually'

Cost: O(n) tally + O(u) report; the histogram is one of the cheapest, most honest tools in the kit.

Worked example

The Double-Click Question: conditional counting

cs-102

Of 100 sessions: 40 were mobile, and 10 sessions double-clicked. 8 of the double-clicks were mobile. P(mobile | double-click) vs P(double-click | mobile) - compute both, and why do they differ?

overlap, n_double, n_mobile = 8, 10, 40
p_m_given_d = overlap / n_double
p_d_given_m = overlap / n_mobile
print(round(p_m_given_d, 2), round(p_d_given_m, 2))

Expected: P(mobile | double-click) = 0.8; P(double-click | mobile) = 0.2

Cost: O(1) after the table exists; building the 2x2 from raw events is O(n).

Case attempt · lecture 26
Case cs-101 · Beginner

The Late Bus Frequency

From two weeks of bus logs, estimate the probability of a late arrival, and of two lates in a row (testing independence empirically).

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-101 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-101 — not embedded in this deck.

Case attempt · lecture 26
Case cs-102 · Foundational

The Double-Click Question

If 4% of clicks are accidental and 10% of clicks are on ads, what's the range for both-at-once? Bounds without independence; exact value with it.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-102 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-102 — not embedded in this deck.

Case attempt · lecture 26
Case cs-103 · Intermediate

The Grid Decision Review

A rare-condition flagger: 99% accurate, 1-in-1000 prevalence. Compute the chance a flagged person actually has it. The counterintuitive base-rate classic.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-103 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-103 — not embedded in this deck.

Case attempt · lecture 26
Case cs-137 · Advanced

The Backup Window

A lab server must back up 500 GB every night inside a one-hour window. Two designs are on the table; all failure probabilities are **stated assumptions about this lab's hardware**, not measurements:

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-137 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-137 — not embedded in this deck.

Case attempt · lecture 26
Case cs-104 · Expert

The Streak Simulation

How likely is a 5-win streak in a 20-game season at 55% win probability? Exact recursion vs Monte Carlo; compare and explain differences.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-104 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-104 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. What is a base rate and what does it do to PPV?
  2. When may two events not be multiplied?
  3. What did simulation add to the streak question?
Expected answers (instructor): prior prevalence; low base rates collapse PPV even with good tests when they are dependent — cs-102's paired clicks an empirical check that the counting was right
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF