Skip to content

Instantly share code, notes, and snippets.

@wilkie
Created November 19, 2012 23:51
Show Gist options
  • Save wilkie/4114968 to your computer and use it in GitHub Desktop.
Save wilkie/4114968 to your computer and use it in GitHub Desktop.
Code to produce a probability distribution of how likely a gender breakdown in conference speakers is, and also to experiment to produce the result
# This program will select at random a set of speakers and look at the gender breakdown given 20% women
total_speakers = 15
percentage_of_women = 0.25
hist, list, results = {}, [], []
(1000 * (1 - percentage_of_women)).floor.times{list << 0}
(1000 * percentage_of_women).floor.times{list << 1}
100000.times{results << list.shuffle.take(total_speakers).select{|i|i==1}.count}
results.each{|num_women| hist[num_women] ||= 0; hist[num_women] += 1}
puts "Out of 100000 trials, the number of trials that resulted in X women selected follow."
hist.keys.sort.each{|key| puts "#{key}: #{hist[key]}"}
total_speakers = 15
def fact(n)
return n < 2 ? 1 : n * fact(n - 1)
end
probs = []
(total_speakers+1).times do |i|
num_women = i
num_men = total_speakers - i
# how many ways can this outcome occur?
num_outcomes = fact(total_speakers) / (fact(num_women) * fact(num_men))
# what is the probability of this outcome occuring once?
prob = (8.0/10.0)**(num_men) * (2.0/10.0)**(num_women)
# what is the total probability of the distribution?
total_prob = prob * num_outcomes
simple_percent = (total_prob * 1000).floor.to_f / 10
probs << simple_percent
puts "#{num_men} men and #{num_women} women = #{simple_percent}%"
end
rolling = 100
probs.each_with_index do |p,i|
simple_percent = (rolling * 10).floor.to_f / 10
puts "Probability of >#{i-1} women: #{simple_percent}%" unless i == 0
rolling = rolling - p
end
@DanielHeath
Copy link

Out of 100000 trials, the number of trials that resulted in X women selected follow.

0: 1303
1: 6650
2: 15573
3: 22670
4: 22583
5: 16660
6: 9089
7: 3852
8: 1232
9: 309
10: 70
11: 6
12: 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment