Spatial Double Machine Learning on ERGIS wells

Last updated: 2026-11-30 (repeated cross-fitting, model selection and map cross-validation)

1. Overview

This project estimates one causal effect from oil well data. The question is: how much does the production of a new well change for each parent?

A study well is a well whose production the project examines. A parent is an older well near a study well. A play is a geological area that operators develop with the same method.

Operators drill the best rock most densely. Thus a simple comparison shows that wells with many parents produce more. This is confounding: rock quality drives the number of parents and the production.

The project uses Double Machine Learning (DML) to remove the confounding. DML uses two prediction models. The first model predicts the production from the confounders. The second model predicts the number of parents from the same confounders. A regression then relates the two sets of prediction errors. The file DML.md gives the method. The file GEOSPATIAL_DML_APPLICATIONS.md gives the study design.

The project has two parts:

A second study uses the same method on public US wind turbine data. It has its own documentation: Spatial DML and GWR on US wind turbines.

2. Architecture

ERGIS.ddbtable Wells load()one play build()Y, D, X, blocks plr()effect per parent irm()child vs no parent local_effects()write_map() web mapGeoJSON console table Y = log production, D = number of parents, X = confounders
Figure 1. Data flow in dml.py.

2.1 Steps

  1. load() opens the DuckDB database read-only and gets all wells of one play. It keeps rows with entity_type = 'WELL', a geometry and a completion date from 1950. It keeps one row for each well_api. It sorts the rows by objectid.
  2. build() selects the study wells: wells completed in the year range with an outcome larger than zero. It counts the parents of each study well and makes the confounders.
  3. plr() calculates the effect of one more parent (partially linear model).
  4. irm() calculates the effect of one or more parents against no parent (interactive model, AIPW estimator).
  5. local_effects() and write_map() calculate the effect in each map cell and write a GeoJSON file.

2.2 Variables

VariableDefinition
Outcome YNatural logarithm of the outcome column. The default column is max_oil_production_volume (peak-month oil).
Treatment DNumber of parents. A parent is a well of the same play within the radius (default 800 m) that was completed 180 days or more before the study well.
Binary treatment1 if D is larger than 0. Used by irm().
Confounders XPosition (x, y in km), total depth, production top depth, production base depth, operator code, production zone code, completion year, and two rock-quality values.
Rock-quality valuesMean log outcome, and number, of wells within 5 km that were completed 730 days or more before the study well.
BlockA 10 km grid square. All wells in one block go to the same fold. Standard errors are clustered on blocks.
Map cellA grid square for the map (default 20 km). A map cell with fewer than 30 study wells is not shown.

2.3 Estimation method

3. Requirements

ItemRequirementVersion used for the results
Operating systemmacOS (the only system tested)Darwin 27.2.0, Apple silicon
uvInstalled and on the path0.13.10
Python3.12 or 3.13 (pyproject.toml)3.13.7
duckdbPython package, with the spatial extension2.0.1
numpyPython package2.5.5
scikit-learnPython package1.9.1
scipy, threadpoolctlPython packages (critical t, one thread for the models)1.18.1, 3.8.0
DataThe DuckDB file ERGIS.ddb with the table Wells4,226,137 rows
Web mapA browser with WebGL and internet access to js.arcgis.comArcGIS Maps SDK for JavaScript 5.2 (CDN gave 5.2.9)

4. Installation

  1. Open a terminal.
  2. Go to the project folder.
    cd double-machine-learning
  3. Install the packages.
    uv sync
  4. Run the self-test.
    uv run dml.py --selftest
    The last line shows selftest ok.

The first run with a new DuckDB version downloads the spatial extension. This needs internet access.

5. Configuration

5.1 Command-line options

OptionTypeDefaultEffect
--ddbpath~/Documents/ArcGIS/Projects/ERGIS/ERGIS.ddbThe DuckDB file to read.
--playtextWOLFCAMP DELAWAREThe value of play_name to study. Use upper case.
--outcomechoicemax_oil_production_volumeThe outcome column. The second choice is first_month_oil_volume.
--radius-mnumber, metres800The largest distance between a study well and a parent.
--ringsnumbers, metresnoneAlso calculate one effect for each distance ring. Give the outer edges in increasing order, for example 200 400 800 1600. The script prints the ring results after the usual results.
--yearstwo integers2012 2023First and last completion year of the study wells.
--mappathnoneIf set, the script writes the map data to this GeoJSON file.
--map-kmnumber, km20The size of a map cell.
--selftestflagoffRuns the self-test on synthetic data. The script does not read the database.

5.2 Constants in dml.py

NameValueEffect
LAG_DAYS180 daysMinimum age difference between a parent and a study well.
QUALITY_KM5 kmSearch distance for the rock-quality values.
QUALITY_LAG_DAYS730 daysMinimum age difference for wells in the rock-quality values.
BLOCK_KM10 kmBlock size for the folds and the clustered standard errors.
CVGroupKFold(n_splits=5)Number of folds.
HGBdict(random_state=0)Settings for the gradient boosting models.
SPLINEpipelineThe second prediction model: missing values filled, standardized, 8-knot splines for each column, ridge regression.
REPEATS10Number of random fold assignments in plr().
MAP_MIN_WELLS30 wellsMinimum number of study wells in a map cell.
MAP_BANDWIDTHS_KM5, 10, 20, 40 km, globalBandwidths that the map cross-validation compares.
MAP_CV_WELLS2,000 wellsSize of the random well sample for the bandwidth cross-validation and the ENP.

6. Usage

6.1 Calculate the estimates

  1. Run the script with the default settings.
    uv run dml.py
  2. Examine the output. The output shows these lines:
    WOLFCAMP DELAWARE: max_oil_production_volume, parents within 800 m, completed 2012-2023
    wells 7,882  blocks 215  mean parents 1.82  share with a parent 0.56
    effect on log(outcome):
    naive OLS, per parent              +0.0213  se 0.0071  95% CI [+0.0073, +0.0352]
    DML PLR, per parent                -0.0231  se 0.0049  95% CI [-0.0327, -0.0134]  out-of-fold R2: Y 0.63, D 0.22
    naive difference, child vs parent  +0.1755  se 0.0296  95% CI [+0.1176, +0.2335]
    DML IRM (ATE), child vs parent     -0.0393  se 0.0201  95% CI [-0.0787, +0.0001]  propensities clipped: 0.0%

One run for Wolfcamp Delaware took 31 seconds on the test computer (one measurement). The script uses one processor thread for the prediction models: on this data that is faster than many threads, and repeated runs give the same numbers.

6.2 Read the output

LineMeaning
naive OLS, per parentSlope of log outcome on the number of parents, with no confounders. For comparison only.
DML PLR, per parentThe DML estimate of the effect of one more parent on log outcome.
naive difference, child vs parentDifference in mean log outcome between wells with a parent and wells with no parent. For comparison only.
DML IRM (ATE), child vs parentThe DML estimate of the mean effect of one or more parents.
out-of-fold R2Share of the variation that each prediction model explains on wells it did not see.
propensities clippedShare of wells whose predicted treatment probability was outside 0.02 to 0.98.
map bandwidth by block CVOnly with --map. The selected bandwidth, the cross-validation error of each candidate, the ENP and the critical t for the map cells.

An effect t on log outcome is a change of 100 × (exp(t) − 1) percent. Example: −0.0231 is −2.28%.

6.3 Other runs

uv run dml.py --play "BAKKEN SHALE"
uv run dml.py --radius-m 400
uv run dml.py --rings 200 400 800 1600
uv run dml.py --outcome first_month_oil_volume --years 2015 2023

6.4 Make the map data

  1. Write the map data for Wolfcamp Delaware.
    uv run dml.py --rings 200 400 --map web/data/wolfcamp-delaware.geojson
    The line wrote web/data/wolfcamp-delaware.geojson: 31 cells of 20 km, 2 distance bands shows.
  2. Write the map data for Bakken Shale.
    uv run dml.py --play "BAKKEN SHALE" --rings 200 400 --map web/data/bakken-shale.geojson
    The line wrote web/data/bakken-shale.geojson: 64 cells of 20 km, 2 distance bands shows.

With --rings, the map data also holds one effect for each distance ring. Without --rings, the web map shows only the single-radius estimate.

6.5 Open the web map

  1. Start a local web server.
    python3 -m http.server -d web 8000
  2. Open http://localhost:8000 in a browser.
  3. Select a play in the Play list. The map moves to the play.
  4. Select a distance in the Older wells counted list. All within 800 m shows the single-radius estimate. Only 0–200 m away and Only 200–400 m away show the ring estimates.
  5. Click a map cell. A popup shows the effect, the 95% interval and the number of wells.
  6. To stop the web server, press Ctrl+C in the terminal.

Red map cells show a production decrease for each parent. Blue map cells show an increase. Solid map cells differ from the overall effect after the correction for many cells. Faint map cells do not. For both plays, the block cross-validation selects one global slope (section 7.4). Thus all map cells of a play show the same value, and the panel states this.

Web map of the Wolfcamp Delaware play with 31 light red 20 km cells of one color near Carlsbad and Pecos, a legend at the bottom left and a summary panel at the top right that shows -2.28% per older well
Figure 2. Web map, Wolfcamp Delaware. The panel shows the overall estimate. All cells have the same value because the cross-validation selected one global slope.
Web map of the Bakken Shale play with 64 pale red 20 km cells of one color in western North Dakota and eastern Montana, and a summary panel that shows -0.98% per older well
Figure 3. Web map, Bakken Shale.
Web map of the Bakken Shale play with the distance list set to Only 0-200 m away. The cells have one stronger red color than in Figure 3, and the panel shows -4.04% per older well
Figure 4. Web map, Bakken Shale, parents 0–200 m away.

6.6 Add a play to the web map

  1. Write the map data for the play to a new file in web/data/.
  2. Open web/index.html in an editor.
  3. Add one <option> line to the <select id="play"> element. Set its value to the path of the new file.

7. Results

All results use the outcome max_oil_production_volume and study wells completed from 2012 to 2023. The effects are on log outcome. Each DML PLR value is the median of 10 cross-fitting splits. Measured on 2026-11-30.

7.1 Effect of one more parent

PlayRadius (m)Study wellsBlocksMean parentsNaive OLSDML PLRStandard error95% intervalPercent change
Wolfcamp Delaware8007,8822151.82+0.0213−0.02310.0049−0.0327 to −0.0134−2.28%
Wolfcamp Delaware4007,8822150.77+0.0281−0.04380.0076−0.0587 to −0.0289−4.29%
Bakken Shale80014,7783632.90+0.0619−0.00990.0028−0.0154 to −0.0043−0.98%

The naive estimate is positive in each run. The DML estimate is negative in each run. In Wolfcamp Delaware the effect is larger at 400 m than at 800 m. Before 2026-11-30 the script used one split and only gradient boosting: Wolfcamp Delaware −0.0205 (0.0041) at 800 m and −0.0427 (0.0053) at 400 m, Bakken Shale −0.0082 (0.0025). The new estimates are 0.1 to 0.6 standard errors more negative.

7.2 Effect of one or more parents against no parent

PlayRadius (m)Share with a parentNaive differenceDML IRMStandard error95% intervalPropensities clipped
Wolfcamp Delaware8000.56+0.1755−0.03930.0201−0.0787 to +0.00010.0%
Wolfcamp Delaware4000.35+0.0792−0.05520.0186−0.0917 to −0.01870.0%
Bakken Shale8000.77+0.4025−0.04120.0237−0.0877 to +0.00530.1%

All estimates are negative. At 400 m in Wolfcamp Delaware the 95% interval excludes zero; at 800 m the intervals include zero or end at it. At 200 m the interval excludes zero in both plays (Wolfcamp Delaware −0.1332, standard error 0.0258; Bakken Shale −0.0448, 0.0149), and at 400 m in Bakken Shale (−0.0334, 0.0122). Since 2026-11-30 irm() uses the same 10 splits and model choice as plr(), one outcome model family for both groups, and outcome predictions limited to the observed range. The standard errors are half of the earlier one-split values, and almost no propensities are clipped (before: 0.4%, 4.1%, 6.7%).

7.3 Quality of the prediction models

PlayRadius (m)Out-of-fold R² for YOut-of-fold R² for D
Wolfcamp Delaware8000.630.22
Wolfcamp Delaware4000.630.13
Bakken Shale8000.550.40

The R² values use the predictions averaged over the 10 splits. In a comparison with one split, gradient boosting predicted Y better in both plays, and the splines predicted D better in Wolfcamp Delaware (R² 0.17 against 0.09). The script selects the model for each prediction and split.

7.4 Map cells

Play5 km10 km20 km40 kmGlobalSelectedMap cellsCells that differ
Wolfcamp Delaware0.24610.23630.23300.23150.2310global310
Bakken Shale0.22220.21890.21730.21680.2166global640

The table shows the block cross-validation error for each bandwidth. The error falls as the bandwidth increases, and the global fit has the smallest error in both plays. Thus the data do not show a local difference in the effect. All map cells of a play have the same value: −2.38% in Wolfcamp Delaware and −1.00% in Bakken Shale. This value differs slightly from the overall effect in the panel because the map uses the residuals averaged over the 10 splits, and the overall effect is the median of the 10 split estimates.

Before 2026-11-30 the map used a 10 km bandwidth selected by inspection, standard errors without the HC3 correction and the limit 1.96. That map marked 4 of 31 cells in Wolfcamp Delaware and 12 of 64 in Bakken Shale. On the wind data, this combination marked 12% of plants in a shuffled-location placebo. The wind study (wind documentation) tested the new rules first.

Caution: the global bandwidth means that the map cannot show local differences in these plays. A local map, with a bandwidth that the data do not support, would show differences that are mostly chance.

7.5 Operator check

The operator column gives the current owner, not the company that drilled the well. The check removed the operator from the confounders, with the current estimator, at 800 m.

PlayDML PLR with operatorDML PLR without operatorStandard error
Wolfcamp Delaware−0.0231−0.02380.0049
Bakken Shale−0.0099−0.00960.0028 to 0.0029

The change is less than a fifth of a standard error. The operator stays in the confounders. The first check, on 2026-11-29 with the earlier estimator, also moved the estimates by less than half a standard error.

7.6 Self-test

The self-test uses 6,000 synthetic rows with a known effect and strong confounding.

EstimatorTrue effectNaive estimateDML estimateStandard error
plr()0.51.8980.4940.013
plr(), two treatments−0.3, 0.20.915, 0.780−0.284, 0.1970.013, 0.010
irm()1.03.9061.0840.043

Before 2026-11-30, plr() gave 0.596 (standard error 0.023): 4 standard errors from the true effect. A study with 30 random data sets of the self-test design measured how often the 95% interval contains the true effect:

DesignOne split, gradient boosting10 splits, gradient boosting10 splits, model selected (now)
Strong confounding (the self-test), 6,000 rows0% (bias 3.6 standard errors)0% (bias 3.5 standard errors)97% (bias −0.07 standard errors)
Moderate confounding, 2,000 rows97%97%97% (bias 0.03 standard errors)

The repeated splits alone did not correct the intervals: the cause was bias in the prediction models. The spline model learns the smooth confounders of the self-test and removes the bias. The splines continue as straight lines beyond the training range: with flat tails, irm() gave 1.132 (3.1 standard errors from the truth) and the strong-confounding coverage of plr() was 93%. The self-test still asserts that DML removes 90% of the naive bias, and that irm() is within 3 standard errors of the truth.

The self-test also examines build() with five hand-made wells, and the map: a sharp step in the effect selects a local bandwidth and all cells differ; a constant effect marks no cell.

7.7 Distance

The first table repeats the usual run with different values of --radius-m. Each value is a separate run.

RadiusWolfcamp Delaware, DML per parentWolfcamp Delaware, mean parentsBakken Shale, DML per parentBakken Shale, mean parents
200 m−0.0794 (se 0.0142)0.29−0.0488 (se 0.0119)0.35
400 m−0.0438 (se 0.0076)0.77−0.0234 (se 0.0055)1.17
800 m−0.0231 (se 0.0049)1.82−0.0099 (se 0.0028)2.90
1,600 m−0.0102 (se 0.0032)4.55−0.0042 (se 0.0016)6.57

A larger radius includes parents that are farther away. Thus each value is an average over all distances inside the radius.

The second table comes from one run with --rings 200 400 800 1600. The script counts the parents in each ring. One model then calculates all four effects together.

RingWolfcamp Delaware, naiveWolfcamp Delaware, DML per parentBakken Shale, naiveBakken Shale, DML per parent
0–200 m−0.0211−0.0583 (se 0.0148), −5.7%+0.0071−0.0408 (se 0.0119), −4.0%
200–400 m+0.0209−0.0254 (se 0.0103), −2.5%+0.0194−0.0142 (se 0.0053), −1.4%
400–800 m+0.0125−0.0084 (se 0.0073)+0.0379−0.0003 (se 0.0036)
800–1,600 m+0.0192−0.0018 (se 0.0041)+0.0440−0.0004 (se 0.0021)

In the two plays, only parents within 400 m reduce production. Parents farther than 400 m have no measurable effect. The 800 m result in section 7.1 is thus an average of a loss near the well and no effect farther away.

Caution: the treatment models explain only 9% (Wolfcamp Delaware) and 16% (Bakken Shale) of the parent count in the 0–200 m ring.

The web map uses a run with --rings 200 400. That run does not count parents farther than 400 m. Its overall ring effects are: Wolfcamp Delaware −0.0628 and −0.0310, Bakken Shale −0.0412 and −0.0149.

7.8 Sensitivity to the folds

A correction of the year filter once added 2 Bakken Shale wells (14,776 to 14,778). The 2 wells changed the assignment of the 10 km blocks to the folds, and with one split each Bakken Shale estimate moved by up to half a standard error (for example, the 0–200 m ring from −0.0412 to −0.0354).

Since 2026-11-30, plr() repeats the cross-fitting with 10 random fold assignments and reports the median. The standard error includes the spread between the splits. Thus this uncertainty is now part of the 95% interval.

7.9 Wells with the most parents

In the wind study, the pooled estimate once depended on the 1% of plants with the most older capacity. This check removes the wells with a parent count at or above the 99th percentile, at 800 m, with the current estimator.

PlayWells removedParent count removedDML PLR, all wellsDML PLR, without them
Wolfcamp Delaware10012 to 21−0.0231 (se 0.0049)−0.0213 (se 0.0049)
Bakken Shale22713 to 23−0.0099 (se 0.0028)−0.0126 (se 0.0028)

The estimates move by less than one standard error, in both directions. The oil results do not depend on the wells with the most parents.

8. Troubleshooting

SymptomCauseFix
no wells for play '…'The play name is not in play_name.Use the exact upper-case name, for example "BAKKEN SHALE".
no producing wells completed 2030-2031No well of the play has an outcome larger than zero in the year range.Change --years.
child vs parent skipped: fewer than 5% of wells are in one groupAlmost all wells have a parent, or almost none. This occurs with a very large or very small radius.Change --radius-m. The other estimates stay valid.
argument --outcome: invalid choiceThe outcome column is not permitted. Cumulative volumes are not permitted.Use max_oil_production_volume or first_month_oil_volume.
DuckDB cannot open the fileThe path is wrong, or a different program holds a write lock.Set --ddb. Close the program that writes to the file.
Error at INSTALL spatialThe spatial extension is not installed and there is no internet access.Connect to the internet for the first run.
The web page shows Could not load the dataThe GeoJSON file is not in web/data/, or the web server does not serve the web folder.Make the map data (section 6.4). Start the web server with -d web.
The web page shows no mapThe page was opened as a file, or there is no access to js.arcgis.com.Use the web server (section 6.5). Examine the internet connection.

9. Limitations and next steps

9.1 Limitations

9.2 Next steps

10. Reference

10.1 Files

PathContents
dml.pyThe script: data load, estimators, map data, self-test.
pyproject.toml, uv.lockPython version range, packages and locked versions.
web/index.htmlThe web map (one page, plain JavaScript, ArcGIS Maps SDK for JavaScript 5.2 from the CDN).
web/data/*.geojsonMap data, one file for each play.
DML.mdThe DML method and the adjustments for spatial data.
GEOSPATIAL_DML_APPLICATIONS.mdLiterature, the three candidate datasets and the study design.
LITERATURE.mdPublished parent–child well studies compared with the results.
tasks/todo.mdChecklist of the audit fixes.
docs/index.htmlThis page.
docs/wind.html, docs/wind-app/The wind study documentation and its web app.

10.2 Functions in dml.py

FunctionInputOutput
load(ddb, play, outcome)Database path, play name, outcome columnDictionary of arrays: position, completion day, outcome, depths, codes
build(w, radius_km, year_from, year_to)The dictionary from load(). radius_km can be a list of ring edges.Y, D, X, block numbers, positions in km. With ring edges, D has one column for each ring.
slope(u, v, groups)Two arrays and cluster numbers. v can have several columns.Slope and clustered standard error, one for each column of v
nuisance(X, t, groups, cv)Confounders, a target, blocks, foldsOut-of-fold predictions from the model with the higher out-of-fold R²
plr(y, d, X, groups, repeats=10)Y, D (one or more columns), X, block numbersMedian effect over the splits, standard error, two R² values, two residual arrays. One effect for each column of D.
run(y, d, X, groups, lab=LABELS)Output of build(). lab sets the printed labels.Prints the naive, PLR and IRM effects. Returns the PLR effect, standard error and residuals.
run_rings(y, D, X, groups, edges, lab=LABELS, unit="m")Output of build() with ring edges. unit is the unit of the edges.Prints the naive and DML effect for each ring
oof(model, X, target, splits, rows, proba)A model, data, folds, optional training rowsOut-of-fold predictions for every row
irm(y, t, X, groups, clip=0.02, repeats=10)Y, binary treatment, X, block numbersMedian effect over the splits, standard error, median share of clipped propensities
wls(u, V, w)Residuals, treatment residuals, weightsWeighted slopes, HC3 standard errors, all coefficients
map_bandwidth(u, v, xy, groups)Residuals, positions, blocksSelected bandwidth, cross-validation error for each candidate
map_tcrit(v, xy, h)Treatment residuals, positions, bandwidthENP, critical t
local_effects(u, v, xy, cell_km, h)Residual arrays, positions, cell size, bandwidthList of cell x, cell y, effect, standard error, number of wells in the cell
write_map(path, effects, cell_km, kx, meta, th0, se0, t_crit, bands)Output of local_effects(), the overall effect, the critical tA GeoJSON file
selftest()NonePrints the results. Stops with an error if a check fails.

10.3 Map data format

The file is a GeoJSON FeatureCollection in longitude and latitude. Each feature is one square map cell.

PropertyTypeMeaning
effect_pctnumber, percentChange in the outcome for each parent in this map cell.
lo_pct, hi_pctnumber, percentLimits of the 95% interval.
wellsintegerNumber of study wells in the map cell.
differs0 or 11 if the effect differs from the overall effect by more than t standard errors of the map cell, with t from meta.t_crit.
effect_pct_r0_200, lo_pct_r0_200, hi_pct_r0_200, differs_r0_200as aboveThe same values for the 0–200 m ring. Each ring has its own suffix, for example _r200_400. Only present with --rings.

The top-level member meta holds the run settings and the overall estimate:

{"play": "WOLFCAMP DELAWARE", "outcome": "max_oil_production_volume", "radius_m": 800,
 "years": [2012, 2023], "wells": 7882, "effect_pct": -2.03, "lo_pct": -2.81, "hi_pct": -1.24,
 "bands": [{"key": "r0_200", "label": "0–200 m", "effect_pct": -4.56, "lo_pct": -6.61, "hi_pct": -2.47},
           {"key": "r200_400", "label": "200–400 m", "effect_pct": -3.8, "lo_pct": -5.39, "hi_pct": -2.19}]}

The web page makes one entry in the Older wells counted list for each item in bands.

10.4 Sources