statistics.rb 0.6.0 → 0.6.1

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
  SHA256:
3
- metadata.gz: ca6c39ebb723322893b28fc4cc9310e4004120ae20faa3be0aa502df6bae0d1b
4
- data.tar.gz: aad65236bd96a97805c7d0cce74e2c922d34e07ee6c95e3ebc3848a6c9eb92b0
3
+ metadata.gz: 9db0156fb926432eb2d48821c410a0badec96b32dacd485893b0bdf8e7b25cb5
4
+ data.tar.gz: 77ac6c90e08b0017d1f66ad427ed0c5f0560ef1732fa79627bb0a2160eea292e
5
5
  SHA512:
6
- metadata.gz: 16f2c79aa61f53f1b7d6ef30d61e54ad2dde96e541056651c165e8955270c8e77290fc9f50e5a7bb752dc97ed3c87da715e0ec55950a352b45d9cb0ed5481780
7
- data.tar.gz: 4f0129b9ed5fcd74ff7a3940f3e4b578b7d586221b2a514354425a0c6fef2fcf8dfd5b5d8c6992deef0c73b4872805c356e46b3c387cea5e0ca995577dfaadb2
6
+ metadata.gz: 43cf596e2166e2cd345528dd6aae8aaadcf78e9bfe62cfe5f28903729bca2ae60a9b142796ab6ab699da57754b27480c4b7c07dae81c37f569f244064daa05f1
7
+ data.tar.gz: e0f26dd100a0741855f46b466b5c9c28a673f386c674a65feafe2cf5793f7353d4791dc54e1e4f1c95f2200724caefcbcc7901b71a830743f7cb97f653ba3692
data/CHANGELOG.md CHANGED
@@ -1,7 +1,18 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## [0.6.1] - 20260513
4
+ ## Statistics::Histogram::Bin --> Statistics::Bin
5
+
6
+ 1. lib/Statistics/Histogram/Bin.rb --> lib/Statistics/Bin.rb
7
+ 2. test/Statistics/Histogram/Bin\_test.rb --> test/Statistics/Bin\_test.rb
8
+ 3. ~ lib/Statistics/Histogram.rb: /require\_relative './Histogram/Bin'/require\_relative './Bin'/
9
+ 4. ~ lib/statistics.rb: + require\_relative './Statistics/Bin'
10
+ 5. ~ .gitignore: Add standard contents.
11
+ 6. ~ README.md: + Contributions section
12
+ 7. ~ lib/Statistics/VERSION.rb: /0.6.0/0.6.1/
13
+
3
14
  ## [0.6.0] - 20260504
4
- ## Percentile, StandardDeviation, IQR
15
+ ## + Percentile, StandardDeviation, IQR
5
16
 
6
17
  1. + lib/Statistics/Percentile.rb
7
18
  2. + lib/Statistics/StandardDeviation.rb
@@ -15,7 +26,7 @@
15
26
 
16
27
 
17
28
  ## [0.5.0] - 20260503
18
- ## Bin.bin\_for\_value, Bin split, gem scaffolding
29
+ ## + Bin.bin\_for\_value, Bin split, gem scaffolding.
19
30
 
20
31
  1. + Bin.bin\_for\_value (wraps index\_for\_value, now private)
21
32
  2. ~ Histogram#allocate\_values: Use Bin.bin\_for\_value
@@ -32,7 +43,7 @@
32
43
 
33
44
 
34
45
  ## [0.4.0] - 20260502
35
- ## Bin instances and consolidated class methods
46
+ ## + Bin instances; consolidated class methods.
36
47
 
37
48
  1. + Bin instances (count-tracking via increment, attr\_reader :interval)
38
49
  2. ~ Bin.width: handles bin\_width, bin\_count, zero-range, and method selection
@@ -44,7 +55,7 @@
44
55
 
45
56
 
46
57
  ## [0.3.0] - 20260417
47
- ## Hash-based bins, Bin as class-methods-only
58
+ ## Hash-based bins; Bin as class-methods-only.
48
59
 
49
60
  1. + Bin.boundaries class method
50
61
  2. ~ initialize: delegates to Bin.width and Bin.boundaries
@@ -55,7 +66,7 @@
55
66
 
56
67
 
57
68
  ## [0.2.0] - 20260417
58
- ## Count-tracking, interval, zero-range guard
69
+ ## Swap storing values per bin for counting per bin; interval; zero-range guard.
59
70
 
60
71
  1. ~ Bin: count-tracking via increment instead of storing values in array
61
72
  2. ~ Bin: attr\_reader :interval instead of :range
@@ -65,42 +76,42 @@
65
76
 
66
77
 
67
78
  ## [0.1.1] - 20260417
68
- ## Extract determine\_bin\_width
79
+ ## + Statistics::Histogram#determine\_bin\_width
69
80
 
70
81
  1. ~ initialize: extracted determine\_bin\_width from one-liner
71
82
  2. /compute\_boundaries/calculate\_boundaries/
72
83
 
73
84
 
74
85
  ## [0.1.0] - 20260417
75
- ## Bin class
86
+ ## + Statistics::Histogram::Bin
76
87
 
77
88
  1. + Statistics::Histogram::Bin
78
89
  2. ~ allocate\_values: creates Bin instances instead of hash entries
79
90
 
80
91
 
81
92
  ## [0.0.0] - 20260417
82
- ## Statistics::Histogram
93
+ ## + Statistics::Histogram
83
94
 
84
95
  1. + lib/Statistics/Histogram.rb
85
96
  2. - lib/BinWidth.rb
86
97
 
87
98
 
88
- ## 20260416
89
- ## BinWidth 0.1.0 to 0.2.0: Reintroduce all named strategies
99
+ ## [pre-0.0.0] - 20260416
100
+ ## ~ BinWidth: Reintroduce binning strategies; extra guard clauses.
90
101
 
91
- 1. /0.1.0/0.2.0/
92
- 2. Reintroduce all named strategies.
102
+ 1. Reintroduce all named strategies.
103
+ 2. /0.1.0/0.2.0/
93
104
 
94
105
 
95
- ## 20260416
96
- ## BinWidth 0.0.0 to 0.1.0: Tuneable root generalisation
106
+ ## [pre-0.0.0] - 20260416
107
+ ## + BinWidth#tuneable_root
97
108
 
98
- 1. /0.0.0/0.1.0/
99
- 2. + tuneable\_root as the general form
100
- 3. ~ square\_root rewritten as range * n^(-1/2) form
109
+ 1. + tuneable\_root as the general form
110
+ 2. ~ square\_root rewritten as range * n^(-1/2) form
111
+ 3. /0.0.0/0.1.0/
101
112
 
102
113
 
103
- ## 20260416
104
- ## BinWidth 0.0.0
114
+ ## [pre-0.0.0] - 20260416
115
+ ## + BinWidth
105
116
 
106
117
  1. + lib/BinWidth.rb
data/README.md CHANGED
@@ -101,6 +101,14 @@ Statistics::IQR.of(values) # => 4.5
101
101
  - Additional bin width methods (Freedman-Diaconis, Scott, Sturges, cube root, tuneable root)
102
102
  - Aligned/neat bin boundaries
103
103
 
104
+ ## Contributing
105
+
106
+ 1. Fork it [https://github.com/thoran/statistics/fork](https://github.com/thoran/statistics/fork)
107
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
108
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
109
+ 4. Push to the branch (`git push origin my-new-feature`)
110
+ 5. Create a new pull request
111
+
104
112
  ## License
105
113
 
106
114
  MIT
@@ -0,0 +1,74 @@
1
+ # Statistics/Bin.rb
2
+ # Statistics::Bin
3
+
4
+ module Statistics
5
+ class Bin
6
+ class << self
7
+ def width(values, bin_width: nil, bin_count: nil, method: :square_root)
8
+ if bin_width
9
+ bin_width
10
+ elsif bin_count
11
+ data_range(values) / bin_count.to_f
12
+ elsif data_range(values) == 0
13
+ 1.0
14
+ else
15
+ send("#{method}_width", values)
16
+ end
17
+ end
18
+
19
+ def count(values, method: :square_root)
20
+ send("#{method}_count", values)
21
+ end
22
+
23
+ def data_range(values)
24
+ values.last - values.first
25
+ end
26
+
27
+ def boundaries(values, bin_width: nil, bin_count: nil, method: :square_root)
28
+ w = bin_width || width(values, bin_count: bin_count, method: method)
29
+ values.first.step(to: values.last + w, by: w).to_a
30
+ end
31
+
32
+ def bin_for_value(value, bins, bottom_boundary, bin_width)
33
+ index = index_for_value(value, bins.count, bottom_boundary, bin_width)
34
+ bins[index]
35
+ end
36
+
37
+ private
38
+
39
+ def index_for_value(value, bin_count, bottom_boundary, bin_width)
40
+ i = ((value - bottom_boundary) / bin_width).floor
41
+ i >= bin_count ? bin_count - 1 : i
42
+ end
43
+
44
+ def square_root_width(values)
45
+ data_range(values) * values.size ** (-1.0 / 2)
46
+ end
47
+
48
+ def square_root_count(values)
49
+ Math.sqrt(values.size).ceil
50
+ end
51
+ end # class << self
52
+
53
+ attr_reader :count, :interval
54
+
55
+ def increment
56
+ @count += 1
57
+ end
58
+
59
+ def width
60
+ @interval.end - @interval.begin
61
+ end
62
+
63
+ def empty?
64
+ @count == 0
65
+ end
66
+
67
+ private
68
+
69
+ def initialize(interval)
70
+ @interval = interval
71
+ @count = 0
72
+ end
73
+ end
74
+ end
@@ -1,7 +1,7 @@
1
1
  # Statistics/Histogram.rb
2
2
  # Statistics::Histogram
3
3
 
4
- require_relative './Histogram/Bin'
4
+ require_relative './Bin'
5
5
 
6
6
  module Statistics
7
7
  class Histogram
@@ -2,5 +2,5 @@
2
2
  # Statistics::VERSION
3
3
 
4
4
  module Statistics
5
- VERSION = '0.6.0'
5
+ VERSION = '0.6.1'
6
6
  end
data/lib/statistics.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # statistics.rb
2
2
 
3
+ require_relative './Statistics/Bin'
3
4
  require_relative './Statistics/Histogram'
4
5
  require_relative './Statistics/IQR'
5
6
  require_relative './Statistics/Percentile'
@@ -1,33 +1,33 @@
1
- # test/Statistics/Histogram/Bin_test.rb
1
+ # test/Statistics/Bin_test.rb
2
2
 
3
3
  require 'minitest/autorun'
4
4
 
5
- require_relative '../../../lib/Statistics/Histogram/Bin'
5
+ require_relative '../../lib/Statistics/Bin'
6
6
 
7
- describe Statistics::Histogram::Bin do
7
+ describe Statistics::Bin do
8
8
  describe '.width' do
9
9
  it 'calculates square root width by default' do
10
10
  values = (1..100).to_a.map(&:to_f)
11
- width = Statistics::Histogram::Bin.width(values)
11
+ width = Statistics::Bin.width(values)
12
12
  expected = (values.last - values.first) * values.size ** (-1.0 / 2)
13
13
  _(width).must_be_close_to expected
14
14
  end
15
15
 
16
16
  it 'returns explicit bin_width when provided' do
17
17
  values = (1..100).to_a.map(&:to_f)
18
- width = Statistics::Histogram::Bin.width(values, bin_width: 7.5)
18
+ width = Statistics::Bin.width(values, bin_width: 7.5)
19
19
  _(width).must_equal 7.5
20
20
  end
21
21
 
22
22
  it 'calculates width from bin_count when provided' do
23
23
  values = (1..100).to_a.map(&:to_f)
24
- width = Statistics::Histogram::Bin.width(values, bin_count: 10)
24
+ width = Statistics::Bin.width(values, bin_count: 10)
25
25
  _(width).must_be_close_to 9.9
26
26
  end
27
27
 
28
28
  it 'returns 1.0 for zero-range data' do
29
29
  values = [5.0, 5.0, 5.0]
30
- width = Statistics::Histogram::Bin.width(values)
30
+ width = Statistics::Bin.width(values)
31
31
  _(width).must_equal 1.0
32
32
  end
33
33
  end
@@ -35,13 +35,13 @@ describe Statistics::Histogram::Bin do
35
35
  describe '.boundaries' do
36
36
  it 'returns an array starting at the minimum value' do
37
37
  values = (1..10).to_a.map(&:to_f)
38
- boundaries = Statistics::Histogram::Bin.boundaries(values, bin_width: 3.0)
38
+ boundaries = Statistics::Bin.boundaries(values, bin_width: 3.0)
39
39
  _(boundaries.first).must_equal 1.0
40
40
  end
41
41
 
42
42
  it 'extends past the maximum value' do
43
43
  values = (1..10).to_a.map(&:to_f)
44
- boundaries = Statistics::Histogram::Bin.boundaries(values, bin_width: 3.0)
44
+ boundaries = Statistics::Bin.boundaries(values, bin_width: 3.0)
45
45
  _(boundaries.last).must_be :>=, 10.0
46
46
  end
47
47
  end
@@ -49,24 +49,24 @@ describe Statistics::Histogram::Bin do
49
49
  describe '.bin_for_value' do
50
50
  it 'returns the correct bin' do
51
51
  bins = [5.0, 10.0, 15.0, 20.0].each_cons(2).map do |lower, upper|
52
- Statistics::Histogram::Bin.new(lower...upper)
52
+ Statistics::Bin.new(lower...upper)
53
53
  end
54
- bin = Statistics::Histogram::Bin.bin_for_value(7.0, bins, 5.0, 5.0)
54
+ bin = Statistics::Bin.bin_for_value(7.0, bins, 5.0, 5.0)
55
55
  _(bin.interval).must_equal(5.0...10.0)
56
56
  end
57
57
 
58
58
  it 'clamps to the last bin for values at the upper boundary' do
59
59
  bins = [5.0, 10.0, 15.0].each_cons(2).map do |lower, upper|
60
- Statistics::Histogram::Bin.new(lower...upper)
60
+ Statistics::Bin.new(lower...upper)
61
61
  end
62
- bin = Statistics::Histogram::Bin.bin_for_value(15.0, bins, 5.0, 5.0)
62
+ bin = Statistics::Bin.bin_for_value(15.0, bins, 5.0, 5.0)
63
63
  _(bin.interval).must_equal(10.0...15.0)
64
64
  end
65
65
  end
66
66
 
67
67
  describe 'instance methods' do
68
68
  before do
69
- @bin = Statistics::Histogram::Bin.new(1.0...5.0)
69
+ @bin = Statistics::Bin.new(1.0...5.0)
70
70
  end
71
71
 
72
72
  it 'starts with count of zero' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statistics.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -48,15 +48,15 @@ files:
48
48
  - LICENSE
49
49
  - README.md
50
50
  - Rakefile
51
+ - lib/Statistics/Bin.rb
51
52
  - lib/Statistics/Histogram.rb
52
- - lib/Statistics/Histogram/Bin.rb
53
53
  - lib/Statistics/IQR.rb
54
54
  - lib/Statistics/Percentile.rb
55
55
  - lib/Statistics/StandardDeviation.rb
56
56
  - lib/Statistics/VERSION.rb
57
57
  - lib/statistics.rb
58
58
  - statistics.rb.gemspec
59
- - test/Statistics/Histogram/Bin_test.rb
59
+ - test/Statistics/Bin_test.rb
60
60
  - test/Statistics/Histogram_test.rb
61
61
  - test/Statistics/IQR_test.rb
62
62
  - test/Statistics/Percentile_test.rb
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  - !ruby/object:Gem::Version
80
80
  version: '0'
81
81
  requirements: []
82
- rubygems_version: 4.0.10
82
+ rubygems_version: 4.0.11
83
83
  specification_version: 4
84
84
  summary: A statistics library for Ruby.
85
85
  test_files: []
@@ -1,76 +0,0 @@
1
- # Statistics/Histogram/Bin.rb
2
- # Statistics::Histogram::Bin
3
-
4
- module Statistics
5
- class Histogram
6
- class Bin
7
- class << self
8
- def width(values, bin_width: nil, bin_count: nil, method: :square_root)
9
- if bin_width
10
- bin_width
11
- elsif bin_count
12
- data_range(values) / bin_count.to_f
13
- elsif data_range(values) == 0
14
- 1.0
15
- else
16
- send("#{method}_width", values)
17
- end
18
- end
19
-
20
- def count(values, method: :square_root)
21
- send("#{method}_count", values)
22
- end
23
-
24
- def data_range(values)
25
- values.last - values.first
26
- end
27
-
28
- def boundaries(values, bin_width: nil, bin_count: nil, method: :square_root)
29
- w = bin_width || width(values, bin_count: bin_count, method: method)
30
- values.first.step(to: values.last + w, by: w).to_a
31
- end
32
-
33
- def bin_for_value(value, bins, bottom_boundary, bin_width)
34
- index = index_for_value(value, bins.count, bottom_boundary, bin_width)
35
- bins[index]
36
- end
37
-
38
- private
39
-
40
- def index_for_value(value, bin_count, bottom_boundary, bin_width)
41
- i = ((value - bottom_boundary) / bin_width).floor
42
- i >= bin_count ? bin_count - 1 : i
43
- end
44
-
45
- def square_root_width(values)
46
- data_range(values) * values.size ** (-1.0 / 2)
47
- end
48
-
49
- def square_root_count(values)
50
- Math.sqrt(values.size).ceil
51
- end
52
- end # class << self
53
-
54
- attr_reader :count, :interval
55
-
56
- def increment
57
- @count += 1
58
- end
59
-
60
- def width
61
- @interval.end - @interval.begin
62
- end
63
-
64
- def empty?
65
- @count == 0
66
- end
67
-
68
- private
69
-
70
- def initialize(interval)
71
- @interval = interval
72
- @count = 0
73
- end
74
- end
75
- end
76
- end