Compute the weighted sum of A with weights w over the dimension dim and store the result in R. If init=false, the sum is added to R rather than starting from zero.
Compute the weighted sum of A with weights w over the dimension dim and store the result in R. If init=false, the sum is added to R rather than starting from zero.
Compute the weighted mean of array A with weight vector w (of type AbstractWeights). If dim is provided, compute the weighted mean along dimension dims.
Examples
n = 20
x = rand(n)
w = rand(n)
mean(x, weights(w))
Return the generalized/power mean with exponent p of a real-valued array, i.e. , where n = length(a). It is taken to be the geometric mean when p == 0.
Compute the variance of a real-valued array x, optionally over a dimension dim. Observations in x are weighted using weight vector w. The uncorrected (when corrected=false) sample variance is defined as:
where is the length of the input and is the mean. The unbiased estimate (when corrected=true) of the population variance is computed by replacing with a factor dependent on the type of weights used:
AnalyticWeights:
FrequencyWeights:
ProbabilityWeights: where equals count(!iszero, w)
Weights: ArgumentError (bias correction not supported)
Compute the standard deviation of a real-valued array x, optionally over a dimension dim. Observations in x are weighted using weight vector w. The uncorrected (when corrected=false) sample standard deviation is defined as:
where is the length of the input and is the mean. The unbiased estimate (when corrected=true) of the population standard deviation is computed by replacing with a factor dependent on the type of weights used:
AnalyticWeights:
FrequencyWeights:
ProbabilityWeights: where equals count(!iszero, w)
Weights: ArgumentError (bias correction not supported)
Return the mean and variance of collection x. If x is an AbstractArray, dim can be specified as a tuple to compute statistics over these dimensions. A weighting vector w can be specified to weight the estimates. Finally, bias correction is be applied to the variance calculation if corrected=true. See var documentation for more details.
Return the mean and standard deviation of collection x. If x is an AbstractArray, dim can be specified as a tuple to compute statistics over these dimensions. A weighting vector w can be specified to weight the estimates. Finally, bias correction is applied to the standard deviation calculation if corrected=true. See std documentation for more details.
Return the kth order cumulant of a real-valued array v, optionally specifying a weighting vector wv and a pre-computed mean m.
If k is a range of Integers, then return all the cumulants of orders in this range as a vector.
This quantity is calculated using a recursive definition on lower-order cumulants and central moments.
Reference: Smith, P. J. 1995. A Recursive Formulation of the Old Problem of Obtaining Moments from Cumulants and Vice Versa. The American Statistician, 49(2), 217—218. https://doi.org/10.2307/2684642
Return the coefficient of variation of collection x, optionally specifying a precomputed mean m. The coefficient of variation is the ratio of the standard deviation to the mean.
Return the standard error of the mean for a collection x. A pre-computed mean may be provided.
When not using weights, this is the (sample) standard deviation divided by the sample size. If weights are used, the variance of the sample mean is calculated as follows:
AnalyticWeights: Not implemented.
FrequencyWeights:
ProbabilityWeights:
The standard error is then the square root of the above quantities.
References
Carl-Erik Särndal, Bengt Swensson, Jan Wretman (1992). Model Assisted Survey Sampling. New York: Springer. pp. 51-53.
Compute the median absolute deviation (MAD) of collection x around center (by default, around the median).
If normalize is set to true, the MAD is multiplied by 1 / quantile(Normal(), 3/4) ≈ 1.4826, in order to obtain a consistent estimator of the standard deviation under the assumption that the data is normally distributed.
Compute the median absolute deviation (MAD) of array x around center (by default, around the median), overwriting x in the process. x must be able to hold values of generated by calling middle on its elements (for example an integer vector is not appropriate since middle can produce non-integer values).
If normalize is set to true, the MAD is multiplied by 1 / quantile(Normal(), 3/4) ≈ 1.4826, in order to obtain a consistent estimator of the standard deviation under the assumption that the data is normally distributed.
Compute the z-scores of X, optionally specifying a precomputed mean μ and standard deviation σ. z-scores are the signed number of standard deviations above the mean that an observation lies, i.e. .
μ and σ should be both scalars or both arrays. The computation is broadcasting. In particular, when μ and σ are arrays, they should have the same size, and size(μ, i) == 1 || size(μ, i) == size(X, i) for each dimension.
Compute the z-scores of an array X with mean μ and standard deviation σ. z-scores are the signed number of standard deviations above the mean that an observation lies, i.e. .
If a destination array Z is provided, the scores are stored in Z and it must have the same shape as X. Otherwise X is overwritten.
Compute the entropy of a collection of probabilities p, optionally specifying a real number b such that the entropy is scaled by 1/log(b). Elements with probability 0 or 1 add 0 to the entropy.
Compute the Kullback-Leibler divergence from q to p, also called the relative entropy of p with respect to q, that is the sum pᵢ * log(pᵢ / qᵢ). Optionally a real number b can be specified such that the divergence is scaled by 1/log(b).
Compute the weighted quantiles of a vector v at a specified set of probability values p, using weights given by a weight vector w (of type AbstractWeights). Weights must not be negative. The weights and data vectors must have the same length. NaN is returned if x contains any NaN values. An error is raised if w contains any NaN values.
With FrequencyWeights, the function returns the same result as quantile for a vector with repeated values. Weights must be integers.
With non FrequencyWeights, denote the length of the vector, the vector of weights, the cumulative weight corresponding to the probability and the cumulative weight for each observation, define the smallest element of v such that is strictly superior to . The weighted quantile is given by with . In particular, when all weights are equal, the function returns the same result as the unweighted quantile.
Compute the quantile position in the [0, 1] interval of value relative to collection itr.
Different definitions can be chosen via the method keyword argument. Let count_less be the number of elements of itr that are less than value, count_equal the number of elements of itr that are equal to value, n the length of itr, greatest_smaller the highest value below value and smallest_greater the lowest value above value. Then method supports the following definitions:
:inc (default): Return a value in the range 0 to 1 inclusive.
Return count_less / (n - 1) if value ∈ itr, otherwise apply interpolation based on definition 7 of quantile in Hyndman and Fan (1996) (equivalent to Excel PERCENTRANK and PERCENTRANK.INC). This definition corresponds to the lower semi-continuous inverse of quantile with its default parameters.
:exc: Return a value in the range 0 to 1 exclusive.
Return (count_less + 1) / (n + 1) if value ∈ itr otherwise apply interpolation based on definition 6 of quantile in Hyndman and Fan (1996) (equivalent to Excel PERCENTRANK.EXC).
:compete: Return count_less / (n - 1) if value ∈ itr, otherwise
return (count_less - 1) / (n - 1), without interpolation (equivalent to MariaDB PERCENT_RANK, dplyr percent_rank).
:tied: Return (count_less + count_equal/2) / n, without interpolation.
Based on the definition in Roscoe, J. T. (1975) (equivalent to "mean" kind of SciPy percentileofscore).
:strict: Return count_less / n, without interpolation
(equivalent to "strict" kind of SciPy percentileofscore).
:weak: Return (count_less + count_equal) / n, without interpolation
(equivalent to "weak" kind of SciPy percentileofscore).
An ArgumentError is thrown if itr contains NaN or missing values or if itr contains fewer than two elements.
Return the mode (most common number) of an array, optionally over a specified range r or weighted via a vector wv. If several modes exist, the first one (in order of appearance) is returned.
Compute summary statistics for a real-valued array a. Returns a SummaryStats object containing the mean, minimum, 25th percentile, median, 75th percentile, and maxmimum.