From d195d30d6491741b0a8d95777fc05d00efc71d60 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 2 Nov 2026 08:31:40 +0000 Subject: [PATCH] Fix the VaR quick start install and var-backtest on long samples Review of the VaR model validation release (#4) found three problems. The README quick start told users to pip install the v0.1.0 release wheel. That release predates quantkit.var. Followed as written, the third README doctest block fails with ModuleNotFoundError: No module named 'quantkit.var', and `quantkit var-backtest` is an invalid choice. Cutting a release is out of scope, so the quick start now installs from GitHub and says what the v0.1.0 wheel lacks. CI gains a step that builds the wheel, installs it in a clean environment and runs the README doctests against it. `quantkit var-backtest` printed its table, then exited with status 1 when the sample was too long for null_rejection_rates to enumerate. One case is 20 years of daily returns at 95%: "enumerating 4789 days with up to 369 exceptions is too slow". The command now only leaves out the exact-size note. `quantkit --help` now mentions VaR model validation. Verification: - New test_var_backtest_handles_samples_too_long_for_the_exact_size fails on main (main() returns 1) and passes with the fix. - The v0.1.0 wheel, installed in a fresh venv: the README doctests fail in the VaR block and var-backtest is rejected. The wheel built from this branch, and pip install of git+https://github.com/ult666666/ quantkit.git, pass the README doctests (about 6 s for the CI step). - uv run pytest --doctest-glob=README.md --cov=quantkit: 435 passed, 99.62% coverage. ruff check, ruff format --check and mypy pass. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016h2RvhZFj77hPxFn8TQUqq --- .github/workflows/ci.yml | 11 +++++++++++ CHANGELOG.md | 12 ++++++++++++ README.md | 8 ++++---- src/quantkit/cli.py | 22 ++++++++++++++++------ tests/test_cli.py | 19 +++++++++++++++++++ 5 files changed, 62 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 90617a1..9c17534 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,6 +46,17 @@ jobs: uv run pytest --doctest-glob="README.md" --cov=quantkit --cov-report=term-missing --cov-fail-under=90 + - name: Run the README doctests against the installed wheel + # The README tells users to pip install the package, so check what a wheel + # delivers, not only the source tree (the v0.1.0 wheel lacks quantkit.var). + run: | + uv build --wheel --out-dir "$RUNNER_TEMP/dist" + uv venv "$RUNNER_TEMP/wheel-env" + uv pip install --python "$RUNNER_TEMP/wheel-env/bin/python" "$RUNNER_TEMP"/dist/*.whl + cd "$RUNNER_TEMP" + "$RUNNER_TEMP/wheel-env/bin/python" -m doctest "$GITHUB_WORKSPACE/README.md" + "$RUNNER_TEMP/wheel-env/bin/quantkit" var-backtest --help > /dev/null + - name: Smoke-test the command-line interface run: | uv run quantkit --version diff --git a/CHANGELOG.md b/CHANGELOG.md index becd821..45e616c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,18 @@ All notable changes to this project are documented here. The format follows helper, so single-sample and rolling historical VaR always use the same rule. Results are unchanged. +### Fixed + +- The README quick start installed the v0.1.0 release wheel, which predates + `quantkit.var`: the VaR doctests failed with `ModuleNotFoundError` and + `var-backtest` was not a command. The quick start now installs from GitHub + and says what the release lacks. +- `quantkit var-backtest` printed its table and then exited with an error when + the sample was too long for `null_rejection_rates` to enumerate, for example + 20 years of daily returns at 95%. It now only leaves out the exact size of + the tests. +- `quantkit --help` mentions VaR model validation. + ## 0.1.0 - 2026-10-23 First release. diff --git a/README.md b/README.md index 120cee7..a49a2ed 100644 --- a/README.md +++ b/README.md @@ -40,13 +40,13 @@ Quantitative finance code fails quietly: a sign slip in a Greek, an off-by-one d ## Quick start -quantkit is not on PyPI. Install the library and the `quantkit` CLI from the GitHub release (Python 3.11+): +quantkit is not on PyPI. Install the library and the `quantkit` CLI from GitHub (Python 3.11+): ```bash -pip install https://github.com/ult666666/quantkit/releases/download/v0.1.0/quantkit-0.1.0-py3-none-any.whl +pip install "git+https://github.com/ult666666/quantkit.git" ``` -Each release also attaches the source distribution and SHA-256 checksums. To work on the code, install it from source with [uv](https://docs.astral.sh/uv/): +The [v0.1.0 release](https://github.com/ult666666/quantkit/releases/tag/v0.1.0) attaches a wheel, the source distribution and SHA-256 checksums, but it predates VaR model validation: it has no `quantkit.var` and no `var-backtest` command. To work on the code, install it from source with [uv](https://docs.astral.sh/uv/): ```bash git clone https://github.com/ult666666/quantkit.git @@ -375,7 +375,7 @@ flowchart LR | Metrics | Hand-computed series (drawdown and its duration, CAGR, Sharpe, Sortino, Calmar, hit rate) | | Data | Simulated means, volatilities and correlations match their targets; CSV round trips and specific error messages | -Monte Carlo and other statistical tests use fixed seeds, so they cannot fail by chance; the Hypothesis property tests draw fresh random inputs on every run (200 per test, or 5,000 with `pytest --hypothesis-profile=thorough`). The CI workflow runs ruff, `mypy --strict`, the tests with a 90% coverage floor, the README doctests, CLI smoke tests and the example scripts on Python 3.11, 3.12 and 3.13. +Monte Carlo and other statistical tests use fixed seeds, so they cannot fail by chance; the Hypothesis property tests draw fresh random inputs on every run (200 per test, or 5,000 with `pytest --hypothesis-profile=thorough`). The CI workflow runs ruff, `mypy --strict`, the tests with a 90% coverage floor, the README doctests (against the source tree and against an installed wheel), CLI smoke tests and the example scripts on Python 3.11, 3.12 and 3.13. ## Development diff --git a/src/quantkit/cli.py b/src/quantkit/cli.py index aecc0bb..f0a870a 100644 --- a/src/quantkit/cli.py +++ b/src/quantkit/cli.py @@ -24,6 +24,7 @@ run_backtest, ) from quantkit.data import load_prices_csv, simulate_gbm_prices +from quantkit.exceptions import InvalidParameterError from quantkit.portfolio import ( annualized_mean_returns, compute_returns, @@ -342,11 +343,20 @@ def _cmd_var_backtest(args: argparse.Namespace) -> None: } ) print(table.to_string()) - sizes = null_rejection_rates(n_days, level=args.level) + try: + sizes = null_rejection_rates(n_days, level=args.level) + except InvalidParameterError: + # The inputs were validated above, so this is the enumeration-size limit: a + # long sample with many expected exceptions. The backtest itself stands. + size_note = f"Over {n_days} days at {level}, the exact size of the tests is not computed." + else: + size_note = ( + f"Over {n_days} days at {level}, a correct model fails the 5% tests\n" + f"with probability {sizes.unconditional:.1%} (LR_uc), " + f"{sizes.independence:.1%} (LR_ind) and {sizes.conditional:.1%} (LR_cc)." + ) print( - f"\nLR p-values use chi-squared limits. Over {n_days} days at {level}, a correct model " - f"fails the 5% tests\nwith probability {sizes.unconditional:.1%} (LR_uc), " - f"{sizes.independence:.1%} (LR_ind) and {sizes.conditional:.1%} (LR_cc).\n" + f"\nLR p-values use chi-squared limits. {size_note}\n" f"Z2 p-values compare with {args.es_simulations:,} paths simulated from each method's " f"own forecasts (seed {args.seed})." ) @@ -397,8 +407,8 @@ def _add_option_arguments(parser: argparse.ArgumentParser) -> None: def _build_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser( prog="quantkit", - description="Options pricing, portfolio optimization and backtesting. " - "Educational and research software, not investment advice.", + description="Options pricing, portfolio optimization, backtesting and VaR model " + "validation. Educational and research software, not investment advice.", ) parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}") commands = parser.add_subparsers(dest="command", required=True) diff --git a/tests/test_cli.py b/tests/test_cli.py index 366d0b4..8df79e9 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -118,6 +118,25 @@ def test_var_backtest_one_method_one_ticker( assert "n/a (99% only)" in out +def test_var_backtest_handles_samples_too_long_for_the_exact_size( + tmp_path: Path, capsys: pytest.CaptureFixture[str] +) -> None: + # Twenty years of daily data at 95%: too many expected exceptions for + # null_rejection_rates to enumerate. The backtest must still succeed and only + # leave out the exact size of the tests. + path = tmp_path / "long.csv" + assert main(["simulate", "--output", str(path), "--days", "5040", "--seed", "3"]) == 0 + capsys.readouterr() + args = ["var-backtest", str(path), "--method", "historical", "--level", "0.95"] + assert main([*args, "--es-simulations", "99"]) == 0 + captured = capsys.readouterr() + assert captured.err == "" + assert "backtest: 4789 days" in captured.out + assert "Kupiec LR_uc p-value" in captured.out + assert "the exact size of the tests is not computed" in captured.out + assert "a correct model fails the 5% tests" not in captured.out + + def test_var_backtest_reports_bad_inputs( prices_csv: Path, capsys: pytest.CaptureFixture[str] ) -> None: