Skip to content
Browse files

fixed the standard deviation calculation to actually use residuals

  • Loading branch information...
1 parent aa894c8 commit 7f5f5dc80c463be7647cd2c8708c4f8d62b0acc0 @bentut committed Mar 15, 2013
Showing with 24 additions and 4 deletions.
  1. +9 −1 app/controllers/series_controller.rb
  2. +2 −1 lib/series_external_relationship.rb
  3. +13 −2 lib/series_statistics.rb
View
10 app/controllers/series_controller.rb
@@ -59,7 +59,15 @@ def comparison_graph
def outlier_graph
@series = Series.find params[:id]
@comp = @series.ma_data_side_by_side
- @std_dev = @series.backward_looking_moving_average.standard_deviation
+ residuals = @comp.map { |date, ma_hash| ma_hash[:residual] }
+ residuals.reject!{|a| a.nil?}
+ average = residuals.inject{ |sum, el| sum + el }.to_f / residuals.count
+ @std_dev = Math.sqrt((residuals.inject(0){ | sum, x | sum + (x - average) ** 2 }) / (residuals.count - 1))
+
+
+
+
+ #@series.backward_looking_moving_average.standard_deviation
end
def blog_graph
View
3 lib/series_external_relationship.rb
@@ -154,7 +154,8 @@ def ma_data_side_by_side
all_dates = self.data.keys | ma.data.keys
all_dates.each do |date_string|
ma_point = ma.data[date_string].nil? ? nil : ma.data[date_string]
- comparison_hash[date_string] = {:ma => ma_point, :udaman => self.data[date_string] }
+ residual = ma.data[date_string].nil? ? nil : ma.data[date_string] - self.data[date_string]
+ comparison_hash[date_string] = {:ma => ma_point, :udaman => self.data[date_string], :residual => residual }
end
return comparison_hash
end
View
15 lib/series_statistics.rb
@@ -23,8 +23,19 @@ def standard_deviation
def outlier
begin
outlier_hash = {}
- moving_average = self.backward_looking_moving_average
- std_dev = moving_average.standard_deviation
+ ma = moving_average = self.backward_looking_moving_average
+
+ all_dates = self.data.keys | ma.data.keys
+ residuals = []
+ all_dates.each do |date_string|
+ ma_point = ma.data[date_string].nil? ? nil : ma.data[date_string]
+ residual = ma.data[date_string].nil? ? nil : ma.data[date_string] - self.data[date_string]
+ residuals.push(residual) unless residual.nil?
+ end
+
+ average = residuals.inject{ |sum, el| sum + el }.to_f / residuals.count
+ std_dev = Math.sqrt((residuals.inject(0){ | sum, x | sum + (x - average) ** 2 }) / (residuals.count - 1))
+
mult = 2.5
self.data.each do |date_string, val|
next if moving_average.data[date_string].nil?

0 comments on commit 7f5f5dc

Please sign in to comment.
Something went wrong with that request. Please try again.