@@ -16,189 +16,189 @@ export cor, cov, std, stdm, var, varm, mean!, mean,
1616
1717# #### mean #####
1818
19- """
20- mean(itr)
21-
22- Compute the mean of all elements in a collection.
23-
24- !!! note
25- If `itr` contains `NaN` or [`missing`](@ref) values, the result is also
26- `NaN` or `missing` (`missing` takes precedence if array contains both).
27- Use the [`skipmissing`](@ref) function to omit `missing` entries and compute the
28- mean of non-missing values.
29-
30- # Examples
31- ```jldoctest
32- julia> using Statistics
33-
34- julia> mean(1:20)
35- 10.5
36-
37- julia> mean([1, missing, 3])
38- missing
39-
40- julia> mean(skipmissing([1, missing, 3]))
41- 2.0
42- ```
43- """
44- mean (itr) = mean (identity, itr)
45-
46- """
47- mean(f, itr)
48-
49- Apply the function `f` to each element of collection `itr` and take the mean.
50-
51- ```jldoctest
52- julia> using Statistics
53-
54- julia> mean(√, [1, 2, 3])
55- 1.3820881233139908
56-
57- julia> mean([√1, √2, √3])
58- 1.3820881233139908
59- ```
60- """
61- function mean (f, itr)
62- y = iterate (itr)
63- if y === nothing
64- return Base. mapreduce_empty_iter (f, + , itr,
65- Base. IteratorEltype (itr)) / 0
66- end
67- count = 1
68- value, state = y
69- f_value = f (value)/ 1
70- total = Base. reduce_first (+ , f_value)
71- y = iterate (itr, state)
72- while y != = nothing
19+ if ! isdefined (Base, :mean )
20+ """
21+ mean(itr)
22+
23+ Compute the mean of all elements in a collection.
24+
25+ !!! note
26+ If `itr` contains `NaN` or [`missing`](@ref) values, the result is also
27+ `NaN` or `missing` (`missing` takes precedence if array contains both).
28+ Use the [`skipmissing`](@ref) function to omit `missing` entries and compute the
29+ mean of non-missing values.
30+
31+ # Examples
32+ ```jldoctest
33+ julia> using Statistics
34+
35+ julia> mean(1:20)
36+ 10.5
37+
38+ julia> mean([1, missing, 3])
39+ missing
40+
41+ julia> mean(skipmissing([1, missing, 3]))
42+ 2.0
43+ ```
44+ """
45+ mean (itr) = mean (identity, itr)
46+
47+ """
48+ mean(f, itr)
49+
50+ Apply the function `f` to each element of collection `itr` and take the mean.
51+
52+ ```jldoctest
53+ julia> using Statistics
54+
55+ julia> mean(√, [1, 2, 3])
56+ 1.3820881233139908
57+
58+ julia> mean([√1, √2, √3])
59+ 1.3820881233139908
60+ ```
61+ """
62+ function mean (f, itr)
63+ y = iterate (itr)
64+ if y === nothing
65+ return Base. mapreduce_empty_iter (f, + , itr,
66+ Base. IteratorEltype (itr)) / 0
67+ end
68+ count = 1
7369 value, state = y
74- total += _mean_promote (total, f (value))
75- count += 1
70+ f_value = f (value)/ 1
71+ total = Base . reduce_first ( + , f_value)
7672 y = iterate (itr, state)
73+ while y != = nothing
74+ value, state = y
75+ total += _mean_promote (total, f (value))
76+ count += 1
77+ y = iterate (itr, state)
78+ end
79+ return total/ count
7780 end
78- return total/ count
79- end
80-
81- """
82- mean(f, A::AbstractArray; dims)
83-
84- Apply the function `f` to each element of array `A` and take the mean over dimensions `dims`.
8581
86- !!! compat "Julia 1.3 "
87- This method requires at least Julia 1.3.
82+ "" "
83+ mean(f, A::AbstractArray; dims)
8884
89- ```jldoctest
90- julia> using Statistics
85+ Apply the function `f` to each element of array `A` and take the mean over dimensions `dims`.
9186
92- julia> mean(√, [1, 2, 3])
93- 1.3820881233139908
87+ !!! compat "Julia 1.3"
88+ This method requires at least Julia 1.3.
9489
95- julia> mean([√1, √2, √3])
96- 1.3820881233139908
90+ ```jldoctest
91+ julia> using Statistics
9792
98- julia> mean(√, [1 2 3; 4 5 6], dims=2)
99- 2×1 Matrix{Float64}:
100- 1.3820881233139908
101- 2.2285192400943226
102- ```
103- """
104- mean (f, A:: AbstractArray ; dims= :) = _mean (f, A, dims)
105-
106- function mean (f:: Number , itr:: Number )
107- f_value = try
108- f (itr)
109- catch MethodError
110- rethrow (ArgumentError (""" mean(f, itr) requires a function and an iterable.
111- Perhaps you meant middle(x, y)?""" ,))
112- end
113- Base. reduce_first (+ , f_value)/ 1
114- end
93+ julia> mean(√, [1, 2, 3])
94+ 1.3820881233139908
11595
116- """
117- mean!(r, v)
118-
119- Compute the mean of `v` over the singleton dimensions of `r`, and write results to `r`.
96+ julia> mean([√1, √2, √3])
97+ 1.3820881233139908
12098
121- # Examples
122- ```jldoctest
123- julia> using Statistics
99+ julia> mean(√, [1 2 3; 4 5 6], dims=2)
100+ 2×1 Matrix{Float64}:
101+ 1.3820881233139908
102+ 2.2285192400943226
103+ ```
104+ """
105+ mean (f, A:: AbstractArray ; dims= :) = _mean (f, A, dims)
124106
125- julia> v = [1 2; 3 4]
126- 2×2 Matrix{Int64}:
127- 1 2
128- 3 4
129-
130- julia> mean!([1., 1.], v )
131- 2-element Vector{Float64}:
132- 1.5
133- 3.5
107+ function mean (f :: Number , itr :: Number )
108+ f_value = try
109+ f (itr)
110+ catch MethodError
111+ rethrow ( ArgumentError ( """ mean(f, itr) requires a function and an iterable.
112+ Perhaps you meant middle(x, y)? """ ,) )
113+ end
114+ Base . reduce_first ( + , f_value) / 1
115+ end
134116
135- julia> mean!([1. 1.], v)
136- 1×2 Matrix{Float64}:
137- 2.0 3.0
138- ```
139- """
140- function mean! (R:: AbstractArray , A:: AbstractArray )
141- sum! (R, A; init= true )
142- x = max (1 , length (R)) // length (A)
143- R .= R .* x
144- return R
145- end
117+ """
118+ mean!(r, v)
119+
120+ Compute the mean of `v` over the singleton dimensions of `r`, and write results to `r`.
121+
122+ # Examples
123+ ```jldoctest
124+ julia> using Statistics
125+
126+ julia> v = [1 2; 3 4]
127+ 2×2 Matrix{Int64}:
128+ 1 2
129+ 3 4
130+
131+ julia> mean!([1., 1.], v)
132+ 2-element Vector{Float64}:
133+ 1.5
134+ 3.5
135+
136+ julia> mean!([1. 1.], v)
137+ 1×2 Matrix{Float64}:
138+ 2.0 3.0
139+ ```
140+ """
141+ function mean! (R:: AbstractArray , A:: AbstractArray )
142+ sum! (R, A; init= true )
143+ x = max (1 , length (R)) // length (A)
144+ R .= R .* x
145+ return R
146+ end
146147
147- """
148- mean(A::AbstractArray; dims)
148+ """
149+ mean(A::AbstractArray; dims)
149150
150- Compute the mean of an array over the given dimensions.
151+ Compute the mean of an array over the given dimensions.
151152
152- !!! compat "Julia 1.1"
153- `mean` for empty arrays requires at least Julia 1.1.
153+ !!! compat "Julia 1.1"
154+ `mean` for empty arrays requires at least Julia 1.1.
154155
155- # Examples
156- ```jldoctest
157- julia> using Statistics
156+ # Examples
157+ ```jldoctest
158+ julia> using Statistics
158159
159- julia> A = [1 2; 3 4]
160- 2×2 Matrix{Int64}:
161- 1 2
162- 3 4
160+ julia> A = [1 2; 3 4]
161+ 2×2 Matrix{Int64}:
162+ 1 2
163+ 3 4
163164
164- julia> mean(A, dims=1)
165- 1×2 Matrix{Float64}:
166- 2.0 3.0
165+ julia> mean(A, dims=1)
166+ 1×2 Matrix{Float64}:
167+ 2.0 3.0
167168
168- julia> mean(A, dims=2)
169- 2×1 Matrix{Float64}:
170- 1.5
171- 3.5
172- ```
173- """
174- mean (A:: AbstractArray ; dims= :) = _mean (identity, A, dims)
169+ julia> mean(A, dims=2)
170+ 2×1 Matrix{Float64}:
171+ 1.5
172+ 3.5
173+ ```
174+ """
175+ mean (A:: AbstractArray ; dims= :) = _mean (identity, A, dims)
175176
176- _mean_promote (x:: T , y:: S ) where {T,S} = convert (promote_type (T, S), y)
177+ _mean_promote (x:: T , y:: S ) where {T,S} = convert (promote_type (T, S), y)
177178
178- # ::Dims is there to force specializing on Colon (as it is a Function)
179- function _mean (f, A:: AbstractArray , dims:: Dims = :) where Dims
180- isempty (A) && return sum (f, A, dims= dims)/ 0
181- if dims === (:)
182- n = length (A)
183- else
184- n = mapreduce (i -> size (A, i), * , unique (dims); init= 1 )
185- end
186- x1 = f (first (A)) / 1
187- result = sum (x -> _mean_promote (x1, f (x)), A, dims= dims)
188- if dims === (:)
189- return result / n
190- else
191- return result ./= n
179+ # ::Dims is there to force specializing on Colon (as it is a Function)
180+ function _mean (f, A:: AbstractArray , dims:: Dims = :) where Dims
181+ isempty (A) && return sum (f, A, dims= dims)/ 0
182+ if dims === (:)
183+ n = length (A)
184+ else
185+ n = mapreduce (i -> size (A, i), * , unique (dims); init= 1 )
186+ end
187+ x1 = f (first (A)) / 1
188+ result = sum (x -> _mean_promote (x1, f (x)), A, dims= dims)
189+ if dims === (:)
190+ return result / n
191+ else
192+ return result ./= n
193+ end
192194 end
193- end
194195
195- function mean (r:: AbstractRange{<:Real} )
196- isempty (r) && return oftype ((first (r) + last (r)) / 2 , NaN )
197- (first (r) + last (r)) / 2
196+ function mean (r:: AbstractRange{<:Real} )
197+ isempty (r) && return oftype ((first (r) + last (r)) / 2 , NaN )
198+ (first (r) + last (r)) / 2
199+ end
198200end
199201
200- median (r:: AbstractRange{<:Real} ) = mean (r)
201-
202202# #### variances #####
203203
204204# faster computation of real(conj(x)*y)
@@ -877,6 +877,8 @@ _median(v::AbstractArray, dims) = mapslices(median!, v, dims = dims)
877877
878878_median (v:: AbstractArray{T} , :: Colon ) where {T} = median! (copyto! (Array {T,1} (undef, length (v)), v))
879879
880+ median (r:: AbstractRange{<:Real} ) = mean (r)
881+
880882"""
881883 quantile!([q::AbstractArray, ] v::AbstractVector, p; sorted=false, alpha::Real=1.0, beta::Real=alpha)
882884
0 commit comments