lib/darkext/statistics.rb in darkhelmet-darkext-0.4.0 vs lib/darkext/statistics.rb in darkhelmet-darkext-0.4.1

- old
+ new

@@ -33,33 +33,34 @@ # Finds the mode of the array def mode map = self.histogram max = map.values.max - map.keys.select{ |x| map[x] == max} + map.keys.select { |x| map[x] == max } end # Finds the variance of the array - def variance - (self.sum_of_squares.to_f / self.size.to_f) - self.mean.square + def variance(dof = self.size) + self.sum_of_squares.to_f / dof.to_f end # Finds the standard deviation of the array - def deviation - self.variance.sqrt + def deviation(dof = self.size) + self.variance(dof).abs.sqrt end + alias :stddev :deviation # Randomly samples n elements def sample(n = 1) (1..n).collect { self[rand(self.size)] } end # Makes a two sided confidence interval for the array # Percent must be 0 < percent < 1 for this to work properly def ci(percent = 0.95, rho = 1) m = self.mean - i = ((Statistics.zscore((1 - percent) / 2) * rho) / + i = ((Darkext::Statistics::zscore((1 - percent) / 2) * rho) / self.size.sqrt).abs [m - i, m + i] end # Standardizes the array @@ -76,20 +77,18 @@ end end def sum_of_squares m = self.mean - self.map do |v| - (v - m).square - end.sum + self.map { |v| v - m }.map(&:square).sum end end module Darkext module Darkext::Statistics # Finds the probability of a z-score def self.prob(z) - p = Math.erf(z.abs/2.sqrt) / 2 + p = Math::erf(z.abs/2.sqrt) / 2 return 0.5 + p if 0 < z return 0.5 - p end # Finds the zscore of a probability @@ -109,10 +108,10 @@ # Finds a two tail p-val for a high/low array def self.p_val(r, n = 30, rho = 1, mu = r.mean) probs = r.map do |x| (x - mu) / (rho / n.sqrt) end.map do |x| - Statistics.prob(x) + Statistics::prob(x) end return 1 - (probs[1] - probs[0]) end module Darkext::Statistics::Regression