Class: StatArray
Instance Method Summary collapse
- #gmean ⇒ Object
- #mean ⇒ Object (also: #arithmetic_mean)
- #median ⇒ Object
-
#pstddev ⇒ Object
Standard deviation of a population.
-
#pvariance ⇒ Object
Variance of a population.
-
#rcdf(normalised = 1.0) ⇒ Object
rank cdf - array is not sorted before cdf calculation.
-
#stddev ⇒ Object
Standard deviation of a sample.
-
#stderr ⇒ Object
Calculates the standard error of this sample.
- #sum ⇒ Object
-
#summed_sqdevs ⇒ Object
The sum of the squared deviations from the mean.
-
#variance ⇒ Object
Variance of the sample.
Methods inherited from Array
#ranks, #rep_perm, #shuffle, #threach, #to_statarray
Instance Method Details
#gmean ⇒ Object
140 141 142 143 144 145 |
# File 'lib/wordRS-lib.rb', line 140 def gmean return 0.0 if self.size == 0 return nil if self.any?{|x| x < 0 } # not negative return 0.0 if self.any?{|x| x == 0 } #includes 0 return 10**(self.map{|x| log10(x)}.sum/self.size) end |
#mean ⇒ Object Also known as: arithmetic_mean
86 87 88 89 |
# File 'lib/wordRS-lib.rb', line 86 def mean return 0.0 if self.size == 0 sum.to_f / self.size end |
#median ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/wordRS-lib.rb', line 92 def median return 0 if self.size == 0 tmp = sort mid = tmp.size / 2 if (tmp.size % 2) == 0 (tmp[mid-1] + tmp[mid]).to_f / 2 else tmp[mid] end end |
#pstddev ⇒ Object
Standard deviation of a population.
130 131 132 |
# File 'lib/wordRS-lib.rb', line 130 def pstddev Math::sqrt(pvariance) end |
#pvariance ⇒ Object
Variance of a population.
118 119 120 121 122 |
# File 'lib/wordRS-lib.rb', line 118 def pvariance # Variance of 0 or 1 elements is 0.0 return 0.0 if count < 2 summed_sqdevs / count end |
#rcdf(normalised = 1.0) ⇒ Object
rank cdf - array is not sorted before cdf calculation
148 149 150 151 |
# File 'lib/wordRS-lib.rb', line 148 def rcdf(normalised = 1.0) s = sum.to_f inject([0.0]) { |c,d| c << c[-1] + normalised.to_f*d.to_f/s } end |
#stddev ⇒ Object
Standard deviation of a sample.
125 126 127 |
# File 'lib/wordRS-lib.rb', line 125 def stddev Math::sqrt(variance) end |
#stderr ⇒ Object
Calculates the standard error of this sample.
135 136 137 138 |
# File 'lib/wordRS-lib.rb', line 135 def stderr return 0.0 if count < 2 stddev/Math::sqrt(size) end |
#sum ⇒ Object
82 83 84 |
# File 'lib/wordRS-lib.rb', line 82 def sum inject(0) { |sum, x| sum + x } end |
#summed_sqdevs ⇒ Object
The sum of the squared deviations from the mean.
104 105 106 107 108 |
# File 'lib/wordRS-lib.rb', line 104 def summed_sqdevs return 0 if count < 2 m = mean StatArray.new(map { |x| (x - m) ** 2 }).sum end |
#variance ⇒ Object
Variance of the sample.
111 112 113 114 115 |
# File 'lib/wordRS-lib.rb', line 111 def variance # Variance of 0 or 1 elements is 0.0 return 0.0 if count < 2 summed_sqdevs / (count - 1) end |