Class: Kder

Inherits:
Object
  • Object
show all
Defined in:
lib/kder/kde.rb,
lib/kder/version.rb,
lib/util/statistics.rb

Defined Under Namespace

Modules: Statistics

Constant Summary collapse

Sigmas =
2.5
MeshCount =
2e3
MinimumThresholdValue =
1e-2
MinimumStepSize =
1e-3
VERSION =
"0.0.5"

Class Method Summary collapse

Class Method Details

.kde(arr, bw = nil, opts = {sigmas: Sigmas, sampling_density: MeshCount, threshold: MinimumThresholdValue}) ⇒ Object

:singleton-method: kde Accepts a single member array plus optional additional information Returns a two member array, [x_vals,y_vals] representing the kde



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kder/kde.rb', line 15

def kde(arr, bw = nil, opts = {sigmas: Sigmas, sampling_density: MeshCount, threshold: MinimumThresholdValue})
  unless bw # is nil
    bw = Bandwidth.silverman(arr)
  end
  # Initialization steps
  min = arr.min - bw*opts[:sigmas]
  max = arr.max + bw*opts[:sigmas]
  step_size = (max-min)/(opts[:sampling_density].to_f)
  step_size = step_size < MinimumStepSize ? MinimumStepSize : step_size
  arr.sort!
  # Step through the range
  output = (min..max).step(step_size).map do |mid|
    high_end = mid+ bw*opts[:sigmas]
    lower_end = mid - bw*opts[:sigmas]
    included = arr.select {|a| (lower_end..high_end).include?(a)}
    intensity = included.map {|a| Kder::Statistics.custom_pdf(a-mid, bw) }.inject(:+)
    intensity ||= 0
    [mid, intensity ] unless intensity < opts[:threshold]
  end
  output.compact.transpose
end