# Metric definitions and calculation logic

Every metric is implemented three times, independently, and reconciled by `tests/reconcile.test.js`:

| Implementation | File | Used for |
|---|---|---|
| JavaScript | `assets/js/metrics.js` | The website (its own analytics engine) |
| pandas | `analytics/prepare_data.py` | Power BI tables and `metrics_snapshot.json` |
| SQL | `analytics/sql/queries.sql` | Query-level analysis in SQLite |

All thresholds are illustrative assumptions.

## Populations

- **In inventory** on date D: `registered_date <= D` and not retired by D.
- **Live** on date D: `production_date <= D` and not retired by D. Suspended systems are live: their controls still matter. Control metrics use the live population.

## Point-in-time measures (evaluated as of a date)

| Metric | Numerator | Denominator | Notes |
|---|---|---|---|
| Control coverage | Required controls with `implemented_date <= D` | Required controls on live systems | Design: the control exists |
| Control effectiveness | Required controls whose latest test in `(D-210 days, D]` is Pass | Required controls on live systems | Untested counts as not effective. Ties on date break on test id |
| Evidence currency | Required controls with an evidence record where `collected_date <= D < valid_until` | Required controls on live systems | Evidence expiring on D is no longer current |
| Evidence stale / missing | Controls with only expired evidence / with none | Same | Reported as counts |
| Open findings | `opened_date <= D` and not closed by D, on live systems | none | |
| Unresolved critical findings | Open findings with severity Critical | Open findings | |
| Overdue findings | Open findings with `due_date < D` | Open findings | SLA: Critical 15, High 30, Medium 60, Low 90 days |
| Remediation aging | Open findings by `D - opened_date`: 0-30, 31-60, 61-90, 90+ | none | |
| Overdue reviews | Live systems with `next_review_date < D` | Live systems | Interval by tier: 365, 180, 90, 60 days |
| Active exceptions | Granted by D, not closed, `expiry_date > D` | none | |
| Exceptions past expiry | Granted by D, not closed, `expiry_date <= D` | Open exceptions | |
| Reassessments pending | `reassessment_required = 1`, changed by D, not completed by D | none | Overdue when `reassessment_due_date < D` |
| High-risk agents | Live systems with autonomy A2 or A3 and inherent tier High or Critical | Live systems | |
| High-risk agents exposed | Of those, any critical control not effective | High-risk agents | |

## Residual risk tier

Start at the inherent tier index (Low 0 to Critical 3).

1. Reduce by 1 if coverage >= 0.90, effectiveness >= 0.80, no open Critical or High finding, and no critical control whose latest test failed.
2. Reduce by 2 instead if, in addition, effectiveness >= 0.95 and evidence currency >= 0.90.
3. Cap the reduction at 1 for Critical inherent tier.
4. No reduction for systems that are not live.

Residual can never exceed inherent. This is a conservative convention, not a loss estimate.

## Flow measures (records dated inside a month)

| Metric | Numerator | Denominator |
|---|---|---|
| Test pass rate | Tests with result Pass in the month | Tests in the month |
| Incidents | Incidents with `detected_date` in the month | none |
| Findings opened / closed | Findings with `opened_date` / `closed_date` in the month | none |

Pass rate and effectiveness answer different questions. Pass rate can improve while effectiveness falls if fewer controls are being tested.

## Trends

Each month-end point re-evaluates the point-in-time definitions as of that date. Nothing is carried forward from a stored snapshot in the website engine; `fact_system_month.csv` stores the same values for Power BI.

Overdue reviews have no trend: only current review dates are stored.

## Anomaly screen

For month *i*, compare the value with the mean and population standard deviation of months before *i* (minimum four). Flag if `|z| >= 2`. The dashboard shows positive deviations for incidents and open critical findings. Twelve points is a small sample; the screen prompts a question and proves nothing.

## Attention list

A live system appears when any rule is true: open Critical finding; critical control failing; suspended; exception past expiry; reassessment overdue; review overdue; evidence currency below 70%. Order is a triage rank (rule weights summed, multiplied by inherent tier index plus one). It is a sort key and is never shown as a score.

## Incident INC-0031 figures

The split-refund incident is the one place the site quotes money, so its figures have a single source: the 44 rows of `incident_refunds`, written by `hero_refund_records()` in `analytics/generate_data.py`. The incident description, the scenario page and the tests all count or sum those rows.

| Figure | Definition | Value |
|---|---|---|
| Refunds | Rows for the incident | 44 |
| Customers | Distinct `customer_ref` | 14 |
| Total refunded | Sum of `amount_usd` | $5,220 |
| Amount above the daily level | For each customer and day, day total minus $300 where positive, summed | $1,020 |
| Refunds policy v1.1 would route | Rows where `prior_refunds_today_usd + amount_usd` is above $300 (rule AP-07b) | 16 refunds, $1,970 |

Thresholds come from `agent_policy.limits`: a refund at or under $150 is autonomous (rule AP-08); rule AP-07b holds a refund when the customer's day total would go above $300.

Assumptions, all synthetic: the refunds happened on one day (September 8, 2026) and were found by the next morning's reconciliation; each is a multiple of $5 at or under $150; every customer's day total is above $300, which takes at least three refunds each. The $300 level did not exist when the refunds were issued. It is the level policy v1.1 set afterward, used here as the yardstick for what should have been reviewed.

None of these is a loss figure. The records show what was issued without review, not what was owed. The earlier version of the site quoted 41 refunds, 14 customers and $6,180, which could not all be true: 41 refunds of at most $150 total at most $6,150.
