Last active
July 23, 2018 14:38
-
-
Save liprais/44849d3aead144696fdaa1afdfc9097f to your computer and use it in GitHub Desktop.
gender_bias
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
#!/usr/bin/ruby | |
def birth | |
dice = rand(100) | |
return dice > 50 ? 1 : 0 | |
end | |
### 生了男孩就不生了,生了女孩就一直生 | |
def family_birth(kids=[]) | |
kid = birth | |
kids.push(kid) | |
return kids if kid == 1 | |
family_birth(kids) if kid == 0 | |
end | |
### 家里只准生两个女孩,第三胎是女孩就打掉,是男孩就生 | |
def family_birth_type2(kids=[]) | |
kid = birth | |
if kid == 1 | |
kids.push(kid) | |
return kids | |
elsif kid == 0 and kids.empty? | |
kids.push(kid) | |
family_birth_type2(kids) | |
elsif kid == 0 and kids == [0] | |
kids.push(kid) | |
family_birth_type2(kids) | |
elsif kid == 0 and kids == [0,0] | |
family_birth_type2(kids) | |
end | |
end | |
def bias(n) | |
kids = [] | |
1.upto(n) do |i| | |
family_children = family_birth | |
kids.push(family_children) | |
end | |
return [kids.flatten.count(1),kids.flatten.count(0)] | |
end | |
def bias_type2(n) | |
kids = [] | |
1.upto(n) do |i| | |
family_children = family_birth_type2 | |
kids.push(family_children) | |
end | |
return [kids.flatten.count(1),kids.flatten.count(0)] | |
end | |
def flat(n) | |
kids = [] | |
1.upto(n) do |i| | |
kids.push(birth) | |
end | |
return [kids.count(1),kids.count(0)] | |
end | |
def plot(start,step,count,func) | |
i = 0 | |
while i < count - 1 | |
kids = Object.send(func,start + i * step) | |
p kids[0]/kids[1].to_f | |
i = i + step | |
end | |
end | |
plot(10000,100,50000,'bias_type2') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment