def fit_gamma_samples(samples, num_points=500, bound=np.pi, fix_location=False, return_fitted_data=True, should_plot=True, normalise=True, debug=True):
    '''
        Fit a Gamma distribution on the samples, optionaly plotting the fit
    '''

    if fix_location:
        fit_alpha, fit_loc, fit_beta = spst.gamma.fit(samples, floc=1e-18)
    else:
        fit_alpha, fit_loc, fit_beta = spst.gamma.fit(samples)

    # x = np.linspace(samples.min()*1.5, samples.max()*1.5, 1000)
    x = np.linspace(-bound, bound, num_points)
    # dx = 2.*bound/(num_points-1.)

    fitted_data = spst.gamma.pdf(x, fit_alpha, fit_loc, fit_beta)

    if debug:
        print "Alpha: %.3f, Location: %.3f, Beta: %.3f" % (fit_alpha, fit_loc, fit_beta)

    if should_plot:
        if normalise:
            hist_angular_data(samples, norm='max', bins=num_points)
            plt.plot(x, fitted_data/np.max(fitted_data), 'r')
        else:
            hist_angular_data(samples, bins=num_points)
            plt.plot(x, fitted_data, 'r')

        plt.show()

    if return_fitted_data:
        return dict(parameters=np.array([fit_alpha, fit_loc, fit_beta]), fitted_data=fitted_data)
    else:
        return np.array([fit_alpha, fit_loc, fit_beta])
def fit_vonmises_samples(samples, num_points=500, return_fitted_data=True, should_plot=True, normalise=True, debug=True):
    '''
        Fit a Von Mises distribution on samples, optionaly plotting the fit.
    '''

    fit_kappa, fit_mu, fit_scale = spst.vonmises.fit(samples, fscale=1.0)

    # x = np.linspace(samples.min()*1.5, samples.max()*1.5, 1000)
    x = np.linspace(-np.pi, np.pi, num_points)
    # dx = 2.*np.pi/(num_points-1.)

    fitted_data = spst.vonmises.pdf(x, fit_kappa, loc=fit_mu, scale=fit_scale)

    if debug:
        print "mu: %.3f, kappa: %.3f" % (fit_mu, fit_kappa)

    if should_plot:
        if normalise:
            hist_angular_data(samples, norm='density', bins=num_points)
            plt.plot(x, fitted_data, 'r')
        else:
            hist_angular_data(samples, bins=num_points)
            plt.plot(x, fitted_data, 'r')

        plt.show()

    if return_fitted_data:
        return dict(parameters=np.array([fit_mu, fit_kappa]), fitted_data=fitted_data, support=x)
    else:
        return np.array([fit_mu, fit_kappa])
def fit_gaussian_samples(samples, num_points=500, bound=np.pi, should_plot = True, return_fitted_data = True, normalise = True, debug = False):
    """
        Fit a 1D Gaussian on the samples provided.

        Plot the result if desired
    """

    mean_fit = np.mean(samples)
    std_fit = np.std(samples)

    # x = np.linspace(samples.min()*1.5, samples.max()*1.5, 1000)
    x = np.linspace(-bound, bound, num_points)
    dx = np.diff(x)[0]

    print mean_fit
    print std_fit

    fitted_data = spst.norm.pdf(x, mean_fit, std_fit)

    if debug:
        print "Mean: %.3f, Std: %.3f" % (mean_fit, std_fit)

    if should_plot:
        if normalise:
            hist_angular_data(samples, norm='max', bins=num_points)
            plt.plot(x, fitted_data/np.max(fitted_data), 'r')
        else:
            hist_angular_data(samples, bins=num_points)
            plt.plot(x, fitted_data, 'r')

        plt.show()

    if return_fitted_data:
        return dict(parameters=np.array([mean_fit, std_fit]), fitted_data=fitted_data)
    else:
        return np.array([mean_fit, std_fit])