Permalink
Browse files

fixed formulas and the guess views

  • Loading branch information...
1 parent 450bbd2 commit 7151c847789f945ea7575f56eef23fd6d53c162f Felix Tsai committed Oct 8, 2012
Showing with 34 additions and 25 deletions.
  1. +28 −15 app/models/population_state.rb
  2. +3 −5 app/views/guesses/new.html.haml
  3. +3 −5 app/views/guesses/show.html.haml
@@ -7,6 +7,7 @@ def pop_data
end
def mean(gender, attribute)
+ attribute == :height ? pop_data[gender][attribute]/pop_data[gender][:total].to_f/12.0 :
pop_data[gender][attribute]/pop_data[gender][:total].to_f
end
@@ -16,33 +17,45 @@ def overall_prob(gender)
def variance(gender, attribute)
attribute_collection = []
- Person.all.each do |person|
- if person.sex == gender.to_s
- attribute_collection << person.send(attribute)
- end
+ if attribute == :height
+ Person.all.each do |person|
+ if person.sex == gender.to_s
+ attribute_collection << person.send(attribute)/12.0
+ end #end if
+ end #end each
+ else
+ Person.all.each do |person|
+ if person.sex == gender.to_s
+ attribute_collection << person.send(attribute)
+ end #end if
+ end #end each
end
(1/(pop_data[gender][:total] - 1).to_f)* attribute_collection.inject(0) {|sum, attrib| sum + (attrib - mean(gender, attribute))**2}
- end
+ end #end variance
- def attr_prob(gender, attribute)
- (1/Math.sqrt(2*Math::PI*variance(gender, attribute)**2))**((-(6-mean(gender, attribute))**2)/2*variance(gender, attribute))
+ def attr_prob(gender, attribute, guess_attribute)
+ if attribute == :height
+ (1/Math.sqrt(2*Math::PI*variance(gender, attribute))) * Math.exp((-(guess_attribute/12.0 - mean(gender, attribute))**2)/(2*variance(gender, attribute)))
+ else
+ (1/Math.sqrt(2*Math::PI*variance(gender, attribute))) * Math.exp((-(guess_attribute - mean(gender, attribute))**2)/(2*variance(gender, attribute)))
+ end
end
- def posterior(gender)
- overall_prob(gender)*attr_prob(gender, :height)*attr_prob(gender, :weight)
+ def posterior(gender, guess_height, guess_weight)
+ overall_prob(gender)*attr_prob(gender, :height, guess_height)*attr_prob(gender, :weight, guess_weight)
end
- def guess
+ def guess(height, weight)
if total_males <= 1 || total_females <= 1
"Not enough information. Please enter more person data."
- elsif posterior(:male) > posterior(:female)
- "Our guess is male."
+ elsif posterior(:male, height, weight) > posterior(:female, height, weight)
+ "male."
elsif
- posterior(:female) > posterior(:male)
- "Our guess is female."
+ posterior(:female, height, weight) > posterior(:male, height, weight)
+ "female."
else
- "Equal probability. Flip a coin."
+ "Androgynous. Flip a coin."
end
end
@@ -1,12 +1,10 @@
= form_for @guess do |f|
- .field
- = f.label :name
- = f.text_field :name
+
.field
= f.label "Height in Whole Inches"
= f.number_field :height
.field
= f.label "Weight in Whole pounds"
= f.number_field :weight
- .actions
- = f.submit 'Have Miss Cleo Guess a Gender'
+ .action
+ = f.submit 'Guess a gender, mon'
@@ -1,17 +1,15 @@
%p#notice= notice
-%p
- %b Name:
- = @guess.name
%p
%b Height:
= @guess.height
%p
%b Weight:
= @guess.weight
%p
- %b Miss Cleo thinks this person is a:
- = @pop_state.guess
+ %b Miss Cleo thinks this person is
+ %b
+ = @pop_state.guess(@guess.height, @guess.weight)

0 comments on commit 7151c84

Please sign in to comment.