Sums of Dice
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 15 of 64.
Roll two dice and add them: 7 comes up six times as often as 2. Count the pairs of faces:
A simulation agrees. Add the dice roll by roll, then count each total; value_counts lists the most common first:
rng = np.random.default_rng(2)
first = rng.integers(1, 7, size=3600)
second = rng.integers(1, 7, size=3600)
counts = pd.Series(first + second).value_counts()
print(counts.head(3))This prints:
7 605
6 522
8 443
Name: count, dtype: int64ax.bar(counts.index, counts) draws the whole tent:
A bar chart like this is your first look at any label: its rare values are the ones a model will barely learn.
Challenge
MediumIn a role-playing game, damage is the total of two four-sided dice, 1 to 4 each. The program reads how many attacks; the starter rolls into first and second and labels the chart.
- Count each damage total.
- Print the most likely damage and its share of the attacks, to 3 decimals.
- Draw the counts as bars, one per total, and show them.
For 16 attacks, too few to trust:
Most likely damage: 6, share 0.312Then Coddy describes your chart. With more attacks, the peak settles.
Try it yourself
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
rolls = int(input())
# Don't change the lines below
rng = np.random.default_rng(4)
first = rng.integers(1, 5, size=rolls)
second = rng.integers(1, 5, size=rolls)
fig, ax = plt.subplots()
ax.set_title("Damage Rolls")
ax.set_xlabel("Damage")
ax.set_ylabel("Rolls")
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler