## Feature #18057

open### Introduce Array#mean

**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 ggmichaelgo (Michael Go) over 1 year ago

I have created PR for this feature :)

#### Updated by sawa (Tsuyoshi Sawada) over 1 year 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 1 year 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.

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 1 year 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.

#### Updated by ggmichaelgo (Michael Go) over 1 year ago

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

#### Updated by mrkn (Kenta Murata) over 1 year 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 1 year 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 1 year 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 1 year ago

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

support.

#### Updated by nobu (Nobuyoshi Nakada) over 1 year ago

When `ary`

has only `Integer`

s, `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 1 year ago

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

```
[1,2].mean #=> 1.5
```