Created
October 1, 2011 18:58
This file contains hidden or 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
class Gene | |
end | |
class Person | |
@genes = [] | |
CHROM_PAIRS = 23 | |
def initialize(genes=nil) | |
@genes = [] | |
if !genes | |
CHROM_PAIRS.times do | |
@genes << [Gene.new, Gene.new] | |
end | |
else | |
@genes = genes | |
end | |
end | |
def zygote | |
zyg = [] | |
@genes.each do |gene_pair| | |
zyg << gene_pair[rand(2)] | |
end | |
zyg | |
end | |
def recombine(zyg1, zyg2) | |
genes = [] | |
CHROM_PAIRS.times do |i| | |
genes << [zyg1[i], zyg2[i]] | |
end | |
genes | |
end | |
def mate(person) | |
my_zyg = zygote | |
their_zyg = person.zygote | |
baby = recombine(my_zyg, their_zyg) | |
Person.new(baby) | |
end | |
attr_accessor :genes | |
def similarity(person) | |
similar_count = 0 | |
@genes.flatten.each do |gene| | |
similar_count += 1 if person.genes.flatten.include?(gene) | |
end | |
similar_count.to_f / @genes.flatten.size | |
end | |
end | |
def simulate(example, | |
&block) | |
prob = 0 | |
1000.times do | |
prob += yield | |
end | |
puts "#{example}: #{prob.to_f / 1000.to_f}" | |
end | |
simulate("double cousins") do | |
p1 = Person.new | |
p2 = Person.new | |
p3 = Person.new | |
p4 = Person.new | |
c1 = p1.mate(p2) | |
c2 = p1.mate(p2) | |
c3 = p3.mate(p4) | |
c4 = p3.mate(p4) | |
g3_c1 = c1.mate(c3) | |
g3_c2 = c2.mate(c4) | |
g3_c1.similarity(g3_c2) | |
end | |
simulate("regular cousins") do | |
p1 = Person.new | |
p2 = Person.new | |
p3 = Person.new | |
p4 = Person.new | |
c1 = p1.mate(p2) | |
c2 = p1.mate(p2) | |
c3 = c1.mate(p3) | |
c4 = c2.mate(p4) | |
c3.similarity(c4) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment