code: def range_based_precision_score(
      y_true,
      y_pred,
      alpha: float = 0.5,
      positional_bias: str = "flat",
      cardinality_bias: Optional[str] = None,
      range_based: bool = True,
  ):
      """Compute a precision score for two classes, usually labeled 1 and 0 to denote an anomaly and not an anomaly.
      Both ``y_true`` and ``y_pred`` need to be sorted by timestamp.
  
      This precision implementation is from the paper:
      Precision and Recall for Time Series
      <https://arxiv.org/abs/1803.03639>;
  
      Point-wise real and predicted anomalies are first transformed into anomaly ranges. Then, given the set of real
      anomaly ranges, R = {R_1, ..., R_Nr}, and predicted anomaly ranges, P = {P_1, ..., P_Np}, a precision score Precision_T(R, P_j) is
      calculated for each predicted anomaly range, P_j. Those precision scores are then added into a total precision score and divided by the
      total number of predicted anomaly ranges, Np, to obtain an average precision score for the whole timeseries.
  
      Multiple considerations are taken into account when computing the individual precision scores for each real anomaly range, such as:
          Existence: Catching the existence of an anomaly (even by predicting only a single point in R_i), by itself, might be valuable
          for the application.
          Size: The larger the size of the correctly predicted portion of R_i, the higher the precision score.
          Position: In some cases, not only size, but also the relative position of the correctly predicted portion of R_i might matter
          to the application.
          Cardinality: Detecting R_i with a single prediction range P_j ∈ P may be more valuable than doing so with multiple different
          ranges in P in a fragmented manner.
  
      All of those considerations are captured using two main reward terms: Existence Reward and Overlap Reward, weighted by a weighting
      constant ``alpha``. The precision score for each predicted anomaly range will be calculated as:
          Precision_T = alpha * Existence Reward + (1 - alpha) * Overlap Reward
  
      Parameters
      ----------
      y_true : array-like, 1-D
          The actual point-wise anomalies
      y_pred : array-like, 1-D
          The predicted point-wise anomalies
      alpha : `float`
          Reward weighting term for the two main reward terms for the predicted anomaly range precision score: existence and overlap rewards.
      positional_bias : `str`, default "flat"
          The accepted options are:
          * "flat": Each index position of an anomaly range is equally important. Return the same value of 1.0 as the positional
            reward regardless of the location of the pointwise anomaly within the anomaly range.
          * "front": Positional reward is biased towards early detection, as earlier overlap locations of pointwise
            anomalies with an anomaly range are assigned higher rewards.
          * "middle": Positional reward is biased towards the detection of anomaly closer to its middle point, as overlap locations
            closer to the middle of an anomaly range are assigned higher rewards.
          * "back":  Positional reward is biased towards later detection, as later overlap locations of pointwise anomalies with an
            anomaly range are assigned higher rewards.
      cardinality_bias: `str` or None, default None
          In the overlap reward, this is a penalization factor. If None, no cardinality penalty will be applied. If "reciprocal", the
          overlap reward will be penalized as it gets multiplied by the reciprocal of the number of detected anomaly ranges overlapping
          with the predicted anomaly range.
      range_based: `bool`, default True
          This implementation of range-based precision subsumes the classic precision. If True range based precision will be calculated, otherwise
          classic precision will be calculated.
  
      Returns
      -------
      precision : `float`
          The overall precision score for the time series.
      """
      assert len(y_true) == len(y_pred)
      assert 0 <= alpha <= 1
      assert positional_bias in ["flat", "front", "middle", "back"]
      if cardinality_bias is not None:
          assert cardinality_bias == "reciprocal"
  
      real_anomaly_ranges = prepare_anomaly_ranges(
          np.array(y_true), range_based
      )
      predicted_anomaly_ranges = prepare_anomaly_ranges(
          np.array(y_pred), range_based
      )
  
      precision = compute_range_based_score(
          predicted_anomaly_ranges,
          real_anomaly_ranges,
          alpha,
          positional_bias,
          cardinality_bias,
      )
      return precision
  
imports: import functools
  import warnings
  from typing import Optional
  from typing import Union
  import numpy
  import pandas
  from sklearn import metrics
  from greykite.detection.common.ad_evaluation_utils import compute_range_based_score
  from greykite.detection.common.ad_evaluation_utils import prepare_anomaly_ranges
label_desc: Use image node_id 11 for calling a global function with example usage: range_based_precision_score(y_true, y_pred, alpha, positional_bias, cardinality_bias, range_based) and returns: precision