> For the complete documentation index, see [llms.txt](https://docs.rootcause.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rootcause.ai/more-details/applications/application-modules/counterfactualsim.md).

# Counterfactual

> Find what inputs would produce a desired outcome.

Asks a digital twin what would have happened: the same rows, replayed with one thing different.

Where an intervention reasons forward from a lever, a counterfactual reasons backward from rows that already happened — which is why `baseline` is required here and optional on InterventionSim.

## At a glance

|                          |                        |
| ------------------------ | ---------------------- |
| Module type              | `CounterfactualSim`    |
| Category                 | Intelligence           |
| Default tier             | `reactive`             |
| Runs on                  | `reactive`, `durable`  |
| Starts a flow            | no                     |
| Holds a body             | no                     |
| Keeps state between runs | no                     |
| Available in             | every application type |

## Parameters

### `digitalTwinId`

`string` · required · defaults to `""`

The twin to run against.

### `digitalTwinName`

`string` · optional

Its name when it was chosen, for the node's label.

### `digitalTwinVersionIds`

`string[]` · optional

Pin the run to specific twin versions; empty means the latest.

### `scenario`

`Record<string, unknown>` · optional

The saved scenario. Anything arriving on the `scenario` port is merged over this.

### `scenarioDraft`

`Record<string, unknown>` · optional

What the inspector is editing, before it is applied to `scenario`.

### `scenarioValidationErrors`

`string[]` · optional

What was wrong with the draft the last time it was checked.

### `entityColumns`

`object` · optional · defaults to `{"id":null,"target":null,"constraints":null,"levers":null}`

Which columns of the `entities` frame carry what: `id` the environment id, and `target`, `constraints` and `levers` the optional per-row overrides. Any may be null.

## Inputs

| Port       | Kind        | Required | Description                                                                                                                           |
| ---------- | ----------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `on`       | `event`     | no       | Implicit on every module that does not itself start a flow: wire an event here to make this module the start of what that event runs. |
| `scenario` | `json`      | no       | Per-run overrides, merged over the saved scenario.                                                                                    |
| `entities` | `dataframe` | no       | One row per environment to run for; `entityColumns` maps its columns.                                                                 |
| `baseline` | `dataframe` | yes      | The rows to replay. Required: there is no counterfactual without a factual.                                                           |

## Outputs

| Port      | Kind        | Connects to                | Description                                                                                                                                                                     |
| --------- | ----------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `results` | `dataframe` | `dataframe`, `signal`      | The **best** counterfactual of each sample only, one row per changed variable: `sample`, `variable`, `original`, `counterfactual`, `target_met`. There is no difference column. |
| `summary` | `json`      | `json`, `scalar`, `signal` | The run's identity, not its findings: `{runId, status, outputs}`, where `outputs` is the simulator's raw per-version result. The numbers a reader wants are in `results`.       |

***

[← All application modules](/more-details/applications/application-modules.md)
