Class: Statsample::Histogram
- Inherits:
-
Object
- Object
- Statsample::Histogram
- Includes:
- GetText
- Defined in:
- lib/statsample/histogram.rb
Overview
A histogram consists of a set of bins which count the number of events falling into a given range of a continuous variable x.
This implementations follows convention of GSL for specification.
* Verbatim: *
The range for bin[i] is given by range[i] to range[i+1].
For n bins there are n+1 entries in the array range.
Each bin is inclusive at the lower end and exclusive at the upper end.
Mathematically this means that the bins are defined
by the following inequality,
bin[i] corresponds to range[i] <= x < range[i+1]
Here is a diagram of the correspondence between ranges and bins
on the number-line for x,
[ bin[0] )[ bin[1] )[ bin[2] )[ bin[3] )[ bin[4] )
---|---------|---------|---------|---------|---------|--- x
r[0] r[1] r[2] r[3] r[4] r[5]
In this picture the values of the range array are denoted by r.
On the left-hand side of each bin the square bracket ‘[’ denotes
an inclusive lower bound ( r <= x), and the round parentheses ‘)’
on the right-hand side denote an exclusive upper bound (x < r).
Thus any samples which fall on the upper end of the histogram are
excluded.
If you want to include this value for the last bin you will need to
add an extra bin to your histogram.
Reference: www.gnu.org/software/gsl/manual/html_node/The-histogram-struct.html
Instance Attribute Summary collapse
-
#bin ⇒ Object
readonly
Returns the value of attribute bin.
-
#name ⇒ Object
Returns the value of attribute name.
-
#range ⇒ Object
readonly
Returns the value of attribute range.
Class Method Summary collapse
Instance Method Summary collapse
- #increment(x, w = 1) ⇒ Object
-
#initialize(p1, min_max = false, opts = Hash.new) ⇒ Histogram
constructor
A new instance of Histogram.
- #report_building_text(generator) ⇒ Object
- #set_ranges(range) ⇒ Object
Constructor Details
#initialize(p1, min_max = false, opts = Hash.new) ⇒ Histogram
Returns a new instance of Histogram.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/statsample/histogram.rb', line 52 def initialize(p1, min_max=false, opts=Hash.new) if p1.is_a? Array range=p1 n_bins=p1.size-1 elsif p1.is_a? Integer n_bins=p1 end @bin=[0.0]*(n_bins) if(min_max) min, max=min_max[0], min_max[1] range=Array.new(n_bins+1) (n_bins+1).times {|i| range[i]=min+(i*(max-min).quo(n_bins)) } end range||=[0.0]*(n_bins+1) set_ranges(range) @name="" opts.each{|k,v| self.send("#{k}=",v) if self.respond_to? k } end |
Instance Attribute Details
#bin ⇒ Object (readonly)
Returns the value of attribute bin.
48 49 50 |
# File 'lib/statsample/histogram.rb', line 48 def bin @bin end |
#name ⇒ Object
Returns the value of attribute name.
47 48 49 |
# File 'lib/statsample/histogram.rb', line 47 def name @name end |
#range ⇒ Object (readonly)
Returns the value of attribute range.
49 50 51 |
# File 'lib/statsample/histogram.rb', line 49 def range @range end |
Class Method Details
Instance Method Details
#increment(x, w = 1) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/statsample/histogram.rb', line 75 def increment(x, w=1) if x.is_a? Array x.each{|y| increment(y,w) } elsif x.is_a? Numeric (range.size-1).times do |i| if x>=range[i] and x<range[i+1] @bin[i]+=w break end end end end |
#report_building_text(generator) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/statsample/histogram.rb', line 91 def report_building_text(generator) anchor=generator.toc_entry(_("Histogram %s") % [@name]) range.each_with_index do |r,i| next if i==@bin.size generator.text(sprintf("%4.2f : %d", r, @bin[i])) end end |
#set_ranges(range) ⇒ Object
87 88 89 90 |
# File 'lib/statsample/histogram.rb', line 87 def set_ranges(range) raise "Range size should be bin+1" if range.size!=@bin.size+1 @range=range end |