Exemple #1
0
    def __calculate_single_term_cst_alpha__(self, t, i, alpha):
        i_alpha = i * alpha
        if i_alpha.is_integer():
            return len(t) * [0]
        else:
            sign = (-1)**i
            i_fact = math.factorial(i)

            def catch(func,
                      func_hp,
                      *args,
                      exceptions=Exception,
                      filter_name='error',
                      **kwargs):
                with warnings.catch_warnings():
                    warnings.filterwarnings(filter_name)
                    try:
                        res = func(*args, **kwargs)
                        if res is None:
                            raise exceptions
                        else:
                            return res
                    except exceptions:
                        return func_hp(*args, **kwargs)

            def L_ai_func():
                try:
                    i_fact_recip = i_fact**(-1)
                    L_i_alpha = self.L**i_alpha
                    return sign * L_i_alpha * i_fact_recip
                except:
                    return

            def L_ai_hp_func():
                return fdiv(sign * power(self.L, i_alpha), i_fact)

            L_ai = catch(L_ai_func, L_ai_hp_func, exceptions=OverflowError)
            rec_gamma = rgamma(-i_alpha) if math.isinf(
                sc.rgamma(-i_alpha)) else sc.rgamma(-i_alpha)
            const = L_ai * rec_gamma

            def single_term_func(ti):
                if const < sys.float_info.max:
                    return ti**(-i_alpha - 1) * const
                else:
                    return fmul(ti**(-i_alpha - 1), const)

            def single_term_hp_func(ti):
                return fmul(power(ti, (-i_alpha - 1)), const)

            single_term = [
                catch(single_term_func, single_term_hp_func, time)
                for time in t
            ]

            return single_term
Exemple #2
0
def pdf(x, k, theta):
    """
    Gamma distribution probability density function.

    k is the shape parameter
    theta is the scale parameter (reciprocal of the rate parameter)

    Unlike scipy, a location parameter is not included.
    """
    _validate_k_theta(k, theta)
    x = mpmath.mpf(x)
    k = mpmath.mpf(k)
    theta = mpmath.mpf(theta)
    return mpmath.rgamma(k) / theta**k * x**(k-1) * mpmath.exp(-x/theta)
def f76(x):
    # gammainv
    try:
        return mpmath.rgamma(x)
    except:
        return None
def rgamma_cached(x, dps):
    with mp.workdps(dps):
        return mp.rgamma(x)
Exemple #5
0
    def __calculate_single_term_var_alpha__(self, t, i, alpha):
        i_alpha = [i * a for a in alpha]
        sign = (-1)**i
        i_fact = math.factorial(i)

        def catch(func,
                  func_hp,
                  *args,
                  exceptions=Exception,
                  filter_name='error',
                  **kwargs):
            with warnings.catch_warnings():
                warnings.filterwarnings(filter_name)
                try:
                    res = func(*args, **kwargs)
                    if res is None:
                        raise exceptions
                    else:
                        return res
                except exceptions:
                    return func_hp(*args, **kwargs)

        def L_ai_func(ia):
            try:
                i_fact_recip = i_fact**(-1)
                L_i_alpha = self.L**ia
                return sign * L_i_alpha * i_fact_recip
            except:
                return

        def L_ai_hp_func(ia):
            return fdiv(sign * power(self.L, ia), i_fact)

        L_ai = [
            0 if ia.is_integer() else catch(L_ai_func,
                                            L_ai_hp_func,
                                            ia,
                                            exceptions=(OverflowError,
                                                        Exception))
            for ia in i_alpha
        ]
        rec_gamma = [
            1 if ia.is_integer() else
            rgamma(-ia) if math.isinf(sc.rgamma(-ia)) else sc.rgamma(-ia)
            for ia in i_alpha
        ]
        const = [Lai * rec_g for Lai, rec_g in zip(L_ai, rec_gamma)]

        def single_term_func(ti, ia, c):
            if c < sys.float_info.max:
                return ti**(-ia - 1) * c
            else:
                return fmul(ti**(-ia - 1), c)

        def single_term_hp_func(ti, ia, c):
            return fmul(power(ti, (-ia - 1)), c)

        single_term = [
            0 if ia.is_integer() else catch(single_term_func,
                                            single_term_hp_func, time, ia, c)
            for time, ia, c in zip(t, i_alpha, const)
        ]
        return single_term