Example #1
0
    def solve_for_params(params, x_min, x_max):
        lower_mass = 0.01
        upper_mass = 0.99

        # Trial parameters
        alpha, beta = params

        # Equation for the roots defining params which satisfy the constraint
        return (
            invgamma.cdf(x_min, alpha, scale=beta) - lower_mass,
            invgamma.cdf(x_max, alpha, scale=beta) - upper_mass,
        )
Example #2
0
    def test(self, time, cutoff, probability, less_than=True):
        """ Test posterior belief that median time-to-event parameter is less than or greater than some boundary value.

        :param time: test at this time
        :type time: float
        :param cutoff: test median time against this critical value
        :type cutoff: float
        :param probability: require at least this degree of posterior certainty to declare significance
        :type probability: float
        :param less_than: True, to test parameter is less than cut-off, a-posteriori. False to test greater than
        :type less_than: bool
        :return: JSON-able dict object reporting test output
        :rtype: dict

        """

        event_time = np.array(self._times_to_event)
        recruit_time = np.array(self._recruitment_times)

        # Filter to just patients who are registered by time
        registered_patients = recruit_time <= time
        has_failed = time - recruit_time[registered_patients] > event_time[registered_patients]
        survival_time = np.array([min(x, y) for (x, y) in
                                 zip(time - recruit_time[registered_patients], event_time[registered_patients])
                                      ])
        # Update posterior beliefs for mu_E
        alpha_post = self.alpha_prior + sum(has_failed)
        beta_post = self.beta_prior + np.log(2) * sum(survival_time)
        mu_post = beta_post / (alpha_post-1)

        # Run test:
        test_probability = invgamma.cdf(cutoff, a=alpha_post, scale=beta_post) if less_than \
            else 1 - invgamma.cdf(cutoff, a=alpha_post, scale=beta_post)
        stop_trial = test_probability > probability if less_than else test_probability < probability

        test_report = OrderedDict()
        test_report['Time'] = time
        test_report['Patients'] = sum(registered_patients)
        test_report['Events'] = sum(has_failed)
        test_report['TotalEventTime'] = sum(survival_time)
        test_report['AlphaPosterior'] = alpha_post
        test_report['BetaPosterior'] = beta_post
        test_report['MeanEventTimePosterior'] = mu_post
        test_report['MedianEventTimePosterior'] = mu_post * np.log(2)
        test_report['Cutoff'] = cutoff
        test_report['Certainty'] = probability
        test_report['Probability'] = test_probability
        test_report['LessThan'] = atomic_to_json(less_than)
        test_report['Stop'] = atomic_to_json(stop_trial)
        return test_report
def testInvGamma1(seed):
    # Check that Gamma is parameterized correctly
    ripl = get_ripl(seed=seed)
    # samples
    ripl.assume("a", "(inv_gamma 10.0 10.0)", label="pid")
    observed = collectSamples(ripl, "pid")
    # true CDF
    inv_gamma_cdf = lambda x: invgamma.cdf(x, a=10, scale=10)
    return reportKnownContinuous(inv_gamma_cdf, observed)
Example #4
0
    def test_cdf(self):
        shapes = np.arange(0.01, 20, 0.5)
        scales = np.arange(0.01, 10, 0.5)
        xs = np.arange(0.01, 10, 0.5)

        test_params = cartesian([xs, shapes, scales]).astype(np.float64)

        python_results = np.zeros(test_params.shape[0])
        for ind in range(test_params.shape[0]):
            x = test_params[ind, 0]
            shape = test_params[ind, 1]
            scale = test_params[ind, 2]

            python_results[ind] = invgamma.cdf(x, shape, scale=scale)

        opencl_results = invgamma_cdf().evaluate(
            {
                'x': test_params[:, 0],
                'shape': test_params[:, 1],
                'scale': test_params[:, 2]
            }, test_params.shape[0])

        assert_allclose(opencl_results, python_results, atol=1e-7, rtol=1e-7)
 def inverseIG(u, alpha, q):
     res = norm.ppf(invgamma.cdf(u.local_data, alpha, scale=q))
     return Field.from_local_data(u.domain, res)
Example #6
0
 def cdf(self, x: float) -> float:
     # TODO: where is self.beta
     return float(invgamma.cdf(x, self.alpha))
# Display the probability density function (``pdf``):

x = np.linspace(invgamma.ppf(0.01, a), invgamma.ppf(0.99, a), 100)
ax.plot(x, invgamma.pdf(x, a), 'r-', lw=5, alpha=0.6, label='invgamma 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 = invgamma(a)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

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

vals = invgamma.ppf([0.001, 0.5, 0.999], a)
np.allclose([0.001, 0.5, 0.999], invgamma.cdf(vals, a))
# True

# Generate random numbers:

r = invgamma.rvs(a, size=1000)

# And compare the histogram:

ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
ax.legend(loc='best', frameon=False)
plt.show()
Example #8
0
def fit_and_plot(args):
    """
    Read in the TGAS parallax error data (or an already calculated KDE of the parallax error
    distribution) and the "fit" the KDE by with a Gamma distribution. Fit quality is simply judged by
    eye!

    Parameters
    ----------

    args - command line arguments.
    """

    offset, alpha, beta = args['distroParams']
    tgasPlxErrPdfFile = 'TGAS-parallax-errors-pdf.csv'

    if path.isfile(tgasPlxErrPdfFile):
        errpdf = np.genfromtxt(tgasPlxErrPdfFile,
                               comments='#',
                               skip_header=1,
                               delimiter=',',
                               names=['err', 'logdens'],
                               dtype=None)
        x = errpdf['err'][:, np.newaxis]
        logdens = errpdf['logdens']
    else:
        tgasData = fits.open('TGAS-allPlxErrorsVsGmag.fits')[1].data
        eplx = tgasData['parallax_error'][:, np.newaxis]
        kde = KernelDensity(kernel='epanechnikov', bandwidth=0.008).fit(eplx)
        x = np.linspace(0, 1, 1001)[:, np.newaxis]
        logdens = kde.score_samples(x)
        f = open('TGAS-errors-kde.csv', mode='w')
        f.write('#err,logdens\n')
        for xx, yy in zip(x[:, 0], logdens):
            f.write("{0},{1}\n".format(xx, yy))
        f.close()

    xx = np.linspace(0.004, 1, 1000)
    ig = invgamma.pdf(xx, alpha, loc=offset, scale=beta)
    norm = invgamma.cdf(1.0, alpha, loc=offset, scale=beta)
    ig = ig / norm

    useagab(usetex=False, fontfam='sans', sroncolours=False)

    fig = plt.figure(figsize=(12, 10))
    ax = fig.add_subplot(111)
    apply_tufte(ax)
    ax.plot(x[:, 0],
            np.exp(logdens),
            label='KDE of $\\sigma_\\varpi$ distribution',
            lw=3)
    ax.plot(
        xx,
        ig,
        label="Fit with InvGamma($x-{0:.3f}|\\alpha={1:.3f}$, $\\beta={2:.3f})$"
        .format(offset, alpha, beta))
    ax.set_xlabel('$\\varpi$ [mas]')
    ax.set_ylabel('pdf')
    ax.legend(loc='upper right', fontsize=12)
    ax.set_title('Parallax error distribution for TGAS', fontsize=14)

    basename = 'FitOfParallaxErrorDistribution'
    if args['pdfOutput']:
        plt.savefig(basename + '.pdf')
    elif args['pngOutput']:
        plt.savefig(basename + '.png')
    else:
        plt.show()
 def cdf(self, X):
     cdf = invgamma.cdf(X, self.alpha, scale=self.beta)
     return cdf
Example #10
0
               'gamma(1.25, 1) pdf', 'gamma_pdf', (1.25, 1)),
    TestParams(-1, 10, 1000, lambda x: gamma.pdf(x, 1.25, 0, 1 / 2),
               'gamma(1.25, 2) pdf', 'gamma_pdf', (1.25, 2)),
    TestParams(-1, 10, 1000, lambda x: gamma.cdf(x, 1), 'gamma(1, 1) cdf',
               'gamma_cdf', (1, 1)),
    TestParams(-1, 10, 1000, lambda x: gamma.cdf(x, 1.25, 0, 1),
               'gamma(1.25, 1) cdf', 'gamma_cdf', (1.25, 1)),
    TestParams(-1, 10, 1000, lambda x: gamma.cdf(x, 1.25, 0, 1 / 2),
               'gamma(1.25, 2) cdf', 'gamma_cdf', (1.25, 2)),
    TestParams(-1, 10, 1000, lambda x: invgamma.pdf(x, 1),
               'inverse_gamma(1, 1) pdf', 'inverse_gamma_pdf', (1, 1)),
    TestParams(-1, 10, 1000, lambda x: invgamma.pdf(x, 1.25, 0, 1),
               'inverse_gamma(1.25, 1) pdf', 'inverse_gamma_pdf', (1.25, 1)),
    TestParams(-1, 10, 1000, lambda x: invgamma.pdf(x, 1.25, 0, 2),
               'inverse_gamma(1.25, 2) pdf', 'inverse_gamma_pdf', (1.25, 2)),
    TestParams(-1, 10, 1000, lambda x: invgamma.cdf(x, 1),
               'inverse_gamma(1, 1) cdf', 'inverse_gamma_cdf', (1, 1)),
    TestParams(-1, 10, 1000, lambda x: invgamma.cdf(x, 1.25, 0, 1),
               'inverse_gamma(1.25, 1) cdf', 'inverse_gamma_cdf', (1.25, 1)),
    TestParams(-1, 10, 1000, lambda x: invgamma.cdf(x, 1.25, 0, 1 / 2),
               'inverse_gamma(1.25, 2) cdf', 'inverse_gamma_cdf', (1.25, 2)),
]

head = r"""#include <stdio.h>
#include <stdlib.h>

#include "../src/distrs.h"

#define EPS 1e-5

int test_fn(DTYPE *x, DTYPE *y, size_t n, DTYPE (*f)(DTYPE x), char *name)