Module: Daru::Maths::Statistics::Vector

Included in:
Vector
Defined in:
lib/daru/maths/statistics/vector.rb

Instance Method Summary collapse

Instance Method Details

#average_deviation_population(m = nil) ⇒ Object Also known as: adp



195
196
197
198
199
200
201
# File 'lib/daru/maths/statistics/vector.rb', line 195

def average_deviation_population m=nil
  type == :numeric or raise TypeError, "Vector must be numeric"
  m ||= mean
  (@data.inject( 0 ) { |memo, val| 
    @missing_values.has_key?(val) ? memo : ( val - m ).abs + memo
  }).quo( n_valid )
end

#box_cox_transformation(lambda) ⇒ Object

:nodoc:



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/daru/maths/statistics/vector.rb', line 278

def box_cox_transformation lambda # :nodoc:
  raise "Should be a numeric" unless @type == :numeric

  self.recode do |x|
    if !x.nil?
      if(lambda == 0)
        Math.log(x)
      else
        (x ** lambda - 1).quo(lambda)
      end
    else
      nil
    end
  end
end

#centerObject

Center data by subtracting the mean from each non-nil value.



260
261
262
# File 'lib/daru/maths/statistics/vector.rb', line 260

def center
  self - mean
end

#coefficient_of_variationObject Also known as: cov



106
107
108
# File 'lib/daru/maths/statistics/vector.rb', line 106

def coefficient_of_variation
  standard_deviation_sample / mean
end

#count(value = false) ⇒ Object

Retrieves number of cases which comply condition. If block given, retrieves number of instances where block returns true. If other values given, retrieves the frequency for this value. If no value given, counts the number of non-nil elements in the Vector.



114
115
116
117
118
119
120
121
122
123
# File 'lib/daru/maths/statistics/vector.rb', line 114

def count value=false
  if block_given?
    @data.inject(0){ |memo, val| memo += 1 if yield val; memo}
  elsif value
    val = frequencies[value]
    val.nil? ? 0 : val
  else
    size - @missing_positions.size
  end
end

#dichotomize(low = nil) ⇒ Object

Dichotomize the vector with 0 and 1, based on lowest value. If parameter is defined, this value and lower will be 0 and higher, 1.



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/daru/maths/statistics/vector.rb', line 245

def dichotomize(low = nil)
  low ||= factors.min

  self.recode do |x|
    if x.nil? 
      nil
    elsif x > low
      1
    else
      0
    end
  end
end

#factorsObject

Retrieve unique values of non-nil data



52
53
54
# File 'lib/daru/maths/statistics/vector.rb', line 52

def factors
  only_valid.uniq.reset_index!
end

#freqsObject



86
87
88
# File 'lib/daru/maths/statistics/vector.rb', line 86

def freqs
  Daru::Vector.new(frequencies)
end

#frequenciesObject



76
77
78
79
80
81
82
83
84
# File 'lib/daru/maths/statistics/vector.rb', line 76

def frequencies
  @data.inject({}) do |hash, element|
    unless element.nil?
      hash[element] ||= 0
      hash[element] += 1
    end
    hash
  end
end

#kurtosis(m = nil) ⇒ Object



185
186
187
188
189
190
191
192
193
# File 'lib/daru/maths/statistics/vector.rb', line 185

def kurtosis m=nil
  if @data.respond_to? :kurtosis
    @data.kurtosis
  else
    m ||= mean
    fo  = @data.inject(0){ |a, x| a + ((x - m) ** 4) }
    fo.quo((@size - @missing_positions.size) * standard_deviation_sample(m) ** 4) - 3
  end
end

#max(return_type = :stored_type) ⇒ Object

Maximum element of the vector.

Parameters:

  • return_type (Symbol) (defaults to: :stored_type)

    Data type of the returned value. Defaults to returning only the maximum number but passing :vector will return a Daru::Vector with the index of the corresponding maximum value.



61
62
63
64
65
66
67
68
# File 'lib/daru/maths/statistics/vector.rb', line 61

def max return_type=:stored_type
  max_value = @data.max
  if return_type == :vector
    Daru::Vector.new({index_of(max_value) => max_value}, name: @name, dtype: @dtype)
  else
    max_value
  end
end

#max_indexDaru::Vector

Return a Vector with the max element and its index.

Returns:



72
73
74
# File 'lib/daru/maths/statistics/vector.rb', line 72

def max_index
  max :vector
end

#meanObject



8
9
10
# File 'lib/daru/maths/statistics/vector.rb', line 8

def mean
  @data.mean
end

#medianObject



28
29
30
# File 'lib/daru/maths/statistics/vector.rb', line 28

def median
  @data.respond_to?(:median) ? @data.median : percentile(50)
end

#median_absolute_deviationObject Also known as: mad



37
38
39
40
# File 'lib/daru/maths/statistics/vector.rb', line 37

def median_absolute_deviation
  m = median
  recode {|val| (val - m).abs }.median
end

#minObject



20
21
22
# File 'lib/daru/maths/statistics/vector.rb', line 20

def min
  @data.min
end

#modeObject



32
33
34
35
# File 'lib/daru/maths/statistics/vector.rb', line 32

def mode
  freqs = frequencies.values
  @data[freqs.index(freqs.max)]
end

#percentile(q, strategy = :midpoint) ⇒ Object Also known as: percentil

Returns the value of the percentile q

Accepts an optional second argument specifying the strategy to interpolate when the requested percentile lies between two data points a and b Valid strategies are:

  • :midpoint (Default): (a + b) / 2

  • :linear : a + (b - a) * d where d is the decimal part of the index between a and b.

References

This is the NIST recommended method (en.wikipedia.org/wiki/Percentile#NIST_method)



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/daru/maths/statistics/vector.rb', line 213

def percentile(q, strategy = :midpoint)
  sorted = only_valid(:array).sort

  case strategy
  when :midpoint
    v = (n_valid * q).quo(100)
    if(v.to_i!=v)
      sorted[v.to_i]
    else
      (sorted[(v-0.5).to_i].to_f + sorted[(v+0.5).to_i]).quo(2)
    end
  when :linear
    index = (q / 100.0) * (n_valid + 1)

    k = index.truncate
    d = index % 1

    if k == 0
      sorted[0]
    elsif k >= sorted.size
      sorted[-1]
    else
      sorted[k - 1] + d * (sorted[k] - sorted[k - 1])
    end
  else
    raise NotImplementedError.new "Unknown strategy #{strategy.to_s}"
  end
end

#productObject



16
17
18
# File 'lib/daru/maths/statistics/vector.rb', line 16

def product
  @data.product
end

#proportion(value = 1) ⇒ Object



125
126
127
# File 'lib/daru/maths/statistics/vector.rb', line 125

def proportion value=1
  frequencies[value].quo(n_valid).to_f
end

#proportionsObject



90
91
92
93
# File 'lib/daru/maths/statistics/vector.rb', line 90

def proportions
  len = n_valid
  frequencies.inject({}) { |hash, arr| hash[arr[0]] = arr[1] / len; hash }
end

#rangeObject



24
25
26
# File 'lib/daru/maths/statistics/vector.rb', line 24

def range
  max - min
end

#rankedObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/daru/maths/statistics/vector.rb', line 95

def ranked
  sum = 0
  r = frequencies.sort.inject( {} ) do |memo, val|
    memo[val[0]] = ((sum + 1) + (sum + val[1])).quo(2)
    sum += val[1]
    memo
  end

  recode { |e| r[e] }
end

#sample_with_replacement(sample = 1) ⇒ Object

Returns an random sample of size n, with replacement, only with non-nil data.

In all the trails, every item have the same probability of been selected.



323
324
325
326
327
328
329
330
331
# File 'lib/daru/maths/statistics/vector.rb', line 323

def sample_with_replacement(sample=1)
  if @data.respond_to? :sample_with_replacement
    @data.sample_with_replacement sample
  else
    valid = missing_positions.empty? ? self : self.only_valid
    vds = valid.size
    (0...sample).collect{ valid[rand(vds)] }
  end
end

#sample_without_replacement(sample = 1) ⇒ Object

Returns an random sample of size n, without replacement, only with valid data.

Every element could only be selected once.

A sample of the same size of the vector is the vector itself.



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/daru/maths/statistics/vector.rb', line 339

def sample_without_replacement(sample=1)
  if @data.respond_to? :sample_without_replacement
    @data.sample_without_replacement sample
  else
    valid = missing_positions.empty? ? self : self.only_valid 
    raise ArgumentError, "Sample size couldn't be greater than n" if 
      sample > valid.size
    out  = []
    size = valid.size
    while out.size < sample
      value = rand(size)
      out.push(value) if !out.include?(value)
    end

    out.collect{|i| valid[i]}
  end
end

#skew(m = nil) ⇒ Object

Calculate skewness using (sigma(xi - mean)^3)/((N)*std_dev_sample^3)



175
176
177
178
179
180
181
182
183
# File 'lib/daru/maths/statistics/vector.rb', line 175

def skew m=nil
  if @data.respond_to? :skew
    @data.skew
  else
    m ||= mean
    th  = @data.inject(0) { |memo, val| memo + ((val - m)**3) }
    th.quo ((@size - @missing_positions.size) * (standard_deviation_sample(m)**3))
  end
end

#standard_deviation_population(m = nil) ⇒ Object Also known as: sdp



156
157
158
159
160
161
162
163
# File 'lib/daru/maths/statistics/vector.rb', line 156

def standard_deviation_population m=nil
  m ||= mean
  if @data.respond_to? :standard_deviation_population
    @data.standard_deviation_population(m)
  else
    Math::sqrt(variance_population(m))
  end
end

#standard_deviation_sample(m = nil) ⇒ Object Also known as: sds, sd



165
166
167
168
169
170
171
172
# File 'lib/daru/maths/statistics/vector.rb', line 165

def standard_deviation_sample m=nil
  m ||= mean
  if @data.respond_to? :standard_deviation_sample
    @data.standard_deviation_sample m
  else
    Math::sqrt(variance_sample(m))
  end
end

#standard_errorObject Also known as: se



43
44
45
# File 'lib/daru/maths/statistics/vector.rb', line 43

def standard_error
  standard_deviation_sample/(Math::sqrt((n_valid)))
end

#standardize(use_population = false) ⇒ Object

Standardize data.

Arguments

  • use_population - Pass as true if you want to use population

standard deviation instead of sample standard deviation.



270
271
272
273
274
275
276
# File 'lib/daru/maths/statistics/vector.rb', line 270

def standardize use_population=false
  m ||= mean
  sd = use_population ? sdp : sds
  return Daru::Vector.new([nil]*@size) if m.nil? or sd == 0.0

  vector_standardized_compute m, sd
end

#sumObject



12
13
14
# File 'lib/daru/maths/statistics/vector.rb', line 12

def sum
  @data.sum
end

#sum_of_squared_deviationObject



47
48
49
# File 'lib/daru/maths/statistics/vector.rb', line 47

def sum_of_squared_deviation
  (@data.inject(0) { |a,x| x.square + a } - (sum.square.quo(n_valid)).to_f).to_f
end

#sum_of_squares(m = nil) ⇒ Object Also known as: ss



149
150
151
152
153
154
# File 'lib/daru/maths/statistics/vector.rb', line 149

def sum_of_squares(m=nil)
  m ||= mean
  @data.inject(0) { |memo, val| 
    @missing_values.has_key?(val) ? memo : (memo + (val - m)**2) 
  }
end

#variance_population(m = nil) ⇒ Object

Population variance with denominator (N)



140
141
142
143
144
145
146
147
# File 'lib/daru/maths/statistics/vector.rb', line 140

def variance_population m=nil
  m ||= mean
  if @data.respond_to? :variance_population
    @data.variance_population m
  else
    sum_of_squares(m).quo((n_valid)).to_f            
  end
end

#variance_sample(m = nil) ⇒ Object Also known as: variance

Sample variance with denominator (N-1)



130
131
132
133
134
135
136
137
# File 'lib/daru/maths/statistics/vector.rb', line 130

def variance_sample m=nil
  m ||= self.mean
  if @data.respond_to? :variance_sample
    @data.variance_sample m
  else
    sum_of_squares(m).quo((n_valid) - 1)
  end
end

#vector_centered_compute(m) ⇒ Object



309
310
311
312
313
314
315
316
# File 'lib/daru/maths/statistics/vector.rb', line 309

def vector_centered_compute(m)
  if @data.respond_to? :vector_centered_compute
    @data.vector_centered_compute(m)
  else
    Daru::Vector.new @data.collect { |x| x.nil? ? nil : x.to_f-m },
      index: index, name: name, dtype: dtype
  end
end

#vector_percentileObject

Replace each non-nil value in the vector with its percentile.



295
296
297
298
# File 'lib/daru/maths/statistics/vector.rb', line 295

def vector_percentile
  c = size - missing_positions.size
  ranked.recode! { |i| i.nil? ? nil : (i.quo(c)*100).to_f }
end

#vector_standardized_compute(m, sd) ⇒ Object



300
301
302
303
304
305
306
307
# File 'lib/daru/maths/statistics/vector.rb', line 300

def vector_standardized_compute(m,sd)
  if @data.respond_to? :vector_standardized_compute
    @data.vector_standardized_compute(m,sd)
  else
    Daru::Vector.new @data.collect { |x| x.nil? ? nil : (x.to_f - m).quo(sd) },
      index: index, name: name, dtype: dtype
  end
end