Module: Mikon::Stats

Extended by:
Forwardable
Included in:
DArray
Defined in:
lib/mikon/stats.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



82
83
84
# File 'lib/mikon/stats.rb', line 82

def ==(other)
  @data==other
end

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



10
11
12
13
# File 'lib/mikon/stats.rb', line 10

def average_deviation_population(m=nil)
  m ||= self.mean
  (self.reduce(0){|memo, val| val + (val - m).abs})/self.length
end

#coefficient_of_variationObject



15
16
17
# File 'lib/mikon/stats.rb', line 15

def coefficient_of_variation
  self.standard_deviation_sample/self.mean
end

#count(x = false) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/mikon/stats.rb', line 19

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

#each(&block) ⇒ Object



28
29
30
31
# File 'lib/mikon/stats.rb', line 28

def each(&block)
  return self.to_enum(:each) unless block_given?
  @data.each_along_dim(0, &block)
end

#each_index(&block) ⇒ Object



33
34
35
# File 'lib/mikon/stats.rb', line 33

def each_index(&block)
  self.each.with_index(&block)
end

#factorsObject

uniq



38
39
40
41
# File 'lib/mikon/stats.rb', line 38

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

#frequenciesObject



43
44
45
46
# File 'lib/mikon/stats.rb', line 43

def frequencies
  index = @data.sorted_indices
  index.reduce({}){|memo, val| memo[@data[val]] ||= 0; memo[@data[val]] += 1; memo}
end

#has_missing_data?Boolean Also known as: flawed?

Returns:

  • (Boolean)


48
49
50
# File 'lib/mikon/stats.rb', line 48

def has_missing_data?
  false
end

#is_valid?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/mikon/stats.rb', line 52

def is_valid?
  true
end

#kurtosis(m = nil) ⇒ Object



56
57
58
59
60
# File 'lib/mikon/stats.rb', line 56

def kurtosis(m=nil)
  m ||= self.mean
  fo=self.reduce(0){|a, x| a+((x-m)**4)}
  fo.quo(self.length*sd(m)**4)-3
end

#meanObject

alias_method :label, :labeling labeling(x) would be not implemented



65
66
67
# File 'lib/mikon/stats.rb', line 65

def mean
  @data.mean.first
end

#medianObject



69
70
71
# File 'lib/mikon/stats.rb', line 69

def median
  self.percentil(50)
end

#median_absolute_deviationObject Also known as: mad



73
74
75
76
# File 'lib/mikon/stats.rb', line 73

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

#modeObject



78
79
80
# File 'lib/mikon/stats.rb', line 78

def mode
  self.frequencies.max
end

#n_validObject



86
87
88
# File 'lib/mikon/stats.rb', line 86

def n_valid
  self.length
end

#percentil(percent) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/mikon/stats.rb', line 90

def percentil(percent)
  index = @data.sorted_indices
  pos = (self.length * percent)/100
  if pos.to_i == pos
    @data[index[pos.to_i]]
  else
    pos = (pos-0.5).to_i
    (@data[index[pos]] + @data[index[pos+1]])/2
  end
end

#productObject



101
102
103
# File 'lib/mikon/stats.rb', line 101

def product
  @data.inject(1){|memo, val| memo*val}
end

#proportion(val = 1) ⇒ Object



105
106
107
# File 'lib/mikon/stats.rb', line 105

def proportion(val=1)
  self.frequencies[val]/self.n_valid
end

#proportion_confidence_interval_tObject



109
110
111
# File 'lib/mikon/stats.rb', line 109

def proportion_confidence_interval_t
  raise "NotImplementedError"
end

#proportion_confidence_interval_zObject



113
114
115
# File 'lib/mikon/stats.rb', line 113

def proportion_confidence_interval_z
  raise "NotImplementedError"
end

#proportionsObject



117
118
119
120
# File 'lib/mikon/stats.rb', line 117

def proportions
  len = self.n_valid
  self.frequencies.reduce({}){|memo, arr| memo[arr[0]] = arr[1]/len}
end

#push(val) ⇒ Object



122
123
124
125
# File 'lib/mikon/stats.rb', line 122

def push(val)
  self.expand(self.length+1)
  self[self.length-1] = recode
end

#rangeObject



127
128
129
# File 'lib/mikon/stats.rb', line 127

def range
  max - min
end

#rankedObject

?



132
133
134
135
136
137
138
139
140
# File 'lib/mikon/stats.rb', line 132

def ranked
  sum = 0
  r = self.frequencies.sort.reduce({}) do |memo, val|
    memo[val[0]] = ((sum+1) + (sum+val[1]))/2
    sum += val[1]
    memo
  end
  Mikon::DArray.new(self.reduce{|val| r[val]})
end

#recode(&block) ⇒ Object



142
143
144
# File 'lib/mikon/stats.rb', line 142

def recode(&block)
  Mikon::DArray.new(@data.map(&block))
end

#recode!(&block) ⇒ Object



146
147
148
# File 'lib/mikon/stats.rb', line 146

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

#skew(m = nil) ⇒ Object

set_valid_data



156
157
158
159
160
# File 'lib/mikon/stats.rb', line 156

def skew(m=nil)
  m ||= self.mean
  th = self.reduce(0){|memo, val| memo + ((val - m)**3)}
  th/((self.length)*self.sd(m)**3)
end

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

split_by_separator_freq splitted



165
166
167
168
# File 'lib/mikon/stats.rb', line 165

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

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



170
171
172
173
174
175
176
# File 'lib/mikon/stats.rb', line 170

def standard_deviation_sample(m=nil)
  if !m.nil?
    Math.sqrt(variance_sample(m))
  else
    @data.std.first
  end
end

#standard_errorObject Also known as: se



178
179
180
# File 'lib/mikon/stats.rb', line 178

def standard_error
  self.standard_deviation_sample/(Math.sqrt(self.length))
end

#sumObject



191
192
193
# File 'lib/mikon/stats.rb', line 191

def sum
  @data.sum.first
end

#sum_of_squared_deviationObject



182
183
184
# File 'lib/mikon/stats.rb', line 182

def sum_of_squared_deviation
  self.reduce(0){|memo, val| val**2 + memo}
end

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



186
187
188
189
# File 'lib/mikon/stats.rb', line 186

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

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

def variance_population def variance_proportion



201
202
203
204
# File 'lib/mikon/stats.rb', line 201

def variance_sample(m=nil)
  m ||= self.mean
  self.sum_of_squares(m)/(self.length-1)
end

#vector_standarizedObject Also known as: standarized

def variance_total def vector_centered def vector_labeled def vector_percentil



211
212
213
# File 'lib/mikon/stats.rb', line 211

def vector_standarized
  raise "NotImplementedError"
end