def gmean(a, axis=<NUM_LIT:0>, dtype=None):

if not isinstance(a, np.ndarray):  <EOL><INDENT>log_a = np.log(np.array(a, dtype=dtype))<EOL><DEDENT>elif dtype:  <EOL><INDENT>if isinstance(a,np.ma.MaskedArray):<EOL><INDENT>log_a = np.log(np.ma.asarray(a, dtype=dtype))<EOL><DEDENT>else:<EOL><INDENT>log_a = np.log(np.asarray(a, dtype=dtype))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>log_a = np.log(a)<EOL><DEDENT>return np.exp(log_a.mean(axis=axis))<EOL>

Compute the geometric mean along the specified axis.

Returns the geometric average of the array elements.
That is:  n-th root of (x1 * x2 * ... * xn)

Parameters
----------
a : array_like
    Input array or object that can be converted to an array.
axis : int, optional, default axis=0
    Axis along which the geometric mean is computed.
dtype : dtype, optional
    Type of the returned array and of the accumulator in which the
    elements are summed. If dtype is not specified, it defaults to the
    dtype of a, unless a has an integer dtype with a precision less than
    that of the default platform integer. In that case, the default
    platform integer is used.

Returns
-------
gmean : ndarray
    see dtype parameter above

See Also
--------
numpy.mean : Arithmetic average
numpy.average : Weighted average
hmean : Harmonic mean

Notes
-----
The geometric average is computed over a single dimension of the input
array, axis=0 by default, or all values in the array if axis=None.
float64 intermediate and return values are used for integer inputs.

Use masked arrays to ignore any non-finite values in the input or that
arise in the calculations such as Not a Number and infinity because masked
arrays automatically mask any non-finite values.


def hmean(a, axis=<NUM_LIT:0>, dtype=None):

if not isinstance(a, np.ndarray):<EOL><INDENT>a = np.array(a, dtype=dtype)<EOL><DEDENT>if np.all(a > <NUM_LIT:0>):  <EOL><INDENT>if isinstance(a, np.ma.MaskedArray):<EOL><INDENT>size = a.count(axis)<EOL><DEDENT>else:<EOL><INDENT>if axis is None:<EOL><INDENT>a = a.ravel()<EOL>size = a.shape[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>size = a.shape[axis]<EOL><DEDENT><DEDENT>return size / np.sum(<NUM_LIT:1.0>/a, axis=axis, dtype=dtype)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>

Calculates the harmonic mean along the specified axis.

That is:  n / (1/x1 + 1/x2 + ... + 1/xn)

Parameters
----------
a : array_like
    Input array, masked array or object that can be converted to an array.
axis : int, optional, default axis=0
    Axis along which the harmonic mean is computed.
dtype : dtype, optional
    Type of the returned array and of the accumulator in which the
    elements are summed. If `dtype` is not specified, it defaults to the
    dtype of `a`, unless `a` has an integer `dtype` with a precision less
    than that of the default platform integer. In that case, the default
    platform integer is used.

Returns
-------
hmean : ndarray
    see `dtype` parameter above

See Also
--------
numpy.mean : Arithmetic average
numpy.average : Weighted average
gmean : Geometric mean

Notes
-----
The harmonic mean is computed over a single dimension of the input
array, axis=0 by default, or all values in the array if axis=None.
float64 intermediate and return values are used for integer inputs.

Use masked arrays to ignore any non-finite values in the input or that
arise in the calculations such as Not a Number and infinity.


a, axis = _chk_asarray(a, axis)<EOL>scores = np.unique(np.ravel(a))       <EOL>testshape = list(a.shape)<EOL>testshape[axis] = <NUM_LIT:1><EOL>oldmostfreq = np.zeros(testshape, dtype=a.dtype)<EOL>oldcounts = np.zeros(testshape)<EOL>for score in scores:<EOL><INDENT>template = (a == score)<EOL>counts = np.expand_dims(np.sum(template, axis),axis)<EOL>mostfrequent = np.where(counts > oldcounts, score, oldmostfreq)<EOL>oldcounts = np.maximum(counts, oldcounts)<EOL>oldmostfreq = mostfrequent<EOL><DEDENT>return mostfrequent, oldcounts<EOL>

Returns an array of the modal (most common) value in the passed array.

If there is more than one such value, only the first is returned.
The bin-count for the modal bins is also returned.

Parameters
----------
a : array_like
    n-dimensional array of which to find mode(s).
axis : int, optional
    Axis along which to operate. Default is 0, i.e. the first axis.

Returns
-------
vals : ndarray
    Array of modal values.
counts : ndarray
    Array of counts for each mode.

Examples
--------
>>> a = np.array([[6, 8, 3, 0],
                  [3, 2, 1, 7],
                  [8, 1, 8, 4],
                  [5, 3, 0, 5],
                  [4, 7, 5, 9]])
>>> from scipy import stats
>>> stats.mode(a)
(array([[ 3.,  1.,  0.,  0.]]), array([[ 1.,  1.,  1.,  1.]]))

To get mode of whole array, specify axis=None:

>>> stats.mode(a, axis=None)
(array([ 3.]), array([ 3.]))


def mask_to_limits(a, limits, inclusive):

lower_limit, upper_limit = limits<EOL>lower_include, upper_include = inclusive<EOL>am = ma.MaskedArray(a)<EOL>if lower_limit is not None:<EOL><INDENT>if lower_include:<EOL><INDENT>am = ma.masked_less(am, lower_limit)<EOL><DEDENT>else:<EOL><INDENT>am = ma.masked_less_equal(am, lower_limit)<EOL><DEDENT><DEDENT>if upper_limit is not None:<EOL><INDENT>if upper_include:<EOL><INDENT>am = ma.masked_greater(am, upper_limit)<EOL><DEDENT>else:<EOL><INDENT>am = ma.masked_greater_equal(am, upper_limit)<EOL><DEDENT><DEDENT>if am.count() == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>return am<EOL>

Mask an array for values outside of given limits.

    This is primarily a utility function.

    Parameters
    ----------
    a : array
    limits : (float or None, float or None)
        A tuple consisting of the (lower limit, upper limit).  Values in the
        input array less than the lower limit or greater than the upper limit
        will be masked out. None implies no limit.
    inclusive : (bool, bool)
        A tuple consisting of the (lower flag, upper flag).  These flags
        determine whether values exactly equal to lower or upper are allowed.

    Returns
    -------
    A MaskedArray.

    Raises
    ------
    A ValueError if there are no values within the given limits.


def tmean(a, limits=None, inclusive=(True, True)):

a = asarray(a)<EOL>if limits is None:<EOL><INDENT>return np.mean(a, None)<EOL><DEDENT>am = mask_to_limits(a.ravel(), limits, inclusive)<EOL>return am.mean()<EOL>

Compute the trimmed mean.

This function finds the arithmetic mean of given values, ignoring values
outside the given `limits`.

Parameters
----------
a : array_like
    Array of values.
limits : None or (lower limit, upper limit), optional
    Values in the input array less than the lower limit or greater than the
    upper limit will be ignored.  When limits is None (default), then all
    values are used.  Either of the limit values in the tuple can also be
    None representing a half-open interval.
inclusive : (bool, bool), optional
    A tuple consisting of the (lower flag, upper flag).  These flags
    determine whether values exactly equal to the lower or upper limits
    are included.  The default value is (True, True).

Returns
-------
tmean : float


def tvar(a, limits=None, inclusive=(True, True)):

a = asarray(a)<EOL>a = a.astype(float).ravel()<EOL>if limits is None:<EOL><INDENT>n = len(a)<EOL>return a.var()*(n/(n-<NUM_LIT:1.>))<EOL><DEDENT>am = mask_to_limits(a, limits, inclusive)<EOL>return masked_var(am)<EOL>

Compute the trimmed variance

This function computes the sample variance of an array of values,
while ignoring values which are outside of given `limits`.

Parameters
----------
a : array_like
    Array of values.
limits : None or (lower limit, upper limit), optional
    Values in the input array less than the lower limit or greater than the
    upper limit will be ignored. When limits is None, then all values are
    used. Either of the limit values in the tuple can also be None
    representing a half-open interval.  The default value is None.
inclusive : (bool, bool), optional
    A tuple consisting of the (lower flag, upper flag).  These flags
    determine whether values exactly equal to the lower or upper limits
    are included.  The default value is (True, True).

Returns
-------
tvar : float
    Trimmed variance.

Notes
-----
`tvar` computes the unbiased sample variance, i.e. it uses a correction
factor ``n / (n - 1)``.


def tmin(a, lowerlimit=None, axis=<NUM_LIT:0>, inclusive=True):

a, axis = _chk_asarray(a, axis)<EOL>am = mask_to_limits(a, (lowerlimit, None), (inclusive, False))<EOL>return ma.minimum.reduce(am, axis)<EOL>

Compute the trimmed minimum

This function finds the miminum value of an array `a` along the
specified axis, but only considering values greater than a specified
lower limit.

Parameters
----------
a : array_like
    array of values
lowerlimit : None or float, optional
    Values in the input array less than the given limit will be ignored.
    When lowerlimit is None, then all values are used. The default value
    is None.
axis : None or int, optional
    Operate along this axis.  None means to use the flattened array and
    the default is zero
inclusive : {True, False}, optional
    This flag determines whether values exactly equal to the lower limit
    are included.  The default value is True.

Returns
-------
tmin : float


def tmax(a, upperlimit=None, axis=<NUM_LIT:0>, inclusive=True):

a, axis = _chk_asarray(a, axis)<EOL>am = mask_to_limits(a, (None, upperlimit), (False, inclusive))<EOL>return ma.maximum.reduce(am, axis)<EOL>

Compute the trimmed maximum

This function computes the maximum value of an array along a given axis,
while ignoring values larger than a specified upper limit.

Parameters
----------
a : array_like
    array of values
upperlimit : None or float, optional
    Values in the input array greater than the given limit will be ignored.
    When upperlimit is None, then all values are used. The default value
    is None.
axis : None or int, optional
    Operate along this axis.  None means to use the flattened array and
    the default is zero.
inclusive : {True, False}, optional
    This flag determines whether values exactly equal to the upper limit
    are included.  The default value is True.

Returns
-------
tmax : float


def tstd(a, limits=None, inclusive=(True, True)):

return np.sqrt(tvar(a, limits, inclusive))<EOL>

Compute the trimmed sample standard deviation

This function finds the sample standard deviation of given values,
ignoring values outside the given `limits`.

Parameters
----------
a : array_like
    array of values
limits : None or (lower limit, upper limit), optional
    Values in the input array less than the lower limit or greater than the
    upper limit will be ignored. When limits is None, then all values are
    used. Either of the limit values in the tuple can also be None
    representing a half-open interval.  The default value is None.
inclusive : (bool, bool), optional
    A tuple consisting of the (lower flag, upper flag).  These flags
    determine whether values exactly equal to the lower or upper limits
    are included.  The default value is (True, True).

Returns
-------
tstd : float

Notes
-----
`tstd` computes the unbiased sample standard deviation, i.e. it uses a
correction factor ``n / (n - 1)``.


def tsem(a, limits=None, inclusive=(True, True)):

a = np.asarray(a).ravel()<EOL>if limits is None:<EOL><INDENT>return a.std(ddof=<NUM_LIT:1>) / np.sqrt(a.size)<EOL><DEDENT>am = mask_to_limits(a, limits, inclusive)<EOL>sd = np.sqrt(masked_var(am))<EOL>return sd / np.sqrt(am.count())<EOL>

Compute the trimmed standard error of the mean.

This function finds the standard error of the mean for given
values, ignoring values outside the given `limits`.

Parameters
----------
a : array_like
    array of values
limits : None or (lower limit, upper limit), optional
    Values in the input array less than the lower limit or greater than the
    upper limit will be ignored. When limits is None, then all values are
    used. Either of the limit values in the tuple can also be None
    representing a half-open interval.  The default value is None.
inclusive : (bool, bool), optional
    A tuple consisting of the (lower flag, upper flag).  These flags
    determine whether values exactly equal to the lower or upper limits
    are included.  The default value is (True, True).

Returns
-------
tsem : float

Notes
-----
`tsem` uses unbiased sample standard deviation, i.e. it uses a
correction factor ``n / (n - 1)``.


def moment(a, moment=<NUM_LIT:1>, axis=<NUM_LIT:0>):

a, axis = _chk_asarray(a, axis)<EOL>if moment == <NUM_LIT:1>:<EOL><INDENT>shape = list(a.shape)<EOL>del shape[axis]<EOL>if shape:<EOL><INDENT>return np.zeros(shape, dtype=float)<EOL><DEDENT>else:<EOL><INDENT>return np.float64(<NUM_LIT:0.0>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>mn = np.expand_dims(np.mean(a,axis), axis)<EOL>s = np.power((a-mn), moment)<EOL>return np.mean(s, axis)<EOL><DEDENT>

Calculates the nth moment about the mean for a sample.

Generally used to calculate coefficients of skewness and
kurtosis.

Parameters
----------
a : array_like
   data
moment : int
   order of central moment that is returned
axis : int or None
   Axis along which the central moment is computed. If None, then the data
   array is raveled. The default axis is zero.

Returns
-------
n-th central moment : ndarray or float
   The appropriate moment along the given axis or over all values if axis
   is None. The denominator for the moment calculation is the number of
   observations, no degrees of freedom correction is done.


a, axis = _chk_asarray(a, axis)<EOL>return a.std(axis)/a.mean(axis)<EOL>

Computes the coefficient of variation, the ratio of the biased standard
deviation to the mean.

Parameters
----------
a : array_like
    Input array.
axis : int or None
    Axis along which to calculate the coefficient of variation.

References
----------
.. [1] Zwillinger, D. and Kokoska, S. (2000). CRC Standard
   Probability and Statistics Tables and Formulae. Chapman & Hall: New
   York. 2000.


def skew(a, axis=<NUM_LIT:0>, bias=True):

a, axis = _chk_asarray(a,axis)<EOL>n = a.shape[axis]<EOL>m2 = moment(a, <NUM_LIT:2>, axis)<EOL>m3 = moment(a, <NUM_LIT:3>, axis)<EOL>zero = (m2 == <NUM_LIT:0>)<EOL>vals = np.where(zero, <NUM_LIT:0>, m3 / m2**<NUM_LIT>)<EOL>if not bias:<EOL><INDENT>can_correct = (n > <NUM_LIT:2>) & (m2 > <NUM_LIT:0>)<EOL>if can_correct.any():<EOL><INDENT>m2 = np.extract(can_correct, m2)<EOL>m3 = np.extract(can_correct, m3)<EOL>nval = np.sqrt((n-<NUM_LIT:1.0>)*n)/(n-<NUM_LIT>)*m3/m2**<NUM_LIT><EOL>np.place(vals, can_correct, nval)<EOL><DEDENT><DEDENT>if vals.ndim == <NUM_LIT:0>:<EOL><INDENT>return vals.item()<EOL><DEDENT>return vals<EOL>

Computes the skewness of a data set.

For normally distributed data, the skewness should be about 0. A skewness
value > 0 means that there is more weight in the left tail of the
distribution. The function `skewtest` can be used to determine if the
skewness value is close enough to 0, statistically speaking.

Parameters
----------
a : ndarray
    data
axis : int or None
    axis along which skewness is calculated
bias : bool
    If False, then the calculations are corrected for statistical bias.

Returns
-------
skewness : ndarray
    The skewness of values along an axis, returning 0 where all values are
    equal.

References
----------
[CRCProbStat2000]_ Section 2.2.24.1

.. [CRCProbStat2000] Zwillinger, D. and Kokoska, S. (2000). CRC Standard
   Probability and Statistics Tables and Formulae. Chapman & Hall: New
   York. 2000.


def kurtosis(a, axis=<NUM_LIT:0>, fisher=True, bias=True):

a, axis = _chk_asarray(a, axis)<EOL>n = a.shape[axis]<EOL>m2 = moment(a,<NUM_LIT:2>,axis)<EOL>m4 = moment(a,<NUM_LIT:4>,axis)<EOL>zero = (m2 == <NUM_LIT:0>)<EOL>olderr = np.seterr(all='<STR_LIT:ignore>')<EOL>try:<EOL><INDENT>vals = np.where(zero, <NUM_LIT:0>, m4 / m2**<NUM_LIT>)<EOL><DEDENT>finally:<EOL><INDENT>np.seterr(**olderr)<EOL><DEDENT>if not bias:<EOL><INDENT>can_correct = (n > <NUM_LIT:3>) & (m2 > <NUM_LIT:0>)<EOL>if can_correct.any():<EOL><INDENT>m2 = np.extract(can_correct, m2)<EOL>m4 = np.extract(can_correct, m4)<EOL>nval = <NUM_LIT:1.0>/(n-<NUM_LIT:2>)/(n-<NUM_LIT:3>)*((n*n-<NUM_LIT:1.0>)*m4/m2**<NUM_LIT>-<NUM_LIT:3>*(n-<NUM_LIT:1>)**<NUM_LIT>)<EOL>np.place(vals, can_correct, nval+<NUM_LIT>)<EOL><DEDENT><DEDENT>if vals.ndim == <NUM_LIT:0>:<EOL><INDENT>vals = vals.item()  <EOL><DEDENT>if fisher:<EOL><INDENT>return vals - <NUM_LIT:3><EOL><DEDENT>else:<EOL><INDENT>return vals<EOL><DEDENT>

Computes the kurtosis (Fisher or Pearson) of a dataset.

Kurtosis is the fourth central moment divided by the square of the
variance. If Fisher's definition is used, then 3.0 is subtracted from
the result to give 0.0 for a normal distribution.

If bias is False then the kurtosis is calculated using k statistics to
eliminate bias coming from biased moment estimators

Use `kurtosistest` to see if result is close enough to normal.

Parameters
----------
a : array
    data for which the kurtosis is calculated
axis : int or None
    Axis along which the kurtosis is calculated
fisher : bool
    If True, Fisher's definition is used (normal ==> 0.0). If False,
    Pearson's definition is used (normal ==> 3.0).
bias : bool
    If False, then the calculations are corrected for statistical bias.

Returns
-------
kurtosis : array
    The kurtosis of values along an axis. If all values are equal,
    return -3 for Fisher's definition and 0 for Pearson's definition.

References
----------
.. [1] Zwillinger, D. and Kokoska, S. (2000). CRC Standard
   Probability and Statistics Tables and Formulae. Chapman & Hall: New
   York. 2000.


def describe(a, axis=<NUM_LIT:0>, ddof=<NUM_LIT:1>):

a, axis = _chk_asarray(a, axis)<EOL>n = a.shape[axis]<EOL>mm = (np.min(a, axis=axis), np.max(a, axis=axis))<EOL>m = np.mean(a, axis=axis)<EOL>v = np.var(a, axis=axis, ddof=ddof)<EOL>sk = skew(a, axis)<EOL>kurt = kurtosis(a, axis)<EOL>return _DescribeResult(n, mm, m, v, sk, kurt)<EOL>

Computes several descriptive statistics of the passed array.

Parameters
----------
a : array_like
   Input data.
axis : int, optional
   Axis along which statistics are calculated.  If axis is None, then data
   array is raveled.  The default axis is zero.
ddof : int, optional
    Delta degrees of freedom.  Default is 1.

Returns
-------
nobs : int
   Number of observations (length of data along `axis`).
minmax: tuple of ndarrays or floats
   Minimum and maximum value of data array.
mean : ndarray or float
   Arithmetic mean of data along axis.
variance : ndarray or float
   Unbiased variance of the data along axis, denominator is number of
   observations minus one.
skewness : ndarray or float
   Biased skewness, based on moment calculations with denominator equal to
   the number of observations, i.e. no degrees of freedom correction.
kurtosis : ndarray or float
   Biased kurtosis (Fisher).  The kurtosis is normalized so that it is
   zero for the normal distribution.  No degrees of freedom or bias
   correction is used.

See Also
--------
skew, kurtosis


x = np.asarray(x)<EOL>n = float(x.size)<EOL>if n == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>mu = x.mean()<EOL>diffx = x - mu<EOL>skewness = (<NUM_LIT:1> / n * np.sum(diffx**<NUM_LIT:3>)) / (<NUM_LIT:1> / n * np.sum(diffx**<NUM_LIT:2>))**(<NUM_LIT:3> / <NUM_LIT>)<EOL>kurtosis = (<NUM_LIT:1> / n * np.sum(diffx**<NUM_LIT:4>)) / (<NUM_LIT:1> / n * np.sum(diffx**<NUM_LIT:2>))**<NUM_LIT:2><EOL>jb_value = n / <NUM_LIT:6> * (skewness**<NUM_LIT:2> + (kurtosis - <NUM_LIT:3>)**<NUM_LIT:2> / <NUM_LIT:4>)<EOL>p = <NUM_LIT:1> - distributions.chi2.cdf(jb_value, <NUM_LIT:2>)<EOL>return jb_value, p<EOL>

Perform the Jarque-Bera goodness of fit test on sample data.

The Jarque-Bera test tests whether the sample data has the skewness and
kurtosis matching a normal distribution.

Note that this test only works for a large enough number of data samples
(>2000) as the test statistic asymptotically has a Chi-squared distribution
with 2 degrees of freedom.

Parameters
----------
x : array_like
    Observations of a random variable.

Returns
-------
jb_value : float
    The test statistic.
p : float
    The p-value for the hypothesis test.

References
----------
.. [1] Jarque, C. and Bera, A. (1980) "Efficient tests for normality,
       homoscedasticity and serial independence of regression residuals",
       6 Econometric Letters 255-259.

Examples
--------
>>> from scipy import stats
>>> np.random.seed(987654321)
>>> x = np.random.normal(0, 1, 100000)
>>> y = np.random.rayleigh(1, 100000)
>>> stats.jarque_bera(x)
(4.7165707989581342, 0.09458225503041906)
>>> stats.jarque_bera(y)
(6713.7098548143422, 0.0)


items, inv = np.unique(a, return_inverse=True)<EOL>freq = np.bincount(inv)<EOL>return np.array([items, freq]).T<EOL>

Returns a 2-D array of item frequencies.

Parameters
----------
a : (N,) array_like
    Input array.

Returns
-------
itemfreq : (K, 2) ndarray
    A 2-D frequency table.  Column 1 contains sorted, unique values from
    `a`, column 2 contains their respective counts.

Examples
--------
>>> a = np.array([1, 1, 5, 0, 1, 2, 2, 0, 1, 4])
>>> stats.itemfreq(a)
array([[ 0.,  2.],
       [ 1.,  4.],
       [ 2.,  2.],
       [ 4.,  1.],
       [ 5.,  1.]])
>>> np.bincount(a)
array([2, 4, 2, 0, 1, 1])

>>> stats.itemfreq(a/10.)
array([[ 0. ,  2. ],
       [ 0.1,  4. ],
       [ 0.2,  2. ],
       [ 0.4,  1. ],
       [ 0.5,  1. ]])


def scoreatpercentile(a, per, limit=(), interpolation_method='<STR_LIT>',<EOL>axis=None):

<EOL>a = np.asarray(a)<EOL>if a.size == <NUM_LIT:0>:<EOL><INDENT>if np.isscalar(per):<EOL><INDENT>return np.nan<EOL><DEDENT>else:<EOL><INDENT>return np.ones(np.asarray(per).shape, dtype=np.float64) * np.nan<EOL><DEDENT><DEDENT>if limit:<EOL><INDENT>a = a[(limit[<NUM_LIT:0>] <= a) & (a <= limit[<NUM_LIT:1>])]<EOL><DEDENT>sorted = np.sort(a, axis=axis)<EOL>if axis is None:<EOL><INDENT>axis = <NUM_LIT:0><EOL><DEDENT>return _compute_qth_percentile(sorted, per, interpolation_method, axis)<EOL>

Calculate the score at a given percentile of the input sequence.

For example, the score at `per=50` is the median. If the desired quantile
lies between two data points, we interpolate between them, according to
the value of `interpolation`. If the parameter `limit` is provided, it
should be a tuple (lower, upper) of two values.

Parameters
----------
a : array_like
    A 1-D array of values from which to extract score.
per : array_like
    Percentile(s) at which to extract score.  Values should be in range
    [0,100].
limit : tuple, optional
    Tuple of two scalars, the lower and upper limits within which to
    compute the percentile. Values of `a` outside
    this (closed) interval will be ignored.
interpolation : {'fraction', 'lower', 'higher'}, optional
    This optional parameter specifies the interpolation method to use,
    when the desired quantile lies between two data points `i` and `j`

      - fraction: ``i + (j - i) * fraction`` where ``fraction`` is the
        fractional part of the index surrounded by ``i`` and ``j``.
      - lower: ``i``.
      - higher: ``j``.

axis : int, optional
    Axis along which the percentiles are computed. The default (None)
    is to compute the median along a flattened version of the array.

Returns
-------
score : float or ndarray
    Score at percentile(s).

See Also
--------
percentileofscore, numpy.percentile

Notes
-----
This function will become obsolete in the future.
For Numpy 1.9 and higher, `numpy.percentile` provides all the functionality
that `scoreatpercentile` provides.  And it's significantly faster.
Therefore it's recommended to use `numpy.percentile` for users that have
numpy >= 1.9.

Examples
--------
>>> from scipy import stats
>>> a = np.arange(100)
>>> stats.scoreatpercentile(a, 50)
49.5


def percentileofscore(a, score, kind='<STR_LIT>'):

a = np.array(a)<EOL>n = len(a)<EOL>if kind == '<STR_LIT>':<EOL><INDENT>if not(np.any(a == score)):<EOL><INDENT>a = np.append(a, score)<EOL>a_len = np.array(list(range(len(a))))<EOL><DEDENT>else:<EOL><INDENT>a_len = np.array(list(range(len(a)))) + <NUM_LIT:1.0><EOL><DEDENT>a = np.sort(a)<EOL>idx = [a == score]<EOL>pct = (np.mean(a_len[idx]) / n) * <NUM_LIT><EOL>return pct<EOL><DEDENT>elif kind == '<STR_LIT:strict>':<EOL><INDENT>return sum(a < score) / float(n) * <NUM_LIT:100><EOL><DEDENT>elif kind == '<STR_LIT>':<EOL><INDENT>return sum(a <= score) / float(n) * <NUM_LIT:100><EOL><DEDENT>elif kind == '<STR_LIT>':<EOL><INDENT>return (sum(a < score) + sum(a <= score)) * <NUM_LIT:50> / float(n)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>

The percentile rank of a score relative to a list of scores.

A `percentileofscore` of, for example, 80% means that 80% of the
scores in `a` are below the given score. In the case of gaps or
ties, the exact definition depends on the optional keyword, `kind`.

Parameters
----------
a : array_like
    Array of scores to which `score` is compared.
score : int or float
    Score that is compared to the elements in `a`.
kind : {'rank', 'weak', 'strict', 'mean'}, optional
    This optional parameter specifies the interpretation of the
    resulting score:

    - "rank": Average percentage ranking of score.  In case of
              multiple matches, average the percentage rankings of
              all matching scores.
    - "weak": This kind corresponds to the definition of a cumulative
              distribution function.  A percentileofscore of 80%
              means that 80% of values are less than or equal
              to the provided score.
    - "strict": Similar to "weak", except that only values that are
                strictly less than the given score are counted.
    - "mean": The average of the "weak" and "strict" scores, often used in
              testing.  See

              http://en.wikipedia.org/wiki/Percentile_rank

Returns
-------
pcos : float
    Percentile-position of score (0-100) relative to `a`.

See Also
--------
numpy.percentile

Examples
--------
Three-quarters of the given values lie below a given score:

>>> percentileofscore([1, 2, 3, 4], 3)
75.0

With multiple matches, note how the scores of the two matches, 0.6
and 0.8 respectively, are averaged:

>>> percentileofscore([1, 2, 3, 3, 4], 3)
70.0

Only 2/5 values are strictly less than 3:

>>> percentileofscore([1, 2, 3, 3, 4], 3, kind='strict')
40.0

But 4/5 values are less than or equal to 3:

>>> percentileofscore([1, 2, 3, 3, 4], 3, kind='weak')
80.0

The average between the weak and the strict scores is

>>> percentileofscore([1, 2, 3, 3, 4], 3, kind='mean')
60.0


<EOL>n = np.searchsorted(np.sort(a), bins)<EOL>n = np.concatenate([n, [len(a)]])<EOL>return n[<NUM_LIT:1>:]-n[:-<NUM_LIT:1>]<EOL>

Compute histogram using divisions in bins.

Count the number of times values from array `a` fall into
numerical ranges defined by `bins`.  Range x is given by
bins[x] <= range_x < bins[x+1] where x =0,N and N is the
length of the `bins` array.  The last range is given by
bins[N] <= range_N < infinity.  Values less than bins[0] are
not included in the histogram.

Parameters
----------
a : array_like of rank 1
    The array of values to be assigned into bins
bins : array_like of rank 1
    Defines the ranges of values to use during histogramming.

Returns
-------
histogram2 : ndarray of rank 1
    Each value represents the occurrences for a given bin (range) of
    values.


def histogram(a, numbins=<NUM_LIT:10>, defaultlimits=None, weights=None, printextras=False):

a = np.ravel(a)<EOL>if defaultlimits is None:<EOL><INDENT>data_min = a.min()<EOL>data_max = a.max()<EOL>s = (data_max - data_min) / (<NUM_LIT> * (numbins - <NUM_LIT:1.>))<EOL>defaultlimits = (data_min - s, data_max + s)<EOL><DEDENT>hist, bin_edges = np.histogram(a, bins=numbins, range=defaultlimits,<EOL>weights=weights)<EOL>hist = np.array(hist, dtype=float)<EOL>binsize = bin_edges[<NUM_LIT:1>] - bin_edges[<NUM_LIT:0>]<EOL>extrapoints = len([v for v in a<EOL>if defaultlimits[<NUM_LIT:0>] > v or v > defaultlimits[<NUM_LIT:1>]])<EOL>if extrapoints > <NUM_LIT:0> and printextras:<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>% extrapoints)<EOL><DEDENT>return (hist, defaultlimits[<NUM_LIT:0>], binsize, extrapoints)<EOL>

Separates the range into several bins and returns the number of instances
in each bin.

Parameters
----------
a : array_like
    Array of scores which will be put into bins.
numbins : int, optional
    The number of bins to use for the histogram. Default is 10.
defaultlimits : tuple (lower, upper), optional
    The lower and upper values for the range of the histogram.
    If no value is given, a range slightly larger than the range of the
    values in a is used. Specifically ``(a.min() - s, a.max() + s)``,
    where ``s = (1/2)(a.max() - a.min()) / (numbins - 1)``.
weights : array_like, optional
    The weights for each value in `a`. Default is None, which gives each
    value a weight of 1.0
printextras : bool, optional
    If True, if there are extra points (i.e. the points that fall outside
    the bin limits) a warning is raised saying how many of those points
    there are.  Default is False.

Returns
-------
histogram : ndarray
    Number of points (or sum of weights) in each bin.
low_range : float
    Lowest value of histogram, the lower limit of the first bin.
binsize : float
    The size of the bins (all bins have the same size).
extrapoints : int
    The number of points outside the range of the histogram.

See Also
--------
numpy.histogram

Notes
-----
This histogram is based on numpy's histogram but has a larger range by
default if default limits is not set.


def cumfreq(a, numbins=<NUM_LIT:10>, defaultreallimits=None, weights=None):

h,l,b,e = histogram(a, numbins, defaultreallimits, weights=weights)<EOL>cumhist = np.cumsum(h*<NUM_LIT:1>, axis=<NUM_LIT:0>)<EOL>return cumhist,l,b,e<EOL>

Returns a cumulative frequency histogram, using the histogram function.

Parameters
----------
a : array_like
    Input array.
numbins : int, optional
    The number of bins to use for the histogram. Default is 10.
defaultlimits : tuple (lower, upper), optional
    The lower and upper values for the range of the histogram.
    If no value is given, a range slightly larger than the range of the
    values in `a` is used. Specifically ``(a.min() - s, a.max() + s)``,
    where ``s = (1/2)(a.max() - a.min()) / (numbins - 1)``.
weights : array_like, optional
    The weights for each value in `a`. Default is None, which gives each
    value a weight of 1.0

Returns
-------
cumfreq : ndarray
    Binned values of cumulative frequency.
lowerreallimit : float
    Lower real limit
binsize : float
    Width of each bin.
extrapoints : int
    Extra points.

Examples
--------
>>> import scipy.stats as stats
>>> x = [1, 4, 2, 1, 3, 1]
>>> cumfreqs, lowlim, binsize, extrapoints = stats.cumfreq(x, numbins=4)
>>> cumfreqs
array([ 3.,  4.,  5.,  6.])
>>> cumfreqs, lowlim, binsize, extrapoints = \
...     stats.cumfreq(x, numbins=4, defaultreallimits=(1.5, 5))
>>> cumfreqs
array([ 1.,  2.,  3.,  3.])
>>> extrapoints
3


def relfreq(a, numbins=<NUM_LIT:10>, defaultreallimits=None, weights=None):

h, l, b, e = histogram(a, numbins, defaultreallimits, weights=weights)<EOL>h = np.array(h / float(np.array(a).shape[<NUM_LIT:0>]))<EOL>return h, l, b, e<EOL>

Returns a relative frequency histogram, using the histogram function.

Parameters
----------
a : array_like
    Input array.
numbins : int, optional
    The number of bins to use for the histogram. Default is 10.
defaultreallimits : tuple (lower, upper), optional
    The lower and upper values for the range of the histogram.
    If no value is given, a range slightly larger than the range of the
    values in a is used. Specifically ``(a.min() - s, a.max() + s)``,
    where ``s = (1/2)(a.max() - a.min()) / (numbins - 1)``.
weights : array_like, optional
    The weights for each value in `a`. Default is None, which gives each
    value a weight of 1.0

Returns
-------
relfreq : ndarray
    Binned values of relative frequency.
lowerreallimit : float
    Lower real limit
binsize : float
    Width of each bin.
extrapoints : int
    Extra points.

Examples
--------
>>> import scipy.stats as stats
>>> a = np.array([1, 4, 2, 1, 3, 1])
>>> relfreqs, lowlim, binsize, extrapoints = stats.relfreq(a, numbins=4)
>>> relfreqs
array([ 0.5       ,  0.16666667,  0.16666667,  0.16666667])
>>> np.sum(relfreqs)  # relative frequencies should add up to 1
0.99999999999999989


TINY = np.sqrt(np.finfo(float).eps)<EOL>arrays = []<EOL>for arg in args:<EOL><INDENT>a = np.asarray(arg)<EOL>n = len(a)<EOL>mu = np.mean(a)<EOL>sq = (a - mu)**<NUM_LIT:2><EOL>sumsq = sq.sum()<EOL>t = ((n - <NUM_LIT>) * n * sq - <NUM_LIT:0.5> * sumsq) / ((n - <NUM_LIT:1>) * (n - <NUM_LIT:2>))<EOL>var = sumsq / (n - <NUM_LIT:1>)<EOL>if abs(var - np.mean(t)) > TINY:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>arrays.append(t)<EOL><DEDENT>if len(arrays) < <NUM_LIT:2> or all(x.shape == arrays[<NUM_LIT:0>].shape for x in arrays[<NUM_LIT:1>:]):<EOL><INDENT>dt = None<EOL><DEDENT>else:<EOL><INDENT>dt = object<EOL><DEDENT>return np.array(arrays, dtype=dt)<EOL>

Computes the O'Brien transform on input data (any number of arrays).

Used to test for homogeneity of variance prior to running one-way stats.
Each array in ``*args`` is one level of a factor.
If `f_oneway` is run on the transformed data and found significant,
the variances are unequal.  From Maxwell and Delaney [1]_, p.112.

Parameters
----------
args : tuple of array_like
    Any number of arrays.

Returns
-------
obrientransform : ndarray
    Transformed data for use in an ANOVA.  The first dimension
    of the result corresponds to the sequence of transformed
    arrays.  If the arrays given are all 1-D of the same length,
    the return value is a 2-D array; otherwise it is a 1-D array
    of type object, with each element being an ndarray.

References
----------
.. [1] S. E. Maxwell and H. D. Delaney, "Designing Experiments and
       Analyzing Data: A Model Comparison Perspective", Wadsworth, 1990.

Examples
--------
We'll test the following data sets for differences in their variance.

>>> x = [10, 11, 13, 9, 7, 12, 12, 9, 10]
>>> y = [13, 21, 5, 10, 8, 14, 10, 12, 7, 15]

Apply the O'Brien transform to the data.

>>> tx, ty = obrientransform(x, y)

Use `scipy.stats.f_oneway` to apply a one-way ANOVA test to the
transformed data.

>>> from scipy.stats import f_oneway
>>> F, p = f_oneway(tx, ty)
>>> p
0.1314139477040335

If we require that ``p < 0.05`` for significance, we cannot conclude
that the variances are different.


def signaltonoise(a, axis=<NUM_LIT:0>, ddof=<NUM_LIT:0>):

a = np.asanyarray(a)<EOL>m = a.mean(axis)<EOL>sd = a.std(axis=axis, ddof=ddof)<EOL>return np.where(sd == <NUM_LIT:0>, <NUM_LIT:0>, m/sd)<EOL>

The signal-to-noise ratio of the input data.

Returns the signal-to-noise ratio of `a`, here defined as the mean
divided by the standard deviation.

Parameters
----------
a : array_like
    An array_like object containing the sample data.
axis : int or None, optional
    If axis is equal to None, the array is first ravel'd. If axis is an
    integer, this is the axis over which to operate. Default is 0.
ddof : int, optional
    Degrees of freedom correction for standard deviation. Default is 0.

Returns
-------
s2n : ndarray
    The mean to standard deviation ratio(s) along `axis`, or 0 where the
    standard deviation is 0.


def sem(a, axis=<NUM_LIT:0>, ddof=<NUM_LIT:1>):

a, axis = _chk_asarray(a, axis)<EOL>n = a.shape[axis]<EOL>s = np.std(a, axis=axis, ddof=ddof) / np.sqrt(n)<EOL>return s<EOL>

Calculates the standard error of the mean (or standard error of
measurement) of the values in the input array.

Parameters
----------
a : array_like
    An array containing the values for which the standard error is
    returned.
axis : int or None, optional.
    If axis is None, ravel `a` first. If axis is an integer, this will be
    the axis over which to operate. Defaults to 0.
ddof : int, optional
    Delta degrees-of-freedom. How many degrees of freedom to adjust
    for bias in limited samples relative to the population estimate
    of variance. Defaults to 1.

Returns
-------
s : ndarray or float
    The standard error of the mean in the sample(s), along the input axis.

Notes
-----
The default value for `ddof` is different to the default (0) used by other
ddof containing routines, such as np.std nd stats.nanstd.

Examples
--------
Find standard error along the first axis:

>>> from scipy import stats
>>> a = np.arange(20).reshape(5,4)
>>> stats.sem(a)
array([ 2.8284,  2.8284,  2.8284,  2.8284])

Find standard error across the whole array, using n degrees of freedom:

>>> stats.sem(a, axis=None, ddof=0)
1.2893796958227628


def zscore(a, axis=<NUM_LIT:0>, ddof=<NUM_LIT:0>):

a = np.asanyarray(a)<EOL>mns = a.mean(axis=axis)<EOL>sstd = a.std(axis=axis, ddof=ddof)<EOL>if axis and mns.ndim < a.ndim:<EOL><INDENT>return ((a - np.expand_dims(mns, axis=axis)) /<EOL>np.expand_dims(sstd,axis=axis))<EOL><DEDENT>else:<EOL><INDENT>return (a - mns) / sstd<EOL><DEDENT>

Calculates the z score of each value in the sample, relative to the sample
mean and standard deviation.

Parameters
----------
a : array_like
    An array like object containing the sample data.
axis : int or None, optional
    If `axis` is equal to None, the array is first raveled. If `axis` is
    an integer, this is the axis over which to operate. Default is 0.
ddof : int, optional
    Degrees of freedom correction in the calculation of the
    standard deviation. Default is 0.

Returns
-------
zscore : array_like
    The z-scores, standardized by mean and standard deviation of input
    array `a`.

Notes
-----
This function preserves ndarray subclasses, and works also with
matrices and masked arrays (it uses `asanyarray` instead of `asarray`
for parameters).

Examples
--------
>>> a = np.array([ 0.7972,  0.0767,  0.4383,  0.7866,  0.8091,  0.1954,
                   0.6307, 0.6599,  0.1065,  0.0508])
>>> from scipy import stats
>>> stats.zscore(a)
array([ 1.1273, -1.247 , -0.0552,  1.0923,  1.1664, -0.8559,  0.5786,
        0.6748, -1.1488, -1.3324])

Computing along a specified axis, using n-1 degrees of freedom (``ddof=1``)
to calculate the standard deviation:

>>> b = np.array([[ 0.3148,  0.0478,  0.6243,  0.4608],
                  [ 0.7149,  0.0775,  0.6072,  0.9656],
                  [ 0.6341,  0.1403,  0.9759,  0.4064],
                  [ 0.5918,  0.6948,  0.904 ,  0.3721],
                  [ 0.0921,  0.2481,  0.1188,  0.1366]])
>>> stats.zscore(b, axis=1, ddof=1)
array([[-0.19264823, -1.28415119,  1.07259584,  0.40420358],
       [ 0.33048416, -1.37380874,  0.04251374,  1.00081084],
       [ 0.26796377, -1.12598418,  1.23283094, -0.37481053],
       [-0.22095197,  0.24468594,  1.19042819, -1.21416216],
       [-0.82780366,  1.4457416 , -0.43867764, -0.1792603 ]])


def zmap(scores, compare, axis=<NUM_LIT:0>, ddof=<NUM_LIT:0>):

scores, compare = map(np.asanyarray, [scores, compare])<EOL>mns = compare.mean(axis=axis)<EOL>sstd = compare.std(axis=axis, ddof=ddof)<EOL>if axis and mns.ndim < compare.ndim:<EOL><INDENT>return ((scores - np.expand_dims(mns, axis=axis)) /<EOL>np.expand_dims(sstd,axis=axis))<EOL><DEDENT>else:<EOL><INDENT>return (scores - mns) / sstd<EOL><DEDENT>

Calculates the relative z-scores.

Returns an array of z-scores, i.e., scores that are standardized to zero
mean and unit variance, where mean and variance are calculated from the
comparison array.

Parameters
----------
scores : array_like
    The input for which z-scores are calculated.
compare : array_like
    The input from which the mean and standard deviation of the
    normalization are taken; assumed to have the same dimension as
    `scores`.
axis : int or None, optional
    Axis over which mean and variance of `compare` are calculated.
    Default is 0.
ddof : int, optional
    Degrees of freedom correction in the calculation of the
    standard deviation. Default is 0.

Returns
-------
zscore : array_like
    Z-scores, in the same shape as `scores`.

Notes
-----
This function preserves ndarray subclasses, and works also with
matrices and masked arrays (it uses `asanyarray` instead of `asarray`
for parameters).

Examples
--------
>>> a = [0.5, 2.0, 2.5, 3]
>>> b = [0, 1, 2, 3, 4]
>>> zmap(a, b)
array([-1.06066017,  0.        ,  0.35355339,  0.70710678])


def threshold(a, threshmin=None, threshmax=None, newval=<NUM_LIT:0>):

a = asarray(a).copy()<EOL>mask = zeros(a.shape, dtype=bool)<EOL>if threshmin is not None:<EOL><INDENT>mask |= (a < threshmin)<EOL><DEDENT>if threshmax is not None:<EOL><INDENT>mask |= (a > threshmax)<EOL><DEDENT>a[mask] = newval<EOL>return a<EOL>

Clip array to a given value.

Similar to numpy.clip(), except that values less than `threshmin` or
greater than `threshmax` are replaced by `newval`, instead of by
`threshmin` and `threshmax` respectively.

Parameters
----------
a : array_like
    Data to threshold.
threshmin : float, int or None, optional
    Minimum threshold, defaults to None.
threshmax : float, int or None, optional
    Maximum threshold, defaults to None.
newval : float or int, optional
    Value to put in place of values in `a` outside of bounds.
    Defaults to 0.

Returns
-------
out : ndarray
    The clipped input array, with values less than `threshmin` or
    greater than `threshmax` replaced with `newval`.

Examples
--------
>>> a = np.array([9, 9, 6, 3, 1, 6, 1, 0, 0, 8])
>>> from scipy import stats
>>> stats.threshold(a, threshmin=2, threshmax=8, newval=-1)
array([-1, -1,  6,  3, -1,  6, -1, -1, -1,  8])


def sigmaclip(a, low=<NUM_LIT>, high=<NUM_LIT>):

c = np.asarray(a).ravel()<EOL>delta = <NUM_LIT:1><EOL>while delta:<EOL><INDENT>c_std = c.std()<EOL>c_mean = c.mean()<EOL>size = c.size<EOL>critlower = c_mean - c_std*low<EOL>critupper = c_mean + c_std*high<EOL>c = c[(c > critlower) & (c < critupper)]<EOL>delta = size-c.size<EOL><DEDENT>return c, critlower, critupper<EOL>

Iterative sigma-clipping of array elements.

The output array contains only those elements of the input array `c`
that satisfy the conditions ::

    mean(c) - std(c)*low < c < mean(c) + std(c)*high

Starting from the full sample, all elements outside the critical range are
removed. The iteration continues with a new critical range until no
elements are outside the range.

Parameters
----------
a : array_like
    Data array, will be raveled if not 1-D.
low : float, optional
    Lower bound factor of sigma clipping. Default is 4.
high : float, optional
    Upper bound factor of sigma clipping. Default is 4.

Returns
-------
c : ndarray
    Input array with clipped elements removed.
critlower : float
    Lower threshold value use for clipping.
critlupper : float
    Upper threshold value use for clipping.

Examples
--------
>>> a = np.concatenate((np.linspace(9.5,10.5,31), np.linspace(0,20,5)))
>>> fact = 1.5
>>> c, low, upp = sigmaclip(a, fact, fact)
>>> c
array([  9.96666667,  10.        ,  10.03333333,  10.        ])
>>> c.var(), c.std()
(0.00055555555555555165, 0.023570226039551501)
>>> low, c.mean() - fact*c.std(), c.min()
(9.9646446609406727, 9.9646446609406727, 9.9666666666666668)
>>> upp, c.mean() + fact*c.std(), c.max()
(10.035355339059327, 10.035355339059327, 10.033333333333333)

>>> a = np.concatenate((np.linspace(9.5,10.5,11),
    np.linspace(-100,-50,3)))
>>> c, low, upp = sigmaclip(a, 1.8, 1.8)
>>> (c == np.linspace(9.5,10.5,11)).all()
True


def trimboth(a, proportiontocut, axis=<NUM_LIT:0>):

a = np.asarray(a)<EOL>if axis is None:<EOL><INDENT>a = a.ravel()<EOL>axis = <NUM_LIT:0><EOL><DEDENT>nobs = a.shape[axis]<EOL>lowercut = int(proportiontocut * nobs)<EOL>uppercut = nobs - lowercut<EOL>if (lowercut >= uppercut):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>sl = [slice(None)] * a.ndim<EOL>sl[axis] = slice(lowercut, uppercut)<EOL>return a[sl]<EOL>

Slices off a proportion of items from both ends of an array.

Slices off the passed proportion of items from both ends of the passed
array (i.e., with `proportiontocut` = 0.1, slices leftmost 10% **and**
rightmost 10% of scores).  You must pre-sort the array if you want
'proper' trimming.  Slices off less if proportion results in a
non-integer slice index (i.e., conservatively slices off
`proportiontocut`).

Parameters
----------
a : array_like
    Data to trim.
proportiontocut : float
    Proportion (in range 0-1) of total data set to trim of each end.
axis : int or None, optional
    Axis along which the observations are trimmed. The default is to trim
    along axis=0. If axis is None then the array will be flattened before
    trimming.

Returns
-------
out : ndarray
    Trimmed version of array `a`.

See Also
--------
trim_mean

Examples
--------
>>> from scipy import stats
>>> a = np.arange(20)
>>> b = stats.trimboth(a, 0.1)
>>> b.shape
(16,)


def trim1(a, proportiontocut, tail='<STR_LIT:right>'):

a = asarray(a)<EOL>if tail.lower() == '<STR_LIT:right>':<EOL><INDENT>lowercut = <NUM_LIT:0><EOL>uppercut = len(a) - int(proportiontocut*len(a))<EOL><DEDENT>elif tail.lower() == '<STR_LIT:left>':<EOL><INDENT>lowercut = int(proportiontocut*len(a))<EOL>uppercut = len(a)<EOL><DEDENT>return a[lowercut:uppercut]<EOL>

Slices off a proportion of items from ONE end of the passed array
distribution.

If `proportiontocut` = 0.1, slices off 'leftmost' or 'rightmost'
10% of scores.  Slices off LESS if proportion results in a non-integer
slice index (i.e., conservatively slices off `proportiontocut` ).

Parameters
----------
a : array_like
    Input array
proportiontocut : float
    Fraction to cut off of 'left' or 'right' of distribution
tail : {'left', 'right'}, optional
    Defaults to 'right'.

Returns
-------
trim1 : ndarray
    Trimmed version of array `a`


def trim_mean(a, proportiontocut, axis=<NUM_LIT:0>):

a = np.asarray(a)<EOL>if axis is None:<EOL><INDENT>nobs = a.size<EOL><DEDENT>else:<EOL><INDENT>nobs = a.shape[axis]<EOL><DEDENT>lowercut = int(proportiontocut * nobs)<EOL>uppercut = nobs - lowercut - <NUM_LIT:1><EOL>if (lowercut > uppercut):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>try:<EOL><INDENT>atmp = np.partition(a, (lowercut, uppercut), axis)<EOL><DEDENT>except AttributeError:<EOL><INDENT>atmp = np.sort(a, axis)<EOL><DEDENT>newa = trimboth(atmp, proportiontocut, axis=axis)<EOL>return np.mean(newa, axis=axis)<EOL>

Return mean of array after trimming distribution from both lower and upper
tails.

If `proportiontocut` = 0.1, slices off 'leftmost' and 'rightmost' 10% of
scores. Slices off LESS if proportion results in a non-integer slice
index (i.e., conservatively slices off `proportiontocut` ).

Parameters
----------
a : array_like
    Input array
proportiontocut : float
    Fraction to cut off of both tails of the distribution
axis : int or None, optional
    Axis along which the trimmed means are computed. The default is axis=0.
    If axis is None then the trimmed mean will be computed for the
    flattened array.

Returns
-------
trim_mean : ndarray
    Mean of trimmed array.

See Also
--------
trimboth

Examples
--------
>>> from scipy import stats
>>> x = np.arange(20)
>>> stats.trim_mean(x, 0.1)
9.5
>>> x2 = x.reshape(5, 4)
>>> x2
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])
>>> stats.trim_mean(x2, 0.25)
array([  8.,   9.,  10.,  11.])
>>> stats.trim_mean(x2, 0.25, axis=1)
array([  1.5,   5.5,   9.5,  13.5,  17.5])


args = [np.asarray(arg, dtype=float) for arg in args]<EOL>na = len(args)    <EOL>alldata = np.concatenate(args)<EOL>bign = len(alldata)<EOL>sstot = ss(alldata) - (square_of_sums(alldata) / float(bign))<EOL>ssbn = <NUM_LIT:0><EOL>for a in args:<EOL><INDENT>ssbn += square_of_sums(a) / float(len(a))<EOL><DEDENT>ssbn -= (square_of_sums(alldata) / float(bign))<EOL>sswn = sstot - ssbn<EOL>dfbn = na - <NUM_LIT:1><EOL>dfwn = bign - na<EOL>msb = ssbn / float(dfbn)<EOL>msw = sswn / float(dfwn)<EOL>f = msb / msw<EOL>prob = special.fdtrc(dfbn, dfwn, f)   <EOL>return f, prob<EOL>

Performs a 1-way ANOVA.

The one-way ANOVA tests the null hypothesis that two or more groups have
the same population mean.  The test is applied to samples from two or
more groups, possibly with differing sizes.

Parameters
----------
sample1, sample2, ... : array_like
    The sample measurements for each group.

Returns
-------
F-value : float
    The computed F-value of the test.
p-value : float
    The associated p-value from the F-distribution.

Notes
-----
The ANOVA test has important assumptions that must be satisfied in order
for the associated p-value to be valid.

1. The samples are independent.
2. Each sample is from a normally distributed population.
3. The population standard deviations of the groups are all equal.  This
   property is known as homoscedasticity.

If these assumptions are not true for a given set of data, it may still be
possible to use the Kruskal-Wallis H-test (`scipy.stats.kruskal`) although
with some loss of power.

The algorithm is from Heiman[2], pp.394-7.

References
----------
.. [1] Lowry, Richard.  "Concepts and Applications of Inferential
       Statistics". Chapter 14.
       http://faculty.vassar.edu/lowry/ch14pt1.html

.. [2] Heiman, G.W.  Research Methods in Statistics. 2002.


<EOL>x = np.asarray(x)<EOL>y = np.asarray(y)<EOL>n = len(x)<EOL>mx = x.mean()<EOL>my = y.mean()<EOL>xm, ym = x-mx, y-my<EOL>r_num = np.add.reduce(xm * ym)<EOL>r_den = np.sqrt(ss(xm) * ss(ym))<EOL>r = r_num / r_den<EOL>r = max(min(r, <NUM_LIT:1.0>), -<NUM_LIT:1.0>)<EOL>df = n-<NUM_LIT:2><EOL>if abs(r) == <NUM_LIT:1.0>:<EOL><INDENT>prob = <NUM_LIT:0.0><EOL><DEDENT>else:<EOL><INDENT>t_squared = r*r * (df / ((<NUM_LIT:1.0> - r) * (<NUM_LIT:1.0> + r)))<EOL>prob = betai(<NUM_LIT:0.5>*df, <NUM_LIT:0.5>, df / (df + t_squared))<EOL><DEDENT>return r, prob<EOL>

Calculates a Pearson correlation coefficient and the p-value for testing
non-correlation.

The Pearson correlation coefficient measures the linear relationship
between two datasets. Strictly speaking, Pearson's correlation requires
that each dataset be normally distributed. Like other correlation
coefficients, this one varies between -1 and +1 with 0 implying no
correlation. Correlations of -1 or +1 imply an exact linear
relationship. Positive correlations imply that as x increases, so does
y. Negative correlations imply that as x increases, y decreases.

The p-value roughly indicates the probability of an uncorrelated system
producing datasets that have a Pearson correlation at least as extreme
as the one computed from these datasets. The p-values are not entirely
reliable but are probably reasonable for datasets larger than 500 or so.

Parameters
----------
x : (N,) array_like
    Input
y : (N,) array_like
    Input

Returns
-------
(Pearson's correlation coefficient,
 2-tailed p-value)

References
----------
http://www.statsoft.com/textbook/glosp.html#Pearson%20Correlation


hypergeom = distributions.hypergeom<EOL>c = np.asarray(table, dtype=np.int64)  <EOL>if not c.shape == (<NUM_LIT:2>, <NUM_LIT:2>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if np.any(c < <NUM_LIT:0>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if <NUM_LIT:0> in c.sum(axis=<NUM_LIT:0>) or <NUM_LIT:0> in c.sum(axis=<NUM_LIT:1>):<EOL><INDENT>return np.nan, <NUM_LIT:1.0><EOL><DEDENT>if c[<NUM_LIT:1>,<NUM_LIT:0>] > <NUM_LIT:0> and c[<NUM_LIT:0>,<NUM_LIT:1>] > <NUM_LIT:0>:<EOL><INDENT>oddsratio = c[<NUM_LIT:0>,<NUM_LIT:0>] * c[<NUM_LIT:1>,<NUM_LIT:1>] / float(c[<NUM_LIT:1>,<NUM_LIT:0>] * c[<NUM_LIT:0>,<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>oddsratio = np.inf<EOL><DEDENT>n1 = c[<NUM_LIT:0>,<NUM_LIT:0>] + c[<NUM_LIT:0>,<NUM_LIT:1>]<EOL>n2 = c[<NUM_LIT:1>,<NUM_LIT:0>] + c[<NUM_LIT:1>,<NUM_LIT:1>]<EOL>n = c[<NUM_LIT:0>,<NUM_LIT:0>] + c[<NUM_LIT:1>,<NUM_LIT:0>]<EOL>def binary_search(n, n1, n2, side):<EOL><INDENT>"""<STR_LIT>"""<EOL>if side == "<STR_LIT>":<EOL><INDENT>minval = mode<EOL>maxval = n<EOL><DEDENT>else:<EOL><INDENT>minval = <NUM_LIT:0><EOL>maxval = mode<EOL><DEDENT>guess = -<NUM_LIT:1><EOL>while maxval - minval > <NUM_LIT:1>:<EOL><INDENT>if maxval == minval + <NUM_LIT:1> and guess == minval:<EOL><INDENT>guess = maxval<EOL><DEDENT>else:<EOL><INDENT>guess = (maxval + minval) // <NUM_LIT:2><EOL><DEDENT>pguess = hypergeom.pmf(guess, n1 + n2, n1, n)<EOL>if side == "<STR_LIT>":<EOL><INDENT>ng = guess - <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>ng = guess + <NUM_LIT:1><EOL><DEDENT>if pguess <= pexact and hypergeom.pmf(ng, n1 + n2, n1, n) > pexact:<EOL><INDENT>break<EOL><DEDENT>elif pguess < pexact:<EOL><INDENT>maxval = guess<EOL><DEDENT>else:<EOL><INDENT>minval = guess<EOL><DEDENT><DEDENT>if guess == -<NUM_LIT:1>:<EOL><INDENT>guess = minval<EOL><DEDENT>if side == "<STR_LIT>":<EOL><INDENT>while guess > <NUM_LIT:0> and hypergeom.pmf(guess, n1 + n2, n1, n) < pexact * epsilon:<EOL><INDENT>guess -= <NUM_LIT:1><EOL><DEDENT>while hypergeom.pmf(guess, n1 + n2, n1, n) > pexact / epsilon:<EOL><INDENT>guess += <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>while hypergeom.pmf(guess, n1 + n2, n1, n) < pexact * epsilon:<EOL><INDENT>guess += <NUM_LIT:1><EOL><DEDENT>while guess > <NUM_LIT:0> and hypergeom.pmf(guess, n1 + n2, n1, n) > pexact / epsilon:<EOL><INDENT>guess -= <NUM_LIT:1><EOL><DEDENT><DEDENT>return guess<EOL><DEDENT>if alternative == '<STR_LIT>':<EOL><INDENT>pvalue = hypergeom.cdf(c[<NUM_LIT:0>,<NUM_LIT:0>], n1 + n2, n1, n)<EOL><DEDENT>elif alternative == '<STR_LIT>':<EOL><INDENT>pvalue = hypergeom.cdf(c[<NUM_LIT:0>,<NUM_LIT:1>], n1 + n2, n1, c[<NUM_LIT:0>,<NUM_LIT:1>] + c[<NUM_LIT:1>,<NUM_LIT:1>])<EOL><DEDENT>elif alternative == '<STR_LIT>':<EOL><INDENT>mode = int(float((n + <NUM_LIT:1>) * (n1 + <NUM_LIT:1>)) / (n1 + n2 + <NUM_LIT:2>))<EOL>pexact = hypergeom.pmf(c[<NUM_LIT:0>,<NUM_LIT:0>], n1 + n2, n1, n)<EOL>pmode = hypergeom.pmf(mode, n1 + n2, n1, n)<EOL>epsilon = <NUM_LIT:1> - <NUM_LIT><EOL>if np.abs(pexact - pmode) / np.maximum(pexact, pmode) <= <NUM_LIT:1> - epsilon:<EOL><INDENT>return oddsratio, <NUM_LIT:1.><EOL><DEDENT>elif c[<NUM_LIT:0>,<NUM_LIT:0>] < mode:<EOL><INDENT>plower = hypergeom.cdf(c[<NUM_LIT:0>,<NUM_LIT:0>], n1 + n2, n1, n)<EOL>if hypergeom.pmf(n, n1 + n2, n1, n) > pexact / epsilon:<EOL><INDENT>return oddsratio, plower<EOL><DEDENT>guess = binary_search(n, n1, n2, "<STR_LIT>")<EOL>pvalue = plower + hypergeom.sf(guess - <NUM_LIT:1>, n1 + n2, n1, n)<EOL><DEDENT>else:<EOL><INDENT>pupper = hypergeom.sf(c[<NUM_LIT:0>,<NUM_LIT:0>] - <NUM_LIT:1>, n1 + n2, n1, n)<EOL>if hypergeom.pmf(<NUM_LIT:0>, n1 + n2, n1, n) > pexact / epsilon:<EOL><INDENT>return oddsratio, pupper<EOL><DEDENT>guess = binary_search(n, n1, n2, "<STR_LIT>")<EOL>pvalue = pupper + hypergeom.cdf(guess, n1 + n2, n1, n)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg)<EOL><DEDENT>if pvalue > <NUM_LIT:1.0>:<EOL><INDENT>pvalue = <NUM_LIT:1.0><EOL><DEDENT>return oddsratio, pvalue<EOL>

Performs a Fisher exact test on a 2x2 contingency table.

    Parameters
    ----------
    table : array_like of ints
        A 2x2 contingency table.  Elements should be non-negative integers.
    alternative : {'two-sided', 'less', 'greater'}, optional
        Which alternative hypothesis to the null hypothesis the test uses.
        Default is 'two-sided'.

    Returns
    -------
    oddsratio : float
        This is prior odds ratio and not a posterior estimate.
    p_value : float
        P-value, the probability of obtaining a distribution at least as
        extreme as the one that was actually observed, assuming that the
        null hypothesis is true.

    See Also
    --------
    chi2_contingency : Chi-square test of independence of variables in a
        contingency table.

    Notes
    -----
    The calculated odds ratio is different from the one R uses. In R language,
    this implementation returns the (more common) "unconditional Maximum
    Likelihood Estimate", while R uses the "conditional Maximum Likelihood
    Estimate".

    For tables with large numbers the (inexact) chi-square test implemented
    in the function `chi2_contingency` can also be used.

    Examples
    --------
    Say we spend a few days counting whales and sharks in the Atlantic and
    Indian oceans. In the Atlantic ocean we find 8 whales and 1 shark, in the
    Indian ocean 2 whales and 5 sharks. Then our contingency table is::

                Atlantic  Indian
        whales     8        2
        sharks     1        5

    We use this table to find the p-value:

    >>> oddsratio, pvalue = stats.fisher_exact([[8, 2], [1, 5]])
    >>> pvalue
    0.0349...

    The probability that we would observe this or an even more imbalanced ratio
    by chance is about 3.5%.  A commonly used significance level is 5%, if we
    adopt that we can therefore conclude that our observed imbalance is
    statistically significant; whales prefer the Atlantic while sharks prefer
    the Indian ocean.


def spearmanr(a, b=None, axis=<NUM_LIT:0>):

a, axisout = _chk_asarray(a, axis)<EOL>ar = np.apply_along_axis(rankdata,axisout,a)<EOL>br = None<EOL>if b is not None:<EOL><INDENT>b, axisout = _chk_asarray(b, axis)<EOL>br = np.apply_along_axis(rankdata,axisout,b)<EOL><DEDENT>n = a.shape[axisout]<EOL>rs = np.corrcoef(ar,br,rowvar=axisout)<EOL>olderr = np.seterr(divide='<STR_LIT:ignore>')  <EOL>try:<EOL><INDENT>t = rs * np.sqrt((n-<NUM_LIT:2>) / ((rs+<NUM_LIT:1.0>)*(<NUM_LIT:1.0>-rs)))<EOL><DEDENT>finally:<EOL><INDENT>np.seterr(**olderr)<EOL><DEDENT>prob = distributions.t.sf(np.abs(t),n-<NUM_LIT:2>)*<NUM_LIT:2><EOL>if rs.shape == (<NUM_LIT:2>,<NUM_LIT:2>):<EOL><INDENT>return rs[<NUM_LIT:1>,<NUM_LIT:0>], prob[<NUM_LIT:1>,<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>return rs, prob<EOL><DEDENT>

Calculates a Spearman rank-order correlation coefficient and the p-value
to test for non-correlation.

The Spearman correlation is a nonparametric measure of the monotonicity
of the relationship between two datasets. Unlike the Pearson correlation,
the Spearman correlation does not assume that both datasets are normally
distributed. Like other correlation coefficients, this one varies
between -1 and +1 with 0 implying no correlation. Correlations of -1 or
+1 imply an exact monotonic relationship. Positive correlations imply that
as x increases, so does y. Negative correlations imply that as x
increases, y decreases.

The p-value roughly indicates the probability of an uncorrelated system
producing datasets that have a Spearman correlation at least as extreme
as the one computed from these datasets. The p-values are not entirely
reliable but are probably reasonable for datasets larger than 500 or so.

Parameters
----------
a, b : 1D or 2D array_like, b is optional
    One or two 1-D or 2-D arrays containing multiple variables and
    observations. Each column of `a` and `b` represents a variable, and
    each row entry a single observation of those variables. See also
    `axis`. Both arrays need to have the same length in the `axis`
    dimension.
axis : int or None, optional
    If axis=0 (default), then each column represents a variable, with
    observations in the rows. If axis=0, the relationship is transposed:
    each row represents a variable, while the columns contain observations.
    If axis=None, then both arrays will be raveled.

Returns
-------
rho : float or ndarray (2-D square)
    Spearman correlation matrix or correlation coefficient (if only 2
    variables are given as parameters. Correlation matrix is square with
    length equal to total number of variables (columns or rows) in a and b
    combined.
p-value : float
    The two-sided p-value for a hypothesis test whose null hypothesis is
    that two sets of data are uncorrelated, has same dimension as rho.

Notes
-----
Changes in scipy 0.8.0: rewrite to add tie-handling, and axis.

References
----------
[CRCProbStat2000]_ Section  14.7

.. [CRCProbStat2000] Zwillinger, D. and Kokoska, S. (2000). CRC Standard
   Probability and Statistics Tables and Formulae. Chapman & Hall: New
   York. 2000.

Examples
--------
>>> spearmanr([1,2,3,4,5],[5,6,7,8,7])
(0.82078268166812329, 0.088587005313543798)
>>> np.random.seed(1234321)
>>> x2n=np.random.randn(100,2)
>>> y2n=np.random.randn(100,2)
>>> spearmanr(x2n)
(0.059969996999699973, 0.55338590803773591)
>>> spearmanr(x2n[:,0], x2n[:,1])
(0.059969996999699973, 0.55338590803773591)
>>> rho, pval = spearmanr(x2n,y2n)
>>> rho
array([[ 1.        ,  0.05997   ,  0.18569457,  0.06258626],
       [ 0.05997   ,  1.        ,  0.110003  ,  0.02534653],
       [ 0.18569457,  0.110003  ,  1.        ,  0.03488749],
       [ 0.06258626,  0.02534653,  0.03488749,  1.        ]])
>>> pval
array([[ 0.        ,  0.55338591,  0.06435364,  0.53617935],
       [ 0.55338591,  0.        ,  0.27592895,  0.80234077],
       [ 0.06435364,  0.27592895,  0.        ,  0.73039992],
       [ 0.53617935,  0.80234077,  0.73039992,  0.        ]])
>>> rho, pval = spearmanr(x2n.T, y2n.T, axis=1)
>>> rho
array([[ 1.        ,  0.05997   ,  0.18569457,  0.06258626],
       [ 0.05997   ,  1.        ,  0.110003  ,  0.02534653],
       [ 0.18569457,  0.110003  ,  1.        ,  0.03488749],
       [ 0.06258626,  0.02534653,  0.03488749,  1.        ]])
>>> spearmanr(x2n, y2n, axis=None)
(0.10816770419260482, 0.1273562188027364)
>>> spearmanr(x2n.ravel(), y2n.ravel())
(0.10816770419260482, 0.1273562188027364)

>>> xint = np.random.randint(10,size=(100,2))
>>> spearmanr(xint)
(0.052760927029710199, 0.60213045837062351)


x = np.asarray(x, dtype=bool)<EOL>y = np.asarray(y, dtype=float)<EOL>n = len(x)<EOL>phat = x.sum() / float(len(x))<EOL>y0 = y[~x]  <EOL>y1 = y[x]  <EOL>y0m = y0.mean()<EOL>y1m = y1.mean()<EOL>rpb = (y1m - y0m) * np.sqrt(phat - phat**<NUM_LIT:2>) / y.std()<EOL>df = n-<NUM_LIT:2><EOL>TINY = <NUM_LIT><EOL>t = rpb*np.sqrt(df/((<NUM_LIT:1.0>-rpb+TINY)*(<NUM_LIT:1.0>+rpb+TINY)))<EOL>prob = betai(<NUM_LIT:0.5>*df, <NUM_LIT:0.5>, df/(df+t*t))<EOL>return rpb, prob<EOL>

Calculates a point biserial correlation coefficient and the associated
    p-value.

    The point biserial correlation is used to measure the relationship
    between a binary variable, x, and a continuous variable, y. Like other
    correlation coefficients, this one varies between -1 and +1 with 0
    implying no correlation. Correlations of -1 or +1 imply a determinative
    relationship.

    This function uses a shortcut formula but produces the same result as
    `pearsonr`.

    Parameters
    ----------
    x : array_like of bools
        Input array.
    y : array_like
        Input array.

    Returns
    -------
    r : float
        R value
    p-value : float
        2-tailed p-value

    References
    ----------
    http://en.wikipedia.org/wiki/Point-biserial_correlation_coefficient

    Examples
    --------
    >>> from scipy import stats
    >>> a = np.array([0, 0, 0, 1, 1, 1, 1])
    >>> b = np.arange(7)
    >>> stats.pointbiserialr(a, b)
    (0.8660254037844386, 0.011724811003954652)
    >>> stats.pearsonr(a, b)
    (0.86602540378443871, 0.011724811003954626)
    >>> np.corrcoef(a, b)
    array([[ 1.       ,  0.8660254],
           [ 0.8660254,  1.       ]])


def kendalltau(x, y, initial_lexsort=True):

x = np.asarray(x).ravel()<EOL>y = np.asarray(y).ravel()<EOL>if not x.size or not y.size:<EOL><INDENT>return (np.nan, np.nan)  <EOL><DEDENT>n = np.int64(len(x))<EOL>temp = list(range(n))  <EOL>def mergesort(offs, length):<EOL><INDENT>exchcnt = <NUM_LIT:0><EOL>if length == <NUM_LIT:1>:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>if length == <NUM_LIT:2>:<EOL><INDENT>if y[perm[offs]] <= y[perm[offs+<NUM_LIT:1>]]:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>t = perm[offs]<EOL>perm[offs] = perm[offs+<NUM_LIT:1>]<EOL>perm[offs+<NUM_LIT:1>] = t<EOL>return <NUM_LIT:1><EOL><DEDENT>length0 = length // <NUM_LIT:2><EOL>length1 = length - length0<EOL>middle = offs + length0<EOL>exchcnt += mergesort(offs, length0)<EOL>exchcnt += mergesort(middle, length1)<EOL>if y[perm[middle - <NUM_LIT:1>]] < y[perm[middle]]:<EOL><INDENT>return exchcnt<EOL><DEDENT>i = j = k = <NUM_LIT:0><EOL>while j < length0 or k < length1:<EOL><INDENT>if k >= length1 or (j < length0 and y[perm[offs + j]] <=<EOL>y[perm[middle + k]]):<EOL><INDENT>temp[i] = perm[offs + j]<EOL>d = i - j<EOL>j += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>temp[i] = perm[middle + k]<EOL>d = (offs + i) - (middle + k)<EOL>k += <NUM_LIT:1><EOL><DEDENT>if d > <NUM_LIT:0>:<EOL><INDENT>exchcnt += d<EOL><DEDENT>i += <NUM_LIT:1><EOL><DEDENT>perm[offs:offs+length] = temp[<NUM_LIT:0>:length]<EOL>return exchcnt<EOL><DEDENT>if initial_lexsort:<EOL><INDENT>perm = np.lexsort((y, x))<EOL><DEDENT>else:<EOL><INDENT>perm = list(range(n))<EOL>perm.sort(key=lambda a: (x[a], y[a]))<EOL><DEDENT>first = <NUM_LIT:0><EOL>t = <NUM_LIT:0><EOL>for i in xrange(<NUM_LIT:1>, n):<EOL><INDENT>if x[perm[first]] != x[perm[i]] or y[perm[first]] != y[perm[i]]:<EOL><INDENT>t += ((i - first) * (i - first - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>first = i<EOL><DEDENT><DEDENT>t += ((n - first) * (n - first - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>first = <NUM_LIT:0><EOL>u = <NUM_LIT:0><EOL>for i in xrange(<NUM_LIT:1>,n):<EOL><INDENT>if x[perm[first]] != x[perm[i]]:<EOL><INDENT>u += ((i - first) * (i - first - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>first = i<EOL><DEDENT><DEDENT>u += ((n - first) * (n - first - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>exchanges = mergesort(<NUM_LIT:0>, n)<EOL>first = <NUM_LIT:0><EOL>v = <NUM_LIT:0><EOL>for i in xrange(<NUM_LIT:1>,n):<EOL><INDENT>if y[perm[first]] != y[perm[i]]:<EOL><INDENT>v += ((i - first) * (i - first - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>first = i<EOL><DEDENT><DEDENT>v += ((n - first) * (n - first - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>tot = (n * (n - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>if tot == u or tot == v:<EOL><INDENT>return (np.nan, np.nan)    <EOL><DEDENT>denom = np.exp(<NUM_LIT:0.5> * (np.log(tot - u) + np.log(tot - v)))<EOL>tau = ((tot - (v + u - t)) - <NUM_LIT> * exchanges) / denom<EOL>svar = (<NUM_LIT> * n + <NUM_LIT>) / (<NUM_LIT> * n * (n - <NUM_LIT:1>))<EOL>z = tau / np.sqrt(svar)<EOL>prob = special.erfc(np.abs(z) / <NUM_LIT>)<EOL>return tau, prob<EOL>

Calculates Kendall's tau, a correlation measure for ordinal data.

Kendall's tau is a measure of the correspondence between two rankings.
Values close to 1 indicate strong agreement, values close to -1 indicate
strong disagreement.  This is the tau-b version of Kendall's tau which
accounts for ties.

Parameters
----------
x, y : array_like
    Arrays of rankings, of the same shape. If arrays are not 1-D, they will
    be flattened to 1-D.
initial_lexsort : bool, optional
    Whether to use lexsort or quicksort as the sorting method for the
    initial sort of the inputs. Default is lexsort (True), for which
    `kendalltau` is of complexity O(n log(n)). If False, the complexity is
    O(n^2), but with a smaller pre-factor (so quicksort may be faster for
    small arrays).

Returns
-------
Kendall's tau : float
   The tau statistic.
p-value : float
   The two-sided p-value for a hypothesis test whose null hypothesis is
   an absence of association, tau = 0.

Notes
-----
The definition of Kendall's tau that is used is::

  tau = (P - Q) / sqrt((P + Q + T) * (P + Q + U))

where P is the number of concordant pairs, Q the number of discordant
pairs, T the number of ties only in `x`, and U the number of ties only in
`y`.  If a tie occurs for the same pair in both `x` and `y`, it is not
added to either T or U.

References
----------
W.R. Knight, "A Computer Method for Calculating Kendall's Tau with
Ungrouped Data", Journal of the American Statistical Association, Vol. 61,
No. 314, Part 1, pp. 436-439, 1966.

Examples
--------
>>> import scipy.stats as stats
>>> x1 = [12, 2, 1, 12, 2]
>>> x2 = [1, 4, 7, 1, 0]
>>> tau, p_value = stats.kendalltau(x1, x2)
>>> tau
-0.47140452079103173
>>> p_value
0.24821309157521476


TINY = <NUM_LIT><EOL>if y is None:  <EOL><INDENT>x = asarray(x)<EOL>if x.shape[<NUM_LIT:0>] == <NUM_LIT:2>:<EOL><INDENT>x, y = x<EOL><DEDENT>elif x.shape[<NUM_LIT:1>] == <NUM_LIT:2>:<EOL><INDENT>x, y = x.T<EOL><DEDENT>else:<EOL><INDENT>msg = "<STR_LIT>" % str(x.shape)<EOL>raise ValueError(msg)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>x = asarray(x)<EOL>y = asarray(y)<EOL><DEDENT>n = len(x)<EOL>xmean = np.mean(x,None)<EOL>ymean = np.mean(y,None)<EOL>ssxm, ssxym, ssyxm, ssym = np.cov(x, y, bias=<NUM_LIT:1>).flat<EOL>r_num = ssxym<EOL>r_den = np.sqrt(ssxm*ssym)<EOL>if r_den == <NUM_LIT:0.0>:<EOL><INDENT>r = <NUM_LIT:0.0><EOL><DEDENT>else:<EOL><INDENT>r = r_num / r_den<EOL>if (r > <NUM_LIT:1.0>):<EOL><INDENT>r = <NUM_LIT:1.0><EOL><DEDENT>elif (r < -<NUM_LIT:1.0>):<EOL><INDENT>r = -<NUM_LIT:1.0><EOL><DEDENT><DEDENT>df = n-<NUM_LIT:2><EOL>t = r*np.sqrt(df/((<NUM_LIT:1.0>-r+TINY)*(<NUM_LIT:1.0>+r+TINY)))<EOL>prob = distributions.t.sf(np.abs(t),df)*<NUM_LIT:2><EOL>slope = r_num / ssxm<EOL>intercept = ymean - slope*xmean<EOL>sterrest = np.sqrt((<NUM_LIT:1>-r*r)*ssym / ssxm / df)<EOL>return slope, intercept, r, prob, sterrest<EOL>

Calculate a regression line

This computes a least-squares regression for two sets of measurements.

Parameters
----------
x, y : array_like
    two sets of measurements.  Both arrays should have the same length.
    If only x is given (and y=None), then it must be a two-dimensional
    array where one dimension has length 2.  The two sets of measurements
    are then found by splitting the array along the length-2 dimension.

Returns
-------
slope : float
    slope of the regression line
intercept : float
    intercept of the regression line
r-value : float
    correlation coefficient
p-value : float
    two-sided p-value for a hypothesis test whose null hypothesis is
    that the slope is zero.
stderr : float
    Standard error of the estimate

Examples
--------
>>> from scipy import stats
>>> import numpy as np
>>> x = np.random.random(10)
>>> y = np.random.random(10)
>>> slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)

# To get coefficient of determination (r_squared)

>>> print "r-squared:", r_value**2
r-squared: 0.15286643777


def theilslopes(y, x=None, alpha=<NUM_LIT>):

y = np.asarray(y).flatten()<EOL>if x is None:<EOL><INDENT>x = np.arange(len(y), dtype=float)<EOL><DEDENT>else:<EOL><INDENT>x = np.asarray(x, dtype=float).flatten()<EOL>if len(x) != len(y):<EOL><INDENT>raise ValueError("<STR_LIT>" % (len(y),len(x)))<EOL><DEDENT><DEDENT>deltax = x[:, np.newaxis] - x<EOL>deltay = y[:, np.newaxis] - y<EOL>slopes = deltay[deltax > <NUM_LIT:0>] / deltax[deltax > <NUM_LIT:0>]<EOL>slopes.sort()<EOL>medslope = np.median(slopes)<EOL>medinter = np.median(y) - medslope * np.median(x)<EOL>if alpha > <NUM_LIT:0.5>:<EOL><INDENT>alpha = <NUM_LIT:1.> - alpha<EOL><DEDENT>z = distributions.norm.ppf(alpha / <NUM_LIT>)<EOL>_, nxreps = find_repeats(x)<EOL>_, nyreps = find_repeats(y)<EOL>nt = len(slopes)       <EOL>ny = len(y)            <EOL>sigsq = <NUM_LIT:1>/<NUM_LIT> * (ny * (ny-<NUM_LIT:1>) * (<NUM_LIT:2>*ny+<NUM_LIT:5>) -<EOL>np.sum(k * (k-<NUM_LIT:1>) * (<NUM_LIT:2>*k + <NUM_LIT:5>) for k in nxreps) -<EOL>np.sum(k * (k-<NUM_LIT:1>) * (<NUM_LIT:2>*k + <NUM_LIT:5>) for k in nyreps))<EOL>sigma = np.sqrt(sigsq)<EOL>Ru = min(int(np.round((nt - z*sigma)/<NUM_LIT>)), len(slopes)-<NUM_LIT:1>)<EOL>Rl = max(int(np.round((nt + z*sigma)/<NUM_LIT>)) - <NUM_LIT:1>, <NUM_LIT:0>)<EOL>delta = slopes[[Rl, Ru]]<EOL>return medslope, medinter, delta[<NUM_LIT:0>], delta[<NUM_LIT:1>]<EOL>

r"""
    Computes the Theil-Sen estimator for a set of points (x, y).

    `theilslopes` implements a method for robust linear regression.  It
    computes the slope as the median of all slopes between paired values.

    Parameters
    ----------
    y : array_like
        Dependent variable.
    x : {None, array_like}, optional
        Independent variable. If None, use ``arange(len(y))`` instead.
    alpha : float
        Confidence degree between 0 and 1. Default is 95% confidence.
        Note that `alpha` is symmetric around 0.5, i.e. both 0.1 and 0.9 are
        interpreted as "find the 90% confidence interval".

    Returns
    -------
    medslope : float
        Theil slope.
    medintercept : float
        Intercept of the Theil line, as ``median(y) - medslope*median(x)``.
    lo_slope : float
        Lower bound of the confidence interval on `medslope`.
    up_slope : float
        Upper bound of the confidence interval on `medslope`.

    Notes
    -----
    The implementation of `theilslopes` follows [1]_. The intercept is
    not defined in [1]_, and here it is defined as ``median(y) -
    medslope*median(x)``, which is given in [3]_. Other definitions of
    the intercept exist in the literature. A confidence interval for
    the intercept is not given as this question is not addressed in
    [1]_.

    References
    ----------
    .. [1] P.K. Sen, "Estimates of the regression coefficient based on Kendall's tau",
           J. Am. Stat. Assoc., Vol. 63, pp. 1379-1389, 1968.
    .. [2] H. Theil, "A rank-invariant method of linear and polynomial
           regression analysis I, II and III",  Nederl. Akad. Wetensch., Proc.
           53:, pp. 386-392, pp. 521-525, pp. 1397-1412, 1950.
    .. [3] W.L. Conover, "Practical nonparametric statistics", 2nd ed.,
           John Wiley and Sons, New York, pp. 493.

    Examples
    --------
    >>> from scipy import stats
    >>> import matplotlib.pyplot as plt

    >>> x = np.linspace(-5, 5, num=150)
    >>> y = x + np.random.normal(size=x.size)
    >>> y[11:15] += 10  # add outliers
    >>> y[-5:] -= 7

    Compute the slope, intercept and 90% confidence interval.  For comparison,
    also compute the least-squares fit with `linregress`:

    >>> res = stats.theilslopes(y, x, 0.90)
    >>> lsq_res = stats.linregress(x, y)

    Plot the results. The Theil-Sen regression line is shown in red, with the
    dashed red lines illustrating the confidence interval of the slope (note
    that the dashed red lines are not the confidence interval of the regression
    as the confidence interval of the intercept is not included). The green
    line shows the least-squares fit for comparison.

    >>> fig = plt.figure()
    >>> ax = fig.add_subplot(111)
    >>> ax.plot(x, y, 'b.')
    >>> ax.plot(x, res[1] + res[0] * x, 'r-')
    >>> ax.plot(x, res[1] + res[2] * x, 'r--')
    >>> ax.plot(x, res[1] + res[3] * x, 'r--')
    >>> ax.plot(x, lsq_res[1] + lsq_res[0] * x, 'g-')
    >>> plt.show()


if hasattr(a, '<STR_LIT:count>'):<EOL><INDENT>num = a.count(axis=axis)<EOL>if isinstance(num, np.ndarray) and num.ndim == <NUM_LIT:0>:<EOL><INDENT>num = int(num)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if axis is None:<EOL><INDENT>num = a.size<EOL><DEDENT>else:<EOL><INDENT>num = a.shape[axis]<EOL><DEDENT><DEDENT>return num<EOL>

Count the number of non-masked elements of an array.

This function behaves like np.ma.count(), but is much faster
for ndarrays.


def power_divergence(f_obs, f_exp=None, ddof=<NUM_LIT:0>, axis=<NUM_LIT:0>, lambda_=None):

<EOL>if isinstance(lambda_, string_types):<EOL><INDENT>if lambda_ not in _power_div_lambda_names:<EOL><INDENT>names = repr(list(_power_div_lambda_names.keys()))[<NUM_LIT:1>:-<NUM_LIT:1>]<EOL>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>".format(lambda_, names))<EOL><DEDENT>lambda_ = _power_div_lambda_names[lambda_]<EOL><DEDENT>elif lambda_ is None:<EOL><INDENT>lambda_ = <NUM_LIT:1><EOL><DEDENT>f_obs = np.asanyarray(f_obs)<EOL>if f_exp is not None:<EOL><INDENT>f_exp = np.atleast_1d(np.asanyarray(f_exp))<EOL><DEDENT>else:<EOL><INDENT>with np.errstate(invalid='<STR_LIT:ignore>'):<EOL><INDENT>f_exp = np.atleast_1d(f_obs.mean(axis=axis))<EOL><DEDENT>if axis is not None:<EOL><INDENT>reduced_shape = list(f_obs.shape)<EOL>reduced_shape[axis] = <NUM_LIT:1><EOL>f_exp.shape = reduced_shape<EOL><DEDENT><DEDENT>if lambda_ == <NUM_LIT:1>:<EOL><INDENT>terms = (f_obs - f_exp)**<NUM_LIT:2> / f_exp<EOL><DEDENT>elif lambda_ == <NUM_LIT:0>:<EOL><INDENT>terms = <NUM_LIT> * special.xlogy(f_obs, f_obs / f_exp)<EOL><DEDENT>elif lambda_ == -<NUM_LIT:1>:<EOL><INDENT>terms = <NUM_LIT> * special.xlogy(f_exp, f_exp / f_obs)<EOL><DEDENT>else:<EOL><INDENT>terms = f_obs * ((f_obs / f_exp)**lambda_ - <NUM_LIT:1>)<EOL>terms /= <NUM_LIT:0.5> * lambda_ * (lambda_ + <NUM_LIT:1>)<EOL><DEDENT>stat = terms.sum(axis=axis)<EOL>num_obs = _count(terms, axis=axis)<EOL>ddof = asarray(ddof)<EOL>p = chisqprob(stat, num_obs - <NUM_LIT:1> - ddof)<EOL>return stat, p<EOL>

Cressie-Read power divergence statistic and goodness of fit test.

This function tests the null hypothesis that the categorical data
has the given frequencies, using the Cressie-Read power divergence
statistic.

Parameters
----------
f_obs : array_like
    Observed frequencies in each category.
f_exp : array_like, optional
    Expected frequencies in each category.  By default the categories are
    assumed to be equally likely.
ddof : int, optional
    "Delta degrees of freedom": adjustment to the degrees of freedom
    for the p-value.  The p-value is computed using a chi-squared
    distribution with ``k - 1 - ddof`` degrees of freedom, where `k`
    is the number of observed frequencies.  The default value of `ddof`
    is 0.
axis : int or None, optional
    The axis of the broadcast result of `f_obs` and `f_exp` along which to
    apply the test.  If axis is None, all values in `f_obs` are treated
    as a single data set.  Default is 0.
lambda_ : float or str, optional
    `lambda_` gives the power in the Cressie-Read power divergence
    statistic.  The default is 1.  For convenience, `lambda_` may be
    assigned one of the following strings, in which case the
    corresponding numerical value is used::

        String              Value   Description
        "pearson"             1     Pearson's chi-squared statistic.
                                    In this case, the function is
                                    equivalent to `stats.chisquare`.
        "log-likelihood"      0     Log-likelihood ratio. Also known as
                                    the G-test [3]_.
        "freeman-tukey"      -1/2   Freeman-Tukey statistic.
        "mod-log-likelihood" -1     Modified log-likelihood ratio.
        "neyman"             -2     Neyman's statistic.
        "cressie-read"        2/3   The power recommended in [5]_.

Returns
-------
stat : float or ndarray
    The Cressie-Read power divergence test statistic.  The value is
    a float if `axis` is None or if` `f_obs` and `f_exp` are 1-D.
p : float or ndarray
    The p-value of the test.  The value is a float if `ddof` and the
    return value `stat` are scalars.

See Also
--------
chisquare

Notes
-----
This test is invalid when the observed or expected frequencies in each
category are too small.  A typical rule is that all of the observed
and expected frequencies should be at least 5.

When `lambda_` is less than zero, the formula for the statistic involves
dividing by `f_obs`, so a warning or error may be generated if any value
in `f_obs` is 0.

Similarly, a warning or error may be generated if any value in `f_exp` is
zero when `lambda_` >= 0.

The default degrees of freedom, k-1, are for the case when no parameters
of the distribution are estimated. If p parameters are estimated by
efficient maximum likelihood then the correct degrees of freedom are
k-1-p. If the parameters are estimated in a different way, then the
dof can be between k-1-p and k-1. However, it is also possible that
the asymptotic distribution is not a chisquare, in which case this
test is not appropriate.

This function handles masked arrays.  If an element of `f_obs` or `f_exp`
is masked, then data at that position is ignored, and does not count
towards the size of the data set.

.. versionadded:: 0.13.0

References
----------
.. [1] Lowry, Richard.  "Concepts and Applications of Inferential
       Statistics". Chapter 8. http://faculty.vassar.edu/lowry/ch8pt1.html
.. [2] "Chi-squared test", http://en.wikipedia.org/wiki/Chi-squared_test
.. [3] "G-test", http://en.wikipedia.org/wiki/G-test
.. [4] Sokal, R. R. and Rohlf, F. J. "Biometry: the principles and
       practice of statistics in biological research", New York: Freeman
       (1981)
.. [5] Cressie, N. and Read, T. R. C., "Multinomial Goodness-of-Fit
       Tests", J. Royal Stat. Soc. Series B, Vol. 46, No. 3 (1984),
       pp. 440-464.

Examples
--------

(See `chisquare` for more examples.)

When just `f_obs` is given, it is assumed that the expected frequencies
are uniform and given by the mean of the observed frequencies.  Here we
perform a G-test (i.e. use the log-likelihood ratio statistic):

>>> power_divergence([16, 18, 16, 14, 12, 12], lambda_='log-likelihood')
(2.006573162632538, 0.84823476779463769)

The expected frequencies can be given with the `f_exp` argument:

>>> power_divergence([16, 18, 16, 14, 12, 12],
...                  f_exp=[16, 16, 16, 16, 16, 8],
...                  lambda_='log-likelihood')
(3.5, 0.62338762774958223)

When `f_obs` is 2-D, by default the test is applied to each column.

>>> obs = np.array([[16, 18, 16, 14, 12, 12], [32, 24, 16, 28, 20, 24]]).T
>>> obs.shape
(6, 2)
>>> power_divergence(obs, lambda_="log-likelihood")
(array([ 2.00657316,  6.77634498]), array([ 0.84823477,  0.23781225]))

By setting ``axis=None``, the test is applied to all data in the array,
which is equivalent to applying the test to the flattened array.

>>> power_divergence(obs, axis=None)
(23.31034482758621, 0.015975692534127565)
>>> power_divergence(obs.ravel())
(23.31034482758621, 0.015975692534127565)

`ddof` is the change to make to the default degrees of freedom.

>>> power_divergence([16, 18, 16, 14, 12, 12], ddof=1)
(2.0, 0.73575888234288467)

The calculation of the p-values is done by broadcasting the
test statistic with `ddof`.

>>> power_divergence([16, 18, 16, 14, 12, 12], ddof=[0,1,2])
(2.0, array([ 0.84914504,  0.73575888,  0.5724067 ]))

`f_obs` and `f_exp` are also broadcast.  In the following, `f_obs` has
shape (6,) and `f_exp` has shape (2, 6), so the result of broadcasting
`f_obs` and `f_exp` has shape (2, 6).  To compute the desired chi-squared
statistics, we must use ``axis=1``:

>>> power_divergence([16, 18, 16, 14, 12, 12],
...                  f_exp=[[16, 16, 16, 16, 16, 8],
...                         [8, 20, 20, 16, 12, 12]],
...                  axis=1)
(array([ 3.5 ,  9.25]), array([ 0.62338763,  0.09949846]))


def chisquare(f_obs, f_exp=None, ddof=<NUM_LIT:0>, axis=<NUM_LIT:0>):

return power_divergence(f_obs, f_exp=f_exp, ddof=ddof, axis=axis,<EOL>lambda_="<STR_LIT>")<EOL>

Calculates a one-way chi square test.

The chi square test tests the null hypothesis that the categorical data
has the given frequencies.

Parameters
----------
f_obs : array_like
    Observed frequencies in each category.
f_exp : array_like, optional
    Expected frequencies in each category.  By default the categories are
    assumed to be equally likely.
ddof : int, optional
    "Delta degrees of freedom": adjustment to the degrees of freedom
    for the p-value.  The p-value is computed using a chi-squared
    distribution with ``k - 1 - ddof`` degrees of freedom, where `k`
    is the number of observed frequencies.  The default value of `ddof`
    is 0.
axis : int or None, optional
    The axis of the broadcast result of `f_obs` and `f_exp` along which to
    apply the test.  If axis is None, all values in `f_obs` are treated
    as a single data set.  Default is 0.

Returns
-------
chisq : float or ndarray
    The chi-squared test statistic.  The value is a float if `axis` is
    None or `f_obs` and `f_exp` are 1-D.
p : float or ndarray
    The p-value of the test.  The value is a float if `ddof` and the
    return value `chisq` are scalars.

See Also
--------
power_divergence
mstats.chisquare

Notes
-----
This test is invalid when the observed or expected frequencies in each
category are too small.  A typical rule is that all of the observed
and expected frequencies should be at least 5.

The default degrees of freedom, k-1, are for the case when no parameters
of the distribution are estimated. If p parameters are estimated by
efficient maximum likelihood then the correct degrees of freedom are
k-1-p. If the parameters are estimated in a different way, then the
dof can be between k-1-p and k-1. However, it is also possible that
the asymptotic distribution is not a chisquare, in which case this
test is not appropriate.

References
----------
.. [1] Lowry, Richard.  "Concepts and Applications of Inferential
       Statistics". Chapter 8. http://faculty.vassar.edu/lowry/ch8pt1.html
.. [2] "Chi-squared test", http://en.wikipedia.org/wiki/Chi-squared_test

Examples
--------
When just `f_obs` is given, it is assumed that the expected frequencies
are uniform and given by the mean of the observed frequencies.

>>> chisquare([16, 18, 16, 14, 12, 12])
(2.0, 0.84914503608460956)

With `f_exp` the expected frequencies can be given.

>>> chisquare([16, 18, 16, 14, 12, 12], f_exp=[16, 16, 16, 16, 16, 8])
(3.5, 0.62338762774958223)

When `f_obs` is 2-D, by default the test is applied to each column.

>>> obs = np.array([[16, 18, 16, 14, 12, 12], [32, 24, 16, 28, 20, 24]]).T
>>> obs.shape
(6, 2)
>>> chisquare(obs)
(array([ 2.        ,  6.66666667]), array([ 0.84914504,  0.24663415]))

By setting ``axis=None``, the test is applied to all data in the array,
which is equivalent to applying the test to the flattened array.

>>> chisquare(obs, axis=None)
(23.31034482758621, 0.015975692534127565)
>>> chisquare(obs.ravel())
(23.31034482758621, 0.015975692534127565)

`ddof` is the change to make to the default degrees of freedom.

>>> chisquare([16, 18, 16, 14, 12, 12], ddof=1)
(2.0, 0.73575888234288467)

The calculation of the p-values is done by broadcasting the
chi-squared statistic with `ddof`.

>>> chisquare([16, 18, 16, 14, 12, 12], ddof=[0,1,2])
(2.0, array([ 0.84914504,  0.73575888,  0.5724067 ]))

`f_obs` and `f_exp` are also broadcast.  In the following, `f_obs` has
shape (6,) and `f_exp` has shape (2, 6), so the result of broadcasting
`f_obs` and `f_exp` has shape (2, 6).  To compute the desired chi-squared
statistics, we use ``axis=1``:

>>> chisquare([16, 18, 16, 14, 12, 12],
...           f_exp=[[16, 16, 16, 16, 16, 8], [8, 20, 20, 16, 12, 12]],
...           axis=1)
(array([ 3.5 ,  9.25]), array([ 0.62338763,  0.09949846]))


data1, data2 = map(asarray, (data1, data2))<EOL>n1 = data1.shape[<NUM_LIT:0>]<EOL>n2 = data2.shape[<NUM_LIT:0>]<EOL>n1 = len(data1)<EOL>n2 = len(data2)<EOL>data1 = np.sort(data1)<EOL>data2 = np.sort(data2)<EOL>data_all = np.concatenate([data1,data2])<EOL>cdf1 = np.searchsorted(data1,data_all,side='<STR_LIT:right>')/(<NUM_LIT:1.0>*n1)<EOL>cdf2 = (np.searchsorted(data2,data_all,side='<STR_LIT:right>'))/(<NUM_LIT:1.0>*n2)<EOL>d = np.max(np.absolute(cdf1-cdf2))<EOL>en = np.sqrt(n1*n2/float(n1+n2))<EOL>try:<EOL><INDENT>prob = distributions.kstwobign.sf((en + <NUM_LIT> + <NUM_LIT> / en) * d)<EOL><DEDENT>except:<EOL><INDENT>prob = <NUM_LIT:1.0><EOL><DEDENT>return d, prob<EOL>

Computes the Kolmogorov-Smirnov statistic on 2 samples.

This is a two-sided test for the null hypothesis that 2 independent samples
are drawn from the same continuous distribution.

Parameters
----------
a, b : sequence of 1-D ndarrays
    two arrays of sample observations assumed to be drawn from a continuous
    distribution, sample sizes can be different

Returns
-------
D : float
    KS statistic
p-value : float
    two-tailed p-value

Notes
-----
This tests whether 2 samples are drawn from the same distribution. Note
that, like in the case of the one-sample K-S test, the distribution is
assumed to be continuous.

This is the two-sided test, one-sided tests are not implemented.
The test uses the two-sided asymptotic Kolmogorov-Smirnov distribution.

If the K-S statistic is small or the p-value is high, then we cannot
reject the hypothesis that the distributions of the two samples
are the same.

Examples
--------
>>> from scipy import stats
>>> np.random.seed(12345678)  #fix random seed to get the same result
>>> n1 = 200  # size of first sample
>>> n2 = 300  # size of second sample

For a different distribution, we can reject the null hypothesis since the
pvalue is below 1%:

>>> rvs1 = stats.norm.rvs(size=n1, loc=0., scale=1)
>>> rvs2 = stats.norm.rvs(size=n2, loc=0.5, scale=1.5)
>>> stats.ks_2samp(rvs1, rvs2)
(0.20833333333333337, 4.6674975515806989e-005)

For a slightly different distribution, we cannot reject the null hypothesis
at a 10% or lower alpha since the p-value at 0.144 is higher than 10%

>>> rvs3 = stats.norm.rvs(size=n2, loc=0.01, scale=1.0)
>>> stats.ks_2samp(rvs1, rvs3)
(0.10333333333333333, 0.14498781825751686)

For an identical distribution, we cannot reject the null hypothesis since
the p-value is high, 41%:

>>> rvs4 = stats.norm.rvs(size=n2, loc=0.0, scale=1.0)
>>> stats.ks_2samp(rvs1, rvs4)
(0.07999999999999996, 0.41126949729859719)


def mannwhitneyu(x, y, use_continuity=True):

x = asarray(x)<EOL>y = asarray(y)<EOL>n1 = len(x)<EOL>n2 = len(y)<EOL>ranked = rankdata(np.concatenate((x,y)))<EOL>rankx = ranked[<NUM_LIT:0>:n1]       <EOL>u1 = n1*n2 + (n1*(n1+<NUM_LIT:1>))/<NUM_LIT> - np.sum(rankx,axis=<NUM_LIT:0>)  <EOL>u2 = n1*n2 - u1                            <EOL>bigu = max(u1,u2)<EOL>smallu = min(u1,u2)<EOL>T = tiecorrect(ranked)<EOL>if T == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>sd = np.sqrt(T*n1*n2*(n1+n2+<NUM_LIT:1>)/<NUM_LIT>)<EOL>if use_continuity:<EOL><INDENT>z = abs((bigu-<NUM_LIT:0.5>-n1*n2/<NUM_LIT>) / sd)<EOL><DEDENT>else:<EOL><INDENT>z = abs((bigu-n1*n2/<NUM_LIT>) / sd)  <EOL><DEDENT>return smallu, distributions.norm.sf(z)<EOL>

Computes the Mann-Whitney rank test on samples x and y.

Parameters
----------
x, y : array_like
    Array of samples, should be one-dimensional.
use_continuity : bool, optional
        Whether a continuity correction (1/2.) should be taken into
        account. Default is True.

Returns
-------
u : float
    The Mann-Whitney statistics.
prob : float
    One-sided p-value assuming a asymptotic normal distribution.

Notes
-----
Use only when the number of observation in each sample is > 20 and
you have 2 independent samples of ranks. Mann-Whitney U is
significant if the u-obtained is LESS THAN or equal to the critical
value of U.

This test corrects for ties and by default uses a continuity correction.
The reported p-value is for a one-sided hypothesis, to get the two-sided
p-value multiply the returned p-value by 2.


x,y = map(np.asarray, (x, y))<EOL>n1 = len(x)<EOL>n2 = len(y)<EOL>alldata = np.concatenate((x,y))<EOL>ranked = rankdata(alldata)<EOL>x = ranked[:n1]<EOL>y = ranked[n1:]<EOL>s = np.sum(x,axis=<NUM_LIT:0>)<EOL>expected = n1*(n1+n2+<NUM_LIT:1>) / <NUM_LIT><EOL>z = (s - expected) / np.sqrt(n1*n2*(n1+n2+<NUM_LIT:1>)/<NUM_LIT>)<EOL>prob = <NUM_LIT:2> * distributions.norm.sf(abs(z))<EOL>return z, prob<EOL>

Compute the Wilcoxon rank-sum statistic for two samples.

The Wilcoxon rank-sum test tests the null hypothesis that two sets
of measurements are drawn from the same distribution.  The alternative
hypothesis is that values in one sample are more likely to be
larger than the values in the other sample.

This test should be used to compare two samples from continuous
distributions.  It does not handle ties between measurements
in x and y.  For tie-handling and an optional continuity correction
see `scipy.stats.mannwhitneyu`.

Parameters
----------
x,y : array_like
    The data from the two samples

Returns
-------
z-statistic : float
    The test statistic under the large-sample approximation that the
    rank sum statistic is normally distributed
p-value : float
    The two-sided p-value of the test

References
----------
.. [1] http://en.wikipedia.org/wiki/Wilcoxon_rank-sum_test


args = list(map(np.asarray, args))  <EOL>na = len(args)     <EOL>if na < <NUM_LIT:2>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>n = np.asarray(list(map(len, args)))<EOL>alldata = np.concatenate(args)<EOL>ranked = rankdata(alldata)  <EOL>T = tiecorrect(ranked)      <EOL>if T == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>j = np.insert(np.cumsum(n), <NUM_LIT:0>, <NUM_LIT:0>)<EOL>ssbn = <NUM_LIT:0><EOL>for i in range(na):<EOL><INDENT>ssbn += square_of_sums(ranked[j[i]:j[i+<NUM_LIT:1>]]) / float(n[i])<EOL><DEDENT>totaln = np.sum(n)<EOL>h = <NUM_LIT> / (totaln * (totaln + <NUM_LIT:1>)) * ssbn - <NUM_LIT:3> * (totaln + <NUM_LIT:1>)<EOL>df = na - <NUM_LIT:1><EOL>h = h / float(T)<EOL>return h, chisqprob(h, df)<EOL>

Compute the Kruskal-Wallis H-test for independent samples

The Kruskal-Wallis H-test tests the null hypothesis that the population
median of all of the groups are equal.  It is a non-parametric version of
ANOVA.  The test works on 2 or more independent samples, which may have
different sizes.  Note that rejecting the null hypothesis does not
indicate which of the groups differs.  Post-hoc comparisons between
groups are required to determine which groups are different.

Parameters
----------
sample1, sample2, ... : array_like
   Two or more arrays with the sample measurements can be given as
   arguments.

Returns
-------
H-statistic : float
   The Kruskal-Wallis H statistic, corrected for ties
p-value : float
   The p-value for the test using the assumption that H has a chi
   square distribution

Notes
-----
Due to the assumption that H has a chi square distribution, the number
of samples in each group must not be too small.  A typical rule is
that each sample must have at least 5 measurements.

References
----------
.. [1] http://en.wikipedia.org/wiki/Kruskal-Wallis_one-way_analysis_of_variance


k = len(args)<EOL>if k < <NUM_LIT:3>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>n = len(args[<NUM_LIT:0>])<EOL>for i in range(<NUM_LIT:1>, k):<EOL><INDENT>if len(args[i]) != n:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>data = np.vstack(args).T<EOL>data = data.astype(float)<EOL>for i in range(len(data)):<EOL><INDENT>data[i] = rankdata(data[i])<EOL><DEDENT>ties = <NUM_LIT:0><EOL>for i in range(len(data)):<EOL><INDENT>replist, repnum = find_repeats(array(data[i]))<EOL>for t in repnum:<EOL><INDENT>ties += t*(t*t-<NUM_LIT:1>)<EOL><DEDENT><DEDENT>c = <NUM_LIT:1> - ties / float(k*(k*k-<NUM_LIT:1>)*n)<EOL>ssbn = pysum(pysum(data)**<NUM_LIT:2>)<EOL>chisq = (<NUM_LIT> / (k*n*(k+<NUM_LIT:1>)) * ssbn - <NUM_LIT:3>*n*(k+<NUM_LIT:1>)) / c<EOL>return chisq, chisqprob(chisq,k-<NUM_LIT:1>)<EOL>

Computes the Friedman test for repeated measurements

The Friedman test tests the null hypothesis that repeated measurements of
the same individuals have the same distribution.  It is often used
to test for consistency among measurements obtained in different ways.
For example, if two measurement techniques are used on the same set of
individuals, the Friedman test can be used to determine if the two
measurement techniques are consistent.

Parameters
----------
measurements1, measurements2, measurements3... : array_like
    Arrays of measurements.  All of the arrays must have the same number
    of elements.  At least 3 sets of measurements must be given.

Returns
-------
friedman chi-square statistic : float
    the test statistic, correcting for ties
p-value : float
    the associated p-value assuming that the test statistic has a chi
    squared distribution

Notes
-----
Due to the assumption that the test statistic has a chi squared
distribution, the p-value is only reliable for n > 10 and more than
6 repeated measurements.

References
----------
.. [1] http://en.wikipedia.org/wiki/Friedman_test


def combine_pvalues(pvalues, method='<STR_LIT>', weights=None):

pvalues = np.asarray(pvalues)<EOL>if pvalues.ndim != <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if method == '<STR_LIT>':<EOL><INDENT>Xsq = -<NUM_LIT:2> * np.sum(np.log(pvalues))<EOL>pval = distributions.chi2.sf(Xsq, <NUM_LIT:2> * len(pvalues))<EOL>return (Xsq, pval)<EOL><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>if weights is None:<EOL><INDENT>weights = np.ones_like(pvalues)<EOL><DEDENT>elif len(weights) != len(pvalues):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>weights = np.asarray(weights)<EOL>if weights.ndim != <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>Zi = distributions.norm.isf(pvalues)<EOL>Z = np.dot(weights, Zi) / np.linalg.norm(weights)<EOL>pval = distributions.norm.sf(Z)<EOL>return (Z, pval)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>", method)<EOL><DEDENT>

Methods for combining the p-values of independent tests bearing upon the
same hypothesis.

Parameters
----------
p: array_like, 1-D
    Array of p-values assumed to come from independent tests.
method: str
    Name of method to use to combine p-values. The following methods are
    available:
    - "fisher": Fisher's method (Fisher's combined probability test)
    - "stouffer": Stouffer's Z-score method
weights: array_like, 1-D, optional
    Optional array of weights used only for Stouffer's Z-score method.

Returns
-------
statistic: float
    The statistic calculated by the specified method:
    - "fisher": The chi-squared statistic
    - "stouffer": The Z-score
pval: float
    The combined p-value.

Notes
-----
Fisher's method (also known as Fisher's combined probability test) [1]_ uses
a chi-squared statistic to compute a combined p-value. The closely related
Stouffer's Z-score method [2]_ uses Z-scores rather than p-values. The
advantage of Stouffer's method is that it is straightforward to introduce
weights, which can make Stouffer's method more powerful than Fisher's
method when the p-values are from studies of different size [3]_ [4]_.

Fisher's method may be extended to combine p-values from dependent tests
[5]_. Extensions such as Brown's method and Kost's method are not currently
implemented.

References
----------
.. [1] https://en.wikipedia.org/wiki/Fisher%27s_method
.. [2] http://en.wikipedia.org/wiki/Fisher's_method#Relation_to_Stouffer.27s_Z-score_method
.. [3] Whitlock, M. C. "Combining probability from independent tests: the
       weighted Z-method is superior to Fisher's approach." Journal of
       Evolutionary Biology 18, no. 5 (2005): 1368-1373.
.. [4] Zaykin, Dmitri V. "Optimally weighted Z-test is a powerful method
       for combining probabilities in meta-analysis." Journal of
       Evolutionary Biology 24, no. 8 (2011): 1836-1841.
.. [5] https://en.wikipedia.org/wiki/Extensions_of_Fisher%27s_method


return special.chdtrc(df,chisq)<EOL>

Probability value (1-tail) for the Chi^2 probability distribution.

Broadcasting rules apply.

Parameters
----------
chisq : array_like or float > 0

df : array_like or float, probably int >= 1

Returns
-------
chisqprob : ndarray
    The area from `chisq` to infinity under the Chi^2 probability
    distribution with degrees of freedom `df`.


x = np.asarray(x)<EOL>x = np.where(x < <NUM_LIT:1.0>, x, <NUM_LIT:1.0>)  <EOL>return special.betainc(a, b, x)<EOL>

Returns the incomplete beta function.

I_x(a,b) = 1/B(a,b)*(Integral(0,x) of t^(a-1)(1-t)^(b-1) dt)

where a,b>0 and B(a,b) = G(a)*G(b)/(G(a+b)) where G(a) is the gamma
function of a.

The standard broadcasting rules apply to a, b, and x.

Parameters
----------
a : array_like or float > 0

b : array_like or float > 0

x : array_like or float
    x will be clipped to be no greater than 1.0 .

Returns
-------
betai : ndarray
    Incomplete beta function.