You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/counts.rst
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Counting over an Integer Range
8
8
9
9
.. function:: counts(x, a:b[, wv])
10
10
11
-
Count the number of times (or total weights if a weight vector ``wv`` is given) values in ``a:b`` appear in array ``x``. Here, the optional argument ``wv`` should be a weight vector of type ``WeightVec`` (see :ref:`weightvec`).
11
+
Count the number of times (or total weights if a weight vector ``wv`` is given) values in ``a:b`` appear in array ``x``. Here, the optional argument ``wv`` should be a weight vector of type ``AbstractWeights`` (see :ref:`weightvec`).
12
12
13
13
This function returns a vector ``r`` of length ``n``, with ``n = length(a:b) = b-a+1``. In particular, we have
Compute the weighted covariance matrix. Similar to ``var`` and ``std`` the biased covariance matrix (``corrected=false``) is computed by multiplying ``scattermat(X, w)`` by :math:`\frac{1}{\sum{w}}` to normalize.
30
+
However, the unbiased covariance matrix (``corrected=true``) is dependent on the type of weights used:
30
31
31
-
**Note:** By default, the covariance is normalized by the sum of weights, that is, ``cov(X, wv)`` is equal to ``scatter(X, wv) / sum(wv)``.
32
+
* ``AnalyticWeights``: :math:`\frac{1}{\sum w - \sum {w^2} / \sum w}`
Copy file name to clipboardExpand all lines: docs/source/means.rst
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -32,7 +32,7 @@ The package provides functions to compute means of different kinds.
32
32
33
33
.. function:: mean(x, w)
34
34
35
-
The ``mean`` function is also extended to accept a weight vector of type ``WeightVec`` (see :ref:`weightvec`) to compute weighted mean.
35
+
The ``mean`` function is also extended to accept a weight vector of type ``AbstractWeights`` (see :ref:`weightvec`) to compute weighted mean.
36
36
37
37
**Examples:**
38
38
@@ -43,7 +43,7 @@ The package provides functions to compute means of different kinds.
43
43
44
44
.. function:: mean(x, w, dim)
45
45
46
-
Compute weighted means of ``x`` along a certain dimension (specified by an integer ``dim``). The weights are given by a weight vector ``w`` (of type ``WeightVec``).
46
+
Compute weighted means of ``x`` along a certain dimension (specified by an integer ``dim``). The weights are given by a weight vector ``w`` (of type ``AbstractWeights``).
Copy file name to clipboardExpand all lines: docs/source/sampling.rst
+13-16Lines changed: 13 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -9,12 +9,11 @@ The package provides functions for sampling from a given population (with or wit
9
9
.. function:: sample([rng], a)
10
10
11
11
Randomly draw an element from an array ``a``.
12
-
13
12
Optionally specify a random number generator ``rng`` as the first argument (defaults to ``Base.GLOBAL_RNG``).
14
13
15
-
.. function:: sample([rng], a, n[; replace=true, ordered=false])
14
+
.. function:: sample([rng], a, n[; replace=true, ordered=false])
16
15
17
-
Randomly draw ``n`` elements from ``a``.
16
+
Randomly draw ``n`` elements from ``a``.
18
17
19
18
Optionally specify a random number generator ``rng`` as the first argument (defaults to ``Base.GLOBAL_RNG``).
20
19
@@ -26,14 +25,13 @@ The package provides functions for sampling from a given population (with or wit
26
25
.. function:: sample!([rng], a, x[; replace=true, ordered=false])
27
26
28
27
Draw ``length(x)`` elements from ``a`` and write them to a pre-allocated array ``x``.
29
-
30
28
Optionally specify a random number generator ``rng`` as the first argument (defaults to ``Base.GLOBAL_RNG``).
31
29
32
-
.. function:: sample([rng], wv)
30
+
.. function:: sample([rng], wv)
33
31
34
-
Draw an integer in ``1:length(wv)`` with probabilities proportional to the weights given in ``wv``.
32
+
Draw an integer in ``1:length(wv)`` with probabilities proportional to the weights given in ``wv``.
35
33
36
-
Here, ``wv`` should be a weight vector of type ``WeightVec`` (see :ref:`weightvec`).
34
+
Here, ``wv`` should be a weight vector of type ``AbstractWeights`` (see :ref:`weightvec`).
37
35
38
36
Optionally specify a random number generator ``rng`` as the first argument (defaults to ``Base.GLOBAL_RNG``).
39
37
@@ -52,7 +50,7 @@ The package provides functions for sampling from a given population (with or wit
52
50
**Keyword arguments**
53
51
54
52
- ``replace``: indicates whether to have replacement (default = ``true``).
55
-
- ``ordered``: indicates whether to arrange the samples in ascending order (default = ``false``).
53
+
- ``ordered``: indicates whether to arrange the samples in ascending order (default = ``false``).
56
54
57
55
.. function:: sample!([rng], a, wv, x[; replace=true, ordered=false])
58
56
@@ -74,7 +72,7 @@ Here are a list of algorithms implemented in the package. The functions below ar
74
72
75
73
- ``a``: source array representing the population
76
74
- ``x``: the destination array
77
-
- ``wv``: the weight vector (of type ``WeightVec``), for weighted sampling
75
+
- ``wv``: the weight vector (of type ``AbstractWeights``), for weighted sampling
78
76
- ``n``: the length of ``a``
79
77
- ``k``: the length of ``x``. For sampling without replacement, ``k`` must not exceed ``n``.
80
78
- ``rng``: optional random number generator (defaults to ``Base.GLOBAL_RNG``)
@@ -108,7 +106,7 @@ All following functions write results to ``x`` (pre-allocated) and return ``x``.
108
106
109
107
.. function:: fisher_yates_sample!([rng], a, x)
110
108
111
-
*Fisher-Yates shuffling* (with early termination).
109
+
*Fisher-Yates shuffling* (with early termination).
112
110
113
111
Pseudo-code ::
114
112
@@ -118,15 +116,15 @@ All following functions write results to ``x`` (pre-allocated) and return ``x``.
118
116
swap inds[i] with a random one in inds[i:n]
119
117
set x[i] = a[inds[i]]
120
118
end
121
-
119
+
122
120
123
121
This algorithm consumes ``k`` random numbers. It uses an integer array of length ``n`` internally to maintain the shuffled indices. It is considerably faster than Knuth's algorithm especially when ``n`` is greater than ``k``.
124
122
125
123
.. function:: self_avoid_sample!([rng], a, x)
126
124
127
-
Use a set to maintain the index that has been sampled. Each time draw a new index, if the index has already been sampled, redraw until it draws an unsampled one.
125
+
Use a set to maintain the index that has been sampled. Each time draw a new index, if the index has already been sampled, redraw until it draws an unsampled one.
128
126
129
-
This algorithm consumes about (or slightly more than) ``k`` random numbers, and requires ``O(k)`` memory to store the set of sampled indices. Very fast when ``n >> k``.
127
+
This algorithm consumes about (or slightly more than) ``k`` random numbers, and requires ``O(k)`` memory to store the set of sampled indices. Very fast when ``n >> k``.
130
128
131
129
However, if ``k`` is large and approaches ``n``, the rejection rate would increase drastically, resulting in poorer performance.
132
130
@@ -153,7 +151,7 @@ All following functions write results to ``x`` (pre-allocated) and return ``x``.
153
151
154
152
*Direct sampling.*
155
153
156
-
Draw each sample by scanning the weight vector.
154
+
Draw each sample by scanning the weight vector.
157
155
158
156
This algorithm: (1) consumes ``k`` random numbers; (2) has time complexity ``O(n k)``, as scanning the weight vector each time takes ``O(n)``; and (3) requires no additional memory space.
159
157
@@ -173,5 +171,4 @@ All following functions write results to ``x`` (pre-allocated) and return ``x``.
173
171
174
172
It makes a copy of the weight vector at initialization, and sets the weight to zero when the corresponding sample is picked.
175
173
176
-
This algorithm consumes ``O(k)`` random numbers, and has overall time complexity ``O(n k)``.
177
-
174
+
This algorithm consumes ``O(k)`` random numbers, and has overall time complexity ``O(n k)``.
Compute the variance of a real-valued array ``x``, optionally over a dimension ``dim``.
12
+
Observations in ``x`` are weighted using weight vector ``w``.
13
+
The uncorrected (when ``corrected=false``) sample variance is defined as:
12
14
13
-
One can set the keyword argument ``mean``, which can be either ``nothing`` (to compute the mean value within the function), ``0``, or a pre-computed mean value.
One can set the keyword argument ``mean``, which can be either ``nothing`` (to compute the mean value within the function), ``0``, or a pre-computed mean value.
34
+
where ``n`` is the length of the input and ``m`` is the mean.
35
+
The unbiased estimate (when ``corrected=true``) of the population standard deviation is
36
+
computed by replacing :math:`\frac{1}{\sum{w}}` with a factor dependent on the type of
37
+
weights used:
26
38
27
-
.. function:: std(x, wv, dim[; mean=...])
39
+
* ``AnalyticWeights``: :math:`\frac{1}{\sum w - \sum {w^2} / \sum w}`
Jointly compute the mean and standard deviation of ``x``.
53
+
Jointly compute the mean and standard deviation of a real-valued array ``x``, optionally over a dimension ``dim``, as a tuple.
54
+
A weighting vector ``w`` can be specified to weight the estimates.
55
+
Finally, bias correction is applied to the standard deviation calculation if ``corrected=true``.
56
+
See ``std`` documentation for more details.
38
57
39
58
.. function:: skewness(x[, wv])
40
59
41
60
Compute the (standardized) `skewness <http://en.wikipedia.org/wiki/Skewness>`_ of ``x``.
42
61
43
-
One can optionally supply a weight vector of type ``WeightVec`` (see :ref:`weightvec`).
62
+
One can optionally supply a weight vector of type ``AbstractWeights`` (see :ref:`weightvec`).
44
63
45
64
.. function:: kurtosis(x[, wv])
46
65
47
66
Compute the (excessive) `kurtosis <http://en.wikipedia.org/wiki/Kurtosis>`_ of ``x``.
48
67
49
-
One can optionally supply a weight vector of type ``WeightVec`` (see :ref:`weightvec`).
68
+
One can optionally supply a weight vector of type ``AbstractWeights`` (see :ref:`weightvec`).
50
69
51
70
.. function:: moment(x, k[, m][, wv])
52
71
53
72
Compute the ``k``-th order central moment of the values in `x`. It is the sample mean of
54
73
``(x - mean(x)).^k``.
55
74
56
-
One can optionally supply the center ``m``, and/or a weight vector of type ``WeightVec`` (see :ref:`weightvec`).
75
+
One can optionally supply the center ``m``, and/or a weight vector of type ``AbstractWeights`` (see :ref:`weightvec`).
57
76
58
77
59
78
Measurements of Variation
@@ -160,7 +179,7 @@ Quantile and Friends
160
179
161
180
.. function:: median(x, w)
162
181
163
-
Compute the weighted median of ``x``, using weights given by a weight vector ``w`` (of type ``WeightVec``). The weight and data vectors must have the same length. The weighted median :math:`x_k` is the element of ``x`` that satisfies :math:`\sum_{x_i < x_k} w_i \le\frac{1}{2} \sum_{j} w_j` and :math:`\sum_{x_i > x_k} w_i \le\frac{1}{2} \sum_{j} w_j`. If a weight has value zero, then its associated data point is ignored. If none of the weights are positive, an error is thrown. ``NaN`` is returned if ``x`` contains any ``NaN`` values. An error is raised if ``w`` contains any ``NaN`` values.
182
+
Compute the weighted median of ``x``, using weights given by a weight vector ``w`` (of type ``AbstractWeights``). The weight and data vectors must have the same length. The weighted median :math:`x_k` is the element of ``x`` that satisfies :math:`\sum_{x_i < x_k} w_i \le\frac{1}{2} \sum_{j} w_j` and :math:`\sum_{x_i > x_k} w_i \le\frac{1}{2} \sum_{j} w_j`. If a weight has value zero, then its associated data point is ignored. If none of the weights are positive, an error is thrown. ``NaN`` is returned if ``x`` contains any ``NaN`` values. An error is raised if ``w`` contains any ``NaN`` values.
164
183
165
184
**Examples:**
166
185
@@ -171,8 +190,8 @@ Quantile and Friends
171
190
172
191
.. function:: quantile(x, w, p)
173
192
174
-
Compute the weighted quantiles of a vector ``x`` at a specified set of probability values ``p``, using weights given by a weight vector ``w`` (of type ``WeightVec``). Weights must not be negative. The weights and data vectors must have the same length. The quantile for :math:`p` is defined as follows. Denoting :math:`S_k = (k-1)w_k + (n-1) \sum_{i<k}w_i`, define :math:`x_{k+1}` the smallest element of ``x`` such that :math:`S_{k+1}/S_{n}` is strictly superior to :math:`p`. The function returns :math:`(1-\gamma) x_k + \gamma x_{k+1}` with :math:`\gamma = (pS_n- S_k)/(S_{k+1}-S_k)`. This corresponds to R-7, Excel, SciPy-(1,1), Maple-6 when ``w`` is one (see https://en.wikipedia.org/wiki/Quantile).
175
-
193
+
Compute the weighted quantiles of a vector ``x`` 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. The quantile for :math:`p` is defined as follows. Denoting :math:`S_k = (k-1)w_k + (n-1) \sum_{i<k}w_i`, define :math:`x_{k+1}` the smallest element of ``x`` such that :math:`S_{k+1}/S_{n}` is strictly superior to :math:`p`. The function returns :math:`(1-\gamma) x_k + \gamma x_{k+1}` with :math:`\gamma = (pS_n- S_k)/(S_{k+1}-S_k)`. This corresponds to R-7, Excel, SciPy-(1,1), Maple-6 when ``w`` is one (see https://en.wikipedia.org/wiki/Quantile).
0 commit comments