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 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 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)
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 simulate_fiveshort(self, sim_length_short): """ simulate the patient over the specified simulation length """ # random number generator for this patient self._rng = rndClasses.RNG(self._id) if self.vaccine == 0: k = 0 # while the patient is alive and simulation length is not yet reached while (self.healthstat != 8) and k * ma.delta_t < sim_length_short: # find the transition probabilities of the future states trans_probs = ma.prob_matrix[0][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) # caculate cost and utality cost = ma.cost_matrix[ self. healthstat] + ma.salary * ma.work_loss_day[self.healthstat] utility = ma.utility_matrix[self.healthstat] * ma.delta_t # update total discounted cost and utility (corrected for the half-cycle effect) self.totalDiscountCost += \ EconCls.pv(cost, ma.discount_rate * ma.delta_t, k + 1) self.totalDiscountUtility += \ EconCls.pv(utility, ma.discount_rate * ma.delta_t, k + 1) # update diseases: if self.healthstat == HealthStats.Pneumoniae.value: self.pneumonaie += 1 if self.healthstat == HealthStats.Meningitis.value: self.meningitis += 1 if self.healthstat == HealthStats.AOM_T.value or self.healthstat == HealthStats.AOM_NT.value: self.aom += 1 # update disability number if self.healthstat == HealthStats.Disability.value: self._ndisability = 1 # update deafness number if self.healthstat == HealthStats.Deaf.value: self._ndeaf = 1 # update health state self.healthstat = new_state_index[0] #update number of deahts if self.healthstat == HealthStats.DEATH.value: self._ndeath = 1 # increment time step k += 1 if self.vaccine == 1: k = 0 while (self.healthstat != 8) and k * ma.delta_t < sim_length_short: # find the transition probabilities of the future states trans_probsv = ma.prob_matrix_vaccine[0][self.healthstat] # create an empirical distribution empirical_distv = rndClasses.Empirical(trans_probsv) # sample from the empirical distribution to get a new state # (returns an integer from {0, 1, 2, ...}) new_state_indexv = empirical_distv.sample(self._rng) # caculate cost and utality cost = ma.cost_matrix[ self. healthstat] + ma.salary * ma.work_loss_day[self.healthstat] utility = ma.utility_matrix[self.healthstat] * ma.delta_t # update total discounted cost and utility (corrected for the half-cycle effect) self.totalDiscountCost += \ EconCls.pv(cost, ma.discount_rate * ma.delta_t, k + 1) self.totalDiscountUtility += \ EconCls.pv(utility, ma.discount_rate * ma.delta_t, k + 1) # update diseases: if self.healthstat == HealthStats.Pneumoniae.value: self.pneumonaie += 1 if self.healthstat == HealthStats.Meningitis.value: self.meningitis += 1 if self.healthstat == HealthStats.AOM_T.value or self.healthstat == HealthStats.AOM_NT.value: self.aom += 1 # update disability number if self.healthstat == HealthStats.Disability.value: self._ndisability = 1 # update deafness number if self.healthstat == HealthStats.Deaf.value: self._ndeaf = 1 # update health state self.healthstat = new_state_indexv[0] #update number of deahts if self.healthstat == HealthStats.DEATH.value: self._ndeath = 1 if k == 3: self.shot += 1 self.totalDiscountCost+= \ EconCls.pv(ma.vaccine_administration+ma.vaccine_cost, ma.discount_rate * ma.delta_t, k + 1) if k == 5: self.shot += 1 self.totalDiscountCost+= \ EconCls.pv(ma.vaccine_administration+ma.vaccine_cost, ma.discount_rate * ma.delta_t, k + 1) if k == 11: self.shot += 1 self.totalDiscountCost+= \ EconCls.pv(ma.vaccine_administration+ma.vaccine_cost, ma.discount_rate * ma.delta_t, k + 1) # increment time step k += 1 if self.healthstat == 3: for i in (6, 16): self.totalDiscountCost += EconCls.pv(2746, ma.discount_rate, i + 1)