lib/darkext/statistics.rb in darkhelmet-darkext-0.11.0 vs lib/darkext/statistics.rb in darkhelmet-darkext-0.11.1
- old
+ new
@@ -35,11 +35,11 @@
return self.sort[self.size / 2]
end
end
# Generates a histogram hash for the array
- def histogram(bins = nil)
+ def histogram
self.sort.inject({}) do |a,x|
a[x] = a[x].to_i + 1
a
end
end
@@ -51,38 +51,22 @@
max = map.values.max
map.keys.select { |x| map[x] == max }
end
# Variance
- def population_variance
+ def variance
raise ArgumentError.new('Array size must be > 0') if self.size.zero?
self.sum_of_squares.to_f / (self.size).to_f
end
- def sample_variance
- raise ArgumentError.new('Array size must be > 0') if self.size.zero?
- self.sum_of_squares.to_f / (self.size - 1).to_f
- end
-
# Standard deviation
- def population_deviation
+ def standard_deviation
raise ArgumentError.new('Array size must be > 0') if self.size.zero?
- self.population_variance.abs.sqrt
+ self.variance.abs.sqrt
end
+ alias :stddev :standard_deviation
- def sample_deviation
- raise ArgumentError.new('Array size must be > 0') if self.size.zero?
- self.sample_variance.abs.sqrt
- end
-
- def geometric_deviation
- raise ArgumentError.new('Array size must be > 0') if self.size.zero?
- gmean = self.g_mean
- Math.exp((self.map { |x| (x.ln - gmean.ln).square }.sum.to_f / self.size.to_f).sqrt)
- end
- alias :gstddev :geometric_deviation
-
# Randomly samples n elements
def sample(n = 1)
(1..n).collect { self[rand(self.size)] }
end
@@ -115,30 +99,19 @@
end
# Destructive standardize
def standardize!
m = self.mean.to_f
- rho = self.sample_deviation.to_f
+ rho = self.standard_deviation.to_f
self.map! { |v| (v.to_f - m) / rho }
end
def sum_of_squares
raise ArgumentError.new('Array size must be > 0') if self.size.zero?
m = self.mean
self.map { |v| v - m }.squares.sum
end
-
- # Normalize the array
- def normalize
- self.clone.normalize!
- end
-
- # Normalize the array destructively
- def normalize!
- m = self.mean.to_f
- self.map! { |v| v / m }
- end
end
module Darkext
module Darkext::Statistics
# Finds the probability of a z-score
@@ -161,17 +134,20 @@
end
return zscore
end
# Finds a two tail p-val for a high/low array
+ # can't remember how to use this
+=begin
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)
end
return 1 - (probs[1] - probs[0])
end
+=end
module Darkext::Statistics::Regression
# Do a least squares linear regression on the two sets of x's and y's
# Returns a hash containing many relevant values
# * n (:n)