code: def sample_stats_to_xarray(self):
          
          if self.model_fn is None or self.observed is None:
              return None
  
          log_likelihood = []
          sample_size = self.posterior[0].shape[0]
  
          for i in range(sample_size):
              variables = {}
              for var_i, var_name in enumerate(self.var_names):
                  variables[var_name] = self.posterior[var_i][i]
  
              with self.ed.interception(self._value_setter(variables)):
                  log_likelihood.append((self.model_fn().distribution.log_prob(self.observed)))
  
          data = {}
          if self.dims is not None:
              coord_name = self.dims.get("obs")
          else:
              coord_name = None
          dims = {"log_likelihood": coord_name}
  
          with self.tf.Session() as sess:
              data["log_likelihood"] = np.expand_dims(
                  sess.run(log_likelihood, feed_dict=self.feed_dict), axis=0
              )
          return dict_to_dataset(data, library=self.tfp, coords=self.coords, dims=dims)
docs: Extract sample_stats from tfp trace.