Statistics: random number generators, PDF and CDF functions, and hypothesis tests. The module also includes some basic statistical functions such as mean, variance, skew, and etc.
``quantile x p`` returns the ``p`` quantile of the data ``x``. ``x`` does not need to be sorted beforehand. When computing several quantiles on the same data, it is more efficient to "pre-apply" the function, as in ``let f = quantile x in List.map f 0.1 ; 0.5 ``, in which case the data is sorted only once.
``sort x`` sorts the elements in the ``x`` in increasing order if ``inc = true``, otherwise in decreasing order if ``inc=false``. By default, ``inc`` is ``true``. Note a copy is returned, the original data is not modified.
The ranking order is from the smallest one to the largest. For example ``rank |54.; 74.; 55.; 86.; 56.|`` returns ``|1.; 4.; 2.; 5.; 3.|``. Note that the ranking starts with one!
``ties_strategy`` controls which ranks are assigned to equal values:
``Average`` the mean of ranks should be assigned to each value. Default.
``Min`` the minimum of ranks is assigned to each value.
``Max`` the maximum of ranks is assigned to each value.
Type for computed histograms, with optional weighted counts and normalized counts.
Sourceval histogram :
[ `Bins of float array| `N of int ]->?weights:float array->float array->histogram
``histogram bins x`` creates a histogram from values in ``x``. If bins matches `` `N n`` it will construct ``n`` equally spaced bins from the minimum to the maximum in ``x``. If bins matches `` `Bins b``, ``b`` is taken as the sorted array of boundaries of adjacent bin intervals. Bin boundaries are taken as left-inclusive, right-exclusive, except for the last bin which is also right-inclusive. Values outside the bins are dropped silently.
``histogram bins ~weights x`` creates a weighted histogram with the given ``weights`` which must match ``x`` in length. The bare counts are also provided.
Returns a histogram including the ``n+1`` bin boundaries, ``n`` counts and weighted counts if applicable, but without normalisation.
Sourceval histogram_sorted :
[ `Bins of float array| `N of int ]->?weights:float array->float array->histogram
``histogram_sorted bins x`` is like ``histogram`` but assumes that ``x`` is sorted already. This increases efficiency if there are less bins than data. Undefined results if ``x`` is not in fact sorted.
``normalize hist`` calculates a probability mass function using ``hist.weighted_counts`` if present, otherwise using ``hist.counts``. The result is stored in the ``normalised_counts`` field and sums to one.
``normalize_density hist`` calculates a probability density function using ``hist.weighted_counts`` if present, otherwise using ``hist.counts``. The result is normalized as a density that is piecewise constant over the bin intervals. That is, the sum over density times corresponding bin width is one. If bins are infinitely wide, their density is 0 and the sum over width times density of all finite bins is the total weight in the finite bins. The result is stored in the ``density`` field.
``ecdf x`` returns ``(x',f)`` which are the empirical cumulative distribution function ``f`` of ``x`` at points ``x'``. ``x'`` is just ``x`` sorted in increasing order with duplicates removed.
``tukey_fences ?k x`` returns a tuple of the lower and upper boundaries for values that are not outliers. ``k`` defaults to the standard coefficient of ``1.5``. For first and third quartiles ``Q1`` and `Q3`, the range is computed as follows:
``gaussian_kde x`` is a Gaussian kernel density estimator. The estimation of the pdf runs in `O(sample_size * n_points)`, and returns an array tuple ``(a, b)`` where ``a`` is a uniformly spaced points from the sample range at which the density function was estimated, and ``b`` is the estimates at these points.
Bandwidth selection rules is as follows: * Silverman: use `rule-of-thumb` for choosing the bandwidth. It defaults to 0.9 * min(SD, IQR / 1.34) * n^-0.2. * Scott: same as Silverman, but with a factor, equal to 1.06.
``z_test ~mu ~sigma ~alpha ~side x`` returns a test decision for the null hypothesis that the data ``x`` comes from a normal distribution with mean ``mu`` and a standard deviation ``sigma``, using the z-test of ``alpha`` significance level. The alternative hypothesis is that the mean is not ``mu``.
The result ``(h,p,z)`` : ``h`` is ``true`` if the test rejects the null hypothesis at the ``alpha`` significance level, and ``false`` otherwise. ``p`` is the p-value and ``z`` is the z-score.
``t_test ~mu ~alpha ~side x`` returns a test decision of one-sample t-test which is a parametric test of the location parameter when the population standard deviation is unknown. ``mu`` is population mean, ``alpha`` is the significance level.
``t_test_paired ~alpha ~side x y`` returns a test decision for the null hypothesis that the data in ``x – y`` comes from a normal distribution with mean equal to zero and unknown variance, using the paired-sample t-test.
``t_test_unpaired ~alpha ~side ~equal_var x y`` returns a test decision for the null hypothesis that the data in vectors ``x`` and ``y`` comes from independent random samples from normal distributions with equal means and equal but unknown variances, using the two-sample t-test. The alternative hypothesis is that the data in ``x`` and ``y`` comes from populations with unequal means.
``equal_var`` indicates whether two samples have the same variance. If the two variances are not the same, the test is referred to as Welche's t-test.
``ks_test ~alpha x f`` returns a test decision for the null hypothesis that the data in vector ``x`` comes from independent random samples of the distribution with CDF f. The alternative hypothesis is that the data in ``x`` comes from a different distribution.
The result ``(h,p,d)`` : ``h`` is ``true`` if the test rejects the null hypothesis at the ``alpha`` significance level, and ``false`` otherwise. ``p`` is the p-value and ``d`` is the Kolmogorov-Smirnov test statistic.
``ks2_test ~alpha x y`` returns a test decision for the null hypothesis that the data in vectors ``x`` and ``y`` come from independent random samples of the same distribution. The alternative hypothesis is that the data in ``x`` and ``y`` are sampled from different distributions.
The result ``(h,p,d)``: ``h`` is ``true`` if the test rejects the null hypothesis at the ``alpha`` significance level, and ``false`` otherwise. ``p`` is the p-value and ``d`` is the Kolmogorov-Smirnov test statistic.
``var_test ~alpha ~side ~variance x`` returns a test decision for the null hypothesis that the data in ``x`` comes from a normal distribution with input ``variance``, using the chi-square variance test. The alternative hypothesis is that ``x`` comes from a normal distribution with a different variance.
``jb_test ~alpha x`` returns a test decision for the null hypothesis that the data ``x`` comes from a normal distribution with an unknown mean and variance, using the Jarque-Bera test.
``fisher_test ~alpha ~side a b c d`` fisher's exact test for contingency table | ``a``, ``b`` | | ``c``, ``d`` |
The result ``(h,p,z)`` : ``h`` is ``true`` if the test rejects the null hypothesis at the ``alpha`` significance level, and ``false`` otherwise. ``p`` is the p-value and ``z`` is prior odds ratio.
``runs_test ~alpha ~v x`` returns a test decision for the null hypothesis that the data ``x`` comes in random order, against the alternative that they do not, by runnign Wald–Wolfowitz runs test. The test is based on the number of runs of consecutive values above or below the mean of ``x``. ``~v`` is the reference value, the default value is the median of ``x``.
``mannwhitneyu ~alpha ~side x y`` Computes the Mann-Whitney rank test on samples x and y. If length of each sample less than 10 and no ties, then using exact test (see paper Ying Kuen Cheung and Jerome H. Klotz (1997) The Mann Whitney Wilcoxon distribution using linked list Statistica Sinica 7 805-813), else usning asymptotic normal distribution.
The ``_rvs`` functions generate random numbers according to the specified distribution. ``_pdf`` are "density" functions that return the probability of the element specified by the arguments, while ``_cdf`` functions are cumulative distribution functions that return the probability of all elements less than or equal to the chosen element, and ``_sf`` functions are survival functions returning one minus the corresponding CDF function. `log` versions of functions return the result for the natural logarithm of a chosen element.
``binomial_rvs p n`` returns a random integer representing the number of successes in ``n`` trials with probability of success ``p`` on each trial.
Sourceval binomial_pdf : int ->p:float ->n:int -> float
``binomial_pdf k ~p ~n`` returns the binomially distributed probability of ``k`` successes in ``n`` trials with probability ``p`` of success on each trial.
Sourceval binomial_logpdf : int ->p:float ->n:int -> float
``binomial_logpdf k ~p ~n`` returns the log-binomially distributed probability of ``k`` successes in ``n`` trials with probability ``p`` of success on each trial.
Sourceval binomial_cdf : int ->p:float ->n:int -> float
``binomial_cdf k ~p ~n`` returns the binomially distributed cumulative probability of less than or equal to ``k`` successes in ``n`` trials, with probability ``p`` on each trial.
Sourceval binomial_logcdf : int ->p:float ->n:int -> float
``binomial_logcdf k ~p ~n`` returns the log-binomially distributed cumulative probability of less than or equal to ``k`` successes in ``n`` trials, with probability ``p`` on each trial.
Sourceval binomial_sf : int ->p:float ->n:int -> float
``binomial_sf k ~p ~n`` is the binomial survival function, i.e. ``1 - (binomial_cdf k ~p ~n)``.
Sourceval binomial_logsf : int ->p:float ->n:int -> float
``binomial_loggf k ~p ~n`` is the logbinomial survival function, i.e. ``1 - (binomial_logcdf k ~p ~n)``.
Sourceval hypergeometric_rvs : good:int ->bad:int ->sample:int -> int
``hypergeometric_rvs ~good ~bad ~sample`` returns a random hypergeometrically distributed integer representing the number of successes in a sample (without replacement) of size ``~sample`` from a population with ``~good`` successful elements and ``~bad`` unsuccessful elements.
Sourceval hypergeometric_pdf : int ->good:int ->bad:int ->sample:int -> float
``hypergeometric_pdf k ~good ~bad ~sample`` returns the hypergeometrically distributed probability of ``k`` successes in a sample (without replacement) of ``~sample`` elements from a population containing ``~good`` successful elements and ``~bad`` unsuccessful ones.
Sourceval hypergeometric_logpdf : int ->good:int ->bad:int ->sample:int -> float
``hypergeometric_logpdf k ~good ~bad ~sample`` returns a value equivalent to a log-transformed result from ``hypergeometric_pdf``.
Sourceval multinomial_rvs : int ->p:float array->int array
``multinomial_rvs n ~p`` generates random numbers of multinomial distribution from ``n`` trials. The probabilty mass function is as follows.
``p`` is the probabilty mass of ``k`` categories. If the elements in ``p`` do not sum to 1, the last element of the ``p`` array is not used and is replaced with the remaining probability left over from the earlier elements.
For implemantation, refer to :cite:`davis1993computer`.
Sourceval multinomial_pdf : int array->p:float array-> float
``multinomial_rvs x ~p`` return the probabilty of ``x`` given the probabilty mass of a multinomial distribution.
Sourceval multinomial_logpdf : int array->p:float array-> float
``multinomial_rvs x ~p`` returns the logarithm probabilty of ``x`` given the probabilty mass of a multinomial distribution.
``categorical_rvs p`` returns the value of a random variable which follows the categorical distribution. This is equavalent to only one trial from ``multinomial_rvs`` function, so it is just a simple wrapping.