Skip to content

Instantly share code, notes, and snippets.

@jtushman
Created January 12, 2011 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtushman/776473 to your computer and use it in GitHub Desktop.
Save jtushman/776473 to your computer and use it in GitHub Desktop.
# Used to replace Abingo:Statistics
#
# Background here: http://en.wikipedia.org/wiki/F-test
# Has access to the following variables when included in an ABingo::Experiment
#
# TODO: Get a real stat guy to confirm my analysis
# Question: Does this work when there are varing observations per level
#
# alternatives (includes conversion_rate and participants)
# best_alternative
#
module Anova
# Step 1) Calculate the mean within each group
# Step 2) Calculate the overall mean
# Step 3) Calculate the "between group" sum of squares
# Step 4) Calculate the between group degrees of freedom
# Step 4) Calculate the between group mean square value
# Step 5) Calculate the "within-group" sum of squares
# Step 6) Calculate F-ratio
# Step 7) Is significant
def fratio
between_group_mean_square / within_group_mean_square
end
# Step 1) Calculate the mean within each group
# Step 2) Calculate the overall mean
def overall_mean
@overall_mean ||= (alternatives.sum(:conversions) * 1.0) / alternatives.count
end
def between_group_sum
@between_group_sum ||= alternatives.inject(0){|sum,n| sum + ((n.conversion_rate - overall_mean) ** 2)}
end
def between_group_degrees_of_freedom
@between_group_degrees_of_freedom ||= alternatives.count - 1
end
def between_group_mean_square
@between_group_mean_square ||= (between_group_sum * 1.0) / between_group_degrees_of_freedom
end
def within_group_degrees_of_freedom
#@within_group_degrees_of_freedom ||= alternatives.count * alternatives.minimum(:participants)
@within_group_degrees_of_freedom ||= alternatives.sum(:participants) - 1
end
def within_group_mean_square
within_group_sum_of_squares = 0
alternatives.each do |alt|
number_of_ones = alt.conversions
number_of_zeros = alt.participants - number_of_ones
alternative_sum = (number_of_ones * ((1 - alt.conversion_rate) ** 2)) + (number_of_zeros * ((0 - alt.conversion_rate) ** 2))
within_group_sum_of_squares = within_group_sum_of_squares + alternative_sum
end
(within_group_sum_of_squares * 1.0) / within_group_degrees_of_freedom
end
def f(p_value = 0.95)
Distribution::F.p_value(p_value,between_group_degrees_of_freedom,within_group_degrees_of_freedom)
end
def significant?
within_group_degrees_of_freedom > f
end
# def standard_error
#
# end
#
# def tukey_test(larger_mean,smaller_mean)
# (larger_mean - smaller_mean) / standard_error
# end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment