kder 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/kder.rb +5 -0
- data/lib/kder/bandwidth.rb +13 -0
- data/lib/kder/kde.rb +35 -0
- data/lib/util/enum_extensions.rb +19 -0
- data/lib/util/statistics.rb +10 -0
- metadata +52 -0
data/lib/kder.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative '../util/enum_extensions'
|
2
|
+
module Bandwidth
|
3
|
+
class << self
|
4
|
+
def silverman(input_arr, std_deviation = input_arr.standard_deviation)
|
5
|
+
bandwidth = ((4 * std_deviation ** 5)/(3 * input_arr.size))**(1.0/5)
|
6
|
+
end
|
7
|
+
alias_method :estimate, :silverman
|
8
|
+
alias_method :nrd0, :silverman
|
9
|
+
|
10
|
+
#def mean_integrated_squared_error(a,b,c,d)
|
11
|
+
#end
|
12
|
+
end
|
13
|
+
end
|
data/lib/kder/kde.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
class Kder
|
2
|
+
require 'distribution'
|
3
|
+
require_relative 'bandwidth'
|
4
|
+
require_relative '../util/statistics'
|
5
|
+
|
6
|
+
Sigmas = 2.5
|
7
|
+
MeshCount = 2e3
|
8
|
+
MinimumThresholdValue = 1e-2
|
9
|
+
class << self
|
10
|
+
##
|
11
|
+
# :singleton-method: kde
|
12
|
+
# Accepts a single member array plus optional additional information
|
13
|
+
# Returns a two member array, [x_vals,y_vals] representing the kde
|
14
|
+
def kde(arr, bw = nil, opts = {sigmas: Sigmas, sampling_density: MeshCount, threshold: MinimumThresholdValue})
|
15
|
+
unless bw # is nil
|
16
|
+
bw = Bandwidth.silverman(arr)
|
17
|
+
end
|
18
|
+
# Initialization steps
|
19
|
+
min = arr.min - bw*opts[:sigmas]
|
20
|
+
max = arr.max + bw*opts[:sigmas]
|
21
|
+
step_size = (max-min)/opts[:sampling_density].to_f
|
22
|
+
arr.sort!
|
23
|
+
# Step through the range
|
24
|
+
output = (min..max).step(step_size).map do |mid|
|
25
|
+
high_end = mid+ bw*opts[:sigmas]
|
26
|
+
lower_end = mid - bw*opts[:sigmas]
|
27
|
+
included = arr.select {|a| (lower_end..high_end).include?(a)}
|
28
|
+
intensity = included.map {|a| Kder::Statistics.custom_pdf(a-mid, bw) }.inject(:+)
|
29
|
+
intensity ||= 0
|
30
|
+
[mid, intensity ] unless intensity < opts[:threshold]
|
31
|
+
end
|
32
|
+
output.compact.transpose
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Enumerable
|
2
|
+
# Thanks StackOverflow users!
|
3
|
+
# http://stackoverflow.com/questions/7749568/how-can-i-do-standard-deviation-in-ruby
|
4
|
+
def sum
|
5
|
+
self.inject(:+)
|
6
|
+
end
|
7
|
+
def mean
|
8
|
+
self.sum/self.length.to_f
|
9
|
+
end
|
10
|
+
alias average mean
|
11
|
+
def sample_variance
|
12
|
+
m = self.mean
|
13
|
+
sum = self.inject(0){|accum, i| accum + (i-m)**2}
|
14
|
+
sum/(self.length - 1).to_f
|
15
|
+
end
|
16
|
+
def standard_deviation
|
17
|
+
Math.sqrt(self.sample_variance)
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryan Taylor
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A KDE implementation in Ruby.
|
15
|
+
email:
|
16
|
+
- ryanmt@byu.net
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/kder.rb
|
22
|
+
- lib/kder/bandwidth.rb
|
23
|
+
- lib/kder/kde.rb
|
24
|
+
- lib/util/enum_extensions.rb
|
25
|
+
- lib/util/statistics.rb
|
26
|
+
homepage: https://github.com/princelab/KDER
|
27
|
+
licenses: []
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 1.8.24
|
47
|
+
signing_key:
|
48
|
+
specification_version: 3
|
49
|
+
summary: Kernel Density Estimation, and an associated bandwidth estimator, rudimentary
|
50
|
+
at best, but a decent first approximation for backend preparing of KDE plots for
|
51
|
+
plotting in JS libraries, or for whatever you would like to use it.
|
52
|
+
test_files: []
|