You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"""Generate posterior predictive samples from a model given a trace.
451
+
"""Generate forward samples for `var_names`, conditioned on the posterior samples of variables found in the `trace`.
452
+
453
+
This method can be used to perform different kinds of model predictions, including posterior predictive checks.
454
+
455
+
The matching of unobserved model variables, and posterior samples in the `trace` is made based on the variable
456
+
names. Therefore, a different model than the one used for posterior sampling may be used for posterior predictive
457
+
sampling, as long as the variables whose posterior we want to condition on have the same name, and compatible shape
458
+
and coordinates.
459
+
452
460
453
461
Parameters
454
462
----------
455
463
trace : backend, list, xarray.Dataset, arviz.InferenceData, or MultiTrace
456
-
Trace generated from MCMC sampling, or a list of dicts (eg. points or from find_MAP()),
457
-
or xarray.Dataset (eg. InferenceData.posterior or InferenceData.prior)
464
+
Trace generated from MCMC sampling, or a list of dicts (eg. points or from :func:`~pymc.find_MAP`),
465
+
or :class:`xarray.Dataset` (eg. InferenceData.posterior or InferenceData.prior)
458
466
model : Model (optional if in ``with`` context)
459
467
Model to be used to generate the posterior predictive samples. It will
460
-
generally be the model used to generate the ``trace``, but it doesn't need to be.
461
-
var_names : Iterable[str]
468
+
generally be the model used to generate the `trace`, but it doesn't need to be.
469
+
var_names : Iterable[str], optional
462
470
Names of variables for which to compute the posterior predictive samples.
471
+
By default, only observed variables are sampled.
472
+
See the example below for what happens when this argument is customized.
463
473
sample_dims : list of str, optional
464
474
Dimensions over which to loop and generate posterior predictive samples.
465
-
When `sample_dims` is ``None`` (default) both "chain" and "draw" are considered sample
475
+
When ``sample_dims`` is ``None`` (default) both "chain" and "draw" are considered sample
466
476
dimensions. Only taken into account when `trace` is InferenceData or Dataset.
467
477
random_seed : int, RandomState or Generator, optional
468
478
Seed for the random number generator.
469
479
progressbar : bool
470
-
Whether or not to display a progress bar in the command line. The bar shows the percentage
480
+
Whether to display a progress bar in the command line. The bar shows the percentage
471
481
of completion, the sampling speed in samples per second (SPS), and the estimated remaining
472
482
time until completion ("expected time of arrival"; ETA).
473
483
return_inferencedata : bool, default True
474
484
Whether to return an :class:`arviz:arviz.InferenceData` (True) object or a dictionary (False).
475
485
extend_inferencedata : bool, default False
476
486
Whether to automatically use :meth:`arviz.InferenceData.extend` to add the posterior predictive samples to
477
-
``trace`` or not. If True, ``trace`` is modified inplace but still returned.
487
+
`trace` or not. If True, `trace` is modified inplace but still returned.
478
488
predictions : bool, default False
479
-
Flag used to set the location of posterior predictive samples within the returned ``arviz.InferenceData`` object. If False, assumes samples are generated based on the fitting data to be used for posterior predictive checks, and samples are stored in the ``posterior_predictive``. If True, assumes samples are generated based on out-of-sample data as predictions, and samples are stored in the ``predictions`` group.
489
+
Flag used to set the location of posterior predictive samples within the returned ``arviz.InferenceData`` object.
490
+
If False, assumes samples are generated based on the fitting data to be used for posterior predictive checks,
491
+
and samples are stored in the ``posterior_predictive``. If True, assumes samples are generated based on
492
+
out-of-sample data as predictions, and samples are stored in the ``predictions`` group.
480
493
idata_kwargs : dict, optional
481
494
Keyword arguments for :func:`pymc.to_inference_data` if ``predictions=False`` or to
For more about out-of-model predictions, see this `blog post <https://www.pymc-labs.com/blog-posts/out-of-model-predictions-with-pymc/>`_.
571
+
572
+
The behavior of `var_names`
573
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
574
+
575
+
The function returns forward samples for any variable included in `var_names`,
576
+
conditioned on the values of other random variables found in the trace.
577
+
578
+
To ensure the samples are internally consistent, any random variable that depends
579
+
on another random variable that is being sampled is itself sampled, even if
580
+
this variable is present in the trace and was not included in `var_names`.
581
+
The final list of variables being sampled is shown in the log output.
582
+
583
+
Note that if a random variable has no dependency on other random variables,
584
+
these forward samples are equivalent to their prior samples.
585
+
Likewise, if all random variables are being sampled, the behavior of this function
586
+
is equivalent to that of :func:`~pymc.sample_prior_predictive`.
587
+
588
+
.. warning:: A random variable included in `var_names` will never be copied from the posterior group. It will always be sampled as described above. If you want, you can copy manually via ``idata.posterior_predictive["var_name"] = idata.posterior["var_name"]``.
589
+
590
+
591
+
The following code block explores how the behavior changes with different `var_names`:
0 commit comments