示例#1
0
def _posterior(df, r, c, col='new', a=1, b=3, gamma=GAMMA):
    df = df.copy()
    posteriors(df, r, c, col, a, b)
    df['data'] = df[col]
    # Posterior on lambda, the daily rate of infection.
    df['posterior'] = r*betaprime.median(df.alpha, df.beta)
    df['pos5'] = r*betaprime.ppf(0.05, df.alpha, df.beta)
    df['pos95'] = r*betaprime.ppf(0.95, df.alpha, df.beta)
    df['r'] = (np.log(df.posterior.shift(-1)) - np.log(df.posterior))/GAMMA + 1
    df['r5'] = (np.log(df.pos5.shift(-1)) - np.log(df.posterior))/GAMMA + 1
    df['r95'] = (np.log(df.pos95.shift(-1)) - np.log(df.posterior))/GAMMA + 1
    return df[~df.r.isna()]
def generate_betaprime(param_list):
    """
    This method generates a list of pairs (quantile skewness, quantile kurtosis)
    for inverse beta distribution with given parameters
    :param param_list: dict[str, list[float]]
        A dictionary with keys which correspond to the names of parameters of a
        given distribution ('a' and 'b' in case of inverse beta distribution) and
        values that store specific parameter ranges
    :return: list[tuple[float, float]]
        A list of pairs (quantile skewness, quantile kurtosis) which can be viewed
        as a point and act as a representation of inverse beta distribution with
        specific parameters on a Pearson System
    """
    if len(param_list) == 0:
        raise EmptyParametersDict(
            'No parameter dictionary was provided for inverse beta distribution.'
        )

    if 'a' not in param_list or 'b' not in param_list:
        raise WrongParameterName(
            'Wrong parameter names provided for beta distribution.')

    a_arr = param_list['a']
    b_arr = param_list['b']

    if len(a_arr) == 0:
        raise EmptyParameterList(
            'No parameter list was provided for parameter alpha of inverse beta distribution.'
        )
    if len(b_arr) == 0:
        raise EmptyParameterList(
            'No parameter list was provided for parameter beta of inverse beta distribution.'
        )

    st_arr = []
    for curr_a in a_arr:
        for curr_b in b_arr:
            curr_e = [betaprime.ppf(o, curr_a, curr_b) for o in OCTILES]
            curr_s = (curr_e[5] - 2 * curr_e[3] + curr_e[1]) / (curr_e[5] -
                                                                curr_e[1])
            curr_t = (curr_e[6] - curr_e[4] + curr_e[2] -
                      curr_e[0]) / (curr_e[5] - curr_e[1])

            st_arr.append((curr_s, curr_t))

    return st_arr
from scipy.stats import betaprime
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)

# Calculate a few first moments:

a, b = 5, 6
mean, var, skew, kurt = betaprime.stats(a, b, moments='mvsk')

# Display the probability density function (``pdf``):

x = np.linspace(betaprime.ppf(0.01, a, b),
                betaprime.ppf(0.99, a, b), 100)
ax.plot(x, betaprime.pdf(x, a, b),
       'r-', lw=5, alpha=0.6, label='betaprime pdf')

# Alternatively, the distribution object can be called (as a function)
# to fix the shape, location and scale parameters. This returns a "frozen"
# RV object holding the given parameters fixed.

# Freeze the distribution and display the frozen ``pdf``:

rv = betaprime(a, b)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

# Check accuracy of ``cdf`` and ``ppf``:

vals = betaprime.ppf([0.001, 0.5, 0.999], a, b)
np.allclose([0.001, 0.5, 0.999], betaprime.cdf(vals, a, b))
# True
示例#4
0
文件: util.py 项目: RamboWANG/acl2019
def f_measure_ppf(q, a, b):
    from scipy.stats import betaprime
    betaprime_ppf = betaprime.ppf(1 - q, a, b)
    f_ppf = 1.0 / (1 + 0.5 * betaprime_ppf)
    return f_ppf
示例#5
0
 def ppf(self, q):
     from scipy.stats import betaprime
     beta_prime_ppf = betaprime.ppf(1 - q, self.a, self.b)
     f_ppf = 1.0 / (1 + 0.5 * beta_prime_ppf)
     return f_ppf