chao92 0.0.2 → 1.0.0
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.
- checksums.yaml +4 -4
- data/lib/chao92/estimator.rb +40 -2
- data/lib/chao92/version.rb +1 -1
- data/lib/chao92.rb +2 -0
- data/lib/support/core_ext.rb +21 -0
- data/test/estimator_test.rb +9 -4
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a5a9b73405c74abb6de423ca788b1d0ef5279842
|
|
4
|
+
data.tar.gz: 1f63a237bfaa760a325f06176047b3800cd14a28
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 73bb5c9186d5bd0d307b6953a0b5149dd5a2fb22ee5cc778441876c34a526e9e105acbc2cd1cf5437fbe5cf142c76f39763487d7de33e7dc9cffe9a072f5725e
|
|
7
|
+
data.tar.gz: bdfb147517579c653bed584d6437766b9d8fb26361f06d1688fa6d6d70c2f47709ed935d723c6cf9ccb3ada9cdf5709efde5fcc44109cde9eade56594404d2bc
|
data/lib/chao92/estimator.rb
CHANGED
|
@@ -2,9 +2,47 @@ module Chao92
|
|
|
2
2
|
|
|
3
3
|
class Estimator
|
|
4
4
|
|
|
5
|
+
def initialize
|
|
6
|
+
@samples = []
|
|
7
|
+
@observed = {}
|
|
8
|
+
@ff = {}
|
|
9
|
+
end
|
|
10
|
+
|
|
5
11
|
def self.run(samples)
|
|
6
|
-
|
|
7
|
-
end
|
|
12
|
+
Estimator.new.sampling(samples)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def sampling(samples)
|
|
16
|
+
@samples += samples
|
|
17
|
+
update()
|
|
18
|
+
estimate()
|
|
19
|
+
@N
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
def update
|
|
24
|
+
@observed = @samples.to_hash
|
|
25
|
+
@ff = @observed.to_inverse
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def estimate
|
|
29
|
+
@turing_estimator = if @ff[1]
|
|
30
|
+
1 - @ff[1].to_f / @samples.size
|
|
31
|
+
else
|
|
32
|
+
1
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
@N1 = @observed.size / @turing_estimator
|
|
36
|
+
|
|
37
|
+
tmp = 0.0
|
|
38
|
+
tmp = @samples.size.times do |x|
|
|
39
|
+
tmp += x * (x - 1) * @ff[x] if @ff[x]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
@cv_estimator = [@N1 * tmp / (@samples.size.to_f * (@samples.size - 1).to_f - 1), 0.0].max
|
|
43
|
+
|
|
44
|
+
@N = @N1 + @samples.size * (1 - @turing_estimator) / @turing_estimator * @cv_estimator
|
|
45
|
+
end
|
|
8
46
|
|
|
9
47
|
end
|
|
10
48
|
|
data/lib/chao92/version.rb
CHANGED
data/lib/chao92.rb
CHANGED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
class Array
|
|
2
|
+
def to_hash
|
|
3
|
+
hash = {}
|
|
4
|
+
self.each {|e| hash[e] = (hash[e]? hash[e] + 1 : 1) }
|
|
5
|
+
hash
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
class Hash
|
|
10
|
+
def to_inverse
|
|
11
|
+
ff = {}
|
|
12
|
+
self.each do |k, v|
|
|
13
|
+
if ff[v]
|
|
14
|
+
ff[v] += 1
|
|
15
|
+
else
|
|
16
|
+
ff[v] = 1
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
ff
|
|
20
|
+
end
|
|
21
|
+
end
|
data/test/estimator_test.rb
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
require 'test_helper'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
class TestEstimator < Minitest::Test
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
def setup
|
|
6
|
+
@estimator = Chao92::Estimator.new
|
|
6
7
|
@samples = [1, 1, 3, 5, 3, 3, 2, 1, 2, 3]
|
|
7
8
|
end
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
assert_equal Chao92::Estimator.run(@samples),
|
|
10
|
+
def test_estimator_run
|
|
11
|
+
assert_equal Chao92::Estimator.run(@samples).class, Float
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_estimator_sampling
|
|
15
|
+
assert_equal @estimator.sampling(@samples), Chao92::Estimator.run(@samples)
|
|
11
16
|
end
|
|
12
17
|
|
|
13
18
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: chao92
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Eval Air
|
|
@@ -68,6 +68,7 @@ files:
|
|
|
68
68
|
- lib/chao92.rb
|
|
69
69
|
- lib/chao92/estimator.rb
|
|
70
70
|
- lib/chao92/version.rb
|
|
71
|
+
- lib/support/core_ext.rb
|
|
71
72
|
- test/estimator_test.rb
|
|
72
73
|
- test/test_helper.rb
|
|
73
74
|
homepage: ''
|