scipy/scipy — pull request #142 by sepastian (2012-01-27 20:17:32+00:00)
Title: ENH: adding optional param 'interpolationmethod' to scoreatpercentile

If the quantile requested lies between two data points `(i, j)`, `stats.scoreatepercentile` will interpolate between these points to arrive at the score to return. This is not always desirable.

This pull request adds a new optional parameter `interpolationmethod` to `stats.scoreatpercentile`. The default behavior, i.e. when not specifying a value for `interpolationmethod` does not change.

In some situations such as winsorizing a data set to get rid of outliers, no interpolation should take place. Rather, it should be possible to return one of `(i, j)` directly. For explanation, assume that the 10th percentile should be calculated for (the already sorted) array `a` which contains an outlier showing a value of -1,000,000:

```python
>>> import scipy.stats
>>> import numpy
>>> a = np.arange(9)+1
>>> a[0] = -1000000
>>> a
array([-1000000, 2, 3, 4, 5, 6,
7, 8, 9])
>>> scipy.stats.scoreatpercentile(a,10)
-199998.39999999991
```

The score returned by the current version of `scoreatpercentile` would be strongly distorted by the outlier. The updated version in this patch can return the desired value of `2` for the example above, ignoring the outlier:

```python
>>> scipy.stats.scoreatpercentile(a,10,interpolationmethod='lower')
-1000000
>>> scipy.stats.scoreatpercentile(a,10,interpolationmethod='higher')
2
```

Test in `scipy/stats/tests/test_stats.py` have been updated.