# Buggy code
def fit(self, data, *args, **kwds):
    """%(super)s
    In the special case where both `floc` and `fscale` are given, a
    `ValueError` is raised if any value `x` in `data` does not satisfy
    `floc < x < floc + fscale`.
    """
    # Override rv_continuous.fit, so we can more efficiently handle the
    # case where floc and fscale are given.

    f0 = kwds.get("f0", None)
    f1 = kwds.get("f1", None)
    floc = kwds.get("floc", None)
    fscale = kwds.get("fscale", None)

    if floc is None or fscale is None:
        # do general fit
        return super(beta_gen, self).fit(data, *args, **kwds)

    # Special case: loc and scale are constrained, so we are fitting
    # just the shape parameters.  This can be done much more efficiently
    # than the method used in `rv_continuous.fit`.  (See the subsection
    # "Two unknown parameters" in the section "Maximum likelihood" of
    # the Wikipedia article on the Beta distribution for the formulas.)

    # Normalize the data to the interval [0,1].
    data = (ravel(data) - floc) / fscale
    xbar = data.mean()

    if f0 is not None or f1 is not None:
        # One of the shape parameters is fixed.

        if f0 is not None:
            # The shape parameter a is fixed, so swap the parameters
            # and flip the data.  We always solve for `a`.  The result
            # will be swapped back before returning.
            b = f0
            data = 1 - data
            xbar = 1 - xbar
        else:
            b = f1

        # Sum the logs of data.  A FitDataError exception is raised if any
        # value in data is not between 0 and 1 (exclusive).
        s1 = _sumlog(data, floc, fscale, check_upper=True)

        # Initial guess for a.  Use the formula for the mean of the beta
        # distribution, E[x] = a / (a + b), to generate a reasonable
        # starting point based on the mean of the data and the given
        # value of b.
        a = b * xbar / (1 - xbar)

        # Compute the MLE for `a` by solving _beta_mle_a.
        theta, info, ier, mesg = optimize.fsolve(
            _beta_mle_a, a, args=(b, len(data), s1), full_output=True
        )
        if ier != 1:
            raise FitSolverError(mesg=mesg)
        a = theta[0]

        if f0 is not None:
            # The shape parameter a was fixed, so swap back the
            # parameters.
            a, b = b, a

    else:
        # Neither of the shape parameters is fixed.

        # Compute the sums of the logs of data and 1-data. This will raise
        # a FitDataError exception if any value in data is not in the
        # interval (0, 1).
        s1 = _sumlog(data, floc, fscale, check_upper=True)
        s2 = _sumlog(1 - data, floc, fscale)

        # Use the "method of moments" to estimate the initial
        # guess for a and b.
        fac = xbar * (1 - xbar) / data.var(ddof=0) - 1
        a = xbar * fac
        b = (1 - xbar) * fac

        # Compute the MLE for a and b by solving _beta_mle_ab.
        theta, info, ier, mesg = optimize.fsolve(
            _beta_mle_ab, [a, b], args=(len(data), s1, s2), full_output=True
        )
        if ier != 1:
            raise FitSolverError(mesg=mesg)
        a, b = theta

    return a, b, floc, fscale


# Traceback (scipy.stats.distributions.FitSolverError)
======================================================================
ERROR: test_distributions.TestFitMethod.test_fix_fit_beta
----------------------------------------------------------------------
Traceback (most recent call last):
File "X:\Python33-x32\lib\site-packages\nose\case.py", line 198, in
runTest
self.test(*self.arg)
File
"X:\Python33-x32\lib\site-packages\scipy\stats\tests\test_distributions.py",
line 928, in test_fix_fit_beta
assert_raises(ValueError, stats.beta.fit, x, floc=0.5, fscale=1)
File "X:\Python33-x32\lib\site-packages\numpy\testing\utils.py", line
1019, in assert_raises
return nose.tools.assert_raises(*args,**kwargs)
File "X:\Python33-x32\lib\unittest\case.py", line 570, in assertRaises
return context.handle('assertRaises', callableObj, args, kwargs)
File "X:\Python33-x32\lib\unittest\case.py", line 135, in handle
callable_obj(*args, **kwargs)
File
"X:\Python33-x32\lib\site-packages\scipy\stats\distributions.py", line
2533, in fit
raise FitSolverError(mesg=mesg)
scipy.stats.distributions.FitSolverError: Solver for the MLE equations
failed to converge: The iteration is not making good progress, as
measured by the   improvement from the last ten iterations.

# Fixed code
def fit(self, data, *args, **kwds):
    """%(super)s
    In the special case where both `floc` and `fscale` are given, a
    `ValueError` is raised if any value `x` in `data` does not satisfy
    `floc < x < floc + fscale`.
    """
    # Override rv_continuous.fit, so we can more efficiently handle the
    # case where floc and fscale are given.

    f0 = kwds.get("f0", None)
    f1 = kwds.get("f1", None)
    floc = kwds.get("floc", None)
    fscale = kwds.get("fscale", None)

    if floc is None or fscale is None:
        # do general fit
        return super(beta_gen, self).fit(data, *args, **kwds)

    # Special case: loc and scale are constrained, so we are fitting
    # just the shape parameters.  This can be done much more efficiently
    # than the method used in `rv_continuous.fit`.  (See the subsection
    # "Two unknown parameters" in the section "Maximum likelihood" of
    # the Wikipedia article on the Beta distribution for the formulas.)

    # Normalize the data to the interval [0,1].
    data = (ravel(data) - floc) / fscale
    if np.any(data <= 0) or np.any(data >= 1):
        raise FitDataError("beta", lower=floc, upper=floc + fscale)
    xbar = data.mean()

    if f0 is not None or f1 is not None:
        # One of the shape parameters is fixed.

        if f0 is not None:
            # The shape parameter a is fixed, so swap the parameters
            # and flip the data.  We always solve for `a`.  The result
            # will be swapped back before returning.
            b = f0
            data = 1 - data
            xbar = 1 - xbar
        else:
            b = f1

        # Initial guess for a.  Use the formula for the mean of the beta
        # distribution, E[x] = a / (a + b), to generate a reasonable
        # starting point based on the mean of the data and the given
        # value of b.
        a = b * xbar / (1 - xbar)

        # Compute the MLE for `a` by solving _beta_mle_a.
        theta, info, ier, mesg = optimize.fsolve(
            _beta_mle_a, a, args=(b, len(data), np.log(data).sum()), full_output=True
        )
        if ier != 1:
            raise FitSolverError(mesg=mesg)
        a = theta[0]

        if f0 is not None:
            # The shape parameter a was fixed, so swap back the
            # parameters.
            a, b = b, a

    else:
        # Neither of the shape parameters is fixed.

        # s1 and s2 are used in the extra arguments passed to _beta_mle_ab
        # by optimize.fsolve.
        s1 = np.log(data).sum()
        s2 = np.log(1 - data).sum()

        # Use the "method of moments" to estimate the initial
        # guess for a and b.
        fac = xbar * (1 - xbar) / data.var(ddof=0) - 1
        a = xbar * fac
        b = (1 - xbar) * fac

        # Compute the MLE for a and b by solving _beta_mle_ab.
        theta, info, ier, mesg = optimize.fsolve(
            _beta_mle_ab, [a, b], args=(len(data), s1, s2), full_output=True
        )
        if ier != 1:
            raise FitSolverError(mesg=mesg)
        a, b = theta

    return a, b, floc, fscale