------------------------------------------------- lines 15-284 of file: example/csv/sim_fit_pred.py ------------------------------------------------- {xrst_begin csv.sim_fit_pred} {xrst_spell const cv dage dtime iter meas mtexcess num pos sincidence std } Csv Example Simulating, Fitting and Predicting ############################################## Global Variables **************** Global variables besides sim_file and fit_file. Variables that do not appear in a heading are temporaries. integrand_list ============== Generate data and predict for the following integrands: {xrst_code py}''' integrand_list = [ 'Sincidence', 'remission', 'mtexcess', 'prevalence' ] '''{xrst_code} age_grid, time_grid =================== Use this age-time grid for values the covariate grid and the parent rage grid. {xrst_code py}''' age_grid = [0.0, 20.0, 50.0, 80.0, 100.0] time_grid = [1980.0, 2000.0, 2020.0] '''{xrst_code} node_dict ========= Keys are nodes and values are corresponding parent node: {xrst_code py}''' node_dict = { 'n0' : '' , 'n1' : 'n0' , 'n2' : 'n0' , } '''{xrst_code} no_effect_rate_truth ==================== The true values (values used during simulation) for iota, rho, and chi are constant w.r.t age and time: {xrst_code py}''' no_effect_rate_truth = { 'iota' : 0.02 , 'rho' : 20.0 , 'chi' : 0.001 , } '''{xrst_code} omega_truth =========== {xrst_code py}''' omega_truth = 0.01 '''{xrst_code} std_random_effects_truth ======================== This is the true standard deviation of the random effects {xrst_code py}''' std_random_effects_truth = 0.2 '''{xrst_code} sim_file ******** Input CSV files that are placed in the simulate directory: {xrst_code py}''' sim_file = dict() '''{xrst_code} option_sim.csv ============== {xrst_code py}''' sim_file['option_sim.csv'] = \ '''name,value float_precision,4 random_depend_sex,false ''' for rate_name in no_effect_rate_truth : row = f'std_random_effects_{rate_name},{std_random_effects_truth}\n' sim_file['option_sim.csv'] += row '''{xrst_code} node.csv ======== {xrst_code py}''' sim_file['node.csv'] = \ 'node_name,parent_name\n' for node_name in node_dict : parent_name = node_dict[node_name] sim_file['node.csv'] += f'{node_name},{parent_name}\n' '''{xrst_code} covariate.csv ============= {xrst_code py}''' sim_file['covariate.csv'] = 'node_name,sex,age,time,omega\n' for node_name in [ 'n0', 'n1', 'n2' ] : for sex in [ 'female', 'male' ] : for age in age_grid : for time in time_grid : row = f'{node_name},{sex},{age},{time},{omega_truth}\n' sim_file['covariate.csv'] += row '''{xrst_code} multiplier_sim.csv ================== There are no covariate multipliers in this example. {xrst_code py}''' sim_file['multiplier_sim.csv'] = \ 'multiplier_id,rate_name,covariate_or_sex,multiplier_true\n' '''{xrst_code} simulate.csv ============ {xrst_code py}''' header = 'simulate_id,integrand_name,node_name,sex,age_lower,age_upper,' header += 'time_lower,time_upper,meas_std_cv,meas_std_min\n' meas_std_cv = 0.01 simulate_id = 0 sim_file['simulate.csv'] = header for integrand_name in integrand_list : std_min = 0.0 if integrand_name == 'prevalence' : std_min = 1e-6 for node_name in node_dict : for sex in [ 'female', 'male' ] : for age in age_grid : for time in time_grid : row = f'{simulate_id},{integrand_name},{node_name},{sex},' row += f'{age},{age},{time},{time},' row += f'{meas_std_cv},{std_min}\n' sim_file['simulate.csv'] += row simulate_id += 1 '''{xrst_code} no_effect_rate.csv ================== The rates are constant, w.r.t age and time, during the simulation. {xrst_code py}''' sim_file['no_effect_rate.csv'] = 'rate_name,age,time,rate_truth\n' for rate_name in no_effect_rate_truth : rate_truth = no_effect_rate_truth[rate_name] sim_file['no_effect_rate.csv'] += f'{rate_name},0,0,{rate_truth}\n' '''{xrst_code} fit_file ******** Input CSV files that are placed in the fit directory: {xrst_code py}''' fit_file = dict() '''{xrst_code} Copies of Simulation Files ========================== {xrst_code py}''' fit_file['node.csv'] = sim_file['node.csv'] fit_file['covariate.csv'] = sim_file['covariate.csv'] '''{xrst_code} option_fit.csv ============== {xrst_code py}''' fit_file['option_fit.csv'] = \ '''name,value refit_split,false ode_step_size,5.0 quasi_fixed,false max_num_iter_fixed,50 tolerance_fixed,1e-8 ode_method,iota_pos_rho_pos ''' '''{xrst_code} option_predict.csv ================== A predict is run using the same directory as the corresponding fit. All of its input files are also inputs for the fit except for the option_predict.csv file. {xrst_code py}''' fit_file['option_predict.csv'] = \ '''name,value db2csv,true plot,true float_precision,5 ''' '''{xrst_code} fit_goal.csv ============ An empty fit_goal.csv corresponds to fitting all nodes at or below the :ref:`root node` . {xrst_code py}''' fit_file['fit_goal.csv'] = \ '''node_name ''' '''{xrst_code} prior.csv ========= {xrst_code py}''' delta_prior_std = 0.1 std_random_effects_fit = 10.0 * std_random_effects_truth fit_file['prior.csv'] = \ 'name,density,mean,std,eta,lower,upper\n' + \ f'delta_prior,log_gaussian,0.0,{delta_prior_std},1e-10,,\n' + \ f'random_prior,gaussian,0.0,{std_random_effects_fit},,,,\n' for rate_name in no_effect_rate_truth : rate_truth = no_effect_rate_truth[rate_name] lower = rate_truth / 100.0 upper = rate_truth * 100.0 fit_file['prior.csv'] += \ f'prior_{rate_name},uniform,{rate_truth},,,{lower},{upper}\n' '''{xrst_code} parent_rate.csv =============== The rates are constant during simulation, but not during fitting. {xrst_code py}''' fit_file['parent_rate.csv'] = \ 'rate_name,age,time,value_prior,dage_prior,dtime_prior,const_value\n' for age in age_grid : for time in time_grid : for rate_name in no_effect_rate_truth : row = f'{rate_name},{age},{time},prior_{rate_name},' row += 'delta_prior,delta_prior,\n' fit_file['parent_rate.csv'] += row '''{xrst_code} child_rate.csv ============== {xrst_code py}''' fit_file['child_rate.csv'] = 'rate_name,value_prior\n' for rate_name in no_effect_rate_truth : fit_file['child_rate.csv'] += f'{rate_name},random_prior\n' '''{xrst_code} mulcov.csv ========== {xrst_code py}''' fit_file['mulcov.csv'] = 'covariate,type,effected,value_prior,const_value\n' '''{xrst_code} predict_integrand.csv ===================== {xrst_code py}''' fit_file['predict_integrand.csv'] = 'integrand_name\n' for integrand_name in integrand_list : fit_file['predict_integrand.csv'] += f'{integrand_name}\n' '''{xrst_code} Rest of Source Code ******************* {xrst_literal BEGIN PYTHON END PYTHON } {xrst_end csv.sim_fit_pred}