Workshop: Estimating Distributions from Geoscience Data

Chapter 2, Statistical Foundations for ML

Learning objectives

  • Load the learning set's file in Python, select the main sand of the nine wells, and compute its moments by hand
  • Estimate the main sand's mean porosity and its standard error by the formula s/ns/\sqrt{n}
  • Bootstrap the mean and the median with the book's own seeded generator, and read their standard errors and percentile intervals
  • Resample whole wells, and explain why the honest unit across a field is the well

How Far an Estimate Could Move

An estimate from data would come out a little different if the data were drawn again. The standard error says by how much, and the bootstrap measures it without a formula: it draws the data again from themselves, with replacement, many times, and reads how far the estimate moves. Its answer is only as honest as what it treats as the independent draw. The main sand's mean porosity over the nine wells, 0.2581 v/v from 2952 samples, has a standard error of 0.000161 v/v when the samples are resampled, as the formula s/ns/\sqrt{n} says, 0.000159 v/v; resampling the nine wells instead gives 0.00217 v/v, 13.5 times as large, because the wells differ more than a well's samples do.

Bootstrap The Main SandInteractive figure, enable JavaScript to interact.

The Data

The workshop reads the book's own learning set as a CSV file, mlgeo-core/data/ogbon-learning-set.csv on this site: Ogbon-1, the Petrophysics book's well, and Ogbon-2 to Ogbon-9, the book's own hypothetical appraisal wells, 1201 samples each every 0.5 ft. It takes the main sand, the deepest reservoir bed, 328 samples in each well and 2952 in all, and its porosity, the generator's truth. Every well carries the same bed, but the book's lateral model gives each well's main sand its own porosity, from 0.2520 v/v in Ogbon-7 to 0.2726 v/v in Ogbon-9. The whole set is synthetic, and that is why every number below can be checked.

Each step below gives its code and then what the book's kernel gives for the same computation on the same file. The file prints porosity to four decimals, so a number from it can differ from the figure's in its last digit.

Step 1: Load and Select

The Main Sand of Nine Wells

Read the file once, as every workshop does, and keep the rows of one zone.

import numpy as np
import pandas as pd

URL = "https://ogbonlab.com/mlgeo-core/data/ogbon-learning-set.csv"
df = pd.read_csv(URL, comment="#")
ms = df[df["zone"] == "main sand"]
phi = ms["PHI_vv"].to_numpy()
n = len(phi)
print(n, ms["well"].nunique())

The book's kernel gives 2952 samples in 9 wells.

Step 2: The Shape First

Moments by Hand

Before an estimate, look at the shape, as Section 2.1 does. In this file K_md is above 0 exactly in the net reservoir, so it selects Ogbon-1's net reservoir. The moments are written out, dividing by n.

def moments(x):
    d = x - x.mean()
    m2 = (d ** 2).mean()
    m3 = (d ** 3).mean()
    return x.mean(), np.median(x), m3 / m2 ** 1.5

og1 = df[df["well"] == "Ogbon-1"]
nr = og1[og1["K_md"] > 0]
for col in ["RT_ohmm", "K_md", "PHI_vv"]:
    x = nr[col].to_numpy()
    print(col, len(x))
    print(np.round(moments(x), 3))
    print(np.round(moments(np.log10(x)), 3))

The book's kernel gives 772 samples, and a skew of +1.285 for RT_ohmm and −0.373 for its log10; −0.088 and −1.049 for K_md; −0.768 and −1.049 for PHI_vv. Of the three, only resistivity is brought nearer symmetric by the log.

Step 3: The Estimate and Its Formula

s Over the Root of n

For independent samples the standard error of the mean is the standard deviation, over n − 1, divided by the root of n.

s = phi.std(ddof=1)
print(round(phi.mean(), 4), round(s, 4))
print(round(s / np.sqrt(n), 6))

The book's kernel gives a mean of 0.2581 v/v and a standard deviation of 0.0086 v/v, so a standard error of 0.000159 v/v.

Step 4: The Book's Generator

mulberry32 in Python

Every random draw in the book comes from one seeded generator, mulberry32. Written in Python, it draws the kernel's numbers exactly, so the resamples below are the figure's.

M32 = 0xFFFFFFFF

def mulberry32(seed):
    state = seed & M32
    def draw():
        nonlocal state
        state = (state + 0x6D2B79F5) & M32
        t = state
        t = ((t ^ (t >> 15)) * (t | 1)) & M32
        t ^= (t + ((t ^ (t >> 7)) * (t | 61))) & M32
        return ((t ^ (t >> 14)) & M32) / 2 ** 32
    return draw

draw = mulberry32(1)
print([round(draw(), 4) for _ in range(3)])

The book's kernel gives 0.6271, 0.0027 and 0.5274 from seed 1.

Step 5: Resample the Samples

A Bootstrap of the Mean

One function serves every bootstrap below: it draws whole groups with replacement, as many as there are, and computes the statistic on what it drew. Here each group is one sample. The standard error is the standard deviation of the resampled means; the 95 % interval their 2.5 and 97.5 percentiles. It takes a few seconds.

def boot(groups, stat, B=1000, seed=1):
    draw = mulberry32(seed)
    G = len(groups)
    out = np.empty(B)
    for b in range(B):
        pick = [groups[int(draw() * G)]
                for _ in range(G)]
        out[b] = stat(np.concatenate(pick))
    return out

samples = [phi[i:i + 1] for i in range(n)]
bm = boot(samples, np.mean)
print(round(bm.std(ddof=1), 6))
print(np.round(np.percentile(bm, [2.5, 97.5]), 4))

The book's kernel gives a standard error of 0.000161 v/v, beside the formula's 0.000159 v/v, and an interval from 0.2578 v/v to 0.2585 v/v.

Step 6: Resample the Wells

A Well Is One Draw

Now each group is one well with all its samples. A resample draws nine wells, some twice and some not at all.

wells = [g["PHI_vv"].to_numpy() for _, g
         in ms.groupby("well", sort=False)]
means = np.array([w.mean() for w in wells])
print(np.round(means, 4))
sw = means.std(ddof=1)
print(round(sw, 4), round(sw / 3, 5))
bw = boot(wells, np.mean)
print(round(bw.std(ddof=1), 5))
print(np.round(np.percentile(bw, [2.5, 97.5]), 4))

The book's kernel gives the nine wells' means 0.2601, 0.2551, 0.2570, 0.2542, 0.2545, 0.2651, 0.2520, 0.2526 and 0.2726 v/v; their standard deviation 0.0068 v/v, so a formula of 0.00226 v/v; and a bootstrap standard error of 0.00217 v/v, with an interval from 0.2544 v/v to 0.2627 v/v. The bootstrap divides by 9, not 8, so it aims at 8/9\sqrt{8/9} of the formula, 0.00213 v/v.

Step 7: The Median

No Formula Needed

The median has no formula as simple as the mean's; the bootstrap needs none.

print(round(np.median(phi), 4))
bmed = boot(samples, np.median)
print(round(bmed.std(ddof=1), 6))
print(round(boot(wells, np.median).std(ddof=1), 5))

The book's kernel gives a median of 0.2575 v/v, a standard error of 0.000246 v/v resampling the samples and 0.00214 v/v resampling the wells. On the figure's full digits the first is 0.000242 v/v: the file's four decimals tie more samples, and a median moves in steps between them.

What the Bootstrap Assumes

The bootstrap assumes that what it resamples are independent draws of what the estimate is about. The half-foot samples of one well are draws of that well's main sand, not of the field's: a new well finds a new main sand, and the nine wells say how much those differ. Across a field the honest unit is the well, which is why the book's models are scored with whole wells held out once they reach the nine wells, from Section 4.2 on. Nine wells are still few: their bootstrap knows only nine porosities, and its interval is as wide as they allow.

This page is prerendered for SEO and accessibility. The interactive widgets above hydrate on JavaScript load.