Example #1
0
    def simulate(self, num_simulated_cohorts, cohort_size, time_steps):
        """
        :param num_simulated_cohorts: number of cohorts to simulate
        :param cohort_size: population size of cohorts
        :param time_steps: simulation length
        :param cohort_ids: ids of cohort to simulate
        :return:
        """

        # resample cohort IDs and mortality probabilities based on weights
        sampled_row_indices = np.random.choice(a=range(0, len(self._weights)),
                                               size=num_simulated_cohorts,
                                               replace=True,
                                               p=self._weights)

        # use the sampled indices to populate the list of cohort IDs and mortality probabilities
        resampled_IDs = []
        resampled_mortalityprobs = []
        for i in sampled_row_indices:
            resampled_IDs.append(self._cohortIDs[i])
            resampled_mortalityprobs.append(self._mortalityProbs[i])

        # simulate the desired number of cohorts
        self._multiCohorts = SurvivalCls.MultiCohort(
            ids=resampled_IDs,
            pop_sizes=[cohort_size] * num_simulated_cohorts,
            mortality_probs=resampled_mortalityprobs)

        # simulate all the cohorts
        self._multiCohorts.simulate(time_steps)
Example #2
0
    def sample_posterior(self):
        """ sample the posterior distribution of the mortality probability """

        # find values of mortality probability at which the posterior should be evaluated
        self._mortalitySamples = np.random.uniform(low=POST_L,
                                                   high=POST_U,
                                                   size=POST_N)

        # create a multi cohort
        multiCohort = SurvivalCls.MultiCohort(
            ids=self._cohortIDs,
            pop_sizes=[POST_N] * POST_N,
            mortality_probs=self._mortalitySamples)

        # simulate the multi cohort
        multiCohort.simulate(TIME_STEPS)

        # calculate the likelihood of each simulated cohort
        for i in self._cohortIDs:

            # get the 5-year OS for this cohort
            survival = multiCohort.get_cohort_FIVEyear_OS(i)

            # construct weight utilizing study's k and n; and simulated five-year OS
            weight = stat.binom.pmf(k=STUDY_K, n=STUDY_N, p=survival)

            # store the weight
            self._weights.append(weight)

        # normalize the likelihood weights
        sum_weights = np.sum(self._weights)
        self._normalizedWeights = np.divide(self._weights, sum_weights)

        # re-sample mortality probability (with replacement) according to likelihood weights
        self._mortalityResamples = np.random.choice(a=self._mortalitySamples,
                                                    size=NUM_SIM_COHORTS,
                                                    replace=True,
                                                    p=self._normalizedWeights)

        # produce the list to report the results
        for i in range(0, len(self._mortalitySamples)):
            self._csvRows.append([
                self._cohortIDs[i], self._normalizedWeights[i],
                self._mortalitySamples[i]
            ])

        # write the calibration result into a csv file
        InOutSupport.write_csv('CalibrationResults.csv', self._csvRows)