Menu
CoddyTech

Recap: Polling a Town

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 23 of 64.

challenge icon

Challenge

Medium

Time to poll the town about a new park. A poll reports a share of supporters, give or take a margin: 2 times the poll's .std() over the square root of its size. Do margins catch the truth? 25 polls of another town:

25 polls as horizontal bars, each a share with its margin. A dashed line marks the true support, 0.52, and a dotted line the half mark, 0.5. 24 bars cross the true support; the one that misses, poll 5, is orange and labelled poll 5 misses

voters holds 1 per supporter, 0 for the rest; the program reads the poll size.

  1. Print the true support.
  2. Run 400 polls of different voters; print the first one's share and margin.
  3. Print how many margins hold the true support, and how many polls show 0.5 or less.

Shares and margins to 3 decimals. For that other town and 200:

True support: 0.52
First poll: 0.525, give or take 0.071
Margins that caught the truth: 384 of 400
Polls calling it wrong: 127

Try it yourself

import numpy as np

size = int(input())

# Don't change the lines below: 1 for each voter who supports the park, 0 for the others
voters = np.concatenate([np.ones(10800), np.zeros(9200)])
rng = np.random.default_rng(23)

# Write your code below

All lessons in Statistics for ML

Practice on your own: Online Python compiler