Instantly share code, notes, and snippets.

# wilkie/experiment.rb

Created November 19, 2012 23:51
Star You must be signed in to star a gist
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

### adrienne commented Apr 2, 2013

You, sir, are awesome. Thank you for being part of the solution. :)

### DanielHeath commented Feb 24, 2014

Output for those not wanting to run it (probability of a given number of female speakers at a conf with 15 speakers given a 20% female selection pool):

``````Probability of >0 women: 96.5%
Probability of >1 women: 83.4%
Probability of >2 women: 60.4%
Probability of >3 women: 35.4%
Probability of >4 women: 16.7%
Probability of >5 women: 6.4%
Probability of >6 women: 2.2%
Probability of >7 women: 0.9%
Probability of >8 women: 0.6%
Probability of >9 women: 0.6%
Probability of >10 women: 0.6%
Probability of >11 women: 0.6%
Probability of >12 women: 0.6%
Probability of >13 women: 0.6%
Probability of >14 women: 0.6%
``````

Looks like once the probabilities get really low there's some kind of bug (since all after 9 are the same).

### DanielHeath commented Feb 24, 2014

Is the number of people with something interesting to talk about great enough to approximate sampling with replacement? (that is, if you select a male speaker for the first slot does it appreciably shift the probabilities for the second slot?)

### DanielHeath commented Feb 24, 2014

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
``````