Section 4.2 Relative Frequency
When attempting to precisely measure uncertainty one often resorts to examples or experiments that model the theoretical question of interest. Before we investigate statistical experiments, we need to create some notation that we will utilize throughout the rest of this text.- S = Universal Set or Sample Space Experiment or Outcome Space. This is the collection of all possiblilities.
- Random Experiment. A random experiment is a repeatable activity that has more than one possible outcome all of which can be specified in advance but can not be known in advance with certainty.
- Trial. Performing a Random Experiment one time and measuring the result.
- A = Event. A collection of outcomes. Generally denoted by an upper case letter such as A, B, C, etc.
- Success/Failure. When recording the result of a trial, a success for event A occurs when the outcome lies in A. If not, then the trial was a failure. There is no qualitative meaning to this term.
- Mutually Exclusive Events. Two events that share no common outcomes. Also known as disjoint events.
- |A| = Frequency. In a sequence of n events, the frequency is the number of trials which resulted in a success for event A.
- |A| / n = Relative Frequency. A proportion of successes to total number of trials.
- Histogram. A bar chart representation of data where area corresponds to the value being described.
xxxxxxxxxx
coin = ["Heads", "Tails"]
def _(num_rolls = slider([5..5000],label="Number of Flips")):
rolls = [choice(coin) for roll in range(num_rolls)]
pretty_print(rolls)
freq = [0,0]
for outcome in rolls:
if (outcome=='Tails'):
freq[0] = freq[0]+1
else:
freq[1] = freq[1]+1
print("The frequency of tails = %s"%str(freq[0])
+" and heads = %s"%str(freq[1]))
rel = [freq[0]/num_rolls,freq[1]/num_rolls]
print("\nThe relative frequencies for Tails and Heads:"+str(rel))
show(bar_chart(freq,axes=False,ymin=0,xmin=-1/2),figsize=[5,4])
xxxxxxxxxx
def _(num_rolls = slider([20..5000],label='Number of rolls'),
Number_of_Sides = [4,6,8,12,20]):
die = list((1..Number_of_Sides))
rolls = [choice(die) for roll in range(num_rolls)]
pretty_print(rolls)
​
freq = [rolls.count(outcome) for outcome in set(die)]
print('The frequencies of each outcome is %s'%str(freq))
​
print('The relative frequencies of each outcome:')
rel_freq = [freq[outcome-1]/num_rolls for outcome in set(die)]
print(rel_freq)
fs = []
for f in rel_freq:
fs.append(f.n(digits=4))
print(fs)
show(bar_chart(freq,axes=False,ymin=0,xmin=-1/2),figsize=[5,4])
xxxxxxxxxx
def _(num_rolls = slider([10..5000],label='Number of rolls'),
num_sides = slider(4,20,1,6,label='Number of sides')):
die = list((1..num_sides))
dice = list((2..num_sides*2))
rolls = [(choice(die),choice(die)) for roll in range(num_rolls)]
sums = [sum(rolls[roll]) for roll in range(num_rolls)]
pretty_print(rolls)
​
freq = [sums.count(outcome) for outcome in set(dice)]
print('The frequencies of each outcome is %s'%str(freq))
print('The relative frequencies of each outcome:')
rel_freq = [freq[outcome-2]/num_rolls for outcome in set(dice)]
print(rel_freq)
show(bar_chart(freq,axes=False,ymin=0, xmin=-1),figsize=[5,4])
print("Relative Frequence of %s"%str(dice[0])
+" is about %s"%str(rel_freq[0].n(digits=4)))
print("Relative Frequence of %s"%str(dice[num_sides-1])
+" is about %s"%str(rel_freq[num_sides-1].n(digits=4)))
xxxxxxxxxx
var('A C D H J K Q S')
​
suits = [S, D, C, H]
values = [2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A]
​
full_deck = [(value, suit) for suit in suits for value in values]
def _(num_hands=slider[50..5000]): # Set up the number of hands to create
hands= [] # Start with a blank list.
for i in range(num_hands):
deck = copy(full_deck) # start over
shuffle(deck)
hands.append([deck.pop() for card in range(5)])
freq_values = []
one_pair = 0
two_pair = 0
three_kind = 0
full_house = 0
four_kind = 0
for i in range(num_hands):
hand = hands[i]
hand_values = [hand[k][0] for k in range(5)]
freq_values = [hand_values.count(value) for value in set(values)]
freq_values.sort(reverse=True)
if freq_values[0]==4:
four_kind=four_kind+1
if freq_values[0]==3:
if freq_values[1]==2:
full_house=full_house+1
if freq_values[1]==1:
three_kind=three_kind+1
if freq_values[0]==2:
if freq_values[1]==2:
two_pair=two_pair+1
if freq_values[1]==1:
one_pair=one_pair+1
print(" One Pair frequency = %s"%str(one_pair)
+" with relative frequency %s"%str(one_pair/num_hands))
print(" Two Pair frequency = %s"%str(two_pair)
+" with relative frequency %s"%str(two_pair/num_hands))
print("Three of a Kind frequency = %s"%str(three_kind)
+" with relative frequency %s"%str(three_kind/num_hands))
print(" Full House frequency = %s"%str(full_house)
+" with relative frequency %s"%str(full_house/num_hands))
print(" Four of a Kind frequency = %s"%str(four_kind)
+" with relative frequency %s"%str(four_kind/num_hands))
Definition 4.2.1. Cumulative relative frequency.
For a collection of ordered events x1<x2<...<xs with corresponding frequencies f1,f2,...,fs, the cumulative relative frequency is the function
F(x)=∑xk≤xfxk
xxxxxxxxxx
def _(num_rolls = slider([10..5000],label='Number of rolls'),
num_sides = slider(4,20,1,6,label='Number of sides')):
die = list((1..num_sides))
dice = list((2..num_sides*2))
rolls = [(choice(die),choice(die)) for roll in range(num_rolls)]
sums = [sum(rolls[roll]) for roll in range(num_rolls)]
pretty_print(rolls)
​
freq = [sums.count(outcome) for outcome in set(dice)]
n = len(freq)
CF = freq
for k in range(1,n):
CF[k] = freq[k] + CF[k-1]
print('The cumulative relative frequencies of each outcome:')
Crel_freq = [CF[outcome-2]/num_rolls for outcome in set(dice)]
print(Crel_freq)
show(bar_chart(CF,axes=False,ymin=0, xmin= -1),figsize=[5,4])
print("Cumulative Relative Frequence of %s"%str(dice[0])
+" is about %s"%str(Crel_freq[0].n(digits=4)))
print("Cumulative Relative Frequence of %s"%str(dice[num_sides-1])
+" is about %s"%str(Crel_freq[num_sides-1].n(digits=4)))