--------------------------------------------- lines 5-89 of file: example/user/shock_cov.py --------------------------------------------- # {xrst_begin user_shock_cov.py} # {xrst_spell # def # exp # } # {xrst_comment_ch #} # # Example Fitting a Covariate with a Shock # ######################################## # # Purpose # ******* # This example demonstrates fitting a covariate multiplier # where the covariate has a shock. To be more specific, a spike at # a certain age, time, node, and sex. # # Integrand # ********* # There is only one integrand in this example, # :ref:`avg_integrand@Integrand, I_i(a,t)@prevalence` . # # Node Tables # *********** # The node table for this example is # :: # # world # / \ # north_america south_america # # Subgroup Table # ************** # For this example there is only one subgroup (the world). # # Covariates # ********** # There are two covariates in this example, *sex* and *shock* . # Sex has the following values # {xrst_code py} sex_name2value = { 'female' : -0.5, 'both' : 0.0, 'male' : +0.5 } # {xrst_code} # Shock is defined by the following function # {xrst_code py} def shock_fun(age, time, node_name, sex) : shock = 0.0 if (node_name, sex) == ('north_america', 'male') : if 0 <= age and age <= 40 and 1920 <= time and time <= 1960 : age_factor = 1.0 - abs( age - 20.0 ) / 20.0 time_factor = 1.0 - abs(time - 1940.0 ) / 20.0 shock = age_factor * time_factor return shock # {xrst_code} # # Covariate Multipliers # ********************* # There is one covariate multiplier in this example. # It multiples *shock* and effects the rate iota as follows: # {xrst_code py} mulcov_true = 1.0 iota_no_effect = 0.01 def iota_true(age, time, node_name, sex) : effect = mulcov_true * shock_fun(age, time, node_name, sex) return iota_no_effect * math.exp(effect) # {xrst_code} # # Simulated Data # ************** # The data is simulated using the true value for the variables, # and the covariate effects mentioned above. No noise is added to the data, # but it is modeled as having a ten percent coefficient of variation. # # Rate Variables # ************** # There is one non-zero rate for this example iota # and the no effect model for iota is constant and equal to # ``iota_no_effect`` . # # Source Code # *********** # {xrst_literal # BEGIN PYTHON # END PYTHON # } # # {xrst_end user_shock_cov.py}