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)
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)
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)
def __init__(self, csv_file_name): cols = InOutSupport.read_csv_cols(file_name=csv_file_name, n_cols=3, if_ignore_first_row=True, if_convert_float=True) self._cohortIDs = cols[CalibrationColIndex.ID.value].astype(int) self._weights = cols[CalibrationColIndex.W.value] self._survivalProbs = cols[CalibrationColIndex.SURVIVAL_PROB.value] self._multiCohorts = None
def __init__(self, csv_file_name): # read the columns of the generated csv file containing the calibration results cols = InOutSupport.read_csv_cols(file_name=csv_file_name, n_cols=3, if_ignore_first_row=True, if_convert_float=True) # store cohort IDs, likelihood weights, and mortality probabilities self._cohortIDs = cols[CalibrationColIndex.ID.value].astype(int) self._weights = cols[CalibrationColIndex.W.value] self._mortalityProbs = cols[CalibrationColIndex.MORT_PROB.value] self._multiCohorts = None
def __init__(self, csv_file_name): """ extracts seeds, mortality probabilities and the associated likelihood from the csv file where the calibration results are stored :param cvs_file_name: name of the csv file where the calibrated results are stored """ # read the columns of the csv files containing the calibration results cols = InOutSupport.read_csv_cols(file_name=csv_file_name, n_cols = 3, if_ignore_first_row=True, if_convert_float=True) # store likelihood weights, cohort IDs and sampled mortality probabilities self._cohortIDs = cols[CalibrationColIndex.ID.value].astype(int) self._weights = cols[CalibrationColIndex.W.value] self._mortality = cols[CalibrationColIndex.MORT_PROB.value] self._multiCohorts = None
def __init__(self, cvs_file_name, drug_effectiveness_ratio=1): """ extracts seeds, mortality probabilities and the associated likelihood from the csv file where the calibration results are stored :param cvs_file_name: name of the csv file where the calibrated results are stored :param calibrated_model_with_drug: calibrated model simulated when drug is available """ # read the columns of the csv files containing the calibration results cols = InOutSupport.read_csv_cols(file_name=cvs_file_name, n_cols=3, if_ignore_first_row=True, if_convert_float=True) # store likelihood weights, cohort IDs and sampled mortality probabilities self._cohortIDs = cols[CalibrationColIndex.ID.value].astype(int) self._weights = cols[CalibrationColIndex.W.value] self._mortalityProbs = cols[ CalibrationColIndex.MORT_PROB.value] * drug_effectiveness_ratio self._multiCohorts = None # multi-cohort
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)
from scr import InOutFunctions as InOutSupport # test reading by rows rows = InOutSupport.read_csv_rows('myCSV', if_del_first_row=True, if_convert_float=True) print('Testing reading by rows:') for row in rows: print(sum(row)) # test reading by columns cols = InOutSupport.read_csv_cols('myCSV', n_cols=3, if_ignore_first_row=True, if_convert_float=True) print('Testing reading by columns:') for j in range(0, 3): print(sum(cols[j]))