def simulate(self, sim_length): """ simulate the single patient over a simulation length""" # random number generator for this patient self._rng = Rand.RNG(self._id) k = 0 # current time step # while the patient is alive and the simulation length is not yet reached while self._stateMonitor.get_if_alive( ) and k * self._delta_t < sim_length: # find the transition probabilities of the future states trans_prob = self._parameters.get_transition_prob( self._stateMonitor.get_current_state()) # create an empirical distribution empirical_dist = Rand.Empirical(trans_prob) # sample from the empirical distribution to get a new state new_state_index = empirical_dist.sample(self._rng) # update health state self._stateMonitor.update(k, Parameter.HealthState(new_state_index)) # increment time step k += 1
def simulate(self, sim_length): """ simulate patient over specified simulation length """ # random number generated for patient self._rng = RndCls.RNG(self._id) k = 0 # current time step # while patient is alive and simulation length not yet reached while self._stateMonitor.get_if_alive( ) and k * self._delta_t < sim_length: # find the transition probabilities of the future states trans_probs = self._parameters.get_transition_prob( self._stateMonitor.get_current_state()) # create empirical distribution empirical_dist = RndCls.Empirical(trans_probs) # sample from empirical distribution to get new state (returns an integer) new_state_index = empirical_dist.sample(self._rng) # update health state self._stateMonitor.update(k, Parameters.HealthStates(new_state_index)) # increment time step k += 1
def simulate(self, sim_length): """ simulate the patient over the specified simulation length """ # random number generator for this patient self._rng = rndClasses.RNG(self._id) k = 0 # current time step # while the patient is alive and simulation length is not yet reached while (self.healthstat != 3 or self.healthstat != 4) and k * delta_t < sim_length: # find the transition probabilities of the future states trans_probs = TRANS[self.THERAPY][self.healthstat] # create an empirical distribution empirical_dist = rndClasses.Empirical(trans_probs) # sample from the empirical distribution to get a new state # (returns an integer from {0, 1, 2, ...}) new_state_index = empirical_dist.sample(self._rng) if self.healthstat == 1: self.STROKE += 1 #caculate cost and utality cost = TRANS_COST[self.THERAPY][self.healthstat] * delta_t utility = TRANS_UTILITY[self.THERAPY][self.healthstat] * delta_t # update total discounted cost and utility (corrected for the half-cycle effect) self.totalDiscountCost += \ EconCls.pv(cost, Discount_Rate*delta_t, k + 1) self.totalDiscountUtility += \ EconCls.pv(utility, Discount_Rate*delta_t, k + 1) # update health state self.healthstat = new_state_index[0] # increment time step k += 1 self.survival = k * delta_t
def __init__(self, seed, therapy): #initialize base class _Parameters.__init__(self,therapy) self._rng = Random.RNG(seed) # random number generator to sample from parameter distributions self._infectionProbMatrixRVG = [] # list of dirichlet distributions for transition probabilities self._lnRelativeRiskRVG = None # random variate generator for the natural log of the treatment relative risk self._annualStateCostRVG = [] # list of random variate generators for the annual cost of states self._annualStateUtilityRVG = [] # list of random variate generators for the annual utility of states #transition probabilities # j = 0 # for prob in Data.TRANS_MATRIX: # self._infectionProbMatrixRVG.append(Random.Dirichlet(prob[j:])) # j += 1 # annual state cost for cost in Data.ANNUAL_STATE_COST: # find shape and scale of the assumed gamma distribution estDic = Est.get_gamma_params(mean=cost, st_dev=cost/4) # append the distribution self._annualStateCostRVG.append( Random.Gamma(a=estDic["a"], loc=0, scale=estDic["scale"])) # annual state utility for utility in Data.ANNUAL_STATE_UTILITY: # find alpha and beta of the assumed beta distribution estDic = Est.get_beta_params(mean=utility, st_dev=utility/4) # append the distribution self._annualStateUtilityRVG.append( Random.Beta(a=estDic["a"], b=estDic["b"])) # resample parameters self.__resample()
def simulate(self, sim_length): """ simulate the patient over the specified simulation length """ # random number generator for this patient self._rng = rndClasses.RNG(self._id) k = 0 # current time step # while the patient is alive and simulation length is not yet reached while self._stateMonitor.get_if_alive( ) and k * self._delta_t < sim_length: # find the transition probabilities of the future states trans_probs = self._param.get_transition_prob( self._stateMonitor.get_current_state()) # create an empirical distribution empirical_dist = rndClasses.Empirical(trans_probs) # sample from the empirical distribution to get a new state # (returns an integer from {0, 1, 2, ...}) new_state_index = empirical_dist.sample(self._rng) # update health state self._stateMonitor.update(k, P.HealthStats(new_state_index)) # increment time step k += 1
def simulate(self, sim_length): """ simulate the patient over the specified simulation length """ # random number generator for this patient self._rng = rndClasses.RNG(self._id) # from now on use random number generator from support library k = 0 while self._stateMonitor.get_if_alive() and k*self._delta_t < sim_length: trans_prob = self._param.get_transition_prob(self._stateMonitor.get_current_state()) empirical_dist = rndClasses.Empirical(trans_prob) new_state_index = empirical_dist.sample(self._rng) self._stateMonitor.update(k, P.HealthStats(new_state_index)) k += 1
def __init__(self, seed, therapy): # initializing the base class _Parameters.__init__(self, therapy) self._rng = Random.RNG( seed ) # random number generator to sample from parameter distributions self._hivProbMatrixRVG = [ ] # list of dirichlet distributions for transition probabilities self._lnRelativeRiskRVG = None # random variate generator for the natural log of the treatment relative risk self._annualStateCostRVG = [ ] # list of random variate generators for the annual cost of states self._annualStateUtilityRVG = [ ] # list of random variate generators for the annual utility of states # HIV transition probabilities j = 0 for prob in Data.TRANS_MATRIX: self._hivProbMatrixRVG.append(Random.Dirichlet(prob[j:])) j += 1 # treatment relative risk # find the mean and st_dev of the normal distribution assumed for ln(RR) sample_mean_lnRR = math.log(Data.TREATMENT_RR) sample_std_lnRR = \ (math.log(Data.TREATMENT_RR_CI[1])-math.log(Data.TREATMENT_RR_CI[0]))/(2*stat.norm.ppf(1-0.05/2)) self._lnRelativeRiskRVG = Random.Normal(loc=sample_mean_lnRR, scale=sample_std_lnRR) # annual state cost for cost in Data.ANNUAL_STATE_COST: # find shape and scale of the assumed gamma distribution estDic = Est.get_gamma_params(mean=cost, st_dev=cost / 4) # append the distribution self._annualStateCostRVG.append( Random.Gamma(a=estDic["a"], loc=0, scale=estDic["scale"])) # annual state utility for utility in Data.ANNUAL_STATE_UTILITY: # find alpha and beta of the assumed beta distribution estDic = Est.get_beta_params(mean=utility, st_dev=utility / 4) # append the distribution self._annualStateUtilityRVG.append( Random.Beta(a=estDic["a"], b=estDic["b"])) # resample parameters self.__resample()
def simulate(self, sim_length): self._rng = RndClasses.RNG(self._id) t = 0 while self._healthStateMonitor.get_if_alive( ) and t * self._delta_t < sim_length: trans_prob = self._param.get_prob_matrix( self._healthStateMonitor.get_current_state()) empirical_dist = RndClasses.Empirical(trans_prob) new_state_index = empirical_dist.sample(self._rng) self._healthStateMonitor.update( t, Parameters.HealthStates(new_state_index)) t += 1
def __init__(self, seed, therapy): self._rng = Random.RNG( seed ) # random number generator to sample from parameter distributions self._StrokeProbMatrixRVG = [ ] # list of dirichlet distributions for transition probabilities self._lnRelativeRiskRVG = None # random variate generator for the treatment relative risk # Stroke transition probabilities j = 0 for prob in Data.TRANS_MATRIX: self._StrokeProbMatrixRVG.append(Random.Dirichlet(prob[j:])) j += 1 # resample parameters self.__resample()
def __init__(self, seed, therapy): # initializing the base class _Parameters.__init__(self, therapy) self._rng = Random.RNG( seed ) # random number generator to sample from parameter distributions self._hivProbMatrixRVG_LDCT = [ ] # list of dirichlet distributions for transition probabilities of LDCT self._hivProbMatrixRVG_PLCO = [] #self._lnRelativeRiskRVG = None # random variate generator for the natural log of the treatment relative risk self._annualStateCostRVG = [ ] # list of random variate generators for the annual cost of states self._annualStateUtilityRVG = [ ] # list of random variate generators for the annual utility of states # HIV transition probabilities j = 0 for prob in Data.TRANS_MATRIX_LDCT: self._hivProbMatrixRVG_LDCT.append(Random.Dirichlet(prob[j:])) j += 1 # you should make sure everything you use is not "0" in the dirichlet for prob in Data.TRANS_MATRIX_PLCO: self._hivProbMatrixRVG_PLCO.append(Random.Dirichlet(prob[j:])) j += 1 # treatment relative risk # find the mean and st_dev of the normal distribution assumed for ln(RR) # annual state cost, we assume gamma distribution for cost in Data.ANNUAL_STATE_COST: # find shape and scale of the assumed gamma distribution estDic = Est.get_gamma_params(mean=cost, st_dev=cost / 4) # append the distribution self._annualStateCostRVG.append( Random.Gamma(a=estDic["a"], loc=0, scale=estDic["scale"])) # annual state utility, we assume beta distribution for utility in Data.ANNUAL_STATE_UTILITY: # find alpha and beta of the assumed beta distribution estDic = Est.get_beta_params(mean=utility, st_dev=utility / 4) # append the distribution self._annualStateUtilityRVG.append( Random.Beta(a=estDic["a"], b=estDic["b"]))
def test_geometric(rnd, p): # geometric random variate generator geometric_dist = RVGs.Geometric(p) # obtain samples samples = get_samples(geometric_dist, rnd) # report mean and variance print_test_results('Geometric', samples, expectation=1/p, variance=(1-p)/(p**2))
def test_gammapoisson(rnd, shape, scale): # gamma-poisson random variate generator gammapoisson_dist = RVGs.GammaPoisson(shape, scale) # obtain samples samples = get_samples(gammapoisson_dist, rnd) # report mean and variance print_test_results('GammaPoisson', samples, expectation=shape*scale, variance=shape*scale + shape*(scale**2))
def test_multinomial(rnd, n, pvals): # multinomial random variate generator multinomial_dist = RVGs.Binomial(n, pvals) # obtain samples samples = get_samples(multinomial_dist, rnd) # report mean and variance print_test_results('Multinomial', samples, expectation=n*pvals, variance=n*pvals*(1-pvals) )
def test_lognormal(rnd, mean, sigma): #lognormal random variate generator lognormal_dist = RVGs.LogNormal(mean, sigma) # obtain samples samples = get_samples(lognormal_dist, rnd) # report mean and variance print_test_results('LogNormal', samples, expectation=math.exp(mean +sigma**2/2), variance=(math.exp(sigma**2-1))*math.exp(2*mean + sigma**2) )
def test_normal(rnd, mean, sigma): #normal random variate generator normal_dist = RVGs.Normal(mean, sigma) # obtain samples samples = get_samples(normal_dist, rnd) # report mean and variance print_test_results('Normal', samples, expectation=mean, variance=sigma**2 )
def test_betabinomial(rnd, n, a, b): # beta random variate generator betabinomial_dist = RVGs.BetaBinomial(n, a, b) # obtain samples samples = get_samples(betabinomial_dist, rnd) # report mean and variance print_test_results('BetaBinomial', samples, expectation=a*n/(a + b), variance=(n*a*b*(a+b+n))/((a+b)**2*(a+b+1)))
def test_uniformdiscrete(rnd, l, r): # uniform discrete random variate generator uniformdiscrete_dist = RVGs.UniformDiscrete(l, r) # obtain samples samples = get_samples(uniformdiscrete_dist, rnd) # report mean and variance print_test_results('Uniform Discrete', samples, expectation=(l + r) / 2.0, variance=((r-l+1)**2 - 1)/12.0 )
def test_binomial(rnd, n, p): # bimonial random variate generator binomial_dist = RVGs.Binomial(n, p) # obtain samples samples = get_samples(binomial_dist, rnd) # report mean and variance print_test_results('Binomial', samples, expectation=n*p, variance=n*p*(1-p))
def test_bernoulli(rnd, p): # bernoulli random variate generator bernoulli_dist = RVGs.Bernoulli(p) # obtain samples samples = get_samples(bernoulli_dist, rnd) # report mean and variance print_test_results('Bernoulli', samples, expectation=p, variance=p*(1-p))
def test_beta(rnd, a, b): # beta random variate generator beta_dist = RVGs.Beta(a, b) # obtain samples samples = get_samples(beta_dist, rnd) # report mean and variance print_test_results('Beta', samples, expectation=a/(a + b), variance=(a*b)/((a+b+1)*(a+b)**2))
def test_weibull(rnd, a): # weibull random variate generator weibull_dist = RVGs.Weibull(a) # obtain samples samples = get_samples(weibull_dist, rnd) # report mean and variance print_test_results('Weibull', samples, expectation=math.gamma(1 + 1/a), variance=math.gamma(1 + 2/a) - (math.gamma(1 + 1/a)**2) )
def test_exponential(rnd, mean): # exponential random variate generator exp_dist = RVGs.Exponential(mean) # obtain samples samples = get_samples(exp_dist, rnd) # report mean and variance print_test_results('Exponential', samples, expectation=mean, variance=mean**2)
def test_uniform(rnd, l, r): # uniform random variate generator uniform_dist = RVGs.Uniform(l, r) # obtain samples samples = get_samples(uniform_dist, rnd) # report mean and variance print_test_results('Uniform', samples, expectation=(l + r) / 2, variance=(r-l)**2/12 )
def test_triangular(rnd, l, m, r): # triangular random variate generator triangular_dist = RVGs.Triangular(l, m, r) # obtain samples samples = get_samples(triangular_dist, rnd) # report mean and variance print_test_results('Triangular', samples, expectation=(l+m+r)/3, variance=(l**2 + m**2 + r**2 -l*r - l*m - r*m)/18 )
def test_poisson(rnd, lam): # poisson random variate generator poisson_dist = RVGs.Poisson(lam) # obtain samples samples = get_samples(poisson_dist, rnd) # report mean and variance print_test_results('Poisson', samples, expectation=lam, variance=lam )
def simulate(self, sim_length): """ simulate the patient over the specified simulation length """ # random number generator for this patient self._rng = rndClasses.RNG(self._id) k = 0 # current time step # while the patient is alive and simulation length is not yet reached while self.healthstat!=3 and k < sim_length: # find the transition probabilities of the future states trans_probs = TRANS_MATRIX[self.healthstat] # create an empirical distribution empirical_dist = rndClasses.Empirical(trans_probs) # sample from the empirical distribution to get a new state # (returns an integer from {0, 1, 2, ...}) new_state_index = empirical_dist.sample(self._rng) # update health state self.healthstat =new_state_index[0] # increment time step k += 1 self.survival=k+1
def test_negativebinomial(rnd, n, p): # negative bimonial random variate generator negativebinomial_dist = RVGs.NegativeBinomial(n, p) # obtain samples samples = get_samples(negativebinomial_dist, rnd) # report mean and variance print_test_results('Negative Binomial', samples, expectation=(n*p)/(1-p), variance=(n*p)/((1-p)**2) )
def test_johnsonsu(rnd, a, b, loc, scale): # johnsonSu random variate generator johnsonsu_dist = RVGs.JohnsonSu(a, b, loc, scale) # obtain samples samples = get_samples(johnsonsu_dist, rnd) # report mean and variance mean = scipy.johnsonsu.mean(a,b,loc,scale) var = scipy.johnsonsu.var(a,b,loc,scale) print_test_results('JohnsonSu', samples, expectation=mean, variance=var)
def __init__(self, seed, therapy): # initializing the base class _Parameters.__init__(self, therapy) self._rng = Random.RNG( seed ) # random number generator to sample from parameter distributions self._lnRelativeRiskRVG = None # random variate generator for the treatment relative risk self._annualStateCostRVG = [ ] # list of random variate generators for the annual cost of states self._annualStateUtilityRVG = [ ] # list of random variate generators for the annual utility of states # HIV transition probabilities #j = 0 #for prob in Data.TRANS_MATRIX: # self._hivProbMatrixRVG.append(Random.Dirichlet(prob[j:])) # j += 1 # treatment relative risk # find the mean and st_dev of the normal distribution assumed for ln(RR) sample_mean_lnRR = math.log(Data.TREATMENT_RR) sample_std_lnRR = (Data.TREATMENT_RR_CI[1] - Data.TREATMENT_RR_CI[0] ) / (2 * stat.norm.ppf(1 - 0.05 / 2)) self._lnRelativeRiskRVG = Random.Normal(mean=sample_mean_lnRR, st_dev=sample_std_lnRR) # annual state cost for cost in Data.ANNUAL_STATE_COST: # find shape and scale of the assumed gamma distribution shape, scale = Est.get_gamma_parameters(mean=cost, st_dev=cost / 4) # append the distribution self._annualStateCostRVG.append(Random.Gamma(shape, scale)) # resample parameters self.__resample()
def test_empirical(rnd, prob): # empirical random variate generator empirical_dist = RVGs.Empirical(prob) # obtain samples samples = get_samples(empirical_dist, rnd) # report mean and variance if type(prob) == list: prob = np.array(prob) outcome = np.array(range(len(prob))) mean = sum(outcome*prob) var = sum((outcome**2)*prob) - mean**2 print_test_results('Empirical', samples, expectation=mean, variance=var)