Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/stella/core_ext.rb,
lib/stella/core_ext.rb
Class Method Summary collapse
Instance Method Summary collapse
- #deviation ⇒ Object (also: #sd)
- #dump(format) ⇒ Object
- #histogram ⇒ Object
- #mean ⇒ Object
- #median ⇒ Object
- #mode ⇒ Object
- #percentile(perc) ⇒ Object
- #percentile_index(perc) ⇒ Object
- #permute ⇒ Object
- #permute! ⇒ Object
- #random ⇒ Object
- #sample(n = 1) ⇒ Object
- #squares ⇒ Object
- #sum ⇒ Object
- #to_json ⇒ Object
- #variance ⇒ Object
Class Method Details
.from_json(str) ⇒ Object
443 444 445 |
# File 'lib/stella/core_ext.rb', line 443 def self.from_json(str) Yajl::Parser.parse(str, :check_utf8 => false) end |
Instance Method Details
#deviation ⇒ Object Also known as: sd
413 |
# File 'lib/stella/core_ext.rb', line 413 def deviation ; Math::sqrt( self.variance ) ; end |
#dump(format) ⇒ Object
436 437 438 |
# File 'lib/stella/core_ext.rb', line 436 def dump(format) respond_to?(:"to_#{format}") ? send(:"to_#{format}") : raise("Unknown format: #{format}") end |
#histogram ⇒ Object
405 |
# File 'lib/stella/core_ext.rb', line 405 def histogram ; self.sort.inject({}){|a,x|a[x]=a[x].to_i+1;a} ; end |
#mean ⇒ Object
398 |
# File 'lib/stella/core_ext.rb', line 398 def mean; self.sum.to_f/self.size ; end |
#median ⇒ Object
399 400 401 402 403 404 |
# File 'lib/stella/core_ext.rb', line 399 def median case self.size % 2 when 0 then self.sort[self.size/2-1,2].mean when 1 then self.sort[self.size/2].to_f end if self.size > 0 end |
#mode ⇒ Object
406 407 408 409 410 |
# File 'lib/stella/core_ext.rb', line 406 def mode map = self.histogram max = map.values.max map.keys.select{|x|map[x]==max} end |
#percentile(perc) ⇒ Object
426 427 428 |
# File 'lib/stella/core_ext.rb', line 426 def percentile(perc) self.sort[percentile_index(perc)] end |
#percentile_index(perc) ⇒ Object
429 430 431 |
# File 'lib/stella/core_ext.rb', line 429 def percentile_index(perc) (perc * self.length).ceil - 1 end |
#permute ⇒ Object
415 |
# File 'lib/stella/core_ext.rb', line 415 def permute ; self.dup.permute! ; end |
#permute! ⇒ Object
416 417 418 419 420 |
# File 'lib/stella/core_ext.rb', line 416 def permute! (1...self.size).each do |i| ; j=rand(i+1) self[i],self[j] = self[j],self[i] if i!=j end;self end |
#random ⇒ Object
423 424 425 |
# File 'lib/stella/core_ext.rb', line 423 def random self[rand(self.size)] end |
#sample(n = 1) ⇒ Object
421 |
# File 'lib/stella/core_ext.rb', line 421 def sample n=1 ; (0...n).collect{ self[rand(self.size)] } ; end |
#squares ⇒ Object
411 |
# File 'lib/stella/core_ext.rb', line 411 def squares ; self.inject(0){|a,x|x.square+a} ; end |
#sum ⇒ Object
397 |
# File 'lib/stella/core_ext.rb', line 397 def sum ; self.inject(0){|a,x| next if x.nil? || a.nil?; x+a} ; end |
#to_json ⇒ Object
440 441 442 |
# File 'lib/stella/core_ext.rb', line 440 def to_json Yajl::Encoder.encode(self) end |
#variance ⇒ Object
412 |
# File 'lib/stella/core_ext.rb', line 412 def variance ; self.squares.to_f/self.size - self.mean.square; end |