rgsl/statistics.rs
1//
2// A rust binding for the GSL library by Guillaume Gomez (guillaume1.gomez@gmail.com)
3//
4
5/*!
6# Statistics
7
8This chapter describes the statistical functions in the library. The basic statistical functions include routines to compute the mean,
9variance and standard deviation. More advanced functions allow you to calculate absolute deviations, skewness, and kurtosis as well as the
10median and arbitrary percentiles. The algorithms use recurrence relations to compute average quantities in a stable way, without large
11intermediate values that might overflow.
12
13## Weighted Samples
14
15The functions described in this section allow the computation of statistics for weighted samples. The functions accept an array of
16samples, x_i, with associated weights, w_i. Each sample x_i is considered as having been drawn from a Gaussian distribution with variance
17\sigma_i^2. The sample weight w_i is defined as the reciprocal of this variance, w_i = 1/\sigma_i^2. Setting a weight to zero corresponds
18to removing a sample from a dataset.
19
20## Maximum and Minimum values
21
22The following functions find the maximum and minimum values of a dataset (or their indices). If the data contains NaNs then a NaN will be
23returned, since the maximum or minimum value is undefined. For functions which return an index, the location of the first NaN in the array is returned.
24
25## Median and Percentiles
26
27The median and percentile functions described in this section operate on sorted data. For convenience we use quantiles, measured on a
28scale of 0 to 1, instead of percentiles (which use a scale of 0 to 100).
29
30## References and Further Reading
31
32The standard reference for almost any topic in statistics is the multi-volume Advanced Theory of Statistics by Kendall and Stuart.
33
34Maurice Kendall, Alan Stuart, and J. Keith Ord. The Advanced Theory of Statistics (multiple volumes) reprinted as Kendall’s Advanced
35Theory of Statistics. Wiley, ISBN 047023380X.
36Many statistical concepts can be more easily understood by a Bayesian approach. The following book by Gelman, Carlin, Stern and Rubin
37gives a comprehensive coverage of the subject.
38
39Andrew Gelman, John B. Carlin, Hal S. Stern, Donald B. Rubin. Bayesian Data Analysis. Chapman & Hall, ISBN 0412039915.
40For physicists the Particle Data Group provides useful reviews of Probability and Statistics in the “Mathematical Tools” section of its
41Annual Review of Particle Physics.
42
43Review of Particle Properties R.M. Barnett et al., Physical Review D54, 1 (1996)
44The Review of Particle Physics is available online at the website http://pdg.lbl.gov/.
45!*/
46
47/// This function returns the arithmetic mean of data, a dataset of length n with stride stride. The
48/// arithmetic mean, or sample mean, is denoted by \Hat\mu and defined as,
49///
50/// \Hat\mu = (1/N) \sum x_i
51///
52/// where x_i are the elements of the dataset data. For samples drawn from a gaussian distribution
53/// the variance of \Hat\mu is \sigma^2 / N.
54#[doc(alias = "gsl_stats_mean")]
55pub fn mean(data: &[f64], stride: usize, n: usize) -> f64 {
56 unsafe { sys::gsl_stats_mean(data.as_ptr(), stride, n) }
57}
58
59/// This function returns the estimated, or sample, variance of data, a dataset of length n with
60/// stride stride. The estimated variance is denoted by \Hat\sigma^2 and is defined by,
61///
62/// \Hat\sigma^2 = (1/(N-1)) \sum (x_i - \Hat\mu)^2
63///
64/// where x_i are the elements of the dataset data. Note that the normalization factor of 1/(N-1)
65/// results from the derivation of \Hat\sigma^2 as an unbiased estimator of the population variance
66/// \sigma^2. For samples drawn from a Gaussian distribution the variance of \Hat\sigma^2 itself is
67/// 2 \sigma^4 / N.
68///
69/// This function computes the mean via a call to gsl_stats_mean. If you have already computed the
70/// mean then you can pass it directly to gsl_stats_variance_m.
71#[doc(alias = "gsl_stats_variance")]
72pub fn variance(data: &[f64], stride: usize, n: usize) -> f64 {
73 unsafe { sys::gsl_stats_variance(data.as_ptr(), stride, n) }
74}
75
76/// This function returns the sample variance of data relative to the given value of mean. The
77/// function is computed with \Hat\mu replaced by the value of mean that you supply,
78///
79/// \Hat\sigma^2 = (1/(N-1)) \sum (x_i - mean)^2
80#[doc(alias = "gsl_stats_variance_m")]
81pub fn variance_m(data: &[f64], stride: usize, n: usize, mean: f64) -> f64 {
82 unsafe { sys::gsl_stats_variance_m(data.as_ptr(), stride, n, mean) }
83}
84
85/// The standard deviation is defined as the square root of the variance. This function returns the
86/// square root of the corresponding variance functions above.
87#[doc(alias = "gsl_stats_sd")]
88pub fn sd(data: &[f64], stride: usize, n: usize) -> f64 {
89 unsafe { sys::gsl_stats_sd(data.as_ptr(), stride, n) }
90}
91
92/// The standard deviation is defined as the square root of the variance. This function returns the
93/// square root of the corresponding variance functions above.
94#[doc(alias = "gsl_stats_sd_m")]
95pub fn sd_m(data: &[f64], stride: usize, n: usize, mean: f64) -> f64 {
96 unsafe { sys::gsl_stats_sd_m(data.as_ptr(), stride, n, mean) }
97}
98
99/// This function returns the total sum of squares (TSS) of data about the mean. For gsl_stats_tss_m
100/// the user-supplied value of mean is used, and for gsl_stats_tss it is computed using
101/// gsl_stats_mean.
102///
103/// TSS = \sum (x_i - mean)^2
104#[doc(alias = "gsl_stats_tss")]
105pub fn tss(data: &[f64], stride: usize, n: usize) -> f64 {
106 unsafe { sys::gsl_stats_tss(data.as_ptr(), stride, n) }
107}
108
109/// This function returns the total sum of squares (TSS) of data about the mean. For gsl_stats_tss_m
110/// the user-supplied value of mean is used, and for gsl_stats_tss it is computed using
111/// gsl_stats_mean.
112///
113/// TSS = \sum (x_i - mean)^2
114#[doc(alias = "gsl_stats_tss_m")]
115pub fn tss_m(data: &[f64], stride: usize, n: usize, mean: f64) -> f64 {
116 unsafe { sys::gsl_stats_tss_m(data.as_ptr(), stride, n, mean) }
117}
118
119/// This function computes an unbiased estimate of the variance of data when the population mean
120/// mean of the underlying distribution is known a priori. In this case the estimator for the
121/// variance uses the factor 1/N and the sample mean \Hat\mu is replaced by the known population
122/// mean \mu,
123///
124/// \Hat\sigma^2 = (1/N) \sum (x_i - \mu)^2
125#[doc(alias = "gsl_stats_variance_with_fixed_mean")]
126pub fn variance_with_fixed_mean(data: &[f64], stride: usize, n: usize, mean: f64) -> f64 {
127 unsafe { sys::gsl_stats_variance_with_fixed_mean(data.as_ptr(), stride, n, mean) }
128}
129
130/// This function calculates the standard deviation of data for a fixed population mean mean. The
131/// result is the square root of the corresponding variance function.
132#[doc(alias = "gsl_stats_sd_with_fixed_mean")]
133pub fn sd_with_fixed_mean(data: &[f64], stride: usize, n: usize, mean: f64) -> f64 {
134 unsafe { sys::gsl_stats_sd_with_fixed_mean(data.as_ptr(), stride, n, mean) }
135}
136
137/// This function computes the absolute deviation from the mean of data, a dataset of length n with
138/// stride stride. The absolute deviation from the mean is defined as,
139///
140/// absdev = (1/N) \sum |x_i - \Hat\mu|
141///
142/// where x_i are the elements of the dataset data. The absolute deviation from the mean provides a
143/// more robust measure of the width of a distribution than the variance. This function computes the
144/// mean of data via a call to gsl_stats_mean.
145#[doc(alias = "gsl_stats_absdev")]
146pub fn absdev(data: &[f64], stride: usize, n: usize) -> f64 {
147 unsafe { sys::gsl_stats_absdev(data.as_ptr(), stride, n) }
148}
149
150/// This function computes the absolute deviation of the dataset data relative to the given value of
151/// mean,
152///
153/// absdev = (1/N) \sum |x_i - mean|
154///
155/// This function is useful if you have already computed the mean of data (and want to avoid
156/// recomputing it), or wish to calculate the absolute deviation relative to another value (such as
157/// zero, or the median).
158#[doc(alias = "gsl_stats_absdev_m")]
159pub fn absdev_m(data: &[f64], stride: usize, n: usize, mean: f64) -> f64 {
160 unsafe { sys::gsl_stats_absdev_m(data.as_ptr(), stride, n, mean) }
161}
162
163/// This function computes the skewness of data, a dataset of length n with stride stride. The
164/// skewness is defined as,
165///
166/// skew = (1/N) \sum ((x_i - \Hat\mu)/\Hat\sigma)^3
167///
168/// where x_i are the elements of the dataset data. The skewness measures the asymmetry of the tails
169/// of a distribution.
170///
171/// The function computes the mean and estimated standard deviation of data via calls to [`mean`]
172/// and [`sd`].
173#[doc(alias = "gsl_stats_skew")]
174pub fn skew(data: &[f64], stride: usize, n: usize) -> f64 {
175 unsafe { sys::gsl_stats_skew(data.as_ptr(), stride, n) }
176}
177
178/// This function computes the skewness of the dataset data using the given values of the mean mean
179/// and standard deviation sd,
180///
181/// skew = (1/N) \sum ((x_i - mean)/sd)^3
182///
183/// These functions are useful if you have already computed the mean and standard deviation of data
184/// and want to avoid recomputing them.
185#[doc(alias = "gsl_stats_skew_m_sd")]
186pub fn skew_m_sd(data: &[f64], stride: usize, n: usize, mean: f64, sd: f64) -> f64 {
187 unsafe { sys::gsl_stats_skew_m_sd(data.as_ptr(), stride, n, mean, sd) }
188}
189
190/// This function computes the kurtosis of data, a dataset of length n with stride stride. The
191/// kurtosis is defined as,
192///
193/// kurtosis = ((1/N) \sum ((x_i - \Hat\mu)/\Hat\sigma)^4) - 3
194///
195/// The kurtosis measures how sharply peaked a distribution is, relative to its width. The kurtosis
196/// is normalized to zero for a Gaussian distribution.
197#[doc(alias = "gsl_stats_kurtosis")]
198pub fn kurtosis(data: &[f64], stride: usize, n: usize) -> f64 {
199 unsafe { sys::gsl_stats_kurtosis(data.as_ptr(), stride, n) }
200}
201
202/// This function computes the kurtosis of the dataset data using the given values of the mean mean
203/// and standard deviation sd,
204///
205/// kurtosis = ((1/N) \sum ((x_i - mean)/sd)^4) - 3
206///
207/// This function is useful if you have already computed the mean and standard deviation of data and
208/// want to avoid recomputing them.
209#[doc(alias = "gsl_stats_kurtosis_m_sd")]
210pub fn kurtosis_m_sd(data: &[f64], stride: usize, n: usize, mean: f64, sd: f64) -> f64 {
211 unsafe { sys::gsl_stats_kurtosis_m_sd(data.as_ptr(), stride, n, mean, sd) }
212}
213
214/// This function computes the lag-1 autocorrelation of the dataset data.
215///
216/// a_1 = {\sum_{i = 1}^{n} (x_{i} - \Hat\mu) (x_{i-1} - \Hat\mu)
217/// \over
218/// \sum_{i = 1}^{n} (x_{i} - \Hat\mu) (x_{i} - \Hat\mu)}
219#[doc(alias = "gsl_stats_lag1_autocorrelation")]
220pub fn lag1_autocorrelation(data: &[f64], stride: usize, n: usize) -> f64 {
221 unsafe { sys::gsl_stats_lag1_autocorrelation(data.as_ptr(), stride, n) }
222}
223
224/// This function computes the lag-1 autocorrelation of the dataset data using the given value of
225/// the mean mean.
226#[doc(alias = "gsl_stats_lag1_autocorrelation_m")]
227pub fn lag1_autocorrelation_m(data: &[f64], stride: usize, n: usize, mean: f64) -> f64 {
228 unsafe { sys::gsl_stats_lag1_autocorrelation_m(data.as_ptr(), stride, n, mean) }
229}
230
231/// This function computes the covariance of the datasets data1 and data2 which must both be of the
232/// same length n.
233///
234/// covar = (1/(n - 1)) \sum_{i = 1}^{n} (x_i - \Hat x) (y_i - \Hat y)
235#[doc(alias = "gsl_stats_covariance")]
236pub fn covariance(data1: &[f64], stride1: usize, data2: &[f64], stride2: usize, n: usize) -> f64 {
237 unsafe { sys::gsl_stats_covariance(data1.as_ptr(), stride1, data2.as_ptr(), stride2, n) }
238}
239
240/// This function computes the covariance of the datasets data1 and data2 using the given values of
241/// the means, mean1 and mean2. This is useful if you have already computed the means of data1 and
242/// data2 and want to avoid recomputing them.
243#[doc(alias = "gsl_stats_covariance_m")]
244pub fn covariance_m(
245 data1: &[f64],
246 stride1: usize,
247 data2: &[f64],
248 stride2: usize,
249 n: usize,
250 mean1: f64,
251 mean2: f64,
252) -> f64 {
253 unsafe {
254 sys::gsl_stats_covariance_m(
255 data1.as_ptr(),
256 stride1,
257 data2.as_ptr(),
258 stride2,
259 n,
260 mean1,
261 mean2,
262 )
263 }
264}
265
266/// This function efficiently computes the Pearson correlation coefficient between the datasets
267/// data1 and data2 which must both be of the same length n.
268///
269/// r = cov(x, y) / (\Hat\sigma_x \Hat\sigma_y)
270/// = {1/(n-1) \sum (x_i - \Hat x) (y_i - \Hat y)
271/// \over
272/// \sqrt{1/(n-1) \sum (x_i - \Hat x)^2} \sqrt{1/(n-1) \sum (y_i - \Hat y)^2}
273/// }
274#[doc(alias = "gsl_stats_correlation")]
275pub fn correlation(data1: &[f64], stride1: usize, data2: &[f64], stride2: usize, n: usize) -> f64 {
276 unsafe { sys::gsl_stats_correlation(data1.as_ptr(), stride1, data2.as_ptr(), stride2, n) }
277}
278
279/// This function computes the Spearman rank correlation coefficient between the datasets data1 and
280/// data2 which must both be of the same length n. Additional workspace of size 2*n is required in
281/// work. The Spearman rank correlation between vectors x and y is equivalent to the Pearson
282/// correlation between the ranked vectors x_R and y_R, where ranks are defined to be the average of
283/// the positions of an element in the ascending order of the values.
284#[doc(alias = "gsl_stats_spearman")]
285pub fn spearman(
286 data1: &[f64],
287 stride1: usize,
288 data2: &[f64],
289 stride2: usize,
290 n: usize,
291 work: &mut [f64],
292) -> f64 {
293 unsafe {
294 sys::gsl_stats_spearman(
295 data1.as_ptr(),
296 stride1,
297 data2.as_ptr(),
298 stride2,
299 n,
300 work.as_mut_ptr(),
301 )
302 }
303}
304
305/// This function returns the weighted mean of the dataset data with stride stride and length n,
306/// using the set of weights w with stride wstride and length n. The weighted mean is defined as,
307///
308/// \Hat\mu = (\sum w_i x_i) / (\sum w_i)
309#[doc(alias = "gsl_stats_wmean")]
310pub fn wmean(w: &[f64], wstride: usize, data: &[f64], stride: usize, n: usize) -> f64 {
311 unsafe { sys::gsl_stats_wmean(w.as_ptr(), wstride, data.as_ptr(), stride, n) }
312}
313
314/// This function returns the estimated variance of the dataset data with stride stride and length
315/// n, using the set of weights w with
316/// stride wstride and length n. The estimated variance of a weighted dataset is calculated as,
317///
318/// \Hat\sigma^2 = ((\sum w_i)/((\sum w_i)^2 - \sum (w_i^2)))
319/// \sum w_i (x_i - \Hat\mu)^2
320///
321/// Note that this expression reduces to an unweighted variance with the familiar 1/(N-1) factor
322/// when there are N equal non-zero weights.
323#[doc(alias = "gsl_stats_wvariance")]
324pub fn wvariance(w: &[f64], wstride: usize, data: &[f64], stride: usize, n: usize) -> f64 {
325 unsafe { sys::gsl_stats_wvariance(w.as_ptr(), wstride, data.as_ptr(), stride, n) }
326}
327
328/// This function returns the estimated variance of the weighted dataset data using the given
329/// weighted mean wmean.
330#[doc(alias = "gsl_stats_wvariance_m")]
331pub fn wvariance_m(
332 w: &[f64],
333 wstride: usize,
334 data: &[f64],
335 stride: usize,
336 n: usize,
337 wmean: f64,
338) -> f64 {
339 unsafe { sys::gsl_stats_wvariance_m(w.as_ptr(), wstride, data.as_ptr(), stride, n, wmean) }
340}
341
342/// The standard deviation is defined as the square root of the variance. This function returns the
343/// square root of the corresponding variance function [`wvariance`] above.
344#[doc(alias = "gsl_stats_wsd")]
345pub fn wsd(w: &[f64], wstride: usize, data: &[f64], stride: usize, n: usize) -> f64 {
346 unsafe { sys::gsl_stats_wsd(w.as_ptr(), wstride, data.as_ptr(), stride, n) }
347}
348
349/// This function returns the square root of the corresponding variance function
350/// [`wvariance_m`] above.
351#[doc(alias = "gsl_stats_wsd_m")]
352pub fn wsd_m(w: &[f64], wstride: usize, data: &[f64], stride: usize, n: usize, wmean: f64) -> f64 {
353 unsafe { sys::gsl_stats_wsd_m(w.as_ptr(), wstride, data.as_ptr(), stride, n, wmean) }
354}
355
356/// This function computes an unbiased estimate of the variance of the weighted dataset data when
357/// the population mean mean of the underlying distribution is known a priori. In this case the
358/// estimator for the variance replaces the sample mean \Hat\mu by the known population mean \mu,
359///
360/// \Hat\sigma^2 = (\sum w_i (x_i - \mu)^2) / (\sum w_i)
361#[doc(alias = "gsl_stats_wvariance_with_fixed_mean")]
362pub fn wvariance_with_fixed_mean(
363 w: &[f64],
364 wstride: usize,
365 data: &[f64],
366 stride: usize,
367 n: usize,
368 mean: f64,
369) -> f64 {
370 unsafe {
371 sys::gsl_stats_wvariance_with_fixed_mean(
372 w.as_ptr(),
373 wstride,
374 data.as_ptr(),
375 stride,
376 n,
377 mean,
378 )
379 }
380}
381
382/// The standard deviation is defined as the square root of the variance. This function returns the
383/// square root of the corresponding variance function above.
384#[doc(alias = "gsl_stats_wsd_with_fixed_mean")]
385pub fn wsd_with_fixed_mean(
386 w: &[f64],
387 wstride: usize,
388 data: &[f64],
389 stride: usize,
390 n: usize,
391 mean: f64,
392) -> f64 {
393 unsafe {
394 sys::gsl_stats_wsd_with_fixed_mean(w.as_ptr(), wstride, data.as_ptr(), stride, n, mean)
395 }
396}
397
398/// This function returns the weighted total sum of squares (TSS) of data about the weighted mean.
399/// For gsl_stats_wtss_m the user-supplied value of wmean is used, and for gsl_stats_wtss it is
400/// computed using gsl_stats_wmean.
401///
402/// TSS = \sum w_i (x_i - wmean)^2
403#[doc(alias = "gsl_stats_wtss")]
404pub fn wtss(w: &[f64], wstride: usize, data: &[f64], stride: usize, n: usize) -> f64 {
405 unsafe { sys::gsl_stats_wtss(w.as_ptr(), wstride, data.as_ptr(), stride, n) }
406}
407
408/// This function returns the weighted total sum of squares (TSS) of data about the weighted mean.
409/// For gsl_stats_wtss_m the user-supplied value of wmean is used, and for gsl_stats_wtss it is
410/// computed using gsl_stats_wmean.
411///
412/// TSS = \sum w_i (x_i - wmean)^2
413#[doc(alias = "gsl_stats_wtss_m")]
414pub fn wtss_m(w: &[f64], wstride: usize, data: &[f64], stride: usize, n: usize, wmean: f64) -> f64 {
415 unsafe { sys::gsl_stats_wtss_m(w.as_ptr(), wstride, data.as_ptr(), stride, n, wmean) }
416}
417
418/// This function computes the weighted absolute deviation from the weighted mean of data. The absolute deviation from the mean is defined
419/// as,
420///
421/// absdev = (\sum w_i |x_i - \Hat\mu|) / (\sum w_i)
422#[doc(alias = "gsl_stats_wabsdev")]
423pub fn wabsdev(w: &[f64], wstride: usize, data: &[f64], stride: usize, n: usize) -> f64 {
424 unsafe { sys::gsl_stats_wabsdev(w.as_ptr(), wstride, data.as_ptr(), stride, n) }
425}
426
427/// This function computes the absolute deviation of the weighted dataset data about the given weighted mean wmean.
428#[doc(alias = "gsl_stats_wabsdev_m")]
429pub fn wabsdev_m(
430 w: &[f64],
431 wstride: usize,
432 data: &[f64],
433 stride: usize,
434 n: usize,
435 wmean: f64,
436) -> f64 {
437 unsafe { sys::gsl_stats_wabsdev_m(w.as_ptr(), wstride, data.as_ptr(), stride, n, wmean) }
438}
439
440/// This function computes the weighted skewness of the dataset data.
441///
442/// skew = (\sum w_i ((x_i - \Hat x)/\Hat \sigma)^3) / (\sum w_i)
443#[doc(alias = "gsl_stats_wskew")]
444pub fn wskew(w: &[f64], wstride: usize, data: &[f64], stride: usize, n: usize) -> f64 {
445 unsafe { sys::gsl_stats_wskew(w.as_ptr(), wstride, data.as_ptr(), stride, n) }
446}
447
448/// This function computes the weighted skewness of the dataset data using the given values of the
449/// weighted mean and weighted standard deviation, wmean and wsd.
450#[doc(alias = "gsl_stats_wskew_m_sd")]
451pub fn wskew_m_sd(
452 w: &[f64],
453 wstride: usize,
454 data: &[f64],
455 stride: usize,
456 n: usize,
457 wmean: f64,
458 wsd: f64,
459) -> f64 {
460 unsafe { sys::gsl_stats_wskew_m_sd(w.as_ptr(), wstride, data.as_ptr(), stride, n, wmean, wsd) }
461}
462
463/// This function computes the weighted kurtosis of the dataset data.
464///
465/// kurtosis = ((\sum w_i ((x_i - \Hat x)/\Hat \sigma)^4) / (\sum w_i)) - 3
466#[doc(alias = "gsl_stats_wkurtosis")]
467pub fn wkurtosis(w: &[f64], wstride: usize, data: &[f64], stride: usize, n: usize) -> f64 {
468 unsafe { sys::gsl_stats_wkurtosis(w.as_ptr(), wstride, data.as_ptr(), stride, n) }
469}
470
471/// This function computes the weighted kurtosis of the dataset data using the given values of the
472/// weighted mean and weighted standard deviation, wmean and wsd.
473#[doc(alias = "gsl_stats_wkurtosis_m_sd")]
474pub fn wkurtosis_m_sd(
475 w: &[f64],
476 wstride: usize,
477 data: &[f64],
478 stride: usize,
479 n: usize,
480 wmean: f64,
481 wsd: f64,
482) -> f64 {
483 unsafe {
484 sys::gsl_stats_wkurtosis_m_sd(w.as_ptr(), wstride, data.as_ptr(), stride, n, wmean, wsd)
485 }
486}
487
488/// This function returns the maximum value in data, a dataset of length n with stride stride. The
489/// maximum value is defined as the value of the element x_i which satisfies x_i >= x_j for all j.
490///
491/// If you want instead to find the element with the largest absolute magnitude you will need to
492/// apply fabs or abs to your data before calling this function.
493#[doc(alias = "gsl_stats_max")]
494pub fn max(data: &[f64], stride: usize, n: usize) -> f64 {
495 unsafe { sys::gsl_stats_max(data.as_ptr(), stride, n) }
496}
497
498/// This function returns the minimum value in data, a dataset of length n with stride stride. The
499/// minimum value is defined as the value of the element x_i which satisfies x_i <= x_j for all j.
500///
501/// If you want instead to find the element with the smallest absolute magnitude you will need to
502/// apply fabs or abs to your data before calling this function.
503#[doc(alias = "gsl_stats_min")]
504pub fn min(data: &[f64], stride: usize, n: usize) -> f64 {
505 unsafe { sys::gsl_stats_min(data.as_ptr(), stride, n) }
506}
507
508/// This function finds both the minimum and maximum values min, max in data in a single pass.
509///
510/// Returns `(min, max)`.
511#[doc(alias = "gsl_stats_minmax")]
512pub fn minmax(data: &[f64], stride: usize, n: usize) -> (f64, f64) {
513 let mut min = 0.;
514 let mut max = 0.;
515 unsafe { sys::gsl_stats_minmax(&mut min, &mut max, data.as_ptr(), stride, n) };
516 (min, max)
517}
518
519/// This function returns the index of the maximum value in data, a dataset of length n with stride
520/// stride. The maximum value is defined as the value of the element x_i which satisfies x_i >= x_j
521/// for all j. When there are several equal maximum elements then the first one is chosen.
522#[doc(alias = "gsl_stats_max_index")]
523pub fn max_index(data: &[f64], stride: usize, n: usize) -> usize {
524 unsafe { sys::gsl_stats_max_index(data.as_ptr(), stride, n) }
525}
526
527/// This function returns the index of the minimum value in data, a dataset of length n with stride
528/// stride. The minimum value is defined as the value of the element x_i which satisfies x_i >= x_j
529/// for all j. When there are several equal minimum elements then the first one is chosen.
530#[doc(alias = "gsl_stats_min_index")]
531pub fn min_index(data: &[f64], stride: usize, n: usize) -> usize {
532 unsafe { sys::gsl_stats_min_index(data.as_ptr(), stride, n) }
533}
534
535/// This function returns the indexes min_index, max_index of the minimum and maximum values in data
536/// in a single pass.
537///
538/// Returns `(min_index, max_index)`.
539#[doc(alias = "gsl_stats_minmax_index")]
540pub fn minmax_index(data: &[f64], stride: usize, n: usize) -> (usize, usize) {
541 let mut min_index = 0;
542 let mut max_index = 0;
543 unsafe {
544 sys::gsl_stats_minmax_index(&mut min_index, &mut max_index, data.as_ptr(), stride, n)
545 };
546 (min_index, max_index)
547}
548
549/// This function returns the median value of sorted_data, a dataset of length n with stride stride.
550/// The elements of the array must be in ascending numerical order. There are no checks to see
551/// whether the data are sorted, so the function gsl_sort should always be used first.
552///
553/// When the dataset has an odd number of elements the median is the value of element (n-1)/2. When
554/// the dataset has an even number of elements the median is the mean of the two nearest middle
555/// values, elements (n-1)/2 and n/2. Since the algorithm for computing the median involves
556/// interpolation this function always returns a floating-point number, even for integer data types.
557#[doc(alias = "gsl_stats_median_from_sorted_data")]
558pub fn median_from_sorted_data(data: &[f64], stride: usize, n: usize) -> f64 {
559 unsafe { sys::gsl_stats_median_from_sorted_data(data.as_ptr(), stride, n) }
560}
561
562/// This function returns a quantile value of sorted_data, a double-precision array of length n with
563/// stride stride. The elements of the array must be in ascending numerical order. The quantile is
564/// determined by the f, a fraction between 0 and 1. For example, to compute the value of the 75th
565/// percentile f should have the value 0.75.
566///
567/// There are no checks to see whether the data are sorted, so the function gsl_sort should always
568/// be used first.
569///
570/// The quantile is found by interpolation, using the formula
571///
572/// quantile = (1 - \delta) x_i + \delta x_{i+1}
573///
574/// where i is floor((n - 1)f) and \delta is (n-1)f - i.
575///
576/// Thus the minimum value of the array (data[0*stride]) is given by f equal to zero, the maximum
577/// value (data[(n-1)*stride]) is given by f equal to one and the median value is given by f equal
578/// to 0.5. Since the algorithm for computing quantiles involves interpolation this function always
579/// returns a floating-point number, even for integer data types.
580#[doc(alias = "gsl_stats_quantile_from_sorted_data")]
581pub fn quantile_from_sorted_data(data: &[f64], stride: usize, n: usize, f: f64) -> f64 {
582 unsafe { sys::gsl_stats_quantile_from_sorted_data(data.as_ptr(), stride, n, f) }
583}