def precision(context,
              positive_class=None,
              penalty_loss=defaults.DEFAULT_PENALTY_LOSS,
              constraint_loss=defaults.DEFAULT_CONSTRAINT_LOSS):
  """Creates an `Expression`s for precision.

  A precision is the number of positively-labeled examples within the given
  context on which the model makes a positive prediction, divided by the number
  of examples within the context on which the model makes a positive prediction.
  For multiclass problems, the positive_class argument, which tells us which
  class (or classes) should be treated as positive, must also be provided.

  Args:
    context: multiclass `SubsettableContext`, the block of data to use when
      calculating the rate.
    positive_class: None for a non-multiclass problem. Otherwise, an int, the
      index of the class to treat as "positive", *or* a collection of
      num_classes elements, where the ith element is the probability that the
      ith class should be treated as "positive".
    penalty_loss: `MulticlassLoss`, the (differentiable) loss function to use
      when calculating the "penalty" approximation to the rate.
    constraint_loss: `MulticlassLoss`, the (not necessarily differentiable) loss
      function to use when calculating the "constraint" approximation to the
      rate.

  Returns:
    An `Expression` representing the precision.

  Raises:
    TypeError: if the context is not a SubsettableContext, either loss is not a
      BinaryClassificationLoss (if the context is non-multiclass) or a
      MulticlassLoss (if the context is multiclass). In the latter case, an
      error will also be raised if positive_class is a non-integer number.
    ValueError: if the context doesn't contain labels, or positive_class is
      provided for a non-multiclass context, or is *not* provided for a
      multiclass context. In the latter case, an error will also be raised if
      positive_class is an integer outside the range [0,num_classes), or is a
      collection not containing num_classes elements.
  """
  numerator_expression, denominator_expression = precision_ratio(
      context=context,
      positive_class=positive_class,
      penalty_loss=penalty_loss,
      constraint_loss=constraint_loss)
  return _ratio(
      numerator_expression=numerator_expression,
      denominator_expression=denominator_expression)