code: def _energy_np(spl, x=None, weights=None, assume_sorted=False):
      r"""Compute sample energy, fast numpy based subroutine.
  
      Let :math:`X` be the random variable with support being
      values of `spl`, with probability weights `weights`.
  
      This function then returns :math:`\mathbb{E}[|X-Y|]`, with :math:`Y` an
      independent copy of :math:`X`, if `x` is `None`.
  
      If `x` is passed, returns :math:`\mathbb{E}[|X-x|]`.
  
      Parameters
      ----------
      spl : 1D np.ndarray
          empirical sample
      x : None or float, optional, default=None
          if None, computes self-energy, if float, computes energy wrt x
      weights : None or 1D np.ndarray, optional, default=None
          if None, computes unweighted energy, if 1D np.ndarray, computes weighted energy
          if not None, must be of same length as ``spl``, needs not be normalized
      assume_sorted : bool, optional, default=False
          if True, assumes that ``spl`` is sorted in ascending order
  
      Returns
      -------
      float
          energy as described above
      """
      if weights is None:
          weights = np.ones(len(spl))
  
      if not assume_sorted:
          sorter = np.argsort(spl)
          spl = spl[sorter]
          weights = weights[sorter]
  
      w_sum = np.sum(weights)
      weights = weights / w_sum
  
      spl_diff = np.diff(spl)
  
      if x is None:
          cum_fwd = np.cumsum(weights[:-1])
          cum_back = np.cumsum(weights[1:][::-1])[::-1]
          energy = 2 * np.sum(cum_fwd * cum_back * spl_diff)
      else:
          spl_diff = np.abs(spl - x)
          energy = np.sum(weights * spl_diff)
  
      return energy
  
file_path: sktime/sktime/proba/empirical.py
imports: import numpy
  import pandas
  from sktime.proba.base import BaseDistribution
label_desc: Use image node_id 1 for calling a global function with example usage: _energy_np(spl, x, weights, assume_sorted) and returns: energy