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$nlong 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$nwith 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$datafor 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$meanof 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$nwith 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$nwith 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$databut using the mean$meanand standard deviation$sdyou 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$nwith 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$datausing the mean$meanand standard deviation$sdyou 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$datausing the mean$meanyou give it.gsl_stats_covariance($data1, $stride1, $data2, $stride2, $n)β computes the covariance of the array references$data1and$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$data1and$data2using the means you hand over,$mean1and$mean2. useful when you already computed the means of$data1and$data2and you're dodging a recompute. πgsl_stats_correlation($data1, $stride1, $data2, $stride2, $n)β this efficiently computes the Pearson correlation coefficient between the array references$data1and$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$meanvalue 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$meanyou 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$strideand length$n, using the set of weights$w, which is an array reference with stride$wstrideand 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$strideand length$nβ using the set of weights$w(an array reference) with stride$wstrideand 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 functiongsl_stats_wvarianceabove it.gsl_stats_wsd_m($w, $wstride, $data, $stride, $n, $wmean)β and this returns the square root of the corresponding variance functiongsl_stats_wvariance_mabove 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$meanof 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$wmeanand$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,$wmeanand$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$dataarray reference, a dataset of length$nwith stride$stride. the max is defined as the value of the elementx_ithat satisfiesx_i >= x_jfor alljπ and real talk: if what you actually want is the element with the largest absolute magnitude, you gotta applyfabsorabsto 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$nwith stride$stride. the min is the value of the elementx_ithat satisfiesx_i <= x_jfor alljπΈ same deal as max: want the element with the smallest absolute magnitude? applyfabsorabsto 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$dataarray reference, a dataset of length$nwith stride$stride. max is defined as the value of the elementx_isatisfyingx_i >= x_jfor allj. 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$dataarray reference, a dataset of length$nwith stride$stride. min is the value of the elementx_isatisfyingx_i <= x_jfor allj. 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$nwith 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 functiongsl_sortfirst. this function can be found in theMath::GSL::Sortmodule. 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)/2andn/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$nwith stride$stride. elements must be in ascending numerical order β again, no sorting checks, so hitgsl_sortfirst, always. this function lives in theMath::GSL::Sortmodule. the quantile is decided by$f, a fraction between 0 and 1. for example, to compute the value of the 75th percentile,$fshould be0.75. the quantile is found by interpolation, using the formulaquantile = (1 - \delta) x_i + \delta x_{i+1}whereiisfloor((n - 1)f)and\deltais(n-1)f - i. so the minimum value of the array (data[0*stride]) is given by$fequal to zero, the maximum value (data[(n-1)*stride]) is given by$fequal to one, and the median value is given by$fequal to0.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_meangsl_stats_int_variancegsl_stats_int_sdgsl_stats_int_variance_with_fixed_meangsl_stats_int_sd_with_fixed_meangsl_stats_int_tssgsl_stats_int_tss_mgsl_stats_int_absdevgsl_stats_int_skewgsl_stats_int_kurtosisgsl_stats_int_lag1_autocorrelationgsl_stats_int_covariancegsl_stats_int_correlationgsl_stats_int_variance_mgsl_stats_int_sd_mgsl_stats_int_absdev_mgsl_stats_int_skew_m_sdgsl_stats_int_kurtosis_m_sdgsl_stats_int_lag1_autocorrelation_mgsl_stats_int_covariance_mgsl_stats_int_pvariancegsl_stats_int_ttestgsl_stats_int_maxgsl_stats_int_mingsl_stats_int_minmaxgsl_stats_int_max_indexgsl_stats_int_min_indexgsl_stats_int_minmax_indexgsl_stats_int_median_from_sorted_datagsl_stats_int_quantile_from_sorted_data
and now the char chapter π
gsl_stats_char_meangsl_stats_char_variancegsl_stats_char_sdgsl_stats_char_variance_with_fixed_meangsl_stats_char_sd_with_fixed_meangsl_stats_char_tssgsl_stats_char_tss_mgsl_stats_char_absdevgsl_stats_char_skewgsl_stats_char_kurtosisgsl_stats_char_lag1_autocorrelationgsl_stats_char_covariancegsl_stats_char_correlationgsl_stats_char_variance_mgsl_stats_char_sd_mgsl_stats_char_absdev_mgsl_stats_char_skew_m_sdgsl_stats_char_kurtosis_m_sdgsl_stats_char_lag1_autocorrelation_mgsl_stats_char_covariance_mgsl_stats_char_pvariancegsl_stats_char_ttestgsl_stats_char_maxgsl_stats_char_mingsl_stats_char_minmaxgsl_stats_char_max_indexgsl_stats_char_min_indexgsl_stats_char_minmax_indexgsl_stats_char_median_from_sorted_datagsl_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:
allintchar
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 AND LICENSE π
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
- ubuntu stonking: libmath-gsl-perl 0.49-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms