code: def evaluate_pointwise_pred_density(
      model, posterior_samples, baseball_dataset
  ):
      """
      Evaluate the log probability density of observing the unseen data (season hits)
      given a model and posterior distribution over the parameters.
      """
      _, test, player_names = train_test_split(baseball_dataset)
      at_bats_season, hits_season = test[:, 0], test[:, 1]
      trace = Predictive(model, posterior_samples).get_vectorized_trace(
          at_bats_season, hits_season
      )
      # Use LogSumExp trick to evaluate $log(1/num_samples \sum_i p(new_data | \theta^{i})) $,
      # where $\theta^{i}$ are parameter samples from the model's posterior.
      trace.compute_log_prob()
      post_loglik = trace.nodes["obs"]["log_prob"]
      # computes expected log predictive density at each data point
      exp_log_density = (
          post_loglik.logsumexp(0) - math.log(post_loglik.shape[0])
      ).sum()
      logging.info("\nLog pointwise predictive density")
      logging.info("--------------------------------")
      logging.info("{:.4f}\n".format(exp_log_density))
  
file_path: pyro/examples/baseball.py
imports: import argparse
  import logging
  import math
  import pandas
  import torch
  import pyro
  from pyro.distributions import Beta, Binomial, HalfCauchy, Normal, Pareto, Uniform
  from pyro.distributions.util import scalar_like
  from pyro.infer import MCMC, NUTS, Predictive
  from pyro.infer.mcmc.util import initialize_model, summary
  from pyro.util import ignore_experimental_warning
label_desc: Use image node_id 8 for calling a global function with example usage: evaluate_pointwise_pred_density(model, posterior_samples, baseball_dataset) without return types