Code for statistical binning and related scripts