Find the bad months before fitting the decline

Two years of monthly rates with a doubled allocation and a partial shut-in: flag the outliers, then fit the decline three ways (every point, IRLS with Huber weights, and without the flagged months) and compare the parameters and the EUR each one books.

Open in the workbench

Engineering use case UC-DCA-006 (Handle Data Quality Issues).

Allocation errors, shut-ins and test months put points in a rate history that the reservoir did not make. CompositeAnomalyDetector combines a rolling z-score test with physical bounds and returns the indices it flags. FitterBuilder.WithWeights(new ResidualWeightStrategy { WeightFunction = new HuberWeight() }) fits by iteratively reweighted least squares: fit, down-weight the large residuals, refit until the weights settle.

What an engineer learns from it: the detector flags months 6 and 15, the two that were spoiled. The plain fit gives qi = 33,546 STB/month, Di = 0.0698 and b = 0.776 against a truth of 30,000, 0.05 and 0.6. IRLS pulls qi and Di towards the truth (31,527 and 0.0597) but not b (0.797), and b is what sets the tail: to 500 STB/month the plain fit books 1,310 MSTB, IRLS 1,480 and the truth is 1,208. Leaving the flagged months out gives back 30,000, 0.05, 0.6 and 1,208 MSTB. Down-weighting is a guard for the outliers no one has found; once a month is known to be wrong, take it out. Then look at why it was wrong: a shut-in is an operational event, and the decline after it may restart (UC-DCA-007).

Rates in STB/month against months; the EURs are to a 500 STB/month limit. The fit is in log space by default, which already weighs a doubled month and a 60 % dip alike.

Source

using System;
using System.Linq;
using Team76.Engineering.DCA.DeclineModels;
using Team76.Engineering.DCA.Fitting;
using Team76.Engineering.DCA.Fitting.AnomalyDetection;
using Team76.Engineering.DCA.Fitting.Robust;
using Team76.Engineering.DCA.Fitting.Weights;

public class S : ICalcScript
{
    public ScriptResult Run(ICalcContext ctx)
    {
        // Two years of monthly rates, STB/month, from 30,000 STB/month, 5 %/month and b = 0.6. Month 6 is a doubled
        // allocation and month 15 a partial shut-in at 40 % of the rate.
        var truth = new ArpsHyperbolicDecline(Qi: 30_000, Di: 0.05, b: 0.6);
        var month = Enumerable.Range(1, 24).Select(m => (double)m).ToArray();
        var rate = month.Select(truth.Rate).ToArray();
        rate[5] *= 2.0;
        rate[14] *= 0.4;

        // Flag the points that do not belong: a rolling 3-sigma test, and rates outside what the well can make.
        var detector = new CompositeAnomalyDetector([new ZScoreDetector(windowSize: 10, nSigma: 3.0), new PhysicalBoundsDetector(minRate: 0, maxRate: 50_000)]);
        var outliers = detector.GetAnomalyIndices(month, rate, threshold: 0.5);

        // Fit once with every point counted the same, and once with IRLS: fit, down-weight the large residuals, refit.
        double[] start = [25_000, 0.1, 0.5], lower = [0, 0, 0], upper = [100_000, 1, 2];
        var plain = new FitterBuilder().WithNelderMead(maxIterations: 5000).WithBounds(lower, upper)
            .FitWithResult(new ArpsHyperbolicDecline(25_000, 0.1, 0.5), month, rate, start);
        var robust = new FitterBuilder().WithNelderMead(maxIterations: 5000).WithBounds(lower, upper)
            .WithWeights(new ResidualWeightStrategy { WeightFunction = new HuberWeight() })
            .FitWithResult(new ArpsHyperbolicDecline(25_000, 0.1, 0.5), month, rate, start);

        // And once with the flagged months left out.
        var keep = Enumerable.Range(0, month.Length).Where(i => !outliers.Contains(i)).ToArray();
        var cleaned = new FitterBuilder().WithNelderMead(maxIterations: 5000).WithBounds(lower, upper)
            .FitWithResult(new ArpsHyperbolicDecline(25_000, 0.1, 0.5), keep.Select(i => month[i]).ToArray(), keep.Select(i => rate[i]).ToArray(), start);

        var plainModel = new ArpsHyperbolicDecline(plain.Parameters[0], plain.Parameters[1], plain.Parameters[2]);
        var robustModel = new ArpsHyperbolicDecline(robust.Parameters[0], robust.Parameters[1], robust.Parameters[2]);
        var cleanedModel = new ArpsHyperbolicDecline(cleaned.Parameters[0], cleaned.Parameters[1], cleaned.Parameters[2]);
        var chart = new ScriptChart(
            [
                new ScriptSeries("History", month, rate),
                new ScriptSeries("Plain fit", month, month.Select(plainModel.Rate).ToArray()),
                new ScriptSeries("IRLS fit", month, month.Select(robustModel.Rate).ToArray()),
                new ScriptSeries("Fit without the flagged months", month, month.Select(cleanedModel.Rate).ToArray()),
            ],
            title: "Two outliers, three fits", xLabel: "Month", yLabel: "Rate (STB/month)");

        return ctx.Result()
            .Set("Fits", chart)
            .Set("Outlier months", string.Join(", ", outliers.Select(i => (i + 1).ToString())))
            .Set("Plain qi (STB/month)", Math.Round(plain.Parameters[0]))
            .Set("Plain Di (1/month)", Math.Round(plain.Parameters[1], 4))
            .Set("Plain b", Math.Round(plain.Parameters[2], 3))
            .Set("IRLS qi (STB/month)", Math.Round(robust.Parameters[0]))
            .Set("IRLS Di (1/month)", Math.Round(robust.Parameters[1], 4))
            .Set("IRLS b", Math.Round(robust.Parameters[2], 3))
            .Set("IRLS weights settled", robust.WeightsConverged == true)
            .Set("Cleaned qi (STB/month)", Math.Round(cleaned.Parameters[0]))
            .Set("Cleaned Di (1/month)", Math.Round(cleaned.Parameters[1], 4))
            .Set("Cleaned b", Math.Round(cleaned.Parameters[2], 3))
            .Set("EUR to 500 STB/month, plain (MSTB)", Math.Round(plainModel.UltimateByRateLimit(500) / 1000))
            .Set("EUR to 500 STB/month, IRLS (MSTB)", Math.Round(robustModel.UltimateByRateLimit(500) / 1000))
            .Set("EUR to 500 STB/month, cleaned (MSTB)", Math.Round(cleanedModel.UltimateByRateLimit(500) / 1000));
    }
}