Class: StatArray

Inherits:
Array show all
Defined in:
lib/wordRS-lib.rb

Instance Method Summary collapse

Methods inherited from Array

#ranks, #rep_perm, #shuffle, #threach, #to_statarray

Instance Method Details

#gmeanObject



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

#meanObject 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

#medianObject



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

#pstddevObject

Standard deviation of a population.



130
131
132
# File 'lib/wordRS-lib.rb', line 130

def pstddev
  Math::sqrt(pvariance)
end

#pvarianceObject

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

#stddevObject

Standard deviation of a sample.



125
126
127
# File 'lib/wordRS-lib.rb', line 125

def stddev
  Math::sqrt(variance)
end

#stderrObject

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

#sumObject



82
83
84
# File 'lib/wordRS-lib.rb', line 82

def sum
  inject(0) { |sum, x| sum + x }
end

#summed_sqdevsObject

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

#varianceObject

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