require 'arrayx'
class Float
def roundf(decimel_places)
temp = self.to_s.length
sprintf("%#{temp}.#{decimel_places}f",self).to_f
end
end
class Integer
def ts(delimiter=',')
st = self.to_s.reverse
r = ""
max = if st[-1].chr == '-'
st.size - 1
else
st.size
end
if st.to_i == st.to_f
1.upto(st.size) {|i| r << st[i-1].chr ; r << delimiter if i%3 == 0 and i < max}
else
start = nil
1.upto(st.size) {|i|
r << st[i-1].chr
start = 0 if r[-1].chr == '.' and not start
if start
r << delimiter if start % 3 == 0 and start != 0 and i < max
start += 1
end
}
end
r.reverse
end
end
class Array
def sum
inject( nil ) { |sum,x| sum ? sum+x : x }
end
def mean
sum=0
self.each {|v| sum += v}
sum/self.size.to_f
end
def variance
m = self.mean
sum = 0.0
self.each {|v| sum += (v-m)**2 }
sum/self.size
end
def stdev
Math.sqrt(self.variance)
end
def count
k=Hash.new(0)
self.each {|x| k[x]+=1 }
k
end
def ^(other)
(self | other) - (self & other)
end
def freq(x)
h = self.count
h(x)
end
def maxcount
h = self.count
x = h.values.max
end
def mincount
h = self.count
x = h.values.min
end
def outliers(x)
h = self.count
min = self.count.values.uniq.sort.reverse.first(x).min
h.delete_if { |x,y| y < min }.keys.sort
end
def zscore(value)
(value - mean) / stdev
end
end