tiny_outcome 2.0.2 → 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 +27 -16
  3. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f59aa2f60029e67a708ba51b4606394a230ced1f2dbea6c001f01a9012499eb
4
- data.tar.gz: 3e9158d65ccbc0a0d47646684fbe5379fbfd4b3c55b5b17725dde691ac8e9852
3
+ metadata.gz: 86b4b40958991ec5019ebaa4595fd5526cb06547d1e649dadf8dd7098355b45c
4
+ data.tar.gz: 546ace5ff2dde2fa8cd3d4e6e982101d90c57293157a66458ba870cb6860e305
5
5
  SHA512:
6
- metadata.gz: 898c64f22f9c0b4b193f06097ddd6a23e135088b33bb594123a20b2acc4ea8bada2fdfdda0998d25e5818a6cf4a1051f1eeb46a628b1bdafb84e4800d207ff60
7
- data.tar.gz: 63a96dea1d7cbf4d768a4701048307e6feb61a4cced5fb56a80cba84986be0790931c3d20a3c7cd0f1a4a3be7683240f3fae876d6e91da04ad371dbf74fb41b6
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
@@ -112,7 +122,8 @@ class TinyOutcome
112
122
  [:value,
113
123
  :samples,
114
124
  :warmth,
115
- :warmup,:warm?,
125
+ :warmup,
126
+ :warm?,
116
127
  :probability,
117
128
  ].each_with_object({}) do |attr, memo|
118
129
  memo[attr] = send(attr)
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_outcome
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-04 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2023-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
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'
13
27
  description: a tiny outcome tracker with almost no features
14
28
  email: jefflunt@gmail.com
15
29
  executables: []