Skip to content

Instantly share code, notes, and snippets.

@mephistobooks
Created July 13, 2021 11:52
Show Gist options
  • Save mephistobooks/babee74138b7b62498a0d86ecb2de114 to your computer and use it in GitHub Desktop.
Save mephistobooks/babee74138b7b62498a0d86ecb2de114 to your computer and use it in GitHub Desktop.
Numo::NArray (SFloat, DFloatのために) にmoment, skew, kurtosisメソッドを追加する
#
#
#
require 'numo/narray'
module Numo
module StatisticsPlus
def sigma
stddev
end
def moment( n )
mu = mean
sgm = sigma
sgm_p2 = sgm*sgm
ret = if 1 == n
mu
elsif 2 == n
sgm_p2
elsif n > 2
_ret = 0.0
sz = self.size
(0...sz).each do |_i|
v = self[_i]
# _tmp = ((v.to_f-mu)**n)/sz
_tmp = ((v.to_f-mu)**n)
_ret += _tmp
end
_ret /= sz
_ret
else
raise "Strange degree: #{n}"
end
ret
end
def skew
mu_3 = moment(3)
sgm = sigma**3
ret = mu_3/sgm
ret
end
def kurtosis(definition: 0)
mu_4 = moment(4)
mu_2 = moment(2)
_beta_2 = mu_4/(mu_2**2)
if 0 == definition
_beta_2 -= 3.0
end
ret = _beta_2
ret
end
end
end
module Numo
class NArray
include StatisticsPlus
end
#class SFloat
# include StatisticsPlus
#end
#class DFloat
# include StatisticsPlus
#end
end
####
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment