Class: Trifle::Stats::Transponder::StandardDeviation

Inherits:
Object
  • Object
show all
Includes:
Mixins::Packer
Defined in:
lib/trifle/stats/transponder/standard_deviation.rb

Instance Method Summary collapse

Methods included from Mixins::Packer

included

Instance Method Details

#transpond(series:, path:, key: 'sd', sum: 'sum', count: 'count', square: 'square') ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/ParameterLists



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/trifle/stats/transponder/standard_deviation.rb', line 10

def transpond(series:, path:, key: 'sd', sum: 'sum', count: 'count', square: 'square') # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/ParameterLists
  keys = path.to_s.split('.')
  sum = sum.to_s.split('.')
  count = count.to_s.split('.')
  square = square.to_s.split('.')
  key = path.to_s.empty? ? key : [path, key].join('.')
  series[:values] = series[:values].map do |data|
    dcount = data.dig(*keys, *count)
    dsquare = data.dig(*keys, *square)
    dsum = data.dig(*keys, *sum)
    next data unless dcount && dsquare && dsum

    dres = Math.sqrt(
      (dcount * dsquare - dsum * dsum) / (dcount * (dcount - 1)) # rubocop:disable Lint/BinaryOperatorWithIdenticalOperands
    )
    signal = {
      key => dres.nan? ? BigDecimal(0) : dres
    }
    self.class.deep_merge(data, self.class.unpack(hash: signal))
  end
  series
end