tiny_outcome 3.0.0 → 3.1.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 +33 -6
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 86b4b40958991ec5019ebaa4595fd5526cb06547d1e649dadf8dd7098355b45c
4
- data.tar.gz: 546ace5ff2dde2fa8cd3d4e6e982101d90c57293157a66458ba870cb6860e305
3
+ metadata.gz: 7e887122fe969136656c57bf1e8a240df3500f0120694891be4f9ba26cf9c694
4
+ data.tar.gz: 92e30c4c3c72afc4942181ede0badc1865b297c0444a1c7ec7cce272e4a7dac8
5
5
  SHA512:
6
- metadata.gz: 442b6a72cbf0f0457c10de8ce1a8f78b8b7c5187a634c3816e8b3dea5d5eb0c33bed7a1d01f5bc86c1373c6a323df46df6dd6dc78da0b94ba0856e714cba221c
7
- data.tar.gz: e92cbdef295da8f77501608c9d8a4511b2b1f2c184b7eba8e8896ac901c125b5ad2c330c7fc07f346b81b31fd0cd2650b175f301cb6f6248814014216b147aaf
6
+ metadata.gz: 31bf45e93abd24309b440599f7006573dd6b2beb19a588234b6f15aaed4334bde062b252f8bdecf2a7f8b72fe3a42def7a55d38a225b8e37cb19af2dfc375977
7
+ data.tar.gz: b828c61ee05e08de9041bdb5b17a95005ef2cf315419bd3806dc9feec2efb5ba2138231bd8ff9d4bc74e5d02079848c4356d0dc779d02709a12623cccc6ce66d
data/lib/tiny_outcome.rb CHANGED
@@ -32,7 +32,10 @@ class TinyOutcome
32
32
  :warmup,
33
33
  :probability,
34
34
  :one_count,
35
- :value
35
+ :value,
36
+ :min,
37
+ :max,
38
+ :avg
36
39
 
37
40
  WARM_FULL = :full
38
41
  WARM_TWO_THIRDS = :two_thirds
@@ -49,6 +52,9 @@ class TinyOutcome
49
52
  @probability = 0.0
50
53
  @one_count = 0
51
54
  @samples = 0
55
+ @min = -1.0
56
+ @max = -1.0
57
+ @avg = -1.0
52
58
  @warmth = 0
53
59
  @value = [0] * @precision
54
60
  @value_index = 0
@@ -68,10 +74,7 @@ class TinyOutcome
68
74
  (full? ? @value.rotate(@value_index) : @value)[..(samples-1)].join.to_i(2)
69
75
  end
70
76
 
71
- # add a sample to the historic outcomes. the new sample is added to the
72
- # low-order bits. the new sample is literally left-shifted into the value. the
73
- # only reason this is a custom method is because some metadata needs to be
74
- # updated when a new sample is added
77
+ # add a sample to the historic outcomes
75
78
  def <<(sample)
76
79
  raise "Invalid sample: #{sample}" unless sample == 0 || sample == 1
77
80
 
@@ -91,7 +94,7 @@ class TinyOutcome
91
94
  @one_count += 1 if sample == 1
92
95
  @probability = @one_count / samples.to_f
93
96
 
94
- value
97
+ @value
95
98
  end
96
99
 
97
100
  # true if #probability is >= percentage
@@ -117,6 +120,30 @@ class TinyOutcome
117
120
  samples == precision
118
121
  end
119
122
 
123
+ # updates, and memoizes, the min/max/avg numbers. if you read the min/max/avg
124
+ # attributes you are getting the MEMOIZED values.
125
+ def update_stats!
126
+ return if @samples == 0
127
+
128
+ @min = 1.0
129
+ @max = 0.0
130
+ @avg = 0.0
131
+
132
+ sum = 0.0
133
+ raw = (full? ? @value.rotate(@value_index) : @value)[..(samples-1)]
134
+ group_size = [@samples, 100].min
135
+ num_groups = @samples - group_size
136
+ raw.each_cons(group_size) do |samples_group|
137
+ curr = samples_group.count(1) / samples.to_f
138
+ sum += curr
139
+
140
+ @min = curr if curr < @min
141
+ @max = curr if curr > @max
142
+ end
143
+
144
+ @avg = sum / (@num_groups.to_f + 1.0)
145
+ end
146
+
120
147
  # convenient way to see what's up
121
148
  def to_hash
122
149
  [:value,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_outcome
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.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-09-03 00:00:00.000000000 Z
11
+ date: 2023-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest