Statistics

The Statistics module contains basic statistics functionality.

Statistics.std — Function.

  1. std(v; corrected::Bool=true, mean=nothing, dims)

Compute the sample standard deviation of a vector or array v, optionally along the given dimensions. The algorithm returns an estimator of the generative distribution's standard deviation under the assumption that each entry of v is an IID drawn from that generative distribution. This computation is equivalent to calculating sqrt(sum((v - mean(v)).^2) / (length(v) - 1)). A pre-computed mean may be provided. If corrected is true, then the sum is scaled with n-1, whereas the sum is scaled with n if corrected is false where n = length(x).

Note

If array contains NaN or missing values, the result is also NaN or missing (missing takes precedence if array contains both). Use the skipmissing function to omit missing entries and compute the standard deviation of non-missing values.

Statistics.stdm — Function.

  1. stdm(v, m; corrected::Bool=true)

Compute the sample standard deviation of a vector v with known mean m. If corrected is true, then the sum is scaled with n-1, whereas the sum is scaled with n if corrected is false where n = length(x).

Note

If array contains NaN or missing values, the result is also NaN or missing (missing takes precedence if array contains both). Use the skipmissing function to omit missing entries and compute the standard deviation of non-missing values.

Statistics.var — Function.

  1. var(v; dims, corrected::Bool=true, mean=nothing)

Compute the sample variance of a vector or array v, optionally along the given dimensions. The algorithm will return an estimator of the generative distribution's variance under the assumption that each entry of v is an IID drawn from that generative distribution. This computation is equivalent to calculating sum(abs2, v - mean(v)) / (length(v) - 1). If corrected is true, then the sum is scaled with n-1, whereas the sum is scaled with n if corrected is false where n = length(x). The mean mean over the region may be provided.

Note

If array contains NaN or missing values, the result is also NaN or missing (missing takes precedence if array contains both). Use the skipmissing function to omit missing entries and compute the variance of non-missing values.

Statistics.varm — Function.

  1. varm(v, m; dims, corrected::Bool=true)

Compute the sample variance of a collection v with known mean(s) m, optionally over the given dimensions. m may contain means for each dimension of v. If corrected is true, then the sum is scaled with n-1, whereas the sum is scaled with n if corrected is false where n = length(x).

Note

If array contains NaN or missing values, the result is also NaN or missing (missing takes precedence if array contains both). Use the skipmissing function to omit missing entries and compute the variance of non-missing values.

Statistics.cor — Function.

  1. cor(x::AbstractVector)

Return the number one.

  1. cor(X::AbstractMatrix; dims::Int=1)

Compute the Pearson correlation matrix of the matrix X along the dimension dims.

  1. cor(x::AbstractVector, y::AbstractVector)

Compute the Pearson correlation between the vectors x and y.

  1. cor(X::AbstractVecOrMat, Y::AbstractVecOrMat; dims=1)

Compute the Pearson correlation between the vectors or matrices X and Y along the dimension dims.

Statistics.cov — Function.

  1. cov(x::AbstractVector; corrected::Bool=true)

Compute the variance of the vector x. If corrected is true (the default) then the sum is scaled with n-1, whereas the sum is scaled with n if corrected is false where n = length(x).

  1. cov(X::AbstractMatrix; dims::Int=1, corrected::Bool=true)

Compute the covariance matrix of the matrix X along the dimension dims. If corrected is true (the default) then the sum is scaled with n-1, whereas the sum is scaled with n if corrected is false where n = size(X, dims).

  1. cov(x::AbstractVector, y::AbstractVector; corrected::Bool=true)

Compute the covariance between the vectors x and y. If corrected is true (the default), computes $\frac{1}{n-1}\sum{i=1}^n (x_i-\bar x) (y_i-\bar y)^$ where $$ denotes the complex conjugate and n = length(x) = length(y). If corrected is false, computes $\frac{1}{n}\sum{i=1}^n (x_i-\bar x) (y_i-\bar y)^*$.

  1. cov(X::AbstractVecOrMat, Y::AbstractVecOrMat; dims::Int=1, corrected::Bool=true)

Compute the covariance between the vectors or matrices X and Y along the dimension dims. If corrected is true (the default) then the sum is scaled with n-1, whereas the sum is scaled with n if corrected is false where n = size(X, dims) = size(Y, dims).

Statistics.mean! — Function.

  1. mean!(r, v)

Compute the mean of v over the singleton dimensions of r, and write results to r.

Examples

  1. julia> v = [1 2; 3 4]
  2. 2×2 Array{Int64,2}:
  3. 1 2
  4. 3 4
  5. julia> mean!([1., 1.], v)
  6. 2-element Array{Float64,1}:
  7. 1.5
  8. 3.5
  9. julia> mean!([1. 1.], v)
  10. 1×2 Array{Float64,2}:
  11. 2.0 3.0

Statistics.mean — Function.

  1. mean(itr)

Compute the mean of all elements in a collection.

Note

If itr contains NaN or missing values, the result is also NaN or missing (missing takes precedence if array contains both). Use the skipmissing function to omit missing entries and compute the mean of non-missing values.

Examples

  1. julia> mean(1:20)
  2. 10.5
  3. julia> mean([1, missing, 3])
  4. missing
  5. julia> mean(skipmissing([1, missing, 3]))
  6. 2.0
  1. mean(f::Function, itr)

Apply the function f to each element of collection itr and take the mean.

  1. julia> mean(√, [1, 2, 3])
  2. 1.3820881233139908
  3. julia> mean([√1, 2, 3])
  4. 1.3820881233139908
  1. mean(A::AbstractArray; dims)

Compute the mean of an array over the given dimensions.

Examples

  1. julia> A = [1 2; 3 4]
  2. 2×2 Array{Int64,2}:
  3. 1 2
  4. 3 4
  5. julia> mean(A, dims=1)
  6. 1×2 Array{Float64,2}:
  7. 2.0 3.0
  8. julia> mean(A, dims=2)
  9. 2×1 Array{Float64,2}:
  10. 1.5
  11. 3.5

Statistics.median! — Function.

  1. median!(v)

Like median, but may overwrite the input vector.

Statistics.median — Function.

  1. median(itr)

Compute the median of all elements in a collection. For an even number of elements no exact median element exists, so the result is equivalent to calculating mean of two median elements.

Note

If itr contains NaN or missing values, the result is also NaN or missing (missing takes precedence if itr contains both). Use the skipmissing function to omit missing entries and compute the median of non-missing values.

Examples

  1. julia> median([1, 2, 3])
  2. 2.0
  3. julia> median([1, 2, 3, 4])
  4. 2.5
  5. julia> median([1, 2, missing, 4])
  6. missing
  7. julia> median(skipmissing([1, 2, missing, 4]))
  8. 2.0
  1. median(A::AbstractArray; dims)

Compute the median of an array along the given dimensions.

Examples

  1. julia> median([1 2; 3 4], dims=1)
  2. 1×2 Array{Float64,2}:
  3. 2.0 3.0

Statistics.middle — Function.

  1. middle(x)

Compute the middle of a scalar value, which is equivalent to x itself, but of the type of middle(x, x) for consistency.

  1. middle(x, y)

Compute the middle of two reals x and y, which is equivalent in both value and type to computing their mean ((x + y) / 2).

  1. middle(range)

Compute the middle of a range, which consists of computing the mean of its extrema. Since a range is sorted, the mean is performed with the first and last element.

  1. julia> middle(1:10)
  2. 5.5
  1. middle(a)

Compute the middle of an array a, which consists of finding its extrema and then computing their mean.

  1. julia> a = [1,2,3.6,10.9]
  2. 4-element Array{Float64,1}:
  3. 1.0
  4. 2.0
  5. 3.6
  6. 10.9
  7. julia> middle(a)
  8. 5.95

Statistics.quantile! — Function.

  1. quantile!([q::AbstractArray, ] v::AbstractVector, p; sorted=false)

Compute the quantile(s) of a vector v at a specified probability or vector or tuple of probabilities p on the interval [0,1]. If p is a vector, an optional output array q may also be specified. (If not provided, a new output array is created.) The keyword argument sorted indicates whether v can be assumed to be sorted; if false (the default), then the elements of v will be partially sorted in-place.

Quantiles are computed via linear interpolation between the points ((k-1)/(n-1), v[k]), for k = 1:n where n = length(v). This corresponds to Definition 7 of Hyndman and Fan (1996), and is the same as the R default.

Note

An ArgumentError is thrown if v contains NaN or missing values.

  • Hyndman, R.J and Fan, Y. (1996) "Sample Quantiles in Statistical Packages", The American Statistician, Vol. 50, No. 4, pp. 361-365
    Examples
  1. julia> x = [3, 2, 1];
  2. julia> quantile!(x, 0.5)
  3. 2.0
  4. julia> x
  5. 3-element Array{Int64,1}:
  6. 1
  7. 2
  8. 3
  9. julia> y = zeros(3);
  10. julia> quantile!(y, x, [0.1, 0.5, 0.9]) === y
  11. true
  12. julia> y
  13. 3-element Array{Float64,1}:
  14. 1.2
  15. 2.0
  16. 2.8

Statistics.quantile — Function.

  1. quantile(itr, p; sorted=false)

Compute the quantile(s) of a collection itr at a specified probability or vector or tuple of probabilities p on the interval [0,1]. The keyword argument sorted indicates whether itr can be assumed to be sorted.

Quantiles are computed via linear interpolation between the points ((k-1)/(n-1), v[k]), for k = 1:n where n = length(v). This corresponds to Definition 7 of Hyndman and Fan (1996), and is the same as the R default.

Note

An ArgumentError is thrown if itr contains NaN or missing values. Use the skipmissing function to omit missing entries and compute the quantiles of non-missing values.

  • Hyndman, R.J and Fan, Y. (1996) "Sample Quantiles in Statistical Packages", The American Statistician, Vol. 50, No. 4, pp. 361-365
    Examples

```jldoctest julia> quantile(0:20, 0.5) 10.0

julia> quantile(0:20, [0.1, 0.5, 0.9]) 3-element Array{Float64,1}: 2.0 10.0 18.0

julia> quantile(skipmissing([1, 10, missing]), 0.5) 5.5 ```

原文: https://juliacn.github.io/JuliaZH.jl/latest/stdlib/Statistics/