Skip to content

Commit

Permalink
fixed PopulationStatus methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dladowitz committed Oct 6, 2012
1 parent d19d396 commit d03a5df
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 deletions app/models/person.rb
Expand Up @@ -10,8 +10,8 @@ class Person < ActiveRecord::Base

def population_add
pop_state = PopulationState.first
if self.sex == "M"
pop_state.total_males += 1
if self.sex == "male"
pop_state.total_males += 1
pop_state.total_male_height += self.height
pop_state.total_male_weight += self.weight
else
Expand All @@ -25,19 +25,19 @@ def population_add
def population_before_update
pop_state = PopulationState.first

if self.sex == "M"
if self.sex == "male"
pop_state.total_male_height -= Person.find(self.id).height
pop_state.total_male_weight -= Person.find(self.id).weight
else
pop_state.total_female_height -= Person.find(self.id).height
pop_state.total_female_weight -= Person.find(self.id).height
pop_state.total_female_weight -= Person.find(self.id).weight
end
pop_state.save
end

def population_after_update
pop_state = PopulationState.first
if self.sex == "M"
if self.sex == "male"
pop_state.total_male_height += self.height
pop_state.total_male_weight += self.weight
else
Expand All @@ -49,7 +49,7 @@ def population_after_update

def population_destroy
pop_state = PopulationState.first
if self.sex == "M"
if self.sex == "male"
pop_state.total_males -= 1
pop_state.total_male_height -= self.height
pop_state.total_male_weight -= self.weight
Expand Down
6 changes: 3 additions & 3 deletions app/models/population_state.rb
Expand Up @@ -2,7 +2,7 @@ class PopulationState < ActiveRecord::Base
attr_accessible :total_female_height, :total_female_weight, :total_females, :total_male_height, :total_male_weight, :total_males

def pop_data
@population_data ||= {:male {total: total_males, height: total_male_height, weight: total_male_weight},
@population_data ||= {:male => {total: total_males, height: total_male_height, weight: total_male_weight},
female: {total: total_females, height: total_female_height, weight: total_female_weight}}
end

Expand All @@ -18,14 +18,14 @@ def variance(gender, attribute)
attribute_collection = []
Person.all.each do |person|
if person.sex == gender.to_s
attribute_collection << person.send(:attribute)
attribute_collection << person.send(attribute)
end
end
(1/(pop_data[gender][:total] - 1).to_f)* attribute_collection.inject(0) {|sum, attrib| sum + (attrib - mean(gender, attribute))**2}
end

def attr_prob(gender, attribute)
(1/Math.sqrt(2*Math::PI*variance(gender, attribute)))**((-(6-mean(gender, attribute))**2)/2*variance(gender, attribute))
(1/Math.sqrt(2*Math::PI*variance(gender, attribute)**2))**((-(6-mean(gender, attribute))**2)/2*variance(gender, attribute))
end


Expand Down

0 comments on commit d03a5df

Please sign in to comment.