python - Understanding simulation termination -
this part of simulation describes program termination. please me understand how part of simulation work lost. code below:
while time_elapsed < end_time : event=birth () +death () +infection() choice=random.random()*event choice -= birth() time_elapsed += random.expovariate(event) if choice < 0 : do_birth() continue choice -= death() if choice < 0: do_death() continue
i've rearranged make more obvious happening:
# @ each time-step until sim ends while time_elapsed < end_time : # # decide random event occur # # cumulative probability = (chance of birth) + (chance of death) + (chance of infection) event = birth() + death() + infection() # choose value in [0..cum_prob) choice = random.random() * event # interpret value: # if 0 <= value < (chance of birth) => birth # if (chance of birth) <= value < (chance of birth) + (chance of death) => death # if (chance of birth) + (chance of death) <= value < (chance of birth) + (chance of death) + (chance of infection) => infection choice -= birth() if choice < 0 : do_birth() continue choice -= death() if choice < 0: do_death() continue # "what in case of infection" missing; # assume chopped off while cut-and-pasting. # forward next time-step; # uses exponential step-size, giving # normal distribution of event intervals # rather uniform step size time_elapsed += random.expovariate(event)
i write bit differently:
while < stop_at: birth_point = prob_birth() death_point = birth_point + prob_death() all_events = death_point + prob_infection() event = random.random() * all_events if event < birth_point: do_birth() elif event < death_point: do_death() else: do_infection() += random.expovariate(all_events)
Comments
Post a Comment