0
+ def initialize(enumerable = nil)
0
+ add_all(enumerable) unless enumerable.nil?
0
+ def add_with_count(key,c)
0
+ @hash[key] = (@hash[key] || 0) + c
0
+ def add_all(enumerable)
0
+ enumerable.each {|key| self << key }
0
+ @hash.delete_if do |k,v|
0
+ returning(yield(k,v)) do |deleted|
0
+ @counts -= v if deleted
0
+ [(k = @hash.keys.first),@hash[k]]
0
+ @hash.each_value { |v|
0
+ each_to_a(:each_count).max
0
+ return nil if @total == 0
0
+ returning(Histogram.new(max,max+1, 1)) do |histogram|
0
+ s = standard_deviation
0
+ yield compute_t_value(c, xbar, s)
0
+ def compute_t_value(c, xbar = mean, s = standard_deviation)
0
+ Math::sqrt(size)*(xbar - c)/s
0
+ def normal?(allowed_variance = 0.1)
0
+ lr = LinearRegression.new
0
+ lr.add_point(x += 1, t)
0
+ puts "slope is #{lr.slope}"
0
+ m < allowed_variance && m > -allowed_variance
0
+ each_count {|c| sos += c*c}
0
+ def zscores(width = 3, center = 0, &block)
0
+ each_to_a(:each_with_zscore, width, center, &block)
0
+ (sum_of_squares - total*mean)/(size)
0
+ def standard_deviation
0
+ def each_with_zscore(width = 3, center = 0, &block)
0
+ each_with_count { |k,v|
0
+ zscore = (v - mean) / standard_deviation
0
+ zscore = 0 if zscore.nan?
0
+ block.call k, zscore*(width/3)+center
0
+ attr_accessor :bucket_count, :max_value, :min_value
0
+ def initialize(bucket_count, max_value, min_value = 0, counts = nil)
0
+ @bucket_count = bucket_count
0
+ @min_value = min_value
0
+ @max_value = max_value
0
+ @counts = counts || [0] * bucket_count
0
+ @bucket_width ||= (@max_value - @min_value)/@bucket_count
0
+ def bucket_index_for(floor_bucket_value)
0
+ ((floor_bucket_value - min_value).to_f/bucket_width).floor
0
+ def count_for(floor_bucket_value)
0
+ @counts[bucket_index_for(floor_bucket_value)] || 0
0
+ raise ArgumentError.new("incompatible histograms (#{self.bucket_width} != #{other.bucket_width})") unless self.bucket_width == other.bucket_width
0
+ new_max = [max_value, other.max_value].max
0
+ new_min = [min_value, other.min_value].min
0
+ returning(Histogram.new((new_max-new_min)/bucket_width, new_max, new_min)) do |h|
0
+ h[h.bucket_index_for(bv)] = self.count_for(bv) + other.count_for(bv)
0
+ def bucket_index(value)
0
+ (@bucket_count *(value - @min_value).to_f / (@max_value - @min_value)).floor
0
+ returning(bucket_index(value)) do |c|
0
+ def to_s(options = {})
0
+ if options[:max_width]
0
+ if max > options[:max_width]
0
+ scale = options[:max_width] / max.to_f
0
+ @counts.map {|c| "| "+"*"*(c*scale).ceil}.join("\n")
0
+ class LinearRegression
0
+ def initialize(points = [])
0
+ @points.inject(0){|s, p| s+p[index]}
0
+ sum(index) / @points.size
0
+ def sum_x; sum(0); end
0
+ def mean_x; mean(0); end
0
+ def sum_y; sum(1); end
0
+ def mean_y; mean(1); end
0
+ def sum_of_product_of_deviations
0
+ @points.inject(0) {|s,p| s + ((p[0] - mx) * (p[1] - my))}
0
+ def sum_of_squares_of_deviations(index = 0)
0
+ @points.inject(0){|s,p| s+((p[index] - m)**2)}
0
+ sum_of_product_of_deviations / sum_of_squares_of_deviations
0
+ mean_y - mean_x * slope
0
\ No newline at end of file
Comments
No one has commented yet.