Class: Quantile::Quantile

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/quantile/quantile.rb

Overview

Note:

Quantile is concurrency-safe.

A known quantile rank invariant for Estimator.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quantile, inaccuracy) ⇒ Quantile

Create a known quantile estimator invariant.

Parameters:

  • quantile (Float)

    The target quantile value expressed along the interval [0, 1]. For instance, 0.5, would generate a precomputed median value and 0.99 would provide the 99th percentile.

  • inaccuracy (Float)

    The target error allowance expressed along the interval [0, 1]. For instance, 0.05 sets an error allowance of 5 percent and 0.001 of 0.1 percent.



39
40
41
42
43
44
45
# File 'lib/quantile/quantile.rb', line 39

def initialize(quantile, inaccuracy)
  @quantile = quantile
  @inaccuracy = inaccuracy

  @coefficient_i  = (2.0 * inaccuracy) / (1.0 - quantile)
  @coefficient_ii = 2.0 * inaccuracy / quantile
end

Instance Attribute Details

#inaccuracyObject (readonly)

Returns the value of attribute inaccuracy.



24
25
26
# File 'lib/quantile/quantile.rb', line 24

def inaccuracy
  @inaccuracy
end

#quantileObject (readonly)

Returns the value of attribute quantile.



23
24
25
# File 'lib/quantile/quantile.rb', line 23

def quantile
  @quantile
end

Instance Method Details

#<=>(other) ⇒ Fixnum

Compare the given other quantile.

is less than, equal to, or greater than self. This is the basis for the tests in Comparable.

Returns:

  • (Fixnum)

    -1, 0, +1 or nil depending on whether the other quantile



62
63
64
# File 'lib/quantile/quantile.rb', line 62

def <=>(other)
  self.quantile <=> other.quantile && self.inaccuracy <=> other.inaccuracy
end

#delta(rank, n) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/quantile/quantile.rb', line 47

def delta(rank, n)
  if rank <= (@quantile * n).floor
    return @coefficient_i * (n - rank)
  end

  return @coefficient_ii * rank
end