# Custom risk scores

Write a condition as a plain-English question with the answers you choose; a model gives each answer a probability, and your rules act on the answer and how likely it is.

Write a condition as a plain-English question with the answers you choose, like yes or no, or a few options. Our
model scores each answer with a probability, and your rules act on the answer and how likely it is.

## When to use it

- A judgement that doesn't reduce to one field and a threshold: "Does the shipping address fit this customer?"
- Sorting a case into a few kinds: "Which kind of customer is this order from?" with answers like normal, reseller
  and card testing.
- A condition your team applies by hand in the review queue today and wants measured.

A risk score never decides on its own. Your rules read its answer like any other field, and hold the thresholds.

## How it works

1. **You write the question and its answers.** `kind: yes_no` has the answers `yes` and `no`, which you can relabel
   and describe; `kind: choice` has 2 to 8 answers, each a `value`, a `label` and an optional `description`. A choice
   of three or more gets a "None of these" answer (`none_of_these`) unless you send `none_of_these: false`.
2. **You choose the fields it reads.** Only those fields are sent to the model.
3. **A rule asks.** When a rule's outcome still depends on the answer, the model gives each answer a probability. A
   rule that is already decided by its other conditions never asks.
4. **Your rule decides.** It reads the answer and its probability, like any other field.

Each live risk score adds three fields to the rules of its domains:

| Field | What it holds |
| --- | --- |
| `policy.<key>.answer` | The answer with the highest probability. A tie goes to the answer listed first. |
| `policy.<key>.p.<value>` | One per answer: its probability. They add up to 1. |
| `policy.<key>.calibrated` | Whether the probabilities were checked against your own outcomes. |

A rule usually reads the answer and how likely it is together:

```json
{
  "label": "Address doesn't fit, and the model is sure",
  "when": [
    { "fact": "order.amount", "op": "gt", "value": 250 },
    { "fact": "policy.shipping_mismatch.answer", "op": "eq", "value": "no" },
    { "fact": "policy.shipping_mismatch.p.no", "op": "gt", "value": 0.8 }
  ],
  "require": { "approval": true }
}
```

In the rule above, a small cart never reaches the model, because the first condition already doesn't match.

## Write a risk score

1. **Turn them on.** An owner or admin turns on custom risk scores in Settings → AI
   (`PUT /v1/account/risk_scores/settings` with `enabled: true`). They are off by default.
2. **Create it.** `POST /v1/account/risk_scores` with a `key`, the `question` (10 to 300 characters, asking one
   thing), optional `guidance`, the `answers` and the `bindings`: fields from the ones your rules can read
   (`GET /v1/account/policies/fields`), or a request `field` for free text (at most 500 characters, marked as text a
   person wrote).
3. **Look at the data to be scored.** `POST /v1/account/risk_scores/{key}/preview` with a past record or a `state`
   returns the exact payload, and calls no model.

Every change makes a new, immutable version (`POST /v1/account/risk_scores/{key}/versions`). A new version never
changes the answers your live rules read until you promote it.

## Test it

`POST /v1/account/risk_scores/{key}/test` with a past record or a `state` answers the risk score and records the
evaluation. `GET /v1/account/risk_scores/{key}/evaluations` lists recent ones: the answer, every probability, and
`calibrated`.

A checkout result lists every risk score its decision read, with the answer or the reason there was none. The deciding
rule's `policy.trace` shows each risk score condition as a `risk_score` node: the answer, its probabilities as rules
read them and as the model gave them, whether they were calibrated, and the evaluation's id.

## Reasons

Every answer gets a short reason by default, written by a small model after the answer: one or two sentences naming
the fields it leaned on, headed "Reason" on screens. The answer never waits for it and a reason never changes an
outcome; one that couldn't be written reads unavailable.

- `reason.enabled` turns reasons off for a risk score.
- `reason.under` and `reason.over` narrow them to answers whose probability is under or over a number, or both.
- Reasons are written for decisions and tests, never for shadow, replays or eval runs. They are stored encrypted, read
  on one evaluation at a time (`GET /v1/account/risk_score_evaluations/{id}`), never shown to customers, and included
  in the evaluation's price.

## Go live

A risk score decides nothing until it has been measured on your own cases and has run beside your rules. A draft
of your rules may read a risk score that isn't live, so you can replay it; publishing rules that read one answers
`risk_score_not_live`.

1. **Build a golden set.** `POST /v1/account/risk_scores/{key}/golden_cases` adds a past record or a `state` with the
   answer you confirm is right. An eval run needs at least 20 cases covering two or more answers.
2. **Run an eval run.** `POST /v1/account/risk_scores/{key}/eval_runs` answers every case and reports, per answer, how
   well the probabilities tell the answers apart (AUC), precision and recall at the thresholds your rules use, and how
   the top answers compare with the right ones. It also checks that the answers are stable, that text written into a
   free-text field can't move them by more than 0.10, and how much they shift when the answers are listed in reverse,
   when two descriptions are swapped, or when the question is reworded.

   A run that fits in the current minute answers at once;
   a larger one answers with `status: "running"` and finishes in the background, read it with
   `GET /v1/account/risk_scores/{key}/eval_runs/{id}`. Eval runs, replays and Suggestions tests send at most what the
   answering service's per-minute limit leaves room for, keep a fifth of it for live decisions, and wait for the next
   minute rather than stop early.
3. **Shadow.** `POST /v1/account/risk_scores/{key}/versions/{version}/shadow` answers the version on a sample of live
   cases after each response, without changing anything.
4. **Publish.** `POST /v1/account/risk_scores/{key}/versions/{version}/publish` needs a passed eval run, 200 shadow
   evaluations or 7 days of shadow, and a replay you have read. Otherwise it answers `risk_score_not_ready` and lists
   each step still open. A risk score doesn't have to be calibrated to go live.

`GET /v1/account/risk_scores/{key}` lists the same steps before you call publish: `ready.shadow` and `ready.live` each
say whether the newest version is ready and, in `missing`, which steps are still open (`eval_run`, `shadow` with how
many shadow evaluations so far, `replay_viewed`).

## Replay and shadow

- **Replay.** `POST /v1/account/risk_scores/{key}/replay` answers a version over a window of your past records, newest
  first, at most 5,000. Send `dry_run: true` first to see how many records and evaluations it will count and the
  model cost.
- **What a replay shows.** The share of cases by top answer, each answer's probability distribution, and, at five
  thresholds, how many orders and how much order value a rule at that threshold would decline, with the labelled
  results: true positive declines, false positive declines, missed and allowed, each with its order value. Labels come
  from the outcomes you report on orders.
- **The finer grid.** `grid` gives the same every 0.05, with the share of cases above each threshold and the outcome
  rates if a rule declined above it, beside today's. Values are in the records' `currency`.
- **Finding them.** `GET /v1/account/risk_scores/{key}/replays` lists the replays, newest first, and the risk score's
  `latest_replay` names the newest finished one.
- **Shadow.** A version in shadow answers the same cases as the live one, after each response, on a sample (1 in 4
  by default, `shadow_sample_rate`). It never changes a response, a bill line for the decision, an approval or a
  notification.

## Labels and calibration

A model's probabilities aren't trustworthy until they are checked against what happened. A risk score is checked
against your own outcomes, one answer at a time.

- **Labels.** Each version maps the labels you already have to the answer they show was right (`labels.map`, for
  example `{ "no": ["chargeback", "fraud"], "yes": ["legitimate", "matured_clean"] }`). They come from the outcomes you
  report on orders, from `matured_clean` (no dispute or flag after `mature_after_days`), and from
  `POST /v1/account/risk_scores/{key}/labels` with the right answer for a case. The latest label on a case wins, and a
  change keeps the history.
- **The list.** `GET /v1/account/risk_scores/{key}/labels` lists every label that counts, from people, your API,
  payments and `matured_clean`, each with the answer it shows, then the ones you set that a later label replaced.
- **The starting map.** A new yes/no risk score starts with `yes` meaning the risk is there: chargeback, fraud and
  refund abuse show `yes` was right; legitimate and `matured_clean` show `no` was. If your question asks the opposite
  ("Does the address fit?"), flip the map. A risk score with a few options starts with no map, and reads "No label
  mapped yet" (`labels_status: not_mapped`) until you add one.
- **Calibration.** With 100 or more labelled cases (and 20 for every answer your rules read),
  `POST /v1/account/risk_scores/{key}/calibrators` fits a calibrator on the older 70% of cases and measures it on
  the newest 30%. `GET /v1/account/risk_scores/{key}/calibration` shows, per answer, how often it was right when it
  said 80% (the reliability table, after calibration and, in `raw_reliability`, before it), how far off it is on
  average (expected calibration error), how well it tells the answers apart (AUC), and how many cases are labelled.
- **Activation.** The first calibrator waits for you to activate it, because it changes what a threshold in your rules
  means. Later refits replace it on their own only when they are no worse and move no answer's probabilities much.
- **Before calibration** your rules may still use the risk score with any outcome; the probabilities are the model's
  own and `policy.<key>.calibrated` is false.
- **What the labels don't cover.** Outcomes exist for orders that went through or were reviewed, not for the ones
  rejected; the calibration page says how many of the evaluated cases are labelled.

## When there is no answer

A model call can time out, fail, or not be made because a limit is spent. Then the risk score has no answer, never a
guessed one: its fields are unavailable, `result` on the evaluation says why, and your rule decides what happens:

| Setting | What the condition does |
| --- | --- |
| `skip` | Doesn't match: the decision is what it would be without the risk score. |
| `match` | Matches: the rule's outcome applies. |
| `ask` | Matches, and the rule asks a person instead of refusing. |

A condition's `if_unavailable` wins; otherwise the risk score's own `if_unavailable`; otherwise the domain's default:
`skip` for checkout and traffic, `ask` for captures and agent actions. An evaluation with no answer is never billed.

## Usage and billing

- The unit is one evaluation that returned probabilities. An evaluation with no answer, an answer reused for the same
  case within ten minutes, and a rule that never needed to ask are not counted. Reasons are included.
- Each plan includes evaluations every month, and evaluations past them are billed per evaluation (see the pricing
  page). On the trial, calls stop once the included evaluations are used.
- Tests, eval runs and replays first use a separate monthly testing allowance; past it they count like decisions.
- `monthly_cap` in the settings stops calls past a number of evaluations a month.
- `GET /v1/account/risk_scores/usage` shows the month so far, and the list of risk scores gives each one's
  `evaluations_this_month` (live and shadow answers).
- In the sandbox, risk scores give fixed test answers at no charge. Set `sandbox_provider: "real"` on a risk score to
  ask the real model; those answers come out of the testing allowance and stop when it runs out. The published
  sandbox keys always get the test answers; a risk score read with them says `published_account: true`. See
  [the sandbox](https://provenanceauth.com/docs/get-started/sandbox.md).
- A risk score that Suggestions drafts is tested on your labelled records at no charge to you.

## What is sent to the model

- Only the bound fields. `GET /v1/account/policies/fields` gives each field's `sensitivity` before you save: whether
  it is derived, personal, or never sent.
- A sensitive field (a government id, a full card number, a security code, a password, a
  token, biometric data, a precise location) can never be bound.
- A raw personal field (a name, an email address, a street address, a phone number, an IP address, free text a person
  wrote, or one of your own fields) is sent only when you choose it, after you accept the risk score data terms once
  for your account (`accept_terms_version`). In the live environment personal fields can't be bound yet; sandbox
  accepts them.
- Your answers are sent as neutral option names (`a`, `b`, …), with your label and description as each one's
  meaning, because a model can follow an option's name over its meaning.
- We store the answer, the probabilities and hashes of what was sent, never the fields themselves.

## Errors

| Code | When |
| --- | --- |
| `risk_score_off` | Custom risk scores aren't turned on for the account. |
| `risk_score_answers_invalid` | The answer set isn't valid; `param` names the part. |
| `risk_score_binding_refused` | A binding names something a risk score can't read. |
| `risk_score_binding_sensitive` | A binding names a sensitive field. |
| `risk_score_personal_terms` | A raw personal field before the data terms were accepted. |
| `risk_score_personal_not_cleared` | A raw personal field in live. |
| `risk_score_model_unpinned` | The model isn't a versioned id. |
| `risk_score_unconfigured` | The deployment has no model provider for risk scores. |
| `risk_score_not_live` | Rules you publish read a risk score that isn't live yet. |

## Related

- [Analysis queries](https://provenanceauth.com/docs/account/analysis.md): read-only SQL over your own records.
- [Errors](https://provenanceauth.com/docs/platform/errors.md): every error code.
