This strategy reconstructs a recent cumulative return path as a piecewise-linear trend with sparse changes in slope. Its core is a convex optimization problem, not a robust quantile slope or a sequential latent-state Kalman update. The hypothesis is that a directional endpoint slope is more credible when it remains similar under several degrees of curvature regularization. The regularization path therefore acts as a stability check rather than simply selecting one smoothing parameter that looks favorable in hindsight.

The most recent 64-return region supplies an RMS scale. A chronological cumulative path is normalized by that scale to obtain dimensionless observations y[t]. For each penalty lambda in 0.4, 1.2, and 3.6, the target optimization is min_f 0.5*sum((f[t]-y[t])^2) + lambda*sum(abs(D2*f)). Here D2*f[t] = f[t] - 2*f[t+1] + f[t+2]. Penalizing second differences promotes long regions with approximately constant slope, while allowing localized changes when their fit benefit justifies the penalty.

The implementation uses an alternating-direction method of multipliers. It introduces z = D2*f and a scaled dual variable u. The fitted path update approximately minimizes a squared-error term plus the augmented quadratic constraint penalty. Several stable gradient sweeps are used for that subproblem. The sparse curvature update is z = soft_threshold(D2*f + u, lambda/rho), where soft_threshold(v,a) = max(0,v-a) - max(0,-v-a). The dual update is u = u + D2*f - z. Iterations stop early only when the monitored primal and change residuals are small, otherwise they use a fixed numerical budget.

For each penalty, endpoint direction is (fit[63]-fit[57])/6. Its score is normalized by reconstruction error and its own magnitude. The three scores are averaged only if the least and most regularized endpoint slopes have the same sign. Quality decreases with their disagreement and with the final solver residuals. Therefore a poorly stabilized numerical solution can be rejected even when its endpoint looks directional. The bounded mean score feeds the shared entry and exit system. Maximum holding duration is eighteen hourly bars.

The optimization is approximate because the inner quadratic problem is solved iteratively, and the iteration budget is finite. The reported change residual is a practical monitor, not a complete proof of optimality or a statistical confidence interval. Endpoint estimates can lag reversals, and piecewise-linear fitting can interpret a temporary excursion as a new trend segment. Dependence across the three penalties is substantial because they use the same observations. Synthetic sign and state-restoration tests help detect implementation errors, but market-data walk-forward evaluation must still examine numerical convergence, trading costs, signal latency, and robustness to abrupt structural breaks.

Code
// TS18_SparseTrendFilter: Sparse-curvature convex trend filter.
// Standalone model implementation with embedded safety and engine code.
// Market scope: EUR/USD completed-hour return aggregation horizons.
// These are not actual bond maturities or a futures calendar spread.
// Model diagnostic: regularization-path endpoint disagreement.
// Quality heuristic: Primal/dual residual stability and penalty agreement.
// Limitation: Finite-iteration ADMM is approximate. The residual gate is a numerical safeguard, not a confidence interval.
// No claim of profitability, calibration, or independent strategy returns.
// Inputs: 256 newest-first log returns; all must be finite and within [-1,1].
// Output: bounded signed edge; positive buys, negative sells, zero abstains.
// Quality is a heuristic filter, never a calibrated success probability.
// The synthetic suite below is included in the total file line count.
// Regenerate with build_sources.py; compare hashes before deployment.
#include <default.c>
string TS2Algo = "TS18";
string TS2TestLog = "Log/TS18_SparseTrendFilter_test.txt";
int TS2Holding = 18;
// R1/R3: IEEE-754 finite validation without lite-C unordered comparisons.
// Supported Zorro Windows targets use 64-bit var and little-endian 32-bit int.
int TSIsFinite(var Value)
{
    int Words[2];
    memcpy(Words, &Value, sizeof(Value));
    return (Words[1] & 0x7ff00000) != 0x7ff00000;
}

// Forex commission per lot; negative quotes are percent, not cash rebates.
// Return -1 on invalid metadata rather than accepting an unknown risk amount.
var TSFeePerLot(var FeeQuote, var UnitsPerLot, var PipSize, var PipValue, var RefPrice)
{
    var FeeValue;
    if (!TSIsFinite(FeeQuote) || !TSIsFinite(UnitsPerLot) || !TSIsFinite(PipSize) ||
        !TSIsFinite(PipValue) || !TSIsFinite(RefPrice))
        return -1.;
    if (!(UnitsPerLot > 0.) || !(PipSize > 0.) || !(PipValue > 0.) || !(RefPrice > 0.))
        return -1.;
    if (FeeQuote >= 0.)
        FeeValue = FeeQuote * UnitsPerLot / 10000.;
    else
        FeeValue = (-FeeQuote / 100.) * (PipValue / PipSize) * RefPrice;
    if (!TSIsFinite(FeeValue) || FeeValue < 0.)
        return -1.;
    return FeeValue;
}

var TSUnitStopRisk(var RefPrice)
{
    var FeeValue, FeePrice, StopRisk;
    if (!TSIsFinite(RefPrice) || !(RefPrice > 0.) || !TSIsFinite(Stop) || !(Stop > 0.) ||
        !TSIsFinite(Spread) || !TSIsFinite(Balance) || !TSIsFinite(Equity) ||
        !TSIsFinite(MarginCost) || !TSIsFinite(MarginVal))
        return -1.;
    if (!(Balance > 0.) || !(Equity > 0.) || !(MarginCost > 0.))
        return -1.;
    // Conservative percentage-fee price through the stop, absent gaps/slippage.
    FeePrice = RefPrice + abs(Spread) + Stop;
    FeeValue = TSFeePerLot(Commission, LotAmount, PIP, PIPCost, FeePrice);
    if (FeeValue < 0.)
        return -1.;
    StopRisk = (Stop + abs(Spread)) / PIP * PIPCost + FeeValue;
    if (!TSIsFinite(StopRisk) || !(StopRisk > 0.))
        return -1.;
    return StopRisk;
}

// Embedded input, state, and test contracts; identical safety is not novel alpha.
#define TS2_N 256
var TS2Returns[256];
var TS2Quality;
var TS2Statistic;
var TS2Raw;
int TS2Checks;
int TS2Failures;
var TS2Model();
var TS2Bound(var Value)
{
    if (!TSIsFinite(Value))
        return 0.;
    return max(-1., min(1., Value));
}
var TS2Evaluate()
{
    int Index;
    var Energy;
    TS2Quality = 0.;
    TS2Statistic = 0.;
    TS2Raw = 0.;
    Energy = 0.;
    for (Index = 0; Index < TS2_N; Index++)
    {
        if (!TSIsFinite(TS2Returns[Index]))
            return 0.;
        if (abs(TS2Returns[Index]) > 1.)
            return 0.;
        Energy += TS2Returns[Index] * TS2Returns[Index];
    }
    if (Energy < 1.e-24)
        return 0.;
    TS2Raw = TS2Model();
    if (!TSIsFinite(TS2Raw) || abs(TS2Raw) > 1.e12 ||
        !TSIsFinite(TS2Quality) || !TSIsFinite(TS2Statistic))
    {
        TS2Quality = 0.;
        TS2Statistic = 0.;
        TS2Raw = 0.;
        return 0.;
    }
    TS2Quality = max(0., min(1., TS2Quality));
    return TS2Bound(TS2Raw);
}
void TS2Expect(int Passed, string Label)
{
    TS2Checks++;
    if (!Passed)
    {
        TS2Failures++;
        file_append(TS2TestLog, strf("FAIL: %s\n", Label));
    }
}
// BEGIN ANALYTICAL MODEL
// Solve squared-error plus L1 second-difference penalty by ADMM.
// Three independently optimized penalties form a trend regularization path.
var TS2Model()
{
    var Data[64], Fit[64], Z[62], Dual[62], Gradient[64], Next[64];
    var Penalties[3] = {0.4, 1.2, 3.6};
    var Energy, Unit, Lambda, Rho, Difference, Shifted, Change;
    var Primal, DualChange, PreviousZ, Slope, Curvature, Score, Stability;
    var Scores[3], Error, Step, Objective, LastObjective;
    int Index, Iteration, Scale, Sweep;
    Data[0] = 0.; Energy = 0.;
    for (Index = 1; Index < 64; Index++)
        Data[Index] = Data[Index - 1] + TS2Returns[63 - Index];
    for (Index = 0; Index < 64; Index++)
        Energy += TS2Returns[Index] * TS2Returns[Index] / 64.;
    Unit = sqrt(Energy);
    if (Unit < 1.e-16)
        return 0.;
    for (Index = 0; Index < 64; Index++)
        Data[Index] /= Unit;
    Stability = 1.;
    for (Scale = 0; Scale < 3; Scale++)
    {
        Lambda = Penalties[Scale];
        Rho = 1.;
        Step = 1. / 18.;
        for (Index = 0; Index < 64; Index++)
            Fit[Index] = Data[Index];
        for (Index = 0; Index < 62; Index++)
        {
            Z[Index] = Fit[Index] - 2. * Fit[Index + 1] + Fit[Index + 2];
            Dual[Index] = 0.;
        }
        // Fixed iteration budget; residuals explicitly gate the output.
        for (Iteration = 0; Iteration < 160; Iteration++)
        {
            for (Sweep = 0; Sweep < 8; Sweep++)
            {
                for (Index = 0; Index < 64; Index++)
                    Gradient[Index] = Fit[Index] - Data[Index];
                for (Index = 0; Index < 62; Index++)
                {
                    Difference = Fit[Index] - 2. * Fit[Index + 1] + Fit[Index + 2];
                    Shifted = Rho * (Difference - Z[Index] + Dual[Index]);
                    Gradient[Index] += Shifted;
                    Gradient[Index + 1] -= 2. * Shifted;
                    Gradient[Index + 2] += Shifted;
                }
                for (Index = 0; Index < 64; Index++)
                    Next[Index] = Fit[Index] - Step * Gradient[Index];
                for (Index = 0; Index < 64; Index++)
                    Fit[Index] = Next[Index];
            }
            Primal = 0.; DualChange = 0.;
            for (Index = 0; Index < 62; Index++)
            {
                Difference = Fit[Index] - 2. * Fit[Index + 1] + Fit[Index + 2];
                Shifted = Difference + Dual[Index];
                PreviousZ = Z[Index];
                Z[Index] = max(0., Shifted - Lambda / Rho) - max(0., -Shifted - Lambda / Rho);
                Dual[Index] += Difference - Z[Index];
                Primal += pow(Difference - Z[Index], 2.);
                DualChange += pow(Z[Index] - PreviousZ, 2.);
            }
            if (Primal < 1.e-8 && DualChange < 1.e-8)
                break;
        }
        Error = 0.;
        for (Index = 0; Index < 64; Index++)
            Error += pow(Fit[Index] - Data[Index], 2.) / 64.;
        Slope = (Fit[63] - Fit[57]) / 6.;
        Curvature = Fit[63] - 2. * Fit[60] + Fit[57];
        Scores[Scale] = Slope / sqrt(1. + Error + Slope * Slope);
        Stability = min(Stability, 1. / (1. + sqrt(Primal + DualChange)));
    }
    Score = (Scores[0] + Scores[1] + Scores[2]) / 3.;
    TS2Statistic = abs(Scores[0] - Scores[2]);
    TS2Quality = Stability / (1. + 4. * TS2Statistic);
    if (Scores[0] * Scores[2] <= 0.)
        return 0.;
    return TS2Bound(Score);
}
// END ANALYTICAL MODEL

// Separate repair regression log; the original suite counts stay intact.
int TSRepairChecks = 0;
int TSRepairFailures = 0;
string TSRepairLog = "Log/TS18_SparseTrendFilter_repair.txt";
void TSRepairCheck(int Passed, string Label)
{
    TSRepairChecks++;
    if (!Passed)
    {
        TSRepairFailures++;
        file_append(TSRepairLog, strf("FAIL: %s\n", Label));
    }
}
var TSRepairBad(int Kind)
{
    int Bits[2];
    var Value;
    Bits[0] = 0;
    Bits[1] = 0x7ff80000;
    if (Kind == 1) Bits[1] = 0x7ff00000;
    if (Kind == 2) Bits[1] = 0xfff00000;
    memcpy(&Value, Bits, sizeof(Value));
    return Value;
}
void TSRepairTests()
{
    int I, Kind, Position;
    var FeeValue, UnitLoss, Budget;
    TSRepairChecks = 0; TSRepairFailures = 0;
    file_write(TSRepairLog, "Repairs R1-R5: native-safe regression suite\n", 0);
    TSRepairCheck(sizeof(FeeValue) == 8, "64-bit var representation");
    TSRepairCheck(TSIsFinite(0.), "finite zero");
    TSRepairCheck(TSIsFinite(1.), "finite positive");
    TSRepairCheck(TSIsFinite(-1.), "finite negative");
    for (Kind = 0; Kind < 3; Kind++)
        TSRepairCheck(!TSIsFinite(TSRepairBad(Kind)), "reject NaN and both infinities");
    TSRepairCheck(abs(TSFeePerLot(0.7, 1000., 0.0001, 0.1, 1.1) - 0.07) < 1.e-6,
                  "cash commission convention");
    TSRepairCheck(TSFeePerLot(0., 1000., 0.0001, 0.1, 1.1) == 0., "zero commission");
    FeeValue = TSFeePerLot(-0.02, 1000., 0.0001, 0.1, 1.1);
    TSRepairCheck(abs(FeeValue - 0.22) < 1.e-6, "percentage commission convention");
    TSRepairCheck(TSFeePerLot(0.7, 1000., 0., 0.1, 1.1) < 0., "invalid pip size");
    TSRepairCheck(TSFeePerLot(0.7, 1000., 0.0001, 0., 1.1) < 0., "invalid pip value");
    TSRepairCheck(TSFeePerLot(0.7, 0., 0.0001, 0.1, 1.1) < 0., "invalid lot amount");
    TSRepairCheck(TSFeePerLot(0.7, 1000., 0.0001, 0.1, -1.) < 0., "invalid fee price");
    TSRepairCheck(TSFeePerLot(TSRepairBad(0), 1000., 0.0001, 0.1, 1.1) < 0., "NaN fee quote");
    TSRepairCheck(TSFeePerLot(-0.02, 1000., 0.0001, 0.1, TSRepairBad(1)) < 0., "infinite fee price");
    TSRepairCheck(TSFeePerLot(-0.02, 1000., TSRepairBad(0), 0.1, 1.1) < 0., "NaN pip metadata");
    UnitLoss = (20. * 0.0001 + 0.0001) / 0.0001 * 0.1 + FeeValue;
    Budget = 850. * 0.0025;
    TSRepairCheck(floor(Budget / UnitLoss) == 0., "percentage fee respects lot-rounding budget");

    for (Kind = 0; Kind < 3; Kind++)
    {
        for (Position = 0; Position < 2; Position++)
        {
            for (I = 0; I < 256; I++) TS2Returns[I] = 0.0005 + 0.0001 * sin(0.3 * I);
            TS2Returns[Position] = TSRepairBad(Kind);
            TSRepairCheck(TS2Evaluate() == 0, "non-finite consumed input rejected");
            TSRepairCheck(TS2Quality == 0., "invalid return clears quality");
        }
    }
    
    file_append(TSRepairLog, strf("RESULT: %d checks; %d failures\n", TSRepairChecks, TSRepairFailures));
}
// Synthetic property tests: executed only with -d TS2_SELFTEST.
// Tests do not execute trades, query market data, or establish profitability.
// Known-response anchor: not just a generic bounded-output assertion.
void TS2KnownResponse()
{
    int I;
    var Edge, Negative;
    for (I = 0; I < 256; I++)
        TS2Returns[I] = 0.0005;
    Edge = TS2Evaluate();
    TS2Expect(abs(Edge - 1. / sqrt(2.)) < 1.e-7, "known straight path: model-specific response");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -0.0005;
    Negative = TS2Evaluate();
    TS2Expect(abs(Edge + Negative) < 1.e-7, "known straight path: symmetric response");
}
// Case 1: slow_sine; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case1()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = 0.0008 * sin(0.07 * I);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "slow_sine: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "slow_sine: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "slow_sine: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "slow_sine: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "slow_sine: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "slow_sine: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[18] = 2.;
    TS2Expect(TS2Evaluate() == 0., "slow_sine: implausible return rejected");
    TS2Expect(TS2Quality == 0., "slow_sine: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "slow_sine: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "slow_sine: quality restored");
}
// Case 2: fast_sine; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case2()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = 0.0012 * sin(1.1 * I);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "fast_sine: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "fast_sine: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "fast_sine: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "fast_sine: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "fast_sine: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "fast_sine: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[55] = 2.;
    TS2Expect(TS2Evaluate() == 0., "fast_sine: implausible return rejected");
    TS2Expect(TS2Quality == 0., "fast_sine: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "fast_sine: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "fast_sine: quality restored");
}
// Case 3: two_scale_beating; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case3()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = 0.0009 * sin(0.18 * I) + 0.0007 * sin(0.21 * I);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "two_scale_beating: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "two_scale_beating: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "two_scale_beating: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "two_scale_beating: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "two_scale_beating: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "two_scale_beating: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[92] = 2.;
    TS2Expect(TS2Evaluate() == 0., "two_scale_beating: implausible return rejected");
    TS2Expect(TS2Quality == 0., "two_scale_beating: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "two_scale_beating: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "two_scale_beating: quality restored");
}
// Case 4: amplitude_burst; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case4()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = 0.0004 * sin(0.31 * I) * (1. + 3. * exp(-pow((I - 28.) / 8., 2.)));
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "amplitude_burst: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "amplitude_burst: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "amplitude_burst: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "amplitude_burst: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "amplitude_burst: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "amplitude_burst: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[129] = 2.;
    TS2Expect(TS2Evaluate() == 0., "amplitude_burst: implausible return rejected");
    TS2Expect(TS2Quality == 0., "amplitude_burst: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "amplitude_burst: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "amplitude_burst: quality restored");
}
// Case 5: chirp_acceleration; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case5()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = 0.001 * sin(0.0018 * I * I + 0.05 * I);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "chirp_acceleration: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "chirp_acceleration: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "chirp_acceleration: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "chirp_acceleration: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "chirp_acceleration: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "chirp_acceleration: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[166] = 2.;
    TS2Expect(TS2Evaluate() == 0., "chirp_acceleration: implausible return rejected");
    TS2Expect(TS2Quality == 0., "chirp_acceleration: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "chirp_acceleration: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "chirp_acceleration: quality restored");
}
// Case 6: chirp_deceleration; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case6()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = 0.001 * sin(7. * sqrt(I + 1.));
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "chirp_deceleration: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "chirp_deceleration: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "chirp_deceleration: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "chirp_deceleration: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "chirp_deceleration: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "chirp_deceleration: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[203] = 2.;
    TS2Expect(TS2Evaluate() == 0., "chirp_deceleration: implausible return rejected");
    TS2Expect(TS2Quality == 0., "chirp_deceleration: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "chirp_deceleration: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "chirp_deceleration: quality restored");
}
// Case 7: positive_drift_noise; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case7()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = 0.0003 + 0.0009 * sin(0.77 * I);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "positive_drift_noise: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "positive_drift_noise: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "positive_drift_noise: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "positive_drift_noise: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "positive_drift_noise: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "positive_drift_noise: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[240] = 2.;
    TS2Expect(TS2Evaluate() == 0., "positive_drift_noise: implausible return rejected");
    TS2Expect(TS2Quality == 0., "positive_drift_noise: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "positive_drift_noise: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "positive_drift_noise: quality restored");
}
// Case 8: negative_drift_noise; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case8()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = -0.0003 + 0.0009 * sin(0.77 * I);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "negative_drift_noise: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "negative_drift_noise: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "negative_drift_noise: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "negative_drift_noise: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "negative_drift_noise: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "negative_drift_noise: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[21] = 2.;
    TS2Expect(TS2Evaluate() == 0., "negative_drift_noise: implausible return rejected");
    TS2Expect(TS2Quality == 0., "negative_drift_noise: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "negative_drift_noise: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "negative_drift_noise: quality restored");
}
// Case 9: mixed_frequency_drift; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case9()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = 0.0002 + 0.0006 * sin(0.23 * I) + 0.0004 * cos(1.37 * I);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "mixed_frequency_drift: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "mixed_frequency_drift: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "mixed_frequency_drift: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "mixed_frequency_drift: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "mixed_frequency_drift: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "mixed_frequency_drift: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[58] = 2.;
    TS2Expect(TS2Evaluate() == 0., "mixed_frequency_drift: implausible return rejected");
    TS2Expect(TS2Quality == 0., "mixed_frequency_drift: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "mixed_frequency_drift: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "mixed_frequency_drift: quality restored");
}
// Case 10: alternating_sign; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case10()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = ifelse(I % 2 == 0, 0.001, -0.001);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "alternating_sign: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "alternating_sign: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "alternating_sign: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "alternating_sign: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "alternating_sign: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "alternating_sign: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[95] = 2.;
    TS2Expect(TS2Evaluate() == 0., "alternating_sign: implausible return rejected");
    TS2Expect(TS2Quality == 0., "alternating_sign: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "alternating_sign: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "alternating_sign: quality restored");
}
// Case 11: three_bar_blocks; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case11()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = ifelse(I % 6 < 3, 0.001, -0.001);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "three_bar_blocks: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "three_bar_blocks: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "three_bar_blocks: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "three_bar_blocks: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "three_bar_blocks: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "three_bar_blocks: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[132] = 2.;
    TS2Expect(TS2Evaluate() == 0., "three_bar_blocks: implausible return rejected");
    TS2Expect(TS2Quality == 0., "three_bar_blocks: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "three_bar_blocks: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "three_bar_blocks: quality restored");
}
// Case 12: five_bar_blocks; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case12()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = ifelse(I % 10 < 5, 0.001, -0.001);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "five_bar_blocks: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "five_bar_blocks: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "five_bar_blocks: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "five_bar_blocks: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "five_bar_blocks: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "five_bar_blocks: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[169] = 2.;
    TS2Expect(TS2Evaluate() == 0., "five_bar_blocks: implausible return rejected");
    TS2Expect(TS2Quality == 0., "five_bar_blocks: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "five_bar_blocks: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "five_bar_blocks: quality restored");
}
// Case 13: one_sided_steps; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case13()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = ifelse(I < 24, 0.001, 0.0001) + 0.0001 * sin(I);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "one_sided_steps: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "one_sided_steps: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "one_sided_steps: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "one_sided_steps: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "one_sided_steps: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "one_sided_steps: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[206] = 2.;
    TS2Expect(TS2Evaluate() == 0., "one_sided_steps: implausible return rejected");
    TS2Expect(TS2Quality == 0., "one_sided_steps: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "one_sided_steps: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "one_sided_steps: quality restored");
}
// Case 14: late_reversal; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case14()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = ifelse(I < 12, -0.0008, 0.0005) + 0.0002 * cos(0.7 * I);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "late_reversal: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "late_reversal: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "late_reversal: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "late_reversal: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "late_reversal: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "late_reversal: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[243] = 2.;
    TS2Expect(TS2Evaluate() == 0., "late_reversal: implausible return rejected");
    TS2Expect(TS2Quality == 0., "late_reversal: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "late_reversal: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "late_reversal: quality restored");
}
// Case 15: middle_reversal; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case15()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = ifelse(I < 64, 0.0006, -0.0004) + 0.0002 * sin(0.43 * I);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "middle_reversal: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "middle_reversal: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "middle_reversal: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "middle_reversal: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "middle_reversal: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "middle_reversal: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[24] = 2.;
    TS2Expect(TS2Evaluate() == 0., "middle_reversal: implausible return rejected");
    TS2Expect(TS2Quality == 0., "middle_reversal: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "middle_reversal: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "middle_reversal: quality restored");
}
// Case 16: old_reversal; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case16()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = ifelse(I < 160, 0.0004, -0.0007) + 0.0001 * cos(0.91 * I);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "old_reversal: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "old_reversal: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "old_reversal: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "old_reversal: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "old_reversal: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "old_reversal: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[61] = 2.;
    TS2Expect(TS2Evaluate() == 0., "old_reversal: implausible return rejected");
    TS2Expect(TS2Quality == 0., "old_reversal: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "old_reversal: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "old_reversal: quality restored");
}
// Case 17: volatility_expansion; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case17()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = (0.0002 + 0.001 * exp(-0.015 * I)) * sin(0.81 * I);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "volatility_expansion: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "volatility_expansion: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "volatility_expansion: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "volatility_expansion: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "volatility_expansion: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "volatility_expansion: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[98] = 2.;
    TS2Expect(TS2Evaluate() == 0., "volatility_expansion: implausible return rejected");
    TS2Expect(TS2Quality == 0., "volatility_expansion: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "volatility_expansion: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "volatility_expansion: quality restored");
}
// Case 18: volatility_contraction; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case18()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = (0.0002 + 0.000004 * I) * sin(0.81 * I);
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "volatility_contraction: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "volatility_contraction: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "volatility_contraction: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "volatility_contraction: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "volatility_contraction: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "volatility_contraction: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[135] = 2.;
    TS2Expect(TS2Evaluate() == 0., "volatility_contraction: implausible return rejected");
    TS2Expect(TS2Quality == 0., "volatility_contraction: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "volatility_contraction: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "volatility_contraction: quality restored");
}
// Case 19: isolated_recent_up; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case19()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = ifelse(I == 3, 0.008, 0.0002 * sin(0.61 * I));
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "isolated_recent_up: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "isolated_recent_up: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "isolated_recent_up: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "isolated_recent_up: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "isolated_recent_up: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "isolated_recent_up: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[172] = 2.;
    TS2Expect(TS2Evaluate() == 0., "isolated_recent_up: implausible return rejected");
    TS2Expect(TS2Quality == 0., "isolated_recent_up: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "isolated_recent_up: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "isolated_recent_up: quality restored");
}
// Case 20: isolated_recent_down; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case20()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = ifelse(I == 3, -0.008, 0.0002 * sin(0.61 * I));
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "isolated_recent_down: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "isolated_recent_down: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "isolated_recent_down: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "isolated_recent_down: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "isolated_recent_down: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "isolated_recent_down: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[209] = 2.;
    TS2Expect(TS2Evaluate() == 0., "isolated_recent_down: implausible return rejected");
    TS2Expect(TS2Quality == 0., "isolated_recent_down: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "isolated_recent_down: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "isolated_recent_down: quality restored");
}
// Case 21: paired_opposite_shocks; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case21()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = ifelse(I == 7, 0.006, ifelse(I == 11, -0.006, 0.0001 * cos(I)));
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "paired_opposite_shocks: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "paired_opposite_shocks: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "paired_opposite_shocks: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "paired_opposite_shocks: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "paired_opposite_shocks: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "paired_opposite_shocks: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[246] = 2.;
    TS2Expect(TS2Evaluate() == 0., "paired_opposite_shocks: implausible return rejected");
    TS2Expect(TS2Quality == 0., "paired_opposite_shocks: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "paired_opposite_shocks: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "paired_opposite_shocks: quality restored");
}
// Case 22: paired_same_shocks; model 18 perturbation phase 2.466000.
// Covers numerical stability and directional symmetry on this path family.
void TS2Case22()
{
    var Snapshot[256], Original, Repeated, Reflected, Restored;
    var InitialQuality, InitialStatistic;
    int I;
    for (I = 0; I < 256; I++)
    {
        TS2Returns[I] = ifelse(I == 7 || I == 11, 0.006, 0.0001 * cos(I));
        TS2Returns[I] += 0.00000003 * sin(2.466000 + I * 0.424000);
        Snapshot[I] = TS2Returns[I];
    }
    Original = TS2Evaluate();
    InitialQuality = TS2Quality;
    InitialStatistic = TS2Statistic;
    TS2Expect(Original == Original && abs(Original) <= 1., "paired_same_shocks: finite bounded edge");
    TS2Expect(InitialQuality >= 0. && InitialQuality <= 1., "paired_same_shocks: quality contract");
    TS2Expect(InitialStatistic == InitialStatistic && abs(InitialStatistic) < 1.e12, "paired_same_shocks: finite diagnostic");
    Repeated = TS2Evaluate();
    TS2Expect(abs(Repeated - Original) < 1.e-9, "paired_same_shocks: deterministic evaluation");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "paired_same_shocks: deterministic quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = -Snapshot[I];
    Reflected = TS2Evaluate();
    TS2Expect(abs(Reflected + Original) < 1.e-7, "paired_same_shocks: directional reflection");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    TS2Returns[27] = 2.;
    TS2Expect(TS2Evaluate() == 0., "paired_same_shocks: implausible return rejected");
    TS2Expect(TS2Quality == 0., "paired_same_shocks: rejected sample clears quality");
    for (I = 0; I < 256; I++)
        TS2Returns[I] = Snapshot[I];
    Restored = TS2Evaluate();
    TS2Expect(abs(Restored - Original) < 1.e-9, "paired_same_shocks: restored input has no stale state");
    TS2Expect(abs(TS2Quality - InitialQuality) < 1.e-9, "paired_same_shocks: quality restored");
}