# ``igraph`.`statistics``

module documentation
(source)

Statistics related stuff in igraph

Class | `FittedPowerLaw` | Result of fitting a power-law to a vector of samples
Class | `Histogram` | Generic histogram class for real numbers
Class | `RunningMean` | Running mean calculator.
Function | `mean` | Returns the mean of an iterable.
Function | `median` | Returns the median of an unsorted or sorted numeric vector.
Function | `percentile` | Returns the pth percentile of an unsorted or sorted numeric vector.
Function | `power_law_fit` | Fitting a power-law distribution to empirical data
Function | `quantile` | Returns the qth quantile of an unsorted or sorted numeric vector.
Function | `sd` | Returns the standard deviation of an iterable.
Function | `var` | Returns the variance of an iterable.

def mean(xs): (source)

Returns the mean of an iterable.

Example:

    >>> mean([1, 4, 7, 11])
    5.75

Parameters
xs | an iterable yielding numbers.

Returns
the mean of the numbers provided by the iterable.

See Also
RunningMean() if you also need the variance or the standard deviation

def median(xs, sort=True): (source)

Returns the median of an unsorted or sorted numeric vector.

Parameters
xs | the vector itself.
sort | whether to sort the vector. If you know that the vector is sorted already, pass `False` here.

Returns
the median, which will always be a float, even if the vector contained integers originally.

def percentile(xs, p=(25, 50, 75), sort=True): (source)

Returns the pth percentile of an unsorted or sorted numeric vector.

This is equivalent to calling quantile(xs, p/100.0); see `quantile` for more details on the calculation.

Example:

    >>> round(percentile([15, 20, 40, 35, 50], 40), 2)
    26.0
    >>> for perc in percentile([15, 20, 40, 35, 50], (0, 25, 50, 75, 100)):
    ...     print("%.2f" % perc)
    ...
    15.00
    17.50
    35.00
    45.00
    50.00

Parameters
xs | the vector itself.
p | the percentile we are looking for. It may also be a list if you want to calculate multiple quantiles with a single call. The default value calculates the 25th, 50th and 75th percentile.
sort | whether to sort the vector. If you know that the vector is sorted already, pass `False` here.

Returns
the pth percentile, which will always be a float, even if the vector contained integers originally. If p is a list, the result will also be a list containing the percentiles for each item in the list.

def power_law_fit(data, xmin=None, method='auto', p_precision=0.01): (source)

Fitting a power-law distribution to empirical data

References

- MEJ Newman: Power laws, Pareto distributions and Zipf's law. Contemporary Physics 46, 323-351 (2005)
- A Clauset, CR Shalizi, MEJ Newman: Power-law distributions in empirical data. E-print (2007). arXiv:0706.1062

Parameters
data | the data to fit, a list containing integer values
xmin | the lower bound for fitting the power-law. If `None`, the optimal xmin value will be estimated as well. Zero means that the smallest possible xmin value will be used.

method

the fitting method to use. The following methods are implemented so far:

- `continuous`, `hill`: exact maximum likelihood estimation when the input data comes from a continuous scale. This is known as the Hill estimator. The statistical error of this estimator is (alpha− 1) ⁄sqrt(n), where alpha is the estimated exponent and n is the number of data points above xmin. The estimator is known to exhibit a small finite sample-size bias of order O(n−1), which is small when n> 100. igraph will try to compensate for the finite sample size if n is small.
- `discrete`: exact maximum likelihood estimation when the input comes from a discrete scale (see Clauset et al. among the references).
- `auto`: exact maximum likelihood estimation where the continuous method is used if the input vector contains at least one fractional value and the discrete method is used if the input vector contains integers only.

p_precision | desired precision of the p-value calculation. The precision ultimately depends on the number of resampling attempts. The number of resampling trials is determined by 0.25 divided by the square of the required precision. For instance, a required precision of 0.01 means that 2500 samples will be drawn.

Returns
a `FittedPowerLaw` object. The fitted `xmin` value and the power-law exponent can be queried from the `xmin` and `alpha` properties of the returned object.

def quantile(xs, q=(0.25, 0.5, 0.75), sort=True): (source)

Returns the qth quantile of an unsorted or sorted numeric vector.

There are a number of different ways to calculate the sample quantile. The method implemented by igraph is the one recommended by NIST. First we calculate a rank n as q(N+1), where N is the number of items in xs, then we split n into its integer component k and decimal component d. If k <= 1, we return the first element; if k >= N, we return the last element, otherwise we return the linear interpolation between xs[k-1] and xs[k] using a factor d.

Example:

    >>> round(quantile([15, 20, 40, 35, 50], 0.4), 2)
    26.0

Parameters
xs | the vector itself.
q | the quantile we are looking for. It may also be a list if you want to calculate multiple quantiles with a single call. The default value calculates the 25th, 50th and 75th percentile.
sort | whether to sort the vector. If you know that the vector is sorted already, pass `False` here.

Returns
the qth quantile, which will always be a float, even if the vector contained integers originally. If q is a list, the result will also be a list containing the quantiles for each item in the list.

def sd(xs): (source)

Returns the standard deviation of an iterable.

Example:

    >>> sd([1, 4, 7, 11])       #doctest:+ELLIPSIS
    4.2720...

Parameters
xs | an iterable yielding numbers.

Returns
the standard deviation of the numbers provided by the iterable.

See Also
RunningMean() if you also need the mean

def var(xs): (source)

Returns the variance of an iterable.

Example:

    >>> var([1, 4, 8, 11])            #doctest:+ELLIPSIS
    19.333333...

Parameters
xs | an iterable yielding numbers.

Returns
the variance of the numbers provided by the iterable.

See Also
RunningMean() if you also need the mean