Created
November 19, 2012 23:51
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]}"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Out of 100000 trials, the number of trials that resulted in X women selected follow.