lib/fathom/plausible_range.rb in fathom-0.2.2 vs lib/fathom/plausible_range.rb in fathom-0.2.3

- old
+ new

@@ -1,14 +1,15 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'fathom')) module Fathom - class PlausibleRange + class PlausibleRange < Node - include NodeUtilities + include NumericMethods - attr_reader :upper_bound, :lower_bound, :confidence_interval, :name, :description, :hard_lower_bound, :hard_upper_bound + attr_reader :upper_bound, :lower_bound, :confidence_interval, :hard_lower_bound, :hard_upper_bound def initialize(opts={}) + super(opts) opts = OptionsHash.new(opts) @hard_upper_bound = opts[:hard_upper_bound] @upper_bound = opts[:upper_bound] @@ -26,37 +27,35 @@ @confidence_interval = opts[:confidence_interval] @confidence_interval ||= opts[:ci] @confidence_interval ||= 0.9 - @name = opts[:name] - @description = opts[:description] - end alias :min :lower_bound alias :max :upper_bound alias :ci :confidence_interval def midpoint @midpoint ||= lower_bound + (range / 2.0) end + alias :mean :midpoint def range @range ||= upper_bound - lower_bound end def standard_deviation - @standard_deviation ||= range / 3.29 + @standard_deviation ||= range / distribution.standard_deviations_under(confidence_interval) end alias :std :standard_deviation + alias :sd :standard_deviation - # Not using specific distributions yet def rand value = get_rand until is_bounded?(value) do - value= get_rand + value = get_rand end value end def array_of_random_values(n=10) @@ -80,20 +79,17 @@ elsif hard_upper_bound value <= hard_upper_bound end end + # Uses the distribution system, but doesn't use a vector to determine the sd, mean, etc. + # So keeping this part in-house. def get_rand - rng.gaussian(std) + midpoint + distribution.rand(sd) + mean end - def rng - @rng ||= GSL::Rng.alloc(GSL::Rng::MT19937_1999, Kernel.rand(100_000)) - end end - class R < PlausibleRange - end end if __FILE__ == $0 include Fathom # TODO: Is there anything you want to do to run this file on its own?