Module: RedAmber::VectorFunctions

Included in:
Vector
Defined in:
lib/red_amber/vector_functions.rb

Overview

mix-ins for class Vector

Instance Method Summary collapse

Instance Method Details

#coerce(other) ⇒ Object



197
198
199
# File 'lib/red_amber/vector_functions.rb', line 197

def coerce(other)
  [Vector.new(Array(other) * size), self]
end

#is_naObject



103
104
105
# File 'lib/red_amber/vector_functions.rb', line 103

def is_na
  numeric? ? (is_nil | is_nan) : is_nil
end

#quantile(prob = 0.5, interpolation: :linear, skip_nils: true, min_count: 0) ⇒ Float

Return quantile

0.5 quantile (median) is returned by default.
Or return quantile for specified probability (prob).
If quantile lies between two data points, interpolated value is
returned based on selected interpolation method.
Nils and NaNs are ignored.
Nil is returned if there are no valid data point.

Parameters:

  • prob (Float) (defaults to: 0.5)

    probability.

  • interpolation (Symbol) (defaults to: :linear)

    specifies interpolation method to use, when the quantile lies between the data i and j.

    • Default value is :linear, which returns i + (j - i) * fraction.

    • :lower returns i.

    • :higher returns j.

    • :nearest returns i or j, whichever is closer.

    • :midpoint returns (i + j) / 2.

  • skip_nils (Boolean) (defaults to: true)

    wheather to ignore nil.

  • min_count (Integer) (defaults to: 0)

    min count.

Returns:

  • (Float)

    quantile.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/red_amber/vector_functions.rb', line 57

def quantile(prob = 0.5, interpolation: :linear, skip_nils: true, min_count: 0)
  unless (0..1).cover? prob
    raise VectorArgumentError,
          "Invalid: probability #{prob} must be between 0 and 1"
  end

  datum = find(:quantile).execute([data],
                                  q: prob,
                                  interpolation: interpolation,
                                  skip_nulls: skip_nils,
                                  min_count: min_count)
  datum.value.to_a.first
end

#quantiles(probs = [1.0, 0.75, 0.5, 0.25, 0.0], interpolation: :linear, skip_nils: true, min_count: 0) ⇒ Object

Return quantiles in a DataFrame



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/red_amber/vector_functions.rb', line 73

def quantiles(probs = [1.0, 0.75, 0.5, 0.25, 0.0],
              interpolation: :linear, skip_nils: true, min_count: 0)
  if probs.empty? || !probs.all? { |q| (0..1).cover?(q) }
    raise VectorArgumentError, "Invarid probavilities #{probs}"
  end

  DataFrame.new(
    probs: probs,
    quantiles: probs.map do |q|
      quantile(q,
               interpolation: interpolation, skip_nils: skip_nils,
               min_count: min_count)
    end
  )
end

#sdObject Also known as: std



33
34
35
# File 'lib/red_amber/vector_functions.rb', line 33

def sd
  stddev(ddof: 1)
end

#unbiased_varianceObject Also known as: var



28
29
30
# File 'lib/red_amber/vector_functions.rb', line 28

def unbiased_variance
  variance(ddof: 1)
end