Class: Elastic::Stats::KS

Inherits:
Object
  • Object
show all
Includes:
ElasticClient
Defined in:
lib/elastic/stats/ks.rb

Overview

Utility to determine the KolmogorovSmirnov difference between to sets of data fetched from Elasticsearch

Constant Summary collapse

MULTIPLIERS =
{
  0.001 => 1.95,
  0.005 => 1.73,
  0.01 => 1.63,
  0.025 => 1.48,
  0.05 => 1.36,
  0.10 => 1.22
}

Instance Attribute Summary collapse

Attributes included from ElasticClient

#client, #index, #type

Instance Method Summary collapse

Methods included from ElasticClient

#analyze, #client_options, #client_options=, #search

Constructor Details

#initialize(indices, options = {}) ⇒ KS

indices should include all possible indices.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/elastic/stats/ks.rb', line 25

def initialize(indices, options = {})
  @indices  = indices

  options = default_options.update(options)

  @to       = options.delete(:to)
  @span     = options.delete(:span)
  @interval = options.delete(:interval)
  @field    = options.delete(:field)
  @offset   = options.delete(:offset)

  @indices = [indices] unless @indices.is_a? Array
  @from    = @to - @span
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



13
14
15
# File 'lib/elastic/stats/ks.rb', line 13

def field
  @field
end

#fromObject (readonly)

Returns the value of attribute from.



13
14
15
# File 'lib/elastic/stats/ks.rb', line 13

def from
  @from
end

#indicesObject (readonly)

Returns the value of attribute indices.



13
14
15
# File 'lib/elastic/stats/ks.rb', line 13

def indices
  @indices
end

#intervalObject (readonly)

Returns the value of attribute interval.



13
14
15
# File 'lib/elastic/stats/ks.rb', line 13

def interval
  @interval
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/elastic/stats/ks.rb', line 12

def logger
  @logger
end

#queryObject

Returns the value of attribute query.



12
13
14
# File 'lib/elastic/stats/ks.rb', line 12

def query
  @query
end

#spanObject (readonly)

Returns the value of attribute span.



13
14
15
# File 'lib/elastic/stats/ks.rb', line 13

def span
  @span
end

#toObject (readonly)

Returns the value of attribute to.



13
14
15
# File 'lib/elastic/stats/ks.rb', line 13

def to
  @to
end

Instance Method Details

#calculate(current, previous, confidence) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/elastic/stats/ks.rb', line 55

def calculate(current, previous, confidence)
  MULTIPLIERS[confidence] * Math.sqrt(
    (
      (current.count + previous.count).to_f /
      (current.count * previous.count)
    )
  )
end

#fetch(confidence = 0.05) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/elastic/stats/ks.rb', line 40

def fetch(confidence = 0.05)
  current  = range(@from, @to)
  previous = range(@from - @offset, @to - @offset)

  difference = Statsample::Test::KolmogorovSmirnov.new(
    current, previous
  ).d

  comparison = calculate(current, previous, confidence)
  {
    confidence: confidence, comparison: comparison,
    difference: difference, different?: (difference > comparison)
  }
end