Math::...tistics(3pm)

πŸ’– Math::GSL::Statistics πŸ’–

okay so bestie, welcome to Math::GSL::Statistics β€” straight out of the User Contributed Perl Documentation, the 3pm section, the whole vibe. it's the statistical functions corner of GSL and honestly?? it eats. let's get into it 🫢


✨ NAME ✨

Math::GSL::Statistics β€” statistical functions, periodt. πŸ“Š


πŸŽ€ SYNOPSIS πŸŽ€

here's the whole lil demo, copy it exactly bestie:

use Math::GSL::Statistics qw /:all/;

my $data     = [17.2, 18.1, 16.5, 18.3, 12.6];
my $mean     = gsl_stats_mean($data, 1, 5);
my $variance = gsl_stats_variance($data, 1, 5);
my $largest  = gsl_stats_max($data, 1, 5);
my $smallest = gsl_stats_min($data, 1, 5);
print qq{
Dataset : @$data
Sample mean           $mean
Estimated variance    $variance
Largest value         $largest
Smallest value        $smallest
};

look at that. a whole dataset, five functions, one print statement. πŸ¦‹ so clean.


πŸ’… DESCRIPTION πŸ’…

here's the entire list of everything in this module, no skips, no skimming, we're doing all of it:

  • gsl_stats_mean($data, $stride, $n) β€” gives you back the arithmetic mean of the array reference $data, a dataset that's $n long with stride $stride. the arithmetic mean (aka the sample mean) goes by the name \Hat\mu and it's defined like this:
\Hat\mu = (1/N) \sum x_i

where the x_i are the elements of the dataset $data. and if your samples came from a gaussian distribution?? the variance of \Hat\mu is \sigma^2 / N. no cap. ✨

  • gsl_stats_variance($data, $stride, $n) β€” this one hands you the estimated variance (the sample variance) of data, an array reference of length $n with stride $stride. the estimated variance is written \Hat\sigma^2 and it's defined by:
\Hat\sigma^2 = (1/(N-1)) \sum (x_i - \Hat\mu)^2

where x_i are the elements of the dataset data. the tea on that normalization factor of 1/(N-1)? it falls out of deriving \Hat\sigma^2 as an unbiased estimator of the population variance \sigma^2. and for samples drawn from a gaussian distribution, the variance of \Hat\sigma^2 itself is 2 \sigma^4 / N. also this function computes the mean by calling gsl_stats_mean internally β€” so if you already have the mean computed, just pass it straight to gsl_stats_variance_m instead, save yourself the work. 😌

  • gsl_stats_sd($data, $stride, $n)
  • gsl_stats_sd_m($data, $stride, $n, $mean)

standard deviation is literally defined as the square root of the variance, so these two just return the square root of the corresponding variance functions up above. simple as. πŸ’…

  • gsl_stats_variance_with_fixed_mean($data, $stride, $n, $mean) β€” this computes the standard deviation of the array reference $data for a fixed population mean $mean. the result is the square root of the corresponding variance function.
  • gsl_stats_sd_with_fixed_mean($data, $stride, $n, $mean) β€” this computes an unbiased estimate of the variance of data when the population mean $mean of the underlying distribution is known ahead of time (a priori, if we're being fancy). in that case the estimator for the variance uses the factor 1/N and the sample mean \Hat\mu gets swapped out for the known population mean \mu:
\Hat\sigma^2 = (1/N) \sum (x_i - \mu)^2
  • gsl_stats_tss($data, $stride, $n)
  • gsl_stats_tss_m($data, $stride, $n, $mean)

these return the total sum of squares (TSS) of data about the mean. for gsl_stats_tss_m your supplied mean value gets used, and for gsl_stats_tss the mean is computed using gsl_stats_mean. so:

TSS =  \sum (x_i - mean)^2
  • gsl_stats_absdev($data, $stride, $n) β€” computes the absolute deviation from the mean of data, a dataset of length $n with stride $stride. here's the definition:
absdev  = (1/N) \sum |x_i - \Hat\mu|

where x_i are the elements of the array reference $data. ngl, absolute deviation from the mean is a more robust measure of how wide a distribution is than the variance is. this function computes the mean of data by calling gsl_stats_mean. πŸ‘‘

  • gsl_stats_skew($data, $stride, $n) β€” computes the skewness of $data, a dataset in the form of an array reference of length $n with stride $stride. skewness is defined as:
skew = (1/N) \sum ((x_i - \Hat\mu)/\Hat\sigma)^3

where x_i are the elements of the dataset $data. skewness is literally measuring how asymmetric the tails of a distribution are. this function gets the mean and estimated standard deviation of data by calling gsl_stats_mean and gsl_stats_sd. πŸ¦‹

  • gsl_stats_skew_m_sd($data, $stride, $n, $mean, $sd) β€” computes the skewness of the array reference $data but using the mean $mean and standard deviation $sd you hand it:
skew = (1/N) \sum ((x_i - mean)/sd)^3

these functions are a whole blessing if you've already computed the mean and standard deviation of $data and you do not want to recompute them. main character efficiency. ✨

  • gsl_stats_kurtosis($data, $stride, $n) β€” computes the kurtosis of data, an array reference of length $n with stride $stride. the kurtosis is defined as:
kurtosis = ((1/N) \sum ((x_i - \Hat\mu)/\Hat\sigma)^4)  - 3

kurtosis measures how sharply peaked a distribution is relative to its width. and it's normalized to zero for a gaussian distribution. cute. πŸŽ€

  • gsl_stats_kurtosis_m_sd($data, $stride, $n, $mean, $sd) β€” computes the kurtosis of the array reference $data using the mean $mean and standard deviation $sd you provide:
kurtosis = ((1/N) \sum ((x_i - mean)/sd)^4) - 3

this one's for when you've already computed the mean and standard deviation of data and you're trying to avoid recomputing them. we love a shortcut that's also correct. 😭

  • gsl_stats_lag1_autocorrelation($data, $stride, $n) β€” computes the lag-1 autocorrelation of the array reference data. it's giving:
a_1 = {\sum_{i = 1}^{n} (x_{i} - \Hat\mu) (x_{i-1} - \Hat\mu)
 \over
\sum_{i = 1}^{n} (x_{i} - \Hat\mu) (x_{i} - \Hat\mu)}
  • gsl_stats_lag1_autocorrelation_m($data, $stride, $n, $mean) β€” computes the lag-1 autocorrelation of the array reference $data using the mean $mean you give it.
  • gsl_stats_covariance($data1, $stride1, $data2, $stride2, $n) β€” computes the covariance of the array references $data1 and $data2, and heads up, both of them must be the same length $n:
covar = (1/(n - 1)) \sum_{i = 1}^{n} (x_i - \Hat x) (y_i - \Hat y)
  • gsl_stats_covariance_m($data1, $stride1, $data2, $stride2, $n, $mean1, $mean2) β€” computes the covariance of the array references $data1 and $data2 using the means you hand over, $mean1 and $mean2. useful when you already computed the means of $data1 and $data2 and you're dodging a recompute. πŸ’…
  • gsl_stats_correlation($data1, $stride1, $data2, $stride2, $n) β€” this efficiently computes the Pearson correlation coefficient between the array references $data1 and $data2, which again must both be the same length $n:
r = cov(x, y) / (\Hat\sigma_x \Hat\sigma_y)
  = {1/(n-1) \sum (x_i - \Hat x) (y_i - \Hat y)
     \over
     \sqrt{1/(n-1) \sum (x_i - \Hat x)^2} \sqrt{1/(n-1) \sum (y_i - \Hat y)^2}
    }
  • gsl_stats_variance_m($data, $stride, $n, $mean) β€” returns the sample variance of $data, an array reference, relative to the $mean value you give it. here \Hat\mu gets replaced by the mean you supply:
\Hat\sigma^2 = (1/(N-1)) \sum (x_i - mean)^2
  • gsl_stats_absdev_m($data, $stride, $n, $mean) β€” computes the absolute deviation of the dataset $data, an array reference, relative to the $mean you supply:
absdev  = (1/N) \sum |x_i - mean|

this one's handy if you already computed the mean of data (and wanna skip recomputing it), or if you want to calculate the absolute deviation relative to some other value β€” zero, or the median, whatever you're on. πŸ™ˆ

  • gsl_stats_wmean($w, $wstride, $data, $stride, $n) β€” returns the weighted mean of the dataset $data (array reference) with stride $stride and length $n, using the set of weights $w, which is an array reference with stride $wstride and length $n. the weighted mean is defined as:
\Hat\mu = (\sum w_i x_i) / (\sum w_i)
  • gsl_stats_wvariance($w, $wstride, $data, $stride, $n) β€” returns the estimated variance of the dataset $data β€” that's the dataset, with stride $stride and length $n β€” using the set of weights $w (an array reference) with stride $wstride and length $n. the estimated variance of a weighted dataset is defined as:
\Hat\sigma^2 = ((\sum w_i)/((\sum w_i)^2 - \sum (w_i^2))) \sum w_i (x_i - \Hat\mu)^2

and bestie, note that this expression reduces to an unweighted variance with the familiar 1/(N-1) factor when there are N equal non-zero weights. the math is mathing. πŸ”₯

  • gsl_stats_wvariance_m($w, $wstride, $data, $stride, $n, $wmean, $wsd) β€” returns the estimated variance of the weighted dataset $data (which is an array reference) using the given weighted mean $wmean.
  • gsl_stats_wsd($w, $wstride, $data, $stride, $n) β€” standard deviation is defined as the square root of the variance, so this returns the square root of the corresponding variance function gsl_stats_wvariance above it.
  • gsl_stats_wsd_m($w, $wstride, $data, $stride, $n, $wmean) β€” and this returns the square root of the corresponding variance function gsl_stats_wvariance_m above it.
  • gsl_stats_wvariance_with_fixed_mean($w, $wstride, $data, $stride, $n, $mean) β€” computes an unbiased estimate of the variance of the weighted dataset $data (an array reference) when the population mean $mean of the underlying distribution is known a priori. in that case the estimator for the variance replaces the sample mean \Hat\mu with the known population mean \mu:
\Hat\sigma^2 = (\sum w_i (x_i - \mu)^2) / (\sum w_i)
  • gsl_stats_wsd_with_fixed_mean($w, $wstride, $data, $stride, $n, $mean) β€” standard deviation is defined as the square root of the variance, so this returns the square root of the corresponding variance function above.
  • gsl_stats_wtss($w, $wstride, $data, $stride, $n)
  • gsl_stats_wtss_m($w, $wstride, $data, $stride, $n, $wmean)

these return the weighted total sum of squares (TSS) of data about the weighted mean. for gsl_stats_wtss_m your supplied $wmean value is used, and for gsl_stats_wtss it's computed using gsl_stats_wmean. so:

TSS =  \sum w_i (x_i - wmean)^2
  • gsl_stats_wabsdev($w, $wstride, $data, $stride, $n) β€” computes the weighted absolute deviation from the weighted mean of $data, which is an array reference. the absolute deviation from the mean is defined as:
absdev = (\sum w_i |x_i - \Hat\mu|) / (\sum w_i)
  • gsl_stats_wabsdev_m($w, $wstride, $data, $stride, $n, $wmean) β€” computes the absolute deviation of the weighted dataset $data (an array reference) about the given weighted mean $wmean. 🫢

okay that's the whole function list and we are not done yet bestie, there's more coming in the next part πŸ’œ don't go anywhere 🌸

  • gsl_stats_wskew($w, $wstride, $data, $stride, $n) β€” bestie this one computes the weighted skewness of your dataset $data, which is an array reference. the formula ate, here it is: skew = (\sum w_i ((x_i - xbar)/\sigma)^3) / (\sum w_i) ✨
  • gsl_stats_wskew_m_sd($w, $wstride, $data, $stride, $n, $wmean, $wsd) β€” same weighted skewness of $data, but you already did the homework and hand it $wmean and $wsd, the weighted mean and weighted standard deviation. so it doesn't have to recompute them, iykyk πŸ’…
  • gsl_stats_wkurtosis($w, $wstride, $data, $stride, $n) β€” this one gives you the weighted kurtosis of $data (array ref, obviously). the math, untouched: kurtosis = ((\sum w_i ((x_i - xbar)/sigma)^4) / (\sum w_i)) - 3 πŸŽ€
  • gsl_stats_wkurtosis_m_sd($w, $wstride, $data, $stride, $n, $wmean, $wsd) β€” weighted kurtosis of $data, an array reference, but using the values of the weighted mean and weighted standard deviation you already have, $wmean and $wsd. queen behavior, no wasted cycles 🫢
  • gsl_stats_pvariance($data, $stride, $n, $data2, $stride2, $n2) β€” fr the page just hands you the signature on this one and says nothing else, so that's the whole vibe πŸ’€
  • gsl_stats_ttest($data1, $stride1, $n1, $data2, $stride2, $n2) β€” same situation bestie, signature only, no notes, we move 😭
  • gsl_stats_max($data, $stride, $n) β€” returns the max value in the $data array reference, a dataset of length $n with stride $stride. the max is defined as the value of the element x_i that satisfies x_i >= x_j for all j πŸ‘‘ and real talk: if what you actually want is the element with the largest absolute magnitude, you gotta apply fabs or abs to your data before calling this. it won't do it for you ✨
  • gsl_stats_min($data, $stride, $n) β€” returns the min value in $data (an array reference), a dataset of length $n with stride $stride. the min is the value of the element x_i that satisfies x_i <= x_j for all j 🌸 same deal as max: want the element with the smallest absolute magnitude? apply fabs or abs to your data before calling this πŸ¦‹
  • gsl_stats_minmax($data, $stride, $n) β€” finds both the min and the max in $data (array reference, hello) in a single pass and returns them in that order. efficient icon ⭐
  • gsl_stats_max_index($data, $stride, $n) β€” returns the index of the max value in the $data array reference, a dataset of length $n with stride $stride. max is defined as the value of the element x_i satisfying x_i >= x_j for all j. and when several elements tie for max? the first one is chosen, no drama, first come first served πŸ’…
  • gsl_stats_min_index($data, $stride, $n) β€” returns the index of the min value in the $data array reference, a dataset of length $n with stride $stride. min is the value of the element x_i satisfying x_i <= x_j for all j. and again, several equal minimums means the first one wins πŸŽ€
  • gsl_stats_minmax_index($data, $stride, $n) β€” returns the indexes of both the min and the max values in $data, an array reference, in a single pass. they come back in that order πŸ“
  • gsl_stats_median_from_sorted_data($sorted_data, $stride, $n) β€” returns the median value of $sorted_data (array reference, you know the drill), a dataset of length $n with stride $stride. but listen: the elements of the array must be in ascending numerical order. there are no checks to see whether the data is sorted, so you should always use the function gsl_sort first. this function can be found in the Math::GSL::Sort module. when the dataset has an odd number of elements, the median is the value of element (n-1)/2. when it has an even number of elements, the median is the mean of the two nearest middle values, elements (n-1)/2 and n/2. and because computing the median involves interpolation, this function always returns a floating-point number β€” even for integer data types πŸ™ˆ
  • gsl_stats_quantile_from_sorted_data($sorted_data, $stride, $n, $f) β€” returns a quantile value of $sorted_data, a double-precision array reference of length $n with stride $stride. elements must be in ascending numerical order β€” again, no sorting checks, so hit gsl_sort first, always. this function lives in the Math::GSL::Sort module. the quantile is decided by $f, a fraction between 0 and 1. for example, to compute the value of the 75th percentile, $f should be 0.75. the quantile is found by interpolation, using the formula quantile = (1 - \delta) x_i + \delta x_{i+1} where i is floor((n - 1)f) and \delta is (n-1)f - i. so the minimum value of the array (data[0*stride]) is given by $f equal to zero, the maximum value (data[(n-1)*stride]) is given by $f equal to one, and the median value is given by $f equal to 0.5. and since the quantile algorithm involves interpolation, this function also always returns a floating-point number, even for integer data types πŸ’–

the following functions are simply variants for int and char of the last functions, so like... remixes. same song, different data type 🎢

  • gsl_stats_int_mean
  • gsl_stats_int_variance
  • gsl_stats_int_sd
  • gsl_stats_int_variance_with_fixed_mean
  • gsl_stats_int_sd_with_fixed_mean
  • gsl_stats_int_tss
  • gsl_stats_int_tss_m
  • gsl_stats_int_absdev
  • gsl_stats_int_skew
  • gsl_stats_int_kurtosis
  • gsl_stats_int_lag1_autocorrelation
  • gsl_stats_int_covariance
  • gsl_stats_int_correlation
  • gsl_stats_int_variance_m
  • gsl_stats_int_sd_m
  • gsl_stats_int_absdev_m
  • gsl_stats_int_skew_m_sd
  • gsl_stats_int_kurtosis_m_sd
  • gsl_stats_int_lag1_autocorrelation_m
  • gsl_stats_int_covariance_m
  • gsl_stats_int_pvariance
  • gsl_stats_int_ttest
  • gsl_stats_int_max
  • gsl_stats_int_min
  • gsl_stats_int_minmax
  • gsl_stats_int_max_index
  • gsl_stats_int_min_index
  • gsl_stats_int_minmax_index
  • gsl_stats_int_median_from_sorted_data
  • gsl_stats_int_quantile_from_sorted_data

and now the char chapter πŸ’œ

  • gsl_stats_char_mean
  • gsl_stats_char_variance
  • gsl_stats_char_sd
  • gsl_stats_char_variance_with_fixed_mean
  • gsl_stats_char_sd_with_fixed_mean
  • gsl_stats_char_tss
  • gsl_stats_char_tss_m
  • gsl_stats_char_absdev
  • gsl_stats_char_skew
  • gsl_stats_char_kurtosis
  • gsl_stats_char_lag1_autocorrelation
  • gsl_stats_char_covariance
  • gsl_stats_char_correlation
  • gsl_stats_char_variance_m
  • gsl_stats_char_sd_m
  • gsl_stats_char_absdev_m
  • gsl_stats_char_skew_m_sd
  • gsl_stats_char_kurtosis_m_sd
  • gsl_stats_char_lag1_autocorrelation_m
  • gsl_stats_char_covariance_m
  • gsl_stats_char_pvariance
  • gsl_stats_char_ttest
  • gsl_stats_char_max
  • gsl_stats_char_min
  • gsl_stats_char_minmax
  • gsl_stats_char_max_index
  • gsl_stats_char_min_index
  • gsl_stats_char_minmax_index
  • gsl_stats_char_median_from_sorted_data
  • gsl_stats_char_quantile_from_sorted_data

okay now the actually-important part, bestie, do not skip this πŸ™ˆ

you have to add the functions you want to use inside the qw /put_function_here /. like, that's the whole ritual. or, if you're feeling generous with your memory, you can also write:

use Math::GSL::Statistics qw/:all/;

and that grabs every available function of the module. there are other tags too! here's the complete list of all tags for this module:

  • all
  • int
  • char

for more information on the functions, the page points you to the GSL official documentation: http://www.gnu.org/software/gsl/manual/html_node/ πŸ“š


🌟 AUTHORS 🌟

  • Jonathan "Duke" Leto <jonathan@leto.net>
  • Thierry Moisan <thierry.moisan@gmail.com>

give them their flowers, they built the whole thing πŸ’


Copyright (C) 2008-2024 Jonathan "Duke" Leto and Thierry Moisan

this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's a wrap on this absolute unit of a page, bestie πŸ¦‹ this version shipped as part of perl v5.42.3, dated 2026-08-20, and lives under the name Math::...tistics(3pm) (yes that's really how it's abbreviated, iconic honestly). go sort your data first, periodt πŸ’…βœ¨

πŸ“¦ where this page comes from