Skip to content

Commit 65e33c6

Browse files
committed
Updating documentation for comparison operators
1 parent 92ce20f commit 65e33c6

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,31 @@ puts optimized_times.less_than?(baseline_times, alpha: 0.01) # More stringent t
225225
puts optimized_times.less_than?(baseline_times, alpha: 0.10) # More lenient test
226226
```
227227

228+
#### Comparison Operators
229+
230+
The gem provides convenient operator shortcuts for statistical comparisons:
231+
232+
##### `#>(other, alpha: 0.05)` and `#<(other, alpha: 0.05)`
233+
234+
Shorthand operators for `greater_than?` and `less_than?` respectively.
235+
236+
```ruby
237+
# Performance comparison using operators
238+
baseline_times = [150, 165, 155, 170, 160, 145, 175]
239+
optimized_times = [120, 125, 115, 130, 118, 122, 128]
240+
241+
# These are equivalent:
242+
puts baseline_times.greater_than?(optimized_times) # => true
243+
puts baseline_times > optimized_times # => true
244+
245+
puts optimized_times.less_than?(baseline_times) # => true
246+
puts optimized_times < baseline_times # => true
247+
248+
# With custom alpha levels (use explicit method syntax for parameters)
249+
puts baseline_times.>(optimized_times, alpha: 0.01) # More stringent
250+
puts optimized_times.<(baseline_times, alpha: 0.10) # More lenient
251+
```
252+
228253
### Comparison Methods
229254

230255
#### `#percentage_difference(other)`
@@ -589,6 +614,8 @@ end
589614
| `degrees_of_freedom(other)` | Degrees of freedom for t-test | Float | Uses Welch's formula, accounts for unequal variances |
590615
| `greater_than?(other, alpha: 0.05)` | Test if mean is significantly greater | Boolean | One-tailed t-test, customizable alpha level |
591616
| `less_than?(other, alpha: 0.05)` | Test if mean is significantly less | Boolean | One-tailed t-test, customizable alpha level |
617+
| `>(other, alpha: 0.05)` | Alias for `greater_than?` | Boolean | Shorthand operator for statistical comparison |
618+
| `<(other, alpha: 0.05)` | Alias for `less_than?` | Boolean | Shorthand operator for statistical comparison |
592619
| `percentage_difference(other)` | Absolute percentage difference | Float | Always positive, symmetric comparison |
593620
| `signed_percentage_difference(other)` | Signed percentage difference | Float | Preserves direction, useful for A/B tests |
594621
| `remove_outliers(multiplier: 1.5)` | Remove outliers using IQR method | Array | Returns new array, original unchanged |

lib/enumerable_stats/enumerable_ext.rb

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,6 @@ def greater_than?(other, alpha: 0.05)
147147
t_stat > critical_value
148148
end
149149

150-
# Alias for greater_than?
151-
def >(other, alpha: 0.05)
152-
greater_than?(other, alpha: alpha)
153-
end
154-
155-
# Alias for less_than?
156-
def <(other, alpha: 0.05)
157-
less_than?(other, alpha: alpha)
158-
end
159-
160150
# Tests if this collection's mean is significantly less than another collection's mean
161151
# using a one-tailed Student's t-test. Returns true if the test indicates statistical
162152
# significance at the specified alpha level.
@@ -177,6 +167,32 @@ def less_than?(other, alpha: 0.05)
177167
t_stat < -critical_value
178168
end
179169

170+
# Operator alias for greater_than? - tests if this collection's mean is significantly greater
171+
#
172+
# @param other [Enumerable] Another collection to compare against
173+
# @param alpha [Float] The significance level (default: 0.05 for 95% confidence)
174+
# @return [Boolean] true if this collection's mean is significantly greater
175+
# @example
176+
# baseline = [100, 110, 105, 115, 95]
177+
# optimized = [85, 95, 90, 100, 80]
178+
# baseline > optimized # => true (baseline is significantly greater)
179+
def >(other, alpha: 0.05)
180+
greater_than?(other, alpha: alpha)
181+
end
182+
183+
# Operator alias for less_than? - tests if this collection's mean is significantly less
184+
#
185+
# @param other [Enumerable] Another collection to compare against
186+
# @param alpha [Float] The significance level (default: 0.05 for 95% confidence)
187+
# @return [Boolean] true if this collection's mean is significantly less
188+
# @example
189+
# optimized = [85, 95, 90, 100, 80]
190+
# baseline = [100, 110, 105, 115, 95]
191+
# optimized < baseline # => true (optimized is significantly less)
192+
def <(other, alpha: 0.05)
193+
less_than?(other, alpha: alpha)
194+
end
195+
180196
# Calculates the arithmetic mean (average) of the collection
181197
#
182198
# @return [Float] The arithmetic mean of all numeric values

0 commit comments

Comments
 (0)