code: def broadcast_dist_samples_shape(shapes, size=None):
      """Apply shape broadcasting to shape tuples but assuming that the shapes
      correspond to draws from random variables, with the `size` tuple possibly
      prepended to it. The `size` prepend is ignored to consider if the supplied
      `shapes` can broadcast or not. It is prepended to the resulting broadcasted
      `shapes`, if any of the shape tuples had the `size` prepend.
  
      Parameters
      ----------
      shapes: Iterable of tuples holding the distribution samples shapes
      size: None, int or tuple (optional)
          size of the sample set requested.
  
      Returns
      -------
      tuple of the resulting shape
  
      Examples
      --------
      .. code-block:: python
          size = 100
          shape0 = (size,)
          shape1 = (size, 5)
          shape2 = (size, 4, 5)
          out = broadcast_dist_samples_shape([shape0, shape1, shape2],
                                             size=size)
          assert out == (size, 4, 5)
      .. code-block:: python
          size = 100
          shape0 = (size,)
          shape1 = (5,)
          shape2 = (4, 5)
          out = broadcast_dist_samples_shape([shape0, shape1, shape2],
                                             size=size)
          assert out == (size, 4, 5)
      .. code-block:: python
          size = 100
          shape0 = (1,)
          shape1 = (5,)
          shape2 = (4, 5)
          out = broadcast_dist_samples_shape([shape0, shape1, shape2],
                                             size=size)
          assert out == (4, 5)
      """
      if size is None:
          broadcasted_shape = np.broadcast_shapes(*shapes)
          if broadcasted_shape is None:
              raise ValueError(
                  "Cannot broadcast provided shapes {} given size: {}".format(
                      ", ".join([f"{s}" for s in shapes]), size
                  )
              )
          return broadcasted_shape
      shapes = [_check_shape_type(s) for s in shapes]
      _size = to_tuple(size)
      # samples shapes without the size prepend
      sp_shapes = [
          s[len(_size) :]
          if _size == s[: min([len(_size), len(s)])]
          else s
          for s in shapes
      ]
      try:
          broadcast_shape = np.broadcast_shapes(*sp_shapes)
      except ValueError:
          raise ValueError(
              "Cannot broadcast provided shapes {} given size: {}".format(
                  ", ".join([f"{s}" for s in shapes]), size
              )
          )
      broadcastable_shapes = []
      for shape, sp_shape in zip(shapes, sp_shapes):
          if _size == shape[: len(_size)]:
              # If size prepends the shape, then we have to add broadcasting axis
              # in the middle
              p_shape = (
                  shape[: len(_size)]
                  + (1,) * (len(broadcast_shape) - len(sp_shape))
                  + shape[len(_size) :]
              )
          else:
              p_shape = shape
          broadcastable_shapes.append(p_shape)
      return np.broadcast_shapes(*broadcastable_shapes)
  
imports: import warnings
  from functools import singledispatch
  from typing import Any, Optional, Sequence, Tuple, Union, cast
  import numpy
  from pytensor import config
  from pytensor import tensor
  from pytensor.graph.basic import Variable
  from pytensor.graph.op import Op, compute_test_value
  from pytensor.raise_op import Assert
  from pytensor.tensor.random.op import RandomVariable
  from pytensor.tensor.shape import SpecifyShape
  from pytensor.tensor.variable import TensorVariable
  from typing_extensions import TypeAlias
  from pymc.model import modelcontext
  from pymc.pytensorf import convert_observed_data
  from pymc.exceptions import ShapeError
  from pymc.pytensorf import PotentialShapeType
  from pymc.util import _add_future_warning_tag
label_desc: Use image node_id 3 for calling a global function with example usage: broadcast_dist_samples_shape(shapes, size) and returns: np, broadcasted_shape