def logsf(self, x):

        mu = self.loc
        sigma = self.scale
        delta = self.shape

        if isinstance(x, numbers.Number):
            if x < mu:
                result = 1 - 0.5 * gamma.sf(((mu - x) / sigma) ** delta,a=1 / delta, scale=1)
            else:
                result = 0.5 - 0.5 * gamma.cdf(((x - mu) / sigma) ** delta,a=1 / delta, scale=1)
        else:
            result = np.zeros(len(x))
            result[x < mu] = 1 - 0.5 * gamma.sf(((mu - x[x < mu]) / sigma) ** delta,a=1 / delta, scale=1)

            result[x >= mu] = 0.5 - 0.5 * gamma.cdf(((x[x >= mu] - mu) / sigma) ** delta,a=1 / delta, scale=1)

        return np.log(result)
示例#2
0
def pgamma(y, shape, scale, lower_tail=True, log=False):
    from scipy.stats import gamma
    if lower_tail:
        if log:
            return gamma.logcdf(y, shape, scale=1.0 / scale)
        else:
            return gamma.cdf(y, shape, scale=1.0 / scale)
    else:
        if log:
            return gamma.logsf(y, shape, scale=1.0 / scale)
        else:
            return gamma.sf(y, shape, scale=1.0 / scale)
示例#3
0
def calculate_critical_ts_from_gamma(
        ts, h0_ts_quantile, eta=3.0, xi=1.e-2):
    """Calculates the critical test-statistic value corresponding
    to h0_ts_quantile by fitting the ts distribution with a truncated
    gamma function.

    Parameters
    ----------
    ts : (n_trials,)-shaped 1D ndarray
        The ndarray holding the test-statistic values of the trials.
    h0_ts_quantile : float
        Null-hypothesis test statistic quantile.
    eta : float, optional
        Test-statistic value at which the gamma function is truncated
        from below.
    xi : float, optional
        A small number to numerically discriminate against ts=0.0.

    Returns
    -------
    critical_ts : float
    """
    Ntot = len(ts)
    N = len(ts[ts > xi])
    alpha = N/Ntot

    ts_eta = ts[ts > eta]
    N_prime = len(ts_eta)
    alpha_prime = N_prime/N

    obj = lambda x: truncated_gamma_logpdf(x[0], x[1], eta=eta,
                                           ts_above_eta=ts_eta,
                                           N_above_eta=N_prime)
    x0 = [0.75, 1.8]  # Initial values of function parameters.
    bounds = [[0.1, 10], [0.1, 10]]  # Ranges for the minimization fitter.
    r = minimize(obj, x0, bounds=bounds)
    pars = r.x
    
    norm = alpha*(alpha_prime/gamma.sf(eta, a=pars[0], scale=pars[1]))
    critical_ts = gamma.ppf(1 - 1./norm*h0_ts_quantile, a=pars[0], scale=pars[1])

    if(critical_ts < eta):
        raise ValueError(
            'Critical ts value = %e, eta = %e. The calculation of the critical '
            'ts value from the fit is correct only for critical ts larger than '
            'the truncation threshold eta.',
            critical_ts, eta)

    return critical_ts
示例#4
0
def gamma_sf(x, a, loc, b):
    if a == 0 or b == 0:
        sf = 0
    else:
        sf = gamma.sf(x, a, loc, b)
    return sf
示例#5
0
def gamma_dist_prob(array, mean, sd):
    theta = numpy.float64(sd) * sd / mean
    k = mean / theta
    return 1.0 - (gamma.sf(array + 1, k, 0, theta) /
                  gamma.sf(array, k, 0, theta))
            # for fill_between
            px = np.arange(0, X1, 0.01)

            ax.fill_between(px,
                            gamma.pdf(px, a=mean_input, scale=scale_input),
                            alpha=0.5,
                            color='r')

            # for text
            ax.text(1.0,
                    0.075,
                    f"P(X<{X1})\n%.2f" % round(less, 2),
                    fontsize=20)
            st.pyplot()
        elif X1 == 0 and X2 > 0:
            more = gamma.sf(x=X2, a=mean_input, scale=scale_input)
            fig, ax = plt.subplots()
            x = np.arange(0, 12, 0.001)
            ax.plot(x, gamma.pdf(x, a=mean_input, scale=scale_input))
            ax.set_title(
                f"Gamma Dist. with mean = {mean_input}, std_dv = {scale_input}"
            )
            ax.set_xlabel('X-Values')
            ax.set_ylabel('PDF(X)')
            ax.set_ylim(0, 0.4)
            # for fill_between
            px = np.arange(X2, 12, 0.01)

            ax.fill_between(px,
                            gamma.pdf(px, a=mean_input, scale=scale_input),
                            alpha=0.5,
示例#7
0
 def _gamma_inc(self, a, x):
     return pgamma.sf(x, a) * gamma(a)
示例#8
0
def gamma_sf(x, a, loc, b):
    if a == 0 or b == 0:
        sf = 0
    else:
        sf = gamma.sf(x, a, loc, b)
    return sf