Module: Daru::Accessors::ArrayWrapper::Statistics

Included in:
Daru::Accessors::ArrayWrapper
Defined in:
lib/daru/accessors/array_wrapper.rb

Instance Method Summary collapse

Instance Method Details

#average_deviation_population(m = nil) ⇒ Object



7
8
9
10
# File 'lib/daru/accessors/array_wrapper.rb', line 7

def average_deviation_population m=nil
  m ||= mean
  (@vector.inject(0) {|memo, val| val + (val - m).abs }) / n_valid
end

#coefficient_of_variationObject



12
13
14
# File 'lib/daru/accessors/array_wrapper.rb', line 12

def coefficient_of_variation
  standard_deviation_sample / mean
end

#count(value = false) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/daru/accessors/array_wrapper.rb', line 16

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

#factorsObject



25
26
27
28
# File 'lib/daru/accessors/array_wrapper.rb', line 25

def factors
  index = @data.sorted_indices
  index.reduce([]){|memo, val| memo.push(@data[val]) if memo.last != @data[val]; memo}
end

#frequenciesObject

TODO



30
31
32
33
34
35
36
# File 'lib/daru/accessors/array_wrapper.rb', line 30

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

#has_missing_data?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/daru/accessors/array_wrapper.rb', line 38

def has_missing_data?
  has_missing_data
end

#kurtosis(m = nil) ⇒ Object



42
43
44
45
46
# File 'lib/daru/accessors/array_wrapper.rb', line 42

def kurtosis m=nil
  m ||= mean
  fo  = @vector.inject(0){ |a, x| a + ((x - m) ** 4) }
  fo.quo(@size * standard_deviation_sample(m) ** 4) - 3
end

#maxObject



85
86
87
# File 'lib/daru/accessors/array_wrapper.rb', line 85

def max
  @vector.max
end

#meanObject



48
49
50
# File 'lib/daru/accessors/array_wrapper.rb', line 48

def mean
  sum.quo(@size).to_f
end

#medianObject



52
53
54
# File 'lib/daru/accessors/array_wrapper.rb', line 52

def median
  percentile 50
end

#median_absolute_deviationObject



56
57
58
59
# File 'lib/daru/accessors/array_wrapper.rb', line 56

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

#minObject



89
90
91
# File 'lib/daru/accessors/array_wrapper.rb', line 89

def min
  @vector.min
end

#modeObject



61
62
63
64
65
# File 'lib/daru/accessors/array_wrapper.rb', line 61

def mode
  freqs = frequencies.values

  @vector[freqs.index(freqs.max)]
end

#n_validObject



67
68
69
# File 'lib/daru/accessors/array_wrapper.rb', line 67

def n_valid
  @size
end

#percentile(percent) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/daru/accessors/array_wrapper.rb', line 71

def percentile percent
  sorted = @vector.sort
  v      = (n_valid * percent).quo(100)
  if v.to_i != v
    sorted[v.round]
  else
    (sorted[(v - 0.5).round].to_f + sorted[(v + 0.5).round]).quo(2)
  end
end

#productObject



81
82
83
# File 'lib/daru/accessors/array_wrapper.rb', line 81

def product
  @vector.inject(:*)
end

#proportion(value = 1) ⇒ Object



93
94
95
# File 'lib/daru/accessors/array_wrapper.rb', line 93

def proportion value=1
  frequencies[value] / n_valid
end

#proportionsObject



97
98
99
100
# File 'lib/daru/accessors/array_wrapper.rb', line 97

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

#rangeObject



102
103
104
# File 'lib/daru/accessors/array_wrapper.rb', line 102

def range
  max - min
end

#rankedObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/daru/accessors/array_wrapper.rb', line 106

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

  Daru::Vector.new @vector.map { |e| r[e] }, index: @caller.index,
    name: @caller.name, dtype: @caller.dtype
end

#recode(&block) ⇒ Object



118
119
120
# File 'lib/daru/accessors/array_wrapper.rb', line 118

def recode(&block)
  @vector.map(&block)
end

#recode!(&block) ⇒ Object



122
123
124
# File 'lib/daru/accessors/array_wrapper.rb', line 122

def recode!(&block)
  @vector.map!(&block)
end

#skew(m = nil) ⇒ Object

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



127
128
129
130
131
# File 'lib/daru/accessors/array_wrapper.rb', line 127

def skew m=nil
  m ||= mean
  th  = @vector.inject(0) { |memo, val| memo + ((val - m)**3) }
  th.quo (@size * (standard_deviation_sample(m)**3))
end

#standard_deviation_population(m = nil) ⇒ Object



133
134
135
136
# File 'lib/daru/accessors/array_wrapper.rb', line 133

def standard_deviation_population m=nil
  m ||= mean
  Math::sqrt(variance_population(m))
end

#standard_deviation_sample(m = nil) ⇒ Object



138
139
140
# File 'lib/daru/accessors/array_wrapper.rb', line 138

def standard_deviation_sample m=nil
  Math::sqrt(variance_sample(m))
end

#standard_errorObject



142
143
144
# File 'lib/daru/accessors/array_wrapper.rb', line 142

def standard_error
  standard_deviation_sample/(Math::sqrt(@size))
end

#sumObject



155
156
157
# File 'lib/daru/accessors/array_wrapper.rb', line 155

def sum
  @vector.inject(:+)
end

#sum_of_squared_deviationObject



146
147
148
# File 'lib/daru/accessors/array_wrapper.rb', line 146

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

#sum_of_squares(m = nil) ⇒ Object



150
151
152
153
# File 'lib/daru/accessors/array_wrapper.rb', line 150

def sum_of_squares(m=nil)
  m ||= mean
  @vector.inject(0) { |memo, val| memo + (val - m)**2 }
end

#variance_population(m = nil) ⇒ Object

Population variance with denominator (N)



167
168
169
170
171
# File 'lib/daru/accessors/array_wrapper.rb', line 167

def variance_population m=nil
  m ||= mean

  sum_of_squares(m).quo(@size).to_f
end

#variance_sample(m = nil) ⇒ Object

Sample variance with denominator (N-1)



160
161
162
163
164
# File 'lib/daru/accessors/array_wrapper.rb', line 160

def variance_sample m=nil
  m ||= self.mean

  sum_of_squares(m).quo(@size - 1)
end