tiny_outcome 2.1.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tiny_outcome.rb +25 -15
  3. metadata +1 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ccac5bae17f87452576130e604477b25fbaaa70785e0a41894077e8896521a28
4
- data.tar.gz: f108cdf6533898223d088a169645d2b0ddc897c857c725c713086f42519a9d74
3
+ metadata.gz: 86b4b40958991ec5019ebaa4595fd5526cb06547d1e649dadf8dd7098355b45c
4
+ data.tar.gz: 546ace5ff2dde2fa8cd3d4e6e982101d90c57293157a66458ba870cb6860e305
5
5
  SHA512:
6
- metadata.gz: 12b256504992f76ca4e6a22af2b5a9e24699062765f3bf1acb6f9d1e24f9fceefcbf865e856371ab4bf85c9a20fef6923e8582bb9c87b37571bbdba4d1271752
7
- data.tar.gz: fedc09804f2e2c2007cf05e83f0971a2f54a5e9dcb18a2cf245fd5d907e60813f83de23e55024b323bd065cda7acc08d6504928ea1706871b465831326ac33fe
6
+ metadata.gz: 442b6a72cbf0f0457c10de8ce1a8f78b8b7c5187a634c3816e8b3dea5d5eb0c33bed7a1d01f5bc86c1373c6a323df46df6dd6dc78da0b94ba0856e714cba221c
7
+ data.tar.gz: e92cbdef295da8f77501608c9d8a4511b2b1f2c184b7eba8e8896ac901c125b5ad2c330c7fc07f346b81b31fd0cd2650b175f301cb6f6248814014216b147aaf
data/lib/tiny_outcome.rb CHANGED
@@ -30,6 +30,8 @@ class TinyOutcome
30
30
  :samples,
31
31
  :warmth,
32
32
  :warmup,
33
+ :probability,
34
+ :one_count,
33
35
  :value
34
36
 
35
37
  WARM_FULL = :full
@@ -44,9 +46,12 @@ class TinyOutcome
44
46
  # samples that we can trust the probability output
45
47
  def initialize(precision, warmup=WARM_FULL)
46
48
  @precision = precision
49
+ @probability = 0.0
50
+ @one_count = 0
47
51
  @samples = 0
48
52
  @warmth = 0
49
- @value = 0
53
+ @value = [0] * @precision
54
+ @value_index = 0
50
55
  @warmup = case warmup
51
56
  when WARM_FULL then precision
52
57
  when WARM_TWO_THIRDS then (precision / 3) * 2
@@ -59,6 +64,10 @@ class TinyOutcome
59
64
  end
60
65
  end
61
66
 
67
+ def numeric_value
68
+ (full? ? @value.rotate(@value_index) : @value)[..(samples-1)].join.to_i(2)
69
+ end
70
+
62
71
  # add a sample to the historic outcomes. the new sample is added to the
63
72
  # low-order bits. the new sample is literally left-shifted into the value. the
64
73
  # only reason this is a custom method is because some metadata needs to be
@@ -66,9 +75,21 @@ class TinyOutcome
66
75
  def <<(sample)
67
76
  raise "Invalid sample: #{sample}" unless sample == 0 || sample == 1
68
77
 
69
- @value = ((value << 1) | sample) & (2**precision - 1)
78
+ removing_one = full? && @value[(@value_index + 1) % @precision] == 1
79
+
80
+ @value[@value_index] = sample
81
+ @value_index = (@value_index + 1) % @precision
70
82
  @warmth += 1 unless warmth == warmup
71
- @samples += 1 unless samples == precision
83
+ @samples += 1 unless full?
84
+
85
+ # percentage of 1s out of the existing samples
86
+ #
87
+ # number of 1s
88
+ # probabilty = ---------------
89
+ # total samples
90
+ @one_count -= 1 if removing_one
91
+ @one_count += 1 if sample == 1
92
+ @probability = @one_count / samples.to_f
72
93
 
73
94
  value
74
95
  end
@@ -76,18 +97,7 @@ class TinyOutcome
76
97
  # true if #probability is >= percentage
77
98
  # false otherwise
78
99
  def winner_at?(percentage)
79
- probability >= percentage
80
- end
81
-
82
- # float: 0.0-1.0
83
- # percentage of 1s out of the existing samples
84
- #
85
- # number of 1s
86
- # probabilty = ---------------
87
- # total samples
88
- def probability
89
- return 0.0 if samples == 0
90
- value.to_s(2).count('1') / samples.to_f
100
+ @probability >= percentage
91
101
  end
92
102
 
93
103
  # true if we've received at least warmup number of samples
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_outcome
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
@@ -10,20 +10,6 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2023-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: m
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: minitest
29
15
  requirement: !ruby/object:Gem::Requirement