@@ -16,193 +16,191 @@ export cor, cov, std, stdm, var, varm, mean!, mean,
1616
1717# #### mean #####
1818
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
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
6973 value, state = y
70- f_value = f (value)/ 1
71- total = Base . reduce_first ( + , f_value)
74+ total += _mean_promote (total, f (value))
75+ count += 1
7276 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
8077 end
78+ return total/ count
79+ end
8180
82- """
83- mean(f, A::AbstractArray; dims)
84-
85- Apply the function `f` to each element of array `A` and take the mean over dimensions `dims`.
81+ """
82+ mean(f, A::AbstractArray; dims)
8683
87- !!! compat "Julia 1.3"
88- This method requires at least Julia 1.3.
84+ Apply the function `f` to each element of array `A` and take the mean over dimensions `dims`.
8985
90- ```jldoctest
91- julia> using Statistics
86+ !!! compat "Julia 1.3"
87+ This method requires at least Julia 1.3.
9288
93- julia> mean(√, [1, 2, 3])
94- 1.3820881233139908
89+ ```jldoctest
90+ julia> using Statistics
9591
96- julia> mean([√ 1, √ 2, √ 3])
97- 1.3820881233139908
92+ julia> mean(√, [ 1, 2, 3])
93+ 1.3820881233139908
9894
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)
95+ julia> mean([√1, √2, √3])
96+ 1.3820881233139908
10697
107- function mean (f:: Number , itr:: Number )
108- f_value = try
109- f (itr)
110- catch err
111- if err isa MethodError && err. f === f && err. args == (itr,)
112- rethrow (ArgumentError (""" mean(f, itr) requires a function and an iterable.
113- Perhaps you meant mean((x, y))?""" ))
114- else
115- rethrow (err)
116- end
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 err
110+ if err isa MethodError && err. f === f && err. args == (itr,)
111+ rethrow (ArgumentError (""" mean(f, itr) requires a function and an iterable.
112+ Perhaps you meant mean((x, y))?""" ))
113+ else
114+ rethrow (err)
117115 end
118- Base. reduce_first (+ , f_value)/ 1
119116 end
117+ Base. reduce_first (+ , f_value)/ 1
118+ end
120119
121- """
122- mean!(r, v)
123-
124- Compute the mean of `v` over the singleton dimensions of `r`, and write results to `r`.
125-
126- # Examples
127- ```jldoctest
128- julia> using Statistics
129-
130- julia> v = [1 2; 3 4]
131- 2×2 Matrix{Int64}:
132- 1 2
133- 3 4
134-
135- julia> mean!([1., 1.], v)
136- 2-element Vector{Float64}:
137- 1.5
138- 3.5
139-
140- julia> mean!([1. 1.], v)
141- 1×2 Matrix{Float64}:
142- 2.0 3.0
143- ```
144- """
145- function mean! (R:: AbstractArray , A:: AbstractArray )
146- sum! (R, A; init= true )
147- x = max (1 , length (R)) // length (A)
148- R .= R .* x
149- return R
150- end
120+ """
121+ mean!(r, v)
151122
152- """
153- mean(A::AbstractArray; dims)
123+ Compute the mean of `v` over the singleton dimensions of `r`, and write results to `r`.
154124
155- Compute the mean of an array over the given dimensions.
125+ # Examples
126+ ```jldoctest
127+ julia> using Statistics
156128
157- !!! compat "Julia 1.1"
158- `mean` for empty arrays requires at least Julia 1.1.
129+ julia> v = [1 2; 3 4]
130+ 2×2 Matrix{Int64}:
131+ 1 2
132+ 3 4
159133
160- # Examples
161- ```jldoctest
162- julia> using Statistics
134+ julia> mean!([1., 1.], v)
135+ 2-element Vector{Float64}:
136+ 1.5
137+ 3.5
163138
164- julia> A = [1 2; 3 4]
165- 2×2 Matrix{Int64}:
166- 1 2
167- 3 4
139+ julia> mean!([1. 1.], v)
140+ 1×2 Matrix{Float64}:
141+ 2.0 3.0
142+ ```
143+ """
144+ function mean! (R:: AbstractArray , A:: AbstractArray )
145+ sum! (R, A; init= true )
146+ x = max (1 , length (R)) // length (A)
147+ R .= R .* x
148+ return R
149+ end
168150
169- julia> mean(A, dims=1)
170- 1×2 Matrix{Float64}:
171- 2.0 3.0
151+ """
152+ mean(A::AbstractArray; dims)
172153
173- julia> mean(A, dims=2)
174- 2×1 Matrix{Float64}:
175- 1.5
176- 3.5
177- ```
178- """
179- mean (A:: AbstractArray ; dims= :) = _mean (identity, A, dims)
154+ Compute the mean of an array over the given dimensions.
180155
181- _mean_promote (x:: T , y:: S ) where {T,S} = convert (promote_type (T, S), y)
156+ !!! compat "Julia 1.1"
157+ `mean` for empty arrays requires at least Julia 1.1.
182158
183- # ::Dims is there to force specializing on Colon (as it is a Function)
184- function _mean (f, A:: AbstractArray , dims:: Dims = :) where Dims
185- isempty (A) && return sum (f, A, dims= dims)/ 0
186- if dims === (:)
187- n = length (A)
188- else
189- n = mapreduce (i -> size (A, i), * , unique (dims); init= 1 )
190- end
191- x1 = f (first (A)) / 1
192- result = sum (x -> _mean_promote (x1, f (x)), A, dims= dims)
193- if dims === (:)
194- return result / n
195- else
196- return result ./= n
197- end
198- end
159+ # Examples
160+ ```jldoctest
161+ julia> using Statistics
162+
163+ julia> A = [1 2; 3 4]
164+ 2×2 Matrix{Int64}:
165+ 1 2
166+ 3 4
167+
168+ julia> mean(A, dims=1)
169+ 1×2 Matrix{Float64}:
170+ 2.0 3.0
171+
172+ julia> mean(A, dims=2)
173+ 2×1 Matrix{Float64}:
174+ 1.5
175+ 3.5
176+ ```
177+ """
178+ mean (A:: AbstractArray ; dims= :) = _mean (identity, A, dims)
179+
180+ _mean_promote (x:: T , y:: S ) where {T,S} = convert (promote_type (T, S), y)
199181
200- function mean (r:: AbstractRange{<:Real} )
201- isempty (r) && return oftype ((first (r) + last (r)) / 2 , NaN )
202- (first (r) + last (r)) / 2
182+ # ::Dims is there to force specializing on Colon (as it is a Function)
183+ function _mean (f, A:: AbstractArray , dims:: Dims = :) where Dims
184+ isempty (A) && return sum (f, A, dims= dims)/ 0
185+ if dims === (:)
186+ n = length (A)
187+ else
188+ n = mapreduce (i -> size (A, i), * , unique (dims); init= 1 )
189+ end
190+ x1 = f (first (A)) / 1
191+ result = sum (x -> _mean_promote (x1, f (x)), A, dims= dims)
192+ if dims === (:)
193+ return result / n
194+ else
195+ return result ./= n
203196 end
204197end
205198
199+ function mean (r:: AbstractRange{<:Real} )
200+ isempty (r) && return oftype ((first (r) + last (r)) / 2 , NaN )
201+ (first (r) + last (r)) / 2
202+ end
203+
206204# #### variances #####
207205
208206# faster computation of real(conj(x)*y)
0 commit comments