Menu
CoddyTech

Probability as Frequency

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

A spam filter says "90% spam"; a seed packet says "80% sprout". A probability is a long-run share, from 0 (never) to 1 (always). Here are 50 seeds:

50 seeds as a grid of cells: 41 dark cells sprouted and 9 pale cells marked x did not, a share of 0.82 for a chance of 0.8

rng.random(n) draws decimals from 0 to 1, and 80% of them land below 0.8. So one comparison plants 1000 seeds:

rng = np.random.default_rng(3)
sprouted = rng.random(1000) < 0.8
print(sprouted.sum())
print(sprouted.mean())

This prints:

814
0.814

The mean of booleans is the share of True: 0.814, near 0.8. By this chapter's end, that trick prices a whole dice stall.

challenge icon

Challenge

Medium

A striker scores a penalty with some chance. The program reads the chance (0 to 1), then the number of kicks; the starter makes rng.

  1. Simulate the kicks: a goal wherever a rng.random draw is below the chance.
  2. Print the goals and the estimated chance, rounded to 3 decimals.

For 0.5 and 10 kicks, a short run that wobbles far from 0.5:

Goals: 3 of 10
Estimated chance: 0.3

Try it yourself

import numpy as np

chance = float(input())
kicks = int(input())

# Don't change the line below
rng = np.random.default_rng(21)

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler