-------------------------------------------------- lines 6-98 of file: example/user/predict_sample.py -------------------------------------------------- {xrst_begin user_predict_sample.py} {xrst_spell } Predict Using Posterior Samples ############################### Purpose ******* The command | |tab| ``dismod_at`` *database* ``predict sample fit_var`` *scale* computes a prediction for each avgint table :ref:`avgint_table@avgint_id` and each sample table :ref:`sample_table@sample_index` . Note Table ********** This example has only one node called ``world`` Model Variables *************** There is only one model variable *iota* in this example and it is constant w.r.t; age and time. Measurement *********** The data are measurements of :ref:`avg_integrand@Integrand, I_i(a,t)@Sincidence` ; i.e. a direct measurement of *iota* . We use *n_data* for the number of measurements. The data is simulated using a normal with mean equal to the true *iota* and standard deviation equal to *std_data* . This distribution is also used for the model of the measurement noise. {xrst_code py}''' iota_true = 0.01 n_data = 100 std_data = iota_true * 0.2 r'''{xrst_code} Random Seed *********** This is used to seed the random number generator used to simulate the noise in the measurement values: {xrst_code py}''' import time random_seed = int( time.time() ) r'''{xrst_code} Number of Samples ***************** Each sample has a complete set of the :ref:`model_variables-name` which yields a different prediction :` {xrst_code py}''' number_samples = 100 r'''{xrst_code} Sample Covariance ***************** Let :math:`\iota` be *iota* , math:`y_i` be the i-th simulated measurement, :math:`n` be the number of data points *n_data* , and :math:`\sigam` bi the measurement standard deviation *std_data* . Dropping terms that do not depend on *iota* , the negative log-likelihood is .. math:: L( \iota ) = \frac{1}{2} \sum_{i=0}^{m-1} \left( \frac{y_i - \iota }{\sigma} \right)^2 Taking the second derivative w.r.t. *iota* is :math:`m / \sigma^2` . Hence the samples for *iota* should have variance ;math:`\sigma^2 / m` . Prediction Covariance ********************* The samples are scaled by *scale* before making predictions: {xrst_code py}''' scale = 2.0 r'''{xrst_code} This should make the variance for the predictions should be (scale * sigma)^2 / n_data Source Code *********** The source code below does not include the global settings defined above: {xrst_literal # BEGIN SOURCE # END SOURCE } {xrst_end user_predict_sample.py}