"""Offline walk-forward teaching fixture. All outcomes are fictional.
Run: python3 walk-forward-analysis.py. No packages, feeds, accounts or orders.
"""
from fractions import Fraction as F


def folds(total, initial_train, gap, test_size, max_train=None):
    if any(type(x) is not int for x in [total, initial_train, gap, test_size]):
        raise ValueError("Use whole sample counts")
    if total < 1 or initial_train < 1 or gap < 0 or test_size < 1:
        raise ValueError("Invalid window size")
    if max_train is not None and (type(max_train) is not int or max_train < 1):
        raise ValueError("Invalid rolling train cap")
    start = initial_train + gap
    if start + test_size > total:
        raise ValueError("Not enough samples for a complete first test block")
    while start + test_size <= total:
        end_train = start - gap
        begin_train = 0 if max_train is None else max(0, end_train - max_train)
        yield list(range(begin_train, end_train)), list(range(end_train, start)), list(range(start, start + test_size))
        start += test_size


def fit_probability(train_labels):
    # Predeclared smoothed frequency, not a tuned model: (wins+1)/(n+2).
    if not train_labels or any(y not in (0, 1) for y in train_labels):
        raise ValueError("Expected nonempty binary training labels")
    return F(sum(train_labels) + 1, len(train_labels) + 2)


if __name__ == "__main__":
    y = [1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1]
    # Human sample i is forecast on synthetic day i; its label arrives day i+2.
    label_known_day = [i + 3 for i in range(len(y))]
    split = list(folds(12, 4, 2, 2))
    assert [(tr, te) for tr, _, te in split] == [
        ([0,1,2,3],[6,7]), ([0,1,2,3,4,5],[8,9]),
        ([0,1,2,3,4,5,6,7],[10,11])]
    rolling = list(folds(12, 4, 2, 2, max_train=4))
    assert [tr for tr, _, _ in rolling] == [[0,1,2,3],[2,3,4,5],[4,5,6,7]]
    pooled, baseline, seen = [], [], set()
    expected_p = [F(1,2), F(5,8), F(3,5)]
    expected_loss = [F(1,4), F(25,64), F(4,25)]
    for number, (train, gap, test) in enumerate(split):
        fit_day = test[0] + 1
        assert max(train) < min(gap) < min(test)
        assert all(label_known_day[i] < fit_day for i in train)
        assert not seen.intersection(test)
        seen.update(test)
        p = fit_probability([y[i] for i in train])
        # Prediction frozen before the test outcomes are accessed for scoring.
        forecasts = [(i, p) for i in test]
        losses = [(prob - y[i])**2 for i, prob in forecasts]
        mean = sum(losses) / len(losses)
        assert p == expected_p[number] and mean == expected_loss[number]
        pooled.extend(losses)
        baseline.extend((F(1,2) - y[i])**2 for i in test)
        print(f"Fold{number+1}: train{train[0]+1}-{train[-1]+1}, gap{gap[0]+1}-{gap[-1]+1}, test{test[0]+1}-{test[-1]+1}, p={p}, Brier={float(mean):.6f}")
    assert seen == set(range(6,12))
    score, reference = sum(pooled)/len(pooled), sum(baseline)/len(baseline)
    assert score == F(427,1600) and reference == F(1,4)
    assert score-reference == F(27,1600)
    for args in [(4,4,0,1),(12,0,2,2),(12,4,-1,2),(12,4,2,0)]:
        try:
            list(folds(*args))
        except ValueError:
            pass
        else:
            raise AssertionError("Invalid split accepted")
    print(f"Pooled Brier={float(score):.6f}; baseline={float(reference):.6f}; model worse by{float(score-reference):.6f}.")
    print("Exact windows, delayed labels, frozen blocks, unique tests and invalid-window checks passed.")
