> For the complete documentation index, see [llms.txt](https://docs.algenta.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.algenta.ai/machine-learning/ml-model-selection.md).

# Time-series model selection

Walk-forward, purged, k-fold and temporal splits for leakage-free time-series model selection.

`model_selection` provides cross-validation splitters that respect time — the foundation for validating any model trained on sequential data (markets, sensors, demand). Random k-fold leaks the future into training; these splitters never do.

All functions run natively in the Mojo engine (zero Python imports, deterministic, seeded where applicable).

## The modules

| Module            | What it does                                                                                                                                                                                                     |
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `model_selection` | leakage-free cross-validation splitters for sequential data — walk-forward, purged, k-fold and temporal splits that respect time ordering instead of leaking the future into training the way random k-fold does |

## Functions

| Function                                                                 | What it does                                                                                                                                                                                                                                |
| ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `walk_forward_splits(n, n_splits, min_train, expanding)`                 | Ordered walk-forward folds: train on `[0, k)`, test on the next contiguous window. `expanding=True` grows the training window each fold; `False` rolls it at `min_train`. Test windows are contiguous, non-overlapping, and cover the tail. |
| `purged_walk_forward_splits(n, n_splits, min_train, purge_gap, embargo)` | Finance-grade walk-forward: drops `purge_gap` samples between train end and test start (removes label leakage from overlapping forward-return windows) and embargoes `embargo` samples after each test window from later training folds.    |
| `kfold_splits(n, k, seed, shuffle)`                                      | Classic k-fold for non-temporal data.                                                                                                                                                                                                       |
| `stratified_kfold_splits(labels, k, seed)`                               | K-fold preserving class proportions per fold.                                                                                                                                                                                               |
| `train_test_split_temporal(n, test_fraction)`                            | Single ordered split.                                                                                                                                                                                                                       |

## When to use purged CV

If your label at time *t* is computed from data in *(t, t+h]* (a forward return, a settlement outcome), a plain walk-forward split lets training samples within *h* of the test boundary see part of the test window's future. Set `purge_gap >= h` to remove them. This is the standard Lopez de Prado correction for overlapping-label leakage.

## Verification

Every splitter is covered by known-value geometry tests plus a 50k-split invariant fuzzer.

**Next:** promote what you trained with [statistical model comparison](/machine-learning/ml-model-comparison.md), or browse the [engine catalog](/engine-runtime/catalog.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.algenta.ai/machine-learning/ml-model-selection.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
