def simulate(self, n_time_steps): """ simulate the patient over the specified simulation length """ k = 0 # simulation time step # while the patient is alive and simulation length is not yet reached while self.stateMonitor.get_if_alive() and k < n_time_steps: # find the transition probabilities to future states trans_probs = self.params.probMatrix[ self.stateMonitor.currentState.value] # create an empirical distribution empirical_dist = RVGs.Empirical(probabilities=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(rng=self.rng) # update health state self.stateMonitor.update(time_step=k, new_state=P.HealthStates(new_state_index)) # increment time k += 1
def __init__(self, transition_rate_matrix): self._rateMatrix = transition_rate_matrix self._expDists = [] self._empiricalDists = [] for i, row in enumerate(transition_rate_matrix): # find sum of rates out of this state rate_out = out_rate(row, i) # if the rate is 0, put None as the exponential and empirical distributions if rate_out > 0: # create an exponential distribution with rate equal to sum of rates out of this state self._expDists.append(RVG.Exponential(scale=1 / rate_out)) # find the transition rates to other states # assume that the rate into this state is 0 rates = [] for j, v in enumerate(row): if i == j: rates.append(0) else: rates.append(v) # calculate the probability of each event (prob_j = rate_j / (sum over j of rate_j) probs = np.array(rates) / rate_out # create an empirical distribution over the future states from this state self._empiricalDists.append(RVG.Empirical(probs)) else: # if the sum of rates out of this state is 0 self._expDists.append(None) self._empiricalDists.append(None)
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, n_time_steps): t = 0 # while self.stateMonitor.get_if_alive() and t < n_time_steps: while t < n_time_steps: # find the transition probabilities to future states trans_probs = self.params.probMatrix[ self.stateMonitor.currentState.value] # create an empirical distribution empirical_dist = RVGs.Empirical(probabilities=trans_probs) # sample from the empirical distribution to get a new state new_state_index = empirical_dist.sample(rng=self.rng) # update health state self.stateMonitor.update(time_step=t, new_state=P.HealthStates(new_state_index)) # increment time t += 1