lib/darkext/statistics.rb in darkhelmet-darkext-0.4.2 vs lib/darkext/statistics.rb in darkhelmet-darkext-0.4.3

- old
+ new

@@ -36,63 +36,82 @@ map = self.histogram max = map.values.max map.keys.select { |x| map[x] == max } end - # Population variance - def pvariance - self.sum_of_squares.to_f / self.size.to_f + # Variance + def population_variance + self.sum_of_squares.to_f / (self.size).to_f end - # Sample variance - def svariance + def sample_variance self.sum_of_squares.to_f / (self.size - 1).to_f end - # Population standard deviation - def pdeviation - self.pvariance.abs.sqrt + # Standard deviation + def population_deviation + self.population_variance.abs.sqrt end - # Sample standard deviation - def sdeviation - self.svariance.abs.sqrt + def sample_deviation + self.sample_variance.abs.sqrt end # 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) + # Generates a confidence interval + def ci(opts = { }) + opts.with_defaults!({ :percent => 0.95, :rho => 1, :type => :center }) m = self.mean - i = ((Darkext::Statistics::zscore((1 - percent) / 2) * rho) / + ret = Array.new + div = (opts[:type] == :center ? 2 : 1) + i = ((Darkext::Statistics::zscore((1 - percent) / div) * rho) / self.size.sqrt).abs - [m - i, m + i] + case opts[:type] + when :center + ret << m - i + ret << m + i + when :upper + ret << m + i + when :lower + ret << m - i + end + return ret end # Standardizes the array def standardize self.clone.standardize! end # Destructive standardize def standardize! m = self.mean.to_f - rho = self.deviation.to_f - self.map! do |v| - (v.to_f - m) / rho - end + rho = self.sample_deviation.to_f + self.map! { |v| (v.to_f - m) / rho } end def sum_of_squares m = self.mean self.map { |v| v - m }.map(&:square).sum end + + # Normalize the Array + def normalize + self.clone.normalize! + end + + # Normalize the Array destructive + 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 def self.prob(z) p = Math::erf(z.abs/2.sqrt) / 2 @@ -106,10 +125,10 @@ minz, maxz = -6, 6 zscore = 0.5 prob = 0 while (maxz - minz) > epsilon prob = prob(zscore) - if prob > p then maxz = zscore else minz = zscore end + prob > p ? maxz = zscore : minz = zscore zscore = (maxz + minz) * 0.5 end return zscore end