def sample_posterior(self):
        self._survivalSamples = np.random.uniform(low=RunCalib.POST_L,
                                                  high=RunCalib.POST_H,
                                                  size=RunCalib.COHORT_SIZE)

        multiCohort = Cls.MultiCohort(ids=self._cohortIDs,
                                      mortality_probs=self._survivalSamples,
                                      pop_sizes=[RunCalib.SIMULATION_SIZE] *
                                      RunCalib.COHORT_SIZE)

        multiCohort.simulate(RunCalib.TIME_STEPS)

        for cohort_id in self._cohortIDs:
            mean = MultiCohort.get_overall_mean_survival(cohort_id)

            weight = binom.pmf(k=RunCalib.K,
                               n=RunCalib.SAMPLE_SIZE,
                               p=RunCalib.SURVIVAL_PROB)

            self._weights.append(weight)
        sum_weights = np.sum(self._weights)
        self._normalizedWeights = np.divide(self._weights, sum_weights)

        self._survivalResamples = np.random.choice(
            a=self._survivalSamples,
            size=RunCalib.SIMULATION_SIZE,
            replace=True,
            p=self._normalizedWeights)

        for i in range(0, len(self._survivalSamples)):
            self._csvRows.append([
                self._cohortIDs[i], self._normalizedWeights[i],
                self._survivalSamples[i]
            ])
        InOutSupport.write_csv('CalibrationResult.csv', self._csvRows)
    def sample_posterior(self):
        """ sample the posterior distribution of the mortality probability """

        # find values of mortality probability at which the posterior should be evaluated
        # HR: this is a prior p, since we just simulate the parameter without having any observations
        self._mortalitySamples = np.random.uniform(
            low=CalibSets.POST_L,
            high=CalibSets.POST_U,
            size=CalibSets.POST_N)

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

        # simulate the multi cohort
        multiCohort.simulate(CalibSets.TIME_STEPS)

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

            # get the survival percentage for this cohort
            survivalrate= multiCohort.get_cohort_survival_percentages(cohort_id)

            # construct a gaussian likelihood
            # with mean calculated from the simulated data and standard deviation from the clinical study.
            # evaluate this pdf (probability density function) at the mean reported in the clinical study.
            weight = stat.binom.pmf(
                k=CalibSets.OBS_SUR,
                p=survivalrate*0.01,
                n=CalibSets.OBS_N
            )

            # 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=CalibSets.NUM_SIM_COHORTS,
            # HR: size is the number of resampled cohorts we would get eventually, this is not the number of cohorts we simulate first time
            replace=True,
            # HR: values with higher weights get sampled more often
            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)
示例#3
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=CalibSets.POST_L,
            high=CalibSets.POST_U,
            size=CalibSets.POST_N)

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

        # simulate the multi cohort
        multiCohort.simulate(CalibSets.TIME_STEPS)


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

            # get the average survival time for this cohort
            mean = multiCohort.get_cohort_mean_survival(cohort_id)
            percent = multiCohort.get_percents_5yr(cohort_id)
            self._5yr_survival.append(percent)

            # construct a gaussian likelihood
            # with mean calculated from the simulated data and standard deviation from the clinical study.
            # evaluate this pdf (probability density function) at the mean reported in the clinical study.
            weight = stat.binom.pmf(
                k=CalibSets.OBS_K,
                n=CalibSets.OBS_n,
                p=percent)

            # 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=CalibSets.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)
    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=CalibrationSettings.POST_L,
            high=CalibrationSettings.POST_U,
            size=CalibrationSettings.POST_N)

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

        # simulate the multi cohort
        multiCohort.simulate(CalibrationSettings.TIME_STEPS)

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

            # get the average survival time for this cohort
            proportion = multiCohort.get_5yr_survtime(
                cohort_id)  ## modify to get 5 year survival

            weight = stat.binom.pmf(k=CalibrationSettings.OBS_SURV,
                                    n=CalibrationSettings.OBS_N,
                                    p=proportion,
                                    loc=0)

            # 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=CalibrationSettings.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)
示例#5
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)
示例#6
0
from scr import InOutFunctions as OutSupport

myList = [['Col1', 'Col2', 'Col3']]
for i in range(1, 10):
    myList.append([i, 2 * i, 3 * i])

OutSupport.write_csv('myCSV', myList)