Project

General

Profile

Actions

Feature #18057

open

Introduce Array#mean

Added by ggmichaelgo (Michael Go) over 2 years ago. Updated 2 months ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:104740]

Description

Introduce Array#average to calculate the average value of an array.

array = [1, 2, 3]
array.mean # 2

array = [1.5, 2.2, 3.1]
array.mean(&:round) # 2.3333333333333335

array = [-3, -2, -1]
array.mean { |e| e.abs } # 2

Updated by sawa (Tsuyoshi Sawada) over 2 years ago

The proposal is unclear. It seems to be neither arithmetic nor geometric mean. What kind of average do you have in mind? What is the reason you think that kind of average is particularly needed? What does the block do?

Updated by ggmichaelgo (Michael Go) over 2 years ago

Hello Sawa! I apologize for the confusion... (this is my first time contributing, and I am hoping to contribute more :D)

For this function, I am trying to calculate the arithmetic mean.
I have been developing a stock price analytics algorithm, and I would be using this function to calculate the moving average price.

ref on Moving Average

The block is used like the rb_ary_sum, and it is use to calculate the sum of the array.

Updated by sawa (Tsuyoshi Sawada) over 2 years ago

ggmichaelgo, thanks for the reply. I have thought that what you wrote as the comment in the example code snippet are the expected return values. You made clear in note#3 that they are not. Then, what are they representing? If they are random comments irrelevant to the proposal, then you should delete them to avoid confusion.

Actions #5

Updated by ggmichaelgo (Michael Go) over 2 years ago

  • Description updated (diff)

Updated by ggmichaelgo (Michael Go) over 2 years ago

oh goodness... that is embarrassing... Thank you for catching my error... 🙏

Actions #7

Updated by ggmichaelgo (Michael Go) over 2 years ago

  • Description updated (diff)

Updated by mrkn (Kenta Murata) over 2 years ago

I created enumerable-statistics gem to provide the methods for calculating precise statistical summary in Array and Enumerable. Does this gem fit your use cases?

I think this is unnecessary to provide in the core because average values can be calculated by ary.sum / ary.length without floating-point arithmetic errors. If we provide this method in the core, why not provide stddev or variance, which need complex calculations to avoid floating-point arithmetic errors.

If this feature request will be accepted by any chance, I prefer mean over average because in many languages, such a functions are named mean instead of average. For example Python's statistics.mean, pandas's pandas.Series.mean, NumPy's numpy.mean, Julia's Statistics.mean, Boost's <boost/accumulators/statistics/mean.hpp>, etc.

Updated by ggmichaelgo (Michael Go) over 2 years ago

  • Subject changed from Introduce Array#average to Introduce Array#mean
  • Description updated (diff)

I definitely prefer using mean over average!

Updated by ggmichaelgo (Michael Go) over 2 years ago

Thank you @mrkn (Kenta Murata)!
I didn't know about enumerable-statistics, and I can definitely use this in my projects!!
I wish I knew about it sooner!

Updated by bkuhlmann (Brooke Kuhlmann) over 2 years ago

💡 You can also use the Refinements gem which also provides #mean support.

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

When ary has only Integers, ary.sum is also an Integer and ary.sum / ary.length too.

[1,2].sum     #=> 3
[1,2].sum / 2 #=> 1

I don't think this is the expected result, no?
So it should be ary.sum.quo(ary.length) or ary.sum.fdiv(ary.length).

Updated by ggmichaelgo (Michael Go) over 2 years ago

@nobu (Nobuyoshi Nakada) - yup, you are correct. The expected behaviour should be like this

[1,2].mean #=> 1.5

Updated by matheusrich (Matheus Richard) 2 months ago

Was this proposal rejected?

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0