PY-80

Build Residual Evidence

  • Medium
  • Residual Evidence
  • Python

Task

Write build_residual_evidence(observed, predicted, representative_count). observed and predicted are one-dimensional finite numeric NumPy arrays of the same length. At position i, the signed residual is observed[i] - predicted[i]; its absolute residual is its absolute value. representative_count is a non-negative integer.

Return a dictionary with independent float64 arrays under "residuals" and "absolute", plus "summary" and "representatives". The summary is a dictionary containing integer "count" and floating-point "mean_signed", "mean_absolute", and "max_absolute". For an empty input, all three summary values are 0.0. Otherwise use the ordinary means and maximum. Select at most representative_count representative rows by largest absolute residual; break ties by smaller original index. Each representative record contains "index", "observed", "predicted", "residual", and "absolute", in selection order. An empty request returns no records.

Reject mismatched lengths, invalid dimensions, non-finite values, or a boolean and negative count with ValueError. Do not modify either input.

Example

For observed [3., 8.] and predicted [4., 6.], residuals are [-1., 2.], absolute residuals are [1., 2.], and the first representative when the count is one is index 1.

Your implementation

You may import NumPy as np. Do not print or ask for input.