예제 #1
0
def test_gamma(rnd, a, loc=0, scale=1):
    # gamma random variate generator
    gamma_dist = RVGs.Gamma(a=a, scale=scale, loc=loc)

    # obtain samples
    samples = get_samples(gamma_dist, rnd)

    # report mean and variance
    print_test_results('Gamma',
                       samples,
                       expectation=a * scale + loc,
                       variance=a * scale**2)
예제 #2
0
def test_fitting_gamma():

    print('\nTesting Gamma with a=10, scale=1, loc=2:')
    dist = RVGs.Gamma(a=10, scale=1, loc=2)
    print('  mean, st dev: ', dist.get_mean_st_dev())
    print('  percentile interval: ', dist.get_percentile_interval(alpha=0.05))

    data = np.array(get_samples(dist, np.random))
    dict_mm_results = RVGs.Gamma.fit_mm(mean=np.average(data),
                                        st_dev=np.std(data),
                                        fixed_location=2)
    dict_ml_results = RVGs.Gamma.fit_ml(data=data, fixed_location=2)

    print("  Fit:")
    print("    MM:", dict_mm_results)
    print("    ML:", dict_ml_results)

    # plot the fitted distributions
    Plot.plot_gamma_fit(data=data,
                        fit_results=dict_mm_results,
                        title='Method of Moment')
    Plot.plot_gamma_fit(data=data,
                        fit_results=dict_ml_results,
                        title='Maximum Likelihood')
import numpy as np

import SimPy.RandomVariateGenerators as RVGs

# a random number generator
rng = np.random.RandomState(seed=0)

# gamma
gamma = RVGs.Gamma(a=0.9835458832943496, loc=0, scale=1.9199027077975956)
# generate 10 realizations from gamma
print('Realizations from gamma:')
for i in range(10):
    print(gamma.sample(rng))

# gamma-Poisson
gammaPoisson = RVGs.GammaPoisson(a=0.3693765965041004,
                                 gamma_scale=9.154827879319111,
                                 loc=0)
# generate 10 realizations from gamma-Poisson
print('Realizations from gamma-Poisson:')
for i in range(10):
    print(gammaPoisson.sample(rng))
    def __init__(self, therapy):

        self.therapy = therapy
        self.probMatrixRVG = []     # list of dirichlet distributions for transition probabilities
        self.lnRelativeRiskRVG = None  # normal distribution for the natural log of the treatment relative risk
        self.annualStateCostRVG = []  # list of gamma distributions for the annual cost of states
        self.annualStateUtilityRVG = []  # list of beta distributions for the annual utility of states
        self.annualTreatmentCostRVG = None   # gamma distribution for treatment cost

        # create Dirichlet distributions for transition probabilities
        j = 0
        for probs in Data.TRANS_MATRIX:
            # note:  for a Dirichlet distribution all values of the argument 'a' should be non-zero.
            # setting if_ignore_0s to True allows the Dirichlet distribution to take 'a' with zero values.
            self.probMatrixRVG.append(RVGs.Dirichlet(
                a=probs, if_ignore_0s=True))
            j += 1

        # treatment relative risk
        rr_ci = [0.365, 0.71]   # confidence interval of the treatment relative risk

        # find the mean and st_dev of the normal distribution assumed for ln(RR)
        # sample mean ln(RR)
        mean_ln_rr = math.log(Data.TREATMENT_RR)
        # sample standard deviation of ln(RR)
        std_ln_rr = \
            (math.log(rr_ci[1]) - math.log(rr_ci[0])) / (2 * stat.norm.ppf(1 - 0.05 / 2))
        # create a normal distribution for ln(RR)
        self.lnRelativeRiskRVG = RVGs.Normal(loc=mean_ln_rr,
                                             scale=std_ln_rr)

        # create gamma distributions for annual state cost
        for cost in Data.ANNUAL_STATE_COST:

            # if cost is zero, add a constant 0, otherwise add a gamma distribution
            if cost == 0:
                self.annualStateCostRVG.append(RVGs.Constant(value=0))
            else:
                # find shape and scale of the assumed gamma distribution
                # no data available to estimate the standard deviation, so we assumed st_dev=cost / 5
                fit_output = RVGs.Gamma.fit_mm(mean=cost, st_dev=cost / 5)
                # append the distribution
                self.annualStateCostRVG.append(
                    RVGs.Gamma(a=fit_output["a"],
                               loc=0,
                               scale=fit_output["scale"]))

        # create a gamma distribution for annual treatment cost
        if self.therapy == Therapies.MONO:
            annual_cost = Data.Zidovudine_COST
        else:
            annual_cost = Data.Zidovudine_COST + Data.Lamivudine_COST
        fit_output = RVGs.Gamma.fit_mm(mean=annual_cost, st_dev=annual_cost / 5)
        self.annualTreatmentCostRVG = RVGs.Gamma(a=fit_output["a"],
                                                 loc=0,
                                                 scale=fit_output["scale"])

        # create beta distributions for annual state utility
        for utility in Data.ANNUAL_STATE_UTILITY:
            # if utility is zero, add a constant 0, otherwise add a beta distribution
            if utility == 0:
                self.annualStateCostRVG.append(RVGs.Constant(value=0))
            else:
                # find alpha and beta of the assumed beta distribution
                # no data available to estimate the standard deviation, so we assumed st_dev=cost / 4
                fit_output = RVGs.Beta.fit_mm(mean=utility, st_dev=utility / 4)
                # append the distribution
                self.annualStateUtilityRVG.append(
                    RVGs.Beta(a=fit_output["a"], b=fit_output["b"]))