def preston_bin(data, max_num):
log_ub = np.ceil(np.log2(max_num))<EOL>if log_ub == <NUM_LIT:0>:<EOL><INDENT>boundaries = np.array([<NUM_LIT:0>, <NUM_LIT:1>])<EOL><DEDENT>elif log_ub == <NUM_LIT:1>:<EOL><INDENT>boundaries = np.arange(<NUM_LIT:1>, <NUM_LIT:4>)<EOL><DEDENT>else:<EOL><INDENT>boundaries = <NUM_LIT:2> ** np.arange(<NUM_LIT:0>, log_ub + <NUM_LIT:1>)<EOL>boundaries = np.insert(boundaries, <NUM_LIT:2>, <NUM_LIT:3>)<EOL>boundaries[<NUM_LIT:3>:] = boundaries[<NUM_LIT:3>:] + <NUM_LIT:1><EOL><DEDENT>hist_data = np.histogram(data, bins=boundaries)<EOL>return hist_data<EOL>
Bins data on base 2 using Preston's method

Parameters
----------
data : array-like
    Data to be binned
max_num :  float
    The maximum upper value of the data

Returns
-------
tuple
    (binned_data, bin_edges)

Notes
-----
Uses Preston's method of binning, which has exclusive lower boundaries and
inclusive upper boundaries. Densities are not split between bins.

Examples
--------

>>> import macroeco.compare as comp
>>> import numpy as np

>>> # Load some data and get Preston bins
>>> data = np.array([1, 1, 1, 1, 4, 5, 6, 7, 12, 34, 56])
>>> comp.preston_bin(data, np.max(data))
(array([4, 0, 1, 3, 1, 0, 2]),
array([  1.,   2.,   3.,   5.,   9.,  17.,  33.,  65.]))

References
----------
.. [#]
   Preston, F. (1962). The canonical distribution of commonness and rarity.
   Ecology, 43, 185-215