Q:

Interval containing specified percent of values

With numpy or scipy, is there any existing method that will return the endpoints of an interval which contains a specified percent of the values in a 1D array? I realize that this is simple to write myself, but it seems like the kind of thing that might be built in, although I can't find it.
E.g:
>>> import numpy as np
>>> x = np.random.randn(100000)
>>> print(np.bounding_interval(x, 0.68))

Would give approximately (-1, 1)

A:

You can use np.percentile:
In [29]: x = np.random.randn(100000)

In [30]: p = 0.68

In [31]: lo = 50*(1 - p)

In [32]: hi = 50*(1 + p)

In [33]: np.percentile(x, [lo, hi])
Out[33]: array([-0.99206523,  1.0006089 ])

There is also scipy.stats.scoreatpercentile:
In [34]: scoreatpercentile(x, [lo, hi])
Out[34]: array([-0.99206523,  1.0006089 ])

