----------------------------------------------- lines 5-168 of file: example/user/sample_sim.py ----------------------------------------------- # {xrst_begin user_sample_sim.py} # {xrst_spell # exp # mcmc # } # {xrst_comment_ch #} # # Sample Posterior Using Simulated Data # ##################################### # # Purpose # ******* # The command # # ``dismod_at`` *database* ``sample simulate`` *number_sample* # # samples form the posterior distribution of the :ref:`model_variables-name` # using simulated data. # # Node Table # ********** # For this example, the :ref:`node_table-name` is # :: # # north_america # / \ # mexico canada # # Model Variables # *************** # All of the rates are constant w.r.t. age an time in this example. # # iota # ==== # We use :math:`\iota_n` to denote the incidence rate for # the parent region north_america which is a # :ref:`fixed effect` . # # u # = # We use :math:`u_m` (:math:`u_c`) to denote the # :ref:`child rate effect` # for mexico (canada). # # x # = # The model variables for this example are combined into a single vector # :math:`x \in \B{R}^3` where # :math:`x_0 = \iota_n`, # :math:`x_1 = u_m`, and # :math:`x_2 = u_c`. # # Model Parameters # **************** # # y # = # We use :math:`y_n, y_m, y_c` to denote the measured # :ref:`avg_integrand@Integrand, I_i(a,t)@Sincidence` # for north_america, mexico, and canada respectively. # # s # = # We use :math:`s_n, s_m, s_c` to denote the # standard deviation of the measured noise # for north_america, mexico, and canada respectively. # # delta # ===== # We use :math:`\delta` to denote the standard deviation of # the child rate effects. # (The mean for the child rate effects is zero.) # # Values # ====== # {xrst_spell_off} # {xrst_code py} import math y_n = 1e-2 y_m = 2.0 * y_n y_c = y_n / 2.0 s_n = y_n / 10.0 s_m = y_m / 10.0 s_c = y_c / 10.0 delta = math.log( 2.0 ) # {xrst_code} # {xrst_spell_on} # # Likelihood # ********** # We define :math:`h(y, \mu , \delta)` # to be the log-density for a :math:`\B{N}( \mu, \delta^2 )` distribution; # i.e., # # .. math:: # # h(y, \mu, \delta) = # - \frac{ ( y - \mu )^2 }{ \delta^2 } # - \log \left( \delta \sqrt{ 2 \pi } \right) # # The total log-likelihood for this example is # # .. math:: # # h( y_n, \iota_n, s_n ] + # h[ y_m, \exp( u_m ) \iota_n, s_m ] + # h[ y_c, \exp( u_c ) \iota_n, s_c ] + # h( u_m, 0, \delta ) + h( u_c , 0 , \delta ) # # number_sample # ************* # This is the number of samples in the # :ref:`sample_command@number_sample` # specified by the sample command. # {xrst_spell_off} # {xrst_code py} number_sample = 30 # {xrst_code} # {xrst_spell_on} # # number_metropolis # ***************** # This is the number of mcmc samples generated by the # :ref:`metropolis-name` mcmc method. # These samples are used to check the simulate method. # {xrst_spell_off} # {xrst_code py} number_metropolis = 500 * number_sample # {xrst_code} # {xrst_spell_on} # # Truth Table # *********** # For the data simulation, we use the following model variable values: # {xrst_spell_off} # {xrst_code py} iota_n_true = y_n u_m_true = math.log( y_m / y_n ) u_c_true = math.log( y_c / y_n ) # {xrst_code} # {xrst_spell_on} # It follows that *y_m* = ``exp`` ( *u_m_true* ) * *iota_n_true* and # *y_c* = ``exp`` ( *u_c_true* ) * *iota_n_true* . # # random_seed # *********** # Set one random seed for use by both the python code and dismod_at. # In addition, if an error occurs, this random seed will be reported; see # ``random_seed`` in the source code below. # {xrst_spell_off} # {xrst_code py} import time random_seed = int( time.time() ) # {xrst_code} # {xrst_spell_on} # # Source Code # *********** # {xrst_literal # BEGIN PYTHON # END PYTHON # } # # {xrst_end user_sample_sim.py}