Version for 1st PR to StatsBase
Fixed some type instabilities. This version is ready to be incorporated in StatsBase via a pull request.
See discussion at JuliaStats/StatsBase.jl#634
This PR was cancelled after tests revealed "overflow errors" on 32 bit