Class: Quantile::Quantile

Inherits:
Object
  • Object
show all
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.



37
38
39
40
41
42
43
# File 'lib/quantile/quantile.rb', line 37

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.



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

def inaccuracy
  @inaccuracy
end

#quantileObject (readonly)

Returns the value of attribute quantile.



21
22
23
# File 'lib/quantile/quantile.rb', line 21

def quantile
  @quantile
end

Instance Method Details

#delta(rank, n) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/quantile/quantile.rb', line 45

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

  return @coefficient_ii * rank
end