BUG: stats: Several fixes for the fit method of the gamma and beta distributions.

This should close gh-2514.

The specific changes are summarized in the commit messages.