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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e80d9d9fb7fb96e66d48abcaf77e568241d69c8
4
- data.tar.gz: cbd4f91ba21c8f581856921be847e6726b4c89c7
3
+ metadata.gz: a5a9b73405c74abb6de423ca788b1d0ef5279842
4
+ data.tar.gz: 1f63a237bfaa760a325f06176047b3800cd14a28
5
5
  SHA512:
6
- metadata.gz: 45e404d9f67d57ed47749cd90f7adbb92a511267290858090faa7cead4377b7ae1a70e4c31512e3234a9ead42e9bdad6a221fb42335cd2fd7eb6e84b767f1433
7
- data.tar.gz: acfeedd52b40352aaad3aede2b8161a4499943eeb098a1bd581222fcd01b086e0ab866e3a5d2a2b95415e15d2a0113d0ce4f352c08a3d406a922c1166a4f154e
6
+ metadata.gz: 73bb5c9186d5bd0d307b6953a0b5149dd5a2fb22ee5cc778441876c34a526e9e105acbc2cd1cf5437fbe5cf142c76f39763487d7de33e7dc9cffe9a072f5725e
7
+ data.tar.gz: bdfb147517579c653bed584d6437766b9d8fb26361f06d1688fa6d6d70c2f47709ed935d723c6cf9ccb3ada9cdf5709efde5fcc44109cde9eade56594404d2bc
@@ -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
- 1
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
 
@@ -1,3 +1,3 @@
1
1
  module Chao92
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/chao92.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'support/core_ext'
2
+
1
3
  require "chao92/version"
2
4
  require "chao92/estimator"
3
5
 
@@ -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
@@ -1,13 +1,18 @@
1
1
  require 'test_helper'
2
2
 
3
- describe Chao92::Estimator do
3
+ class TestEstimator < Minitest::Test
4
4
 
5
- before do
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
- it "test_estimator" do
10
- assert_equal Chao92::Estimator.run(@samples), 1
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.2
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: ''