nodes: 0, module; 1, function_definition; 2, function_name:threshold_brier_score; 3, parameters; 4, block; 5, identifier:observations; 6, identifier:forecasts; 7, identifier:threshold; 8, default_parameter; 9, default_parameter; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, if_statement; 15, if_statement; 16, if_statement; 17, expression_statement; 18, if_statement; 19, if_statement; 20, expression_statement; 21, if_statement; 22, expression_statement; 23, if_statement; 24, return_statement; 25, identifier:issorted; 26, False; 27, identifier:axis; 28, unary_operator; 29, comment:"""
      Calculate the Brier scores of an ensemble for exceeding given thresholds.
  
      According to the threshold decomposition of CRPS, the resulting Brier
      scores can thus be summed along the last axis to calculate CRPS, as
  
      .. math::
          CRPS(F, x) = \int_z BS(F(z), H(z - x)) dz
  
      where $F(x) = \int_{z \leq x} p(z) dz$ is the cumulative distribution
      function (CDF) of the forecast distribution $F$, $x$ is a point estimate of
      the true observation (observational error is neglected), $BS$ denotes the
      Brier score and $H(x)$ denotes the Heaviside step function, which we define
      here as equal to 1 for x >= 0 and 0 otherwise.
  
      It is more efficient to calculate CRPS directly, but this threshold
      decomposition itself provides a useful summary of model quality as a
      function of measurement values.
  
      The Numba accelerated version of this function is much faster for
      calculating many thresholds simultaneously: it runs in time
      O(N * (E * log(E) + T)), where N is the number of observations, E is the
      ensemble size and T is the number of thresholds.
  
      The non-Numba accelerated version requires time and space O(N * E * T).
  
      Parameters
      ----------
      observations : float or array_like
          Observations float or array. Missing values (NaN) are given scores of
          NaN.
      forecasts : float or array_like
          Array of forecasts ensemble members, of the same shape as observations
          except for the extra axis corresponding to the ensemble. If forecasts
          has the same shape as observations, the forecasts are treated as
          deterministic. Missing values (NaN) are ignored.
      threshold : scalar or 1d array_like
          Threshold value(s) at which to calculate exceedence Brier scores.
      issorted : bool, optional
          Optimization flag to indicate that the elements of `ensemble` are
          already sorted along `axis`.
      axis : int, optional
          Axis in forecasts which corresponds to different ensemble members,
          along which to calculate the threshold decomposition.
  
      Returns
      -------
      out : np.ndarray
          Brier scores at each thresold for each ensemble forecast against the
          observations. If ``threshold`` is a scalar, the result will have the
          same shape as observations. Otherwise, it will have an additional final
          dimension corresponding to the threshold levels.
  
      References
      ----------
      Gneiting, T. and Ranjan, R. Comparing density forecasts using threshold-
         and quantile-weighted scoring rules. J. Bus. Econ. Stat. 29, 411-422
         (2011). http://www.stat.washington.edu/research/reports/2008/tr533.pdf
  
      See also
      --------
      crps_ensemble, brier_score
      """; 30, assignment; 31, assignment; 32, assignment; 33, comparison_operator:axis != -1; 34, block; 35, comparison_operator:forecasts.shape == observations.shape; 36, block; 37, comparison_operator:observations.shape != forecasts.shape[:-1]; 38, block; 39, assignment; 40, comparison_operator:threshold.ndim > 1; 41, block; 42, boolean_operator; 43, block; 44, assignment; 45, not_operator; 46, block; 47, assignment; 48, identifier:scalar_threshold; 49, block; 50, identifier:result; 51, integer:1; 52, identifier:observations; 53, call; 54, identifier:threshold; 55, call; 56, identifier:forecasts; 57, call; 58, identifier:axis; 59, unary_operator; 60, expression_statement; 61, attribute; 62, attribute; 63, expression_statement; 64, attribute; 65, subscript; 66, raise_statement; 67, identifier:scalar_threshold; 68, comparison_operator:threshold.ndim == 0; 69, attribute; 70, integer:1; 71, raise_statement; 72, comparison_operator:threshold.ndim == 1; 73, not_operator; 74, raise_statement; 75, identifier:threshold; 76, call; 77, identifier:issorted; 78, expression_statement; 79, identifier:result; 80, call; 81, expression_statement; 82, attribute; 83, argument_list; 84, attribute; 85, argument_list; 86, attribute; 87, argument_list; 88, integer:1; 89, assignment; 90, identifier:forecasts; 91, identifier:shape; 92, identifier:observations; 93, identifier:shape; 94, assignment; 95, identifier:observations; 96, identifier:shape; 97, attribute; 98, slice; 99, call; 100, attribute; 101, integer:0; 102, identifier:threshold; 103, identifier:ndim; 104, call; 105, attribute; 106, integer:1; 107, call; 108, call; 109, attribute; 110, argument_list; 111, assignment; 112, identifier:_threshold_brier_score_core; 113, argument_list; 114, assignment; 115, identifier:np; 116, identifier:asarray; 117, identifier:observations; 118, identifier:np; 119, identifier:asarray; 120, identifier:threshold; 121, identifier:np; 122, identifier:asarray; 123, identifier:forecasts; 124, identifier:forecasts; 125, call; 126, identifier:forecasts; 127, subscript; 128, identifier:forecasts; 129, identifier:shape; 130, unary_operator; 131, identifier:ValueError; 132, argument_list; 133, identifier:threshold; 134, identifier:ndim; 135, identifier:ValueError; 136, argument_list; 137, identifier:threshold; 138, identifier:ndim; 139, attribute; 140, argument_list; 141, identifier:ValueError; 142, argument_list; 143, identifier:threshold; 144, identifier:reshape; 145, binary_operator:(1,) * observations.ndim + (-1,); 146, identifier:forecasts; 147, call; 148, identifier:observations; 149, identifier:forecasts; 150, identifier:threshold; 151, identifier:result; 152, call; 153, identifier:move_axis_to_end; 154, argument_list; 155, identifier:forecasts; 156, ellipsis:...; 157, attribute; 158, integer:1; 159, binary_operator:'observations and forecasts must have matching '
                           'shapes or matching shapes except along `axis=%s`'
                           % axis; 160, string; 161, parenthesized_expression; 162, identifier:all; 163, string; 164, binary_operator:(1,) * observations.ndim; 165, tuple; 166, attribute; 167, argument_list; 168, attribute; 169, argument_list; 170, identifier:forecasts; 171, identifier:axis; 172, identifier:np; 173, identifier:newaxis; 174, concatenated_string; 175, identifier:axis; 176, string_content:threshold must be scalar or 1-dimensional; 177, comparison_operator:np.sort(threshold) == threshold; 178, string_content:1D thresholds must be sorted; 179, tuple; 180, attribute; 181, unary_operator; 182, identifier:np; 183, identifier:sort; 184, identifier:forecasts; 185, keyword_argument; 186, identifier:result; 187, identifier:squeeze; 188, keyword_argument; 189, string; 190, string; 191, call; 192, identifier:threshold; 193, integer:1; 194, identifier:observations; 195, identifier:ndim; 196, integer:1; 197, identifier:axis; 198, unary_operator; 199, identifier:axis; 200, unary_operator; 201, string_content:observations and forecasts must have matching; 202, string_content:shapes or matching shapes except along `axis=%s`; 203, attribute; 204, argument_list; 205, integer:1; 206, integer:1; 207, identifier:np; 208, identifier:sort; 209, identifier:threshold
code: def threshold_brier_score(observations, forecasts, threshold, issorted=False,
                            axis=-1):
      """
      Calculate the Brier scores of an ensemble for exceeding given thresholds.
  
      According to the threshold decomposition of CRPS, the resulting Brier
      scores can thus be summed along the last axis to calculate CRPS, as
  
      .. math::
          CRPS(F, x) = \int_z BS(F(z), H(z - x)) dz
  
      where $F(x) = \int_{z \leq x} p(z) dz$ is the cumulative distribution
      function (CDF) of the forecast distribution $F$, $x$ is a point estimate of
      the true observation (observational error is neglected), $BS$ denotes the
      Brier score and $H(x)$ denotes the Heaviside step function, which we define
      here as equal to 1 for x >= 0 and 0 otherwise.
  
      It is more efficient to calculate CRPS directly, but this threshold
      decomposition itself provides a useful summary of model quality as a
      function of measurement values.
  
      The Numba accelerated version of this function is much faster for
      calculating many thresholds simultaneously: it runs in time
      O(N * (E * log(E) + T)), where N is the number of observations, E is the
      ensemble size and T is the number of thresholds.
  
      The non-Numba accelerated version requires time and space O(N * E * T).
  
      Parameters
      ----------
      observations : float or array_like
          Observations float or array. Missing values (NaN) are given scores of
          NaN.
      forecasts : float or array_like
          Array of forecasts ensemble members, of the same shape as observations
          except for the extra axis corresponding to the ensemble. If forecasts
          has the same shape as observations, the forecasts are treated as
          deterministic. Missing values (NaN) are ignored.
      threshold : scalar or 1d array_like
          Threshold value(s) at which to calculate exceedence Brier scores.
      issorted : bool, optional
          Optimization flag to indicate that the elements of `ensemble` are
          already sorted along `axis`.
      axis : int, optional
          Axis in forecasts which corresponds to different ensemble members,
          along which to calculate the threshold decomposition.
  
      Returns
      -------
      out : np.ndarray
          Brier scores at each thresold for each ensemble forecast against the
          observations. If ``threshold`` is a scalar, the result will have the
          same shape as observations. Otherwise, it will have an additional final
          dimension corresponding to the threshold levels.
  
      References
      ----------
      Gneiting, T. and Ranjan, R. Comparing density forecasts using threshold-
         and quantile-weighted scoring rules. J. Bus. Econ. Stat. 29, 411-422
         (2011). http://www.stat.washington.edu/research/reports/2008/tr533.pdf
  
      See also
      --------
      crps_ensemble, brier_score
      """
      observations = np.asarray(observations)
      threshold = np.asarray(threshold)
      forecasts = np.asarray(forecasts)
  
      if axis != -1:
          forecasts = move_axis_to_end(forecasts, axis)
  
      if forecasts.shape == observations.shape:
          forecasts = forecasts[..., np.newaxis]
  
      if observations.shape != forecasts.shape[:-1]:
          raise ValueError('observations and forecasts must have matching '
                           'shapes or matching shapes except along `axis=%s`'
                           % axis)
  
      scalar_threshold = threshold.ndim == 0
  
      if threshold.ndim > 1:
          raise ValueError('threshold must be scalar or 1-dimensional')
      if threshold.ndim == 1 and not (np.sort(threshold) == threshold).all():
          raise ValueError('1D thresholds must be sorted')
  
      threshold = threshold.reshape((1,) * observations.ndim + (-1,))
  
      if not issorted:
          forecasts = np.sort(forecasts, axis=-1)
  
      result = _threshold_brier_score_core(observations, forecasts, threshold)
  
      if scalar_threshold:
          result = result.squeeze(axis=-1)
  
      return result