Causal
Estimate treatment effects (ATE, ATT) and propensity scores from observational data with IPW, regression adjustment, and AIPW
- Connector software
- Free and open source
- Service credentials
- Not needed
- Query Farm hosting
- Available · $10/month organization minimum
Use with an agent
Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.
npx skills add Query-farm/vgi-skills --skill orchard
Copy this request into your coding agent to get started.
Use https://query.farm/products/orchard/connectors/causal/index.md to help me query Causal with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.
Estimate the effect of a treatment
Example output · captured
- Connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT method, round(estimate, 2) AS estimate FROM causal.main.ate((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y') ORDER BY method
| method | estimate |
|---|---|
| aipw | 5 |
| ipw | 4.68 |
| regression_adjustment | 5 |
Quick start
Choose where to run Causal, then follow the steps below.
The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.
1. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
2. Attach the connector
Attach with uvx
Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.
ATTACH 'causal' AS causal (
TYPE vgi,
LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-causal-worker'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'causal' AS causal (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-causal:0.5.0'
);
3. Run a query
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT method, round(estimate, 2) AS estimate FROM causal.main.ate((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y') ORDER BY method
Run the connector on a server you manage. You operate the service and choose who can connect.
1. Deploy the connector on your server
Follow the connector’s deployment instructions. Use its supported HTTP transport, or launch it remotely over SSH. The connection guide explains both options.
2. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach the connector
For an HTTP deployment, replace the example URL with your endpoint. Add the authentication options your deployment requires. For SSH, use the SSH connection instructions.
ATTACH 'causal' AS causal
(TYPE vgi, LOCATION 'https://your-connector.example.com');
4. Run a query
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT method, round(estimate, 2) AS estimate FROM causal.main.ate((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y') ORDER BY method
Query Farm operates Causal for your team. Hosting has a $10 monthly minimum per organization; usage counts toward it. From $2.97 per instance-month ($10 monthly minimum).
1. Create your hosted connector
Open the console, choose your organization and deployment size, and add any service credentials there.
2. Install the vgi extension
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach your deployment
Copy the ATTACH statement from your deployment in the console and run it in DuckDB or Haybarn. Use causal as the catalog alias for the query below.
4. Run a query
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT method, round(estimate, 2) AS estimate FROM causal.main.ate((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y') ORDER BY method
Technical details
- Verified connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
- Verified on
- December 1, 2026
Pin a version for production
When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.
Attach with uvx
Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.
ATTACH 'causal' AS causal (
TYPE vgi,
LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-causal-worker'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'causal' AS causal (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-causal:0.5.0'
);
Overview
Causal treatment-effect estimation (ATE, ATT, propensity scores) for SQL

Estimate the causal effect of an intervention directly in DuckDB SQL — average treatment effect (ATE), effect on the treated (ATT), and per-subject propensity scores — from observational cohort data, with confounders adjusted for automatically. This VGI extension turns causal questions like "did the treatment actually work, after controlling for who was likely to get it?" into a single SQL query.
Causal inference goes beyond correlation and naive group means: a simple difference between treated and untreated subjects is biased whenever the same factors that drive treatment also drive the outcome (confounding). The causal catalog corrects that bias using the standard toolkit of observational causal inference — inverse probability weighting (IPW), regression adjustment (the g-formula), and doubly-robust augmented IPW (AIPW) — so analysts, data scientists, and epidemiologists can run treatment-effect, impact, and intervention analysis where their data already lives, without exporting to a notebook.
Under the hood the estimators are powered by scikit-learn (source) for the propensity and outcome models and statsmodels (source) for the regression and inference machinery, with an optional doubly-robust backend via DoWhy (source). Propensities come from an L2-regularized logistic model with score clipping that keeps near-perfect separation from blowing up the weights, and standard errors come from influence functions (IPW/AIPW), OLS (regression adjustment), or a seeded bootstrap (ATT) — every estimate is deterministic and reproducible.
Estimands
Every estimator consumes a whole cohort as a (SELECT ...) relation plus named role arguments; every column not named by a role is treated as a covariate/confounder and adjusted for, and treatment must be binary 0/1.
- Average treatment effect
E[Y(1)-Y(0)], returned one row per method: IPW, regression adjustment, and doubly-robust AIPW, each with a standard error and 95% confidence interval. - Average treatment effect on the treated
E[Y(1)-Y(0)|T=1]via IPW-ATT weighting, with a bootstrap standard error. - Per-row propensity
e(X)=P(T=1|X)from the logistic model, keyed by youridcolumn.
Runnable, catalog-qualified demonstrations live in each object's example queries (and the schema's vgi.example_queries), so they are executed and coverage-checked rather than pasted inline here.
Estimates are causal only under the usual assumptions — unconfoundedness (no unmeasured confounders given the supplied covariates), overlap (0 < e(X) < 1), and SUTVA. Choosing the covariate set is your modeling decision; the worker estimates, it does not assume the assumptions hold.
Catalog
Everything causal exposes once attached — read straight from the worker's
released build.
main 1 view · 3 functions
Causal estimators: ate (IPW/RA/AIPW), att (IPW-ATT), and per-row propensity_scores
Schema examples (6)
Average treatment effect by IPW, regression adjustment, and doubly-robust AIPW.
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT method, round(estimate, 2) AS estimate FROM causal.main.ate((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y') ORDER BY method;
| method | estimate |
|---|---|
| aipw | 5 |
| ipw | 4.68 |
| regression_adjustment | 5 |
Average treatment effect on the treated (ATT).
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT round(estimate, 2) AS att FROM causal.main.att((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y');
The five subjects most likely to have been treated, by fitted propensity.
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT id, round(propensity, 3) AS propensity, treatment FROM causal.main.propensity_scores((SELECT id, t, x FROM cohort), treatment := 't', id := 'id') ORDER BY propensity DESC LIMIT 5;
Naive treated-minus-control difference of means next to the doubly-robust ATE and the ATT: confounding by x inflates the naive contrast well above both adjusted estimates.
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
, naive AS (SELECT avg(y) FILTER (WHERE t = 1) - avg(y) FILTER (WHERE t = 0) AS diff FROM cohort), dr AS (SELECT estimate FROM causal.main.ate((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y') WHERE method = 'aipw'), treated AS (SELECT estimate FROM causal.main.att((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y')) SELECT round(naive.diff, 2) AS naive_diff, round(dr.estimate, 2) AS ate_aipw, round(treated.estimate, 2) AS att FROM naive CROSS JOIN dr CROSS JOIN treated;
2 more examples
Overlap trimming: keep only subjects whose fitted propensity lies in [0.2, 0.8] (44 of the 61), then re-estimate the ATE by every method on that trimmed cohort.
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
, ps AS (SELECT id, propensity FROM causal.main.propensity_scores((SELECT id, t, x FROM cohort), treatment := 't', id := 'id')) SELECT method, round(estimate, 2) AS estimate, round(std_error, 3) AS std_error FROM causal.main.ate((SELECT c.t, c.y, c.x FROM cohort AS c JOIN ps USING (id) WHERE ps.propensity BETWEEN 0.2 AND 0.8), treatment := 't', outcome := 'y') ORDER BY method;
Each ATE estimator's estimate and 95% CI next to its doubly-robust flag and one-line summary from the methods reference view (doubly-robust AIPW first).
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT e.method, m.doubly_robust, round(e.estimate, 2) AS estimate, round(e.ci_lower, 2) AS ci_lower, round(e.ci_upper, 2) AS ci_upper, m.description FROM causal.main.ate((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y') AS e JOIN causal.main.methods AS m ON m.method = e.method AND m.function_name = 'ate' ORDER BY m.doubly_robust DESC, e.method;
methods
Reference registry of the estimator methods each causal function emits
Which estimators does the ate function emit, and which are doubly-robust?
SELECT method, doubly_robust
FROM causal.main.methods
WHERE function_name = 'ate'
ORDER BY method;
List every estimator with its estimand and the function that produces it.
SELECT function_name, method, estimand
FROM causal.main.methods
ORDER BY function_name, method;
ate (data TABLE, outcome := VARCHAR, random_state := BIGINT, treatment := VARCHAR)
Average Treatment Effect E[Y(1)-Y(0)] adjusting for every covariate column. Emits one row per method: ipw, regression_adjustment, aipw (doubly-robust). treatment must be binary 0/1; causal only under unconfoundedness.
Average treatment effect by IPW, regression adjustment, and AIPW.
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT method, round(estimate, 2) AS estimate FROM causal.main.ate((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y') ORDER BY method;
| method | estimate |
|---|---|
| aipw | 5 |
| ipw | 4.68 |
| regression_adjustment | 5 |
Doubly-robust AIPW estimate with its 95% confidence interval.
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT round(estimate, 2) AS estimate, round(ci_lower, 2) AS ci_lower, round(ci_upper, 2) AS ci_upper FROM causal.main.ate((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y') WHERE method = 'aipw';
Each ATE estimator's estimate and 95% CI next to its doubly-robust flag and one-line summary from the methods reference view (doubly-robust AIPW first).
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT e.method, m.doubly_robust, round(e.estimate, 2) AS estimate, round(e.ci_lower, 2) AS ci_lower, round(e.ci_upper, 2) AS ci_upper, m.description FROM causal.main.ate((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y') AS e JOIN causal.main.methods AS m ON m.method = e.method AND m.function_name = 'ate' ORDER BY m.doubly_robust DESC, e.method;
att (data TABLE, outcome := VARCHAR, random_state := BIGINT, treatment := VARCHAR)
Average Treatment effect on the Treated E[Y(1)-Y(0)|T=1] via IPW-ATT weighting, adjusting for every covariate column. Emits one row (estimate, std_error). treatment must be binary 0/1.
Average treatment effect on the treated (IPW-ATT), with its bootstrap standard error.
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT round(estimate, 2) AS estimate, round(std_error, 3) AS std_error FROM causal.main.att((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y');
Naive treated-minus-control difference of means next to the doubly-robust ATE and the ATT: confounding by x inflates the naive contrast well above both adjusted estimates.
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
, naive AS (SELECT avg(y) FILTER (WHERE t = 1) - avg(y) FILTER (WHERE t = 0) AS diff FROM cohort), dr AS (SELECT estimate FROM causal.main.ate((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y') WHERE method = 'aipw'), treated AS (SELECT estimate FROM causal.main.att((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y')) SELECT round(naive.diff, 2) AS naive_diff, round(dr.estimate, 2) AS ate_aipw, round(treated.estimate, 2) AS att FROM naive CROSS JOIN dr CROSS JOIN treated;
propensity_scores (data TABLE, id := VARCHAR, random_state := BIGINT, treatment := VARCHAR)
Fit a logistic propensity model and emit per-row scores: (id, propensity, treatment). The id column is passed through and excluded from the covariates; every other column is a covariate. treatment must be binary 0/1.
The five subjects most likely to have been treated, by fitted propensity.
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT id, round(propensity, 3) AS propensity, treatment FROM causal.main.propensity_scores((SELECT id, t, x FROM cohort), treatment := 't', id := 'id') ORDER BY propensity DESC LIMIT 5;
Count subjects whose fitted propensity exceeds one half (an overlap check).
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT count(*) AS n_high_propensity FROM causal.main.propensity_scores((SELECT id, t, x FROM cohort), treatment := 't', id := 'id') WHERE propensity > 0.5;
Overlap trimming: keep only subjects whose fitted propensity lies in [0.2, 0.8] (44 of the 61), then re-estimate the ATE by every method on that trimmed cohort.
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
, ps AS (SELECT id, propensity FROM causal.main.propensity_scores((SELECT id, t, x FROM cohort), treatment := 't', id := 'id')) SELECT method, round(estimate, 2) AS estimate, round(std_error, 3) AS std_error FROM causal.main.ate((SELECT c.t, c.y, c.x FROM cohort AS c JOIN ps USING (id) WHERE ps.propensity BETWEEN 0.2 AND 0.8), treatment := 't', outcome := 'y') ORDER BY method;
Try these queries
Average treatment effect on the treated (ATT).
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT round(estimate, 2) AS att FROM causal.main.att((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y')
Uses att
The five subjects most likely to have been treated, by fitted propensity.
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT id, round(propensity, 3) AS propensity, treatment FROM causal.main.propensity_scores((SELECT id, t, x FROM cohort), treatment := 't', id := 'id') ORDER BY propensity DESC LIMIT 5
Uses propensity_scores
Naive treated-minus-control difference of means next to the doubly-robust ATE and the ATT: confounding by x inflates the naive contrast well above both adjusted estimates.
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
, naive AS (SELECT avg(y) FILTER (WHERE t = 1) - avg(y) FILTER (WHERE t = 0) AS diff FROM cohort), dr AS (SELECT estimate FROM causal.main.ate((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y') WHERE method = 'aipw'), treated AS (SELECT estimate FROM causal.main.att((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y')) SELECT round(naive.diff, 2) AS naive_diff, round(dr.estimate, 2) AS ate_aipw, round(treated.estimate, 2) AS att FROM naive CROSS JOIN dr CROSS JOIN treated
Overlap trimming: keep only subjects whose fitted propensity lies in [0.2, 0.8] (44 of the 61), then re-estimate the ATE by every method on that trimmed cohort.
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
, ps AS (SELECT id, propensity FROM causal.main.propensity_scores((SELECT id, t, x FROM cohort), treatment := 't', id := 'id')) SELECT method, round(estimate, 2) AS estimate, round(std_error, 3) AS std_error FROM causal.main.ate((SELECT c.t, c.y, c.x FROM cohort AS c JOIN ps USING (id) WHERE ps.propensity BETWEEN 0.2 AND 0.8), treatment := 't', outcome := 'y') ORDER BY method
Uses propensity_scoresate
Each ATE estimator's estimate and 95% CI next to its doubly-robust flag and one-line summary from the methods reference view (doubly-robust AIPW first).
WITH cohort AS (
SELECT
g AS id,
(g - 30)::DOUBLE / 10.0 AS x,
CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END AS t,
5.0 * (CASE WHEN ((g * 7 + 3) % 10) < 5 + 3 * sign(g - 30) THEN 1 ELSE 0 END)
+ 2.0 * ((g - 30)::DOUBLE / 10.0) AS y
FROM generate_series(0, 60) AS s(g)
)
SELECT e.method, m.doubly_robust, round(e.estimate, 2) AS estimate, round(e.ci_lower, 2) AS ci_lower, round(e.ci_upper, 2) AS ci_upper, m.description FROM causal.main.ate((SELECT t, y, x FROM cohort), treatment := 't', outcome := 'y') AS e JOIN causal.main.methods AS m ON m.method = e.method AND m.function_name = 'ate' ORDER BY m.doubly_robust DESC, e.method
Which estimators does the ate function emit, and which are doubly-robust?
SELECT method, doubly_robust
FROM causal.main.methods
WHERE function_name = 'ate'
ORDER BY method
Uses methods
4 more examples in the Catalog, each listed under the table or function it uses.
Causal is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.
Related workers
Anomaly
Statistics & ML
Find outliers, discords, repeated motifs and change points in numeric series from SQL
Explain
Statistics & ML
Explain scikit-learn and XGBoost model predictions with per-row SHAP values and global feature importance
LightGBM
Statistics & ML
Train, tune, evaluate, and explain gradient-boosted classifiers and regressors, with a saved-model registry