enumerable-stats 1.0.0 → 1.1.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 +4 -4
- data/lib/enumerable_stats/enumerable_ext.rb +47 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c5a52c483b38592d8be9155651e63a4d238850927bc3282f27f22a4859e1db1a
|
4
|
+
data.tar.gz: 0e807826f7103e049effcd1bd925279e5b1ea7d34932fddebf7167e184a6b887
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df23176506fd05b2769fc6ee49ed62bc7954ec9f810d6153a69754df90a3cbed7b4744e269c7f26b08b1e2bb56b0384d3c1a46bdacc7a57ecba7f9029c4bcb19
|
7
|
+
data.tar.gz: 8e80e9d596018a704773ca2836f951d1565876b35aedf7428117a61d031f685ea788ac115d02f04fc7b27ab9b06ebae5a617fe891a97130b8da628cdee807c20
|
@@ -108,6 +108,53 @@ module EnumerableStats
|
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
|
+
# Calculates the specified percentile of the collection
|
112
|
+
# Uses linear interpolation between data points when the exact percentile falls between values
|
113
|
+
# This is equivalent to the "linear" method used by many statistical software packages
|
114
|
+
#
|
115
|
+
# @param percentile [Numeric] The percentile to calculate (0-100)
|
116
|
+
# @return [Numeric, nil] The value at the specified percentile, or nil if the collection is empty
|
117
|
+
# @raise [ArgumentError] If percentile is not between 0 and 100
|
118
|
+
# @example
|
119
|
+
# [1, 2, 3, 4, 5].percentile(50) # => 3 (same as median)
|
120
|
+
# [1, 2, 3, 4, 5].percentile(25) # => 2.0 (25th percentile)
|
121
|
+
# [1, 2, 3, 4, 5].percentile(75) # => 4.0 (75th percentile)
|
122
|
+
# [1, 2, 3, 4, 5].percentile(0) # => 1 (minimum value)
|
123
|
+
# [1, 2, 3, 4, 5].percentile(100) # => 5 (maximum value)
|
124
|
+
# [].percentile(50) # => nil (empty collection)
|
125
|
+
def percentile(percentile)
|
126
|
+
return nil if size == 0
|
127
|
+
|
128
|
+
unless percentile.is_a?(Numeric) && percentile >= 0 && percentile <= 100
|
129
|
+
raise ArgumentError, "Percentile must be a number between 0 and 100, got #{percentile}"
|
130
|
+
end
|
131
|
+
|
132
|
+
sorted = sort
|
133
|
+
|
134
|
+
# Handle edge cases
|
135
|
+
return sorted.first if percentile == 0
|
136
|
+
return sorted.last if percentile == 100
|
137
|
+
|
138
|
+
# Calculate the position using the "linear" method (R-7/Excel method)
|
139
|
+
# This is the most commonly used method in statistical software
|
140
|
+
position = (size - 1) * (percentile / 100.0)
|
141
|
+
|
142
|
+
# If position is an integer, return that exact element
|
143
|
+
if position == position.floor
|
144
|
+
sorted[position.to_i]
|
145
|
+
else
|
146
|
+
# Linear interpolation between the two surrounding values
|
147
|
+
lower_index = position.floor
|
148
|
+
upper_index = position.ceil
|
149
|
+
weight = position - position.floor
|
150
|
+
|
151
|
+
lower_value = sorted[lower_index]
|
152
|
+
upper_value = sorted[upper_index]
|
153
|
+
|
154
|
+
lower_value + weight * (upper_value - lower_value)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
111
158
|
# Calculates the sample variance of the collection
|
112
159
|
# Uses the unbiased formula with n-1 degrees of freedom (Bessel's correction)
|
113
160
|
#
|
metadata
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerable-stats
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Daniel
|
8
|
+
autorequire:
|
8
9
|
bindir: bin
|
9
10
|
cert_chain: []
|
10
|
-
date: 2025-
|
11
|
+
date: 2025-08-01 00:00:00.000000000 Z
|
11
12
|
dependencies: []
|
12
13
|
description: |
|
13
14
|
A Ruby gem that extends all Enumerable objects (Arrays, Ranges, Sets, etc.) with essential statistical methods.
|
@@ -28,6 +29,7 @@ metadata:
|
|
28
29
|
source_code_uri: https://github.com/binarycleric/enumerable-stats
|
29
30
|
github_repo: ssh://github.com/binarycleric/enumerable-stats
|
30
31
|
rubygems_mfa_required: 'true'
|
32
|
+
post_install_message:
|
31
33
|
rdoc_options: []
|
32
34
|
require_paths:
|
33
35
|
- lib
|
@@ -42,7 +44,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
44
|
- !ruby/object:Gem::Version
|
43
45
|
version: '0'
|
44
46
|
requirements: []
|
45
|
-
rubygems_version: 3.
|
47
|
+
rubygems_version: 3.5.22
|
48
|
+
signing_key:
|
46
49
|
specification_version: 4
|
47
50
|
summary: Statistical Methods for Enumerable Collections
|
48
51
|
test_files: []
|