Backtest Determinism And Reproducibilityのロゴ

Backtest Determinism And Reproducibility

コミュニティ
HimanshuJ16
backtest-determinism-and-reproducibility

同じバックテストコードとデータを使っても実行ごとにP&LやSharpeが異なり、最適化の信頼性が損なわれる場合に使用します。決定論的なシード設定、入力順序の厳格なソート、実時間の読み取りを置き換えるシミュレーション時計、実行間の差異検出に対応します。

概要

公開者HimanshuJ16
リポジトリAlgo-Trading-Skills
スキル名backtest-determinism-and-reproducibility
スター
50
フォーク
7
同梱ファイル
5
ライセンスApache-2.0
リンク
  • Markdown形式の指示

    モデルが必要に応じて読み込むSKILL.mdファイルです。関連するリクエストがあったときだけトークンを消費します。

  • どのLLMでも利用可能

    AIスキルはプロバイダー固有のコードではなく、通常のMarkdownなので、GPT、Claude、Gemini、Grok、ローカルモデルで利用できます。

  • 5個の同梱ファイル

    モデルが作業中に読み取れるスクリプト、テンプレート、参考資料です。ファイルは読み取り専用で、実行されることはありません。

  • オープンソース

    HimanshuJ16がGitHubで公開しています。インストールする前にソースを確認してください。

インストール

AIスキル「Backtest Determinism And Reproducibility」をTypingMindにインストールすれば、どのLLMでも利用できます。SKILL.mdを読み取れる別のAIエージェントに追加することもできます。

1

TypingMindにインストール

TypingMindはGitHubフォルダから直接スキルをインストールします。SKILL.mdを読み取り、リソースファイルをまとめてローカルに保存します。

  1. アプリを開き、「プラグイン」→「スキル」に移動します。
  2. 「GitHubからインストール」を選択します。
  3. 以下のスキルフォルダのURLを貼り付けて確定します。
    https://github.com/HimanshuJ16/Algo-Trading-Skills/tree/main/skills/backtest-determinism-and-reproducibility
  4. スキルを利用したいチャットごとに、そのスキルを有効にしてください。
「プラグイン」→「スキル」→「スキルを追加」→「GitHubのURLから」を選択し、フォルダのURLを貼り付けて「続行」を押します。
2

別のAIエージェントにインストール

Agent Skills形式を読み取れるAIエージェントなら、このスキルを利用できます。フォルダをそのAIエージェントのスキルディレクトリにコピーしてください。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/HimanshuJ16/Algo-Trading-Skills.git /tmp/Algo-Trading-Skills
mkdir -p .claude/skills
cp -r /tmp/Algo-Trading-Skills/skills/backtest-determinism-and-reproducibility .claude/skills/backtest-determinism-and-reproducibility
コピー後、新しいスキルを読み込むためにClaude Codeを再起動してください。

TypingMindで使用

TypingMindのチャットでBacktest Determinism And Reproducibilityを有効にするだけで、あとはモデルが対応します。名前と説明がシステムプロンプトに含まれ、関連するリクエストがあると、モデルが指示の全文を自動で読み込みます。手動で呼び出す必要はなく、実際に使用されるまでトークンを消費しません。

関連するリクエストがあるとすぐに、モデルが自動的にBacktest Determinism And Reproducibilityを読み込みます。

どのAIモデルでも利用可能

AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、Backtest Determinism And Reproducibilityは作成時に想定されたモデルに縛られません。TypingMindに一度インストールすれば、ご自身のAPIキーを使って、GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama、またはご自身で実行するローカルモデルで利用できます。

  • 必要なときだけ読み込み

    システムプロンプトには名前と説明だけが含まれます。関連するリクエストが初めてあったときに指示を取得するため、スキルを使わない間は指示の読み込みにトークンを消費しません。

  • チャットの途中でモデルを切り替え

    スキルはコードではなく指示なので、モデルを切り替えてもそのまま使えます。次のモデルも同じSKILL.mdを読み込みます。

スキルの指示

モデルが読み込むSKILL.mdの内容です。スキルはモデルが従う指示なので、インストールする前に確認してください。

When to Use

Invoke this skill when building backtest simulation engines or CI regression suites. If backtest runs produce varying P&L curves or Sharpe ratios across identical code and data, parameter optimization becomes impossible to trust — you cannot tell an improvement from noise. Non-determinism comes from unseeded RNGs, set iteration order, unordered event streams, wall-clock reads, and floating-point accumulation order.

What This Can and Cannot Guarantee

It cannot guarantee bit-identical results in general, and no tool can. PyTorch's reproducibility guidance states plainly that "completely reproducible results are not guaranteed across PyTorch releases, individual commits, or different platforms," and that results may differ between CPU and GPU "even when using identical seeds." The same holds for BLAS thread counts, SIMD code paths, and library versions.

Determinism is a property of a pinned environment — same platform, same library versions, same thread configuration. This skill does two things within that envelope:

  1. Eliminates the controllable sources: seeding, event ordering, clock reads.
  2. Detects divergence when it happens, by comparing exact float bits.

Point 2 is the part that must not be compromised. Earlier versions of this skill rounded every float to 6 decimals before hashing, which meant the canonical non-determinism signature — sum([0.1]*10) (0.9999999999999999) versus 0.1*10 (1.0) — was reported as bit-identical. A detector that cannot see the thing it was built to detect is worse than no detector, because it produces confident false assurance.

When NOT to Use

  • As a tamper-evident audit record. The checksum is an unkeyed SHA256: anyone can edit a trade log and recompute it. For an authenticated record see backtest-audit-trail-for-regulatory-review.
  • To compare runs across different environments. Differing Python/NumPy/BLAS versions or thread counts will legitimately diverge; that is an environment-pinning problem, not something a checksum can fix.
  • To validate strategy correctness. A perfectly reproducible backtest can be reproducibly wrong — see lookahead-bias-elimination.

Prerequisites

  • A master integer seed (e.g. 42).
  • PYTHONHASHSEED set in the environment before the interpreter starts.
  • Pinned library versions and a fixed thread configuration for any run you intend to compare.

Workflow

  1. Set PYTHONHASHSEED before launching Python. This is the trap that catches people: hash randomization is fixed at interpreter startup, so os.environ["PYTHONHASHSEED"] = "0" inside running code does nothing while looking like it works. Launch as PYTHONHASHSEED=0 python backtest.py. check_hash_seed() reports whether the current process is safe; it cannot fix it.
  2. Seed the RNGs — and know what seeding misses. apply_master_seeds() seeds random, NumPy's legacy global RandomState, and torch when present. NumPy documents np.random.seed as a legacy function and recommends a dedicated Generator; code calling np.random.default_rng() draws fresh OS entropy and stays non-deterministic no matter how often you seed the global. Use make_numpy_generator() and thread it through explicitly.
  3. Sort the event stream by (timestamp, symbol, sequence_id). All three keys are required — a missing timestamp is rejected rather than defaulted to 0.0, which would silently move that event to the front. Duplicate sort keys are also rejected: Python's sort is stable, so tied events would keep their input order and the result would depend on how the file happened to be read.
  4. Replace wall-clock reads with SimulatedClock. Inject it rather than monkey-patching time.time() — patching a global affects every library in the process, including ones whose correctness depends on real time. The clock refuses to move backwards, which surfaces an unsorted stream immediately.
  5. Checksum with exact float bits and compare runs. audit_reproducibility() reports is_bit_identical and, on failure, first_divergence_index so you can go straight to the offending trade instead of staring at two mismatched hex strings.

Full procedure: see references/workflows.md. Standards and sources: see references/standards.md. Printable pre-flight checklist: see assets/checklist.md.

Common Pitfalls

  • Rounding before hashing. Any tolerance applied before comparison erases exactly the sub-ulp divergences that signal non-determinism. float_precision exists for callers who explicitly want tolerant comparison, and it logs a warning saying what it costs.
  • Setting PYTHONHASHSEED from inside Python. It has no effect. The assignment succeeds, the variable reads back correctly, and set iteration order stays random.
  • Assuming dicts and sets behave alike. Dicts have preserved insertion order since Python 3.7 and are deterministic. Sets are not — iterating a symbol universe as a set gives a different order per process. With PYTHONHASHSEED at 0, 1, and 2, {'AAPL','MSFT','GOOG','TSLA'} iterates in three different orders. Iterate a sorted list.
  • Seeding only the NumPy global. np.random.seed() does not affect any Generator created by default_rng().
  • NaN in a trade log. json.dumps renders every NaN as the same token, so two runs that both corrupted to NaN hash identically and are declared reproducible. Non-finite values are now rejected outright.
  • Unsigned notional mistaken for P&L. Summing quantity * price across a round trip gives gross turnover, not equity: BUY 100@150 then SELL 100@155 sums to 30,500 while the realized P&L is 500. Cash flow must carry the side's sign.
  • Monte Carlo without a fixed seed. Slippage or execution-timing perturbations reseeded per run make every comparison meaningless.
  • Treating a matching checksum as proof of correctness. It proves two runs agreed, nothing more.

Verification

  • Run python -m unittest discover -s skills/backtest-determinism-and-reproducibility/scripts — 40 tests, 100% pass rate.
  • The test that matters: feed sum([0.1]*10) and 0.1*10 as prices for otherwise identical runs and confirm is_bit_identical is False. Pre-2.0 this returned True.
  • Run a backtest twice with identical inputs and confirm matching checksums; change the master seed and confirm the checksum shifts.
  • Confirm a missing timestamp or a duplicate (timestamp, symbol, sequence_id) raises DeterminismError rather than sorting silently.
  • Confirm SimulatedClock.advance_to() rejects a backwards jump.

Related Skills

  • walk-forward-validation-setup
  • monte-carlo-strategy-robustness-testing
  • vectorized-vs-event-driven-backtest-tradeoffs
  • backtest-audit-trail-for-regulatory-review — for an authenticated audit record, which this skill's unkeyed checksum is not
  • reproducible-ml-training-pipelines — the same problem for model training
  • dependency-pinning-and-reproducible-builds — pinning the environment determinism depends on

同梱ファイル

スキルが読み込まれている間、モデルは必要に応じてこれらを読み取ります。読み取り可能なファイルとして提供され、実行されることはありません。

よくある質問

AIスキル「Backtest Determinism And Reproducibility」では何ができますか?

同じバックテストコードとデータを使っても実行ごとにP&LやSharpeが異なり、最適化の信頼性が損なわれる場合に使用します。決定論的なシード設定、入力順序の厳格なソート、実時間の読み取りを置き換えるシミュレーション時計、実行間の差異検出に対応します。

TypingMindでBacktest Determinism And Reproducibilityを使うメリットは何ですか?

一度インストールすれば、どのモデルでも使えるからです。Backtest Determinism And Reproducibilityはプロバイダー固有のコードではなく、通常のMarkdownで記述されているため、同じスキルをGPT-5、Claude、Gemini、Grok、ローカルモデルで利用できます。チャットの途中でモデルを切り替えても、そのまま使い続けられます。TypingMindは自分のAPIキーで動作するため、シート単位のサブスクリプションではなく、プロバイダーに直接料金を支払います。スキルとチャットは自分のストレージに保持されます。

TypingMindにBacktest Determinism And Reproducibilityをインストールするにはどうすればよいですか?

TypingMindで「プラグイン」→「スキル」→「GitHubからインストール」を開き、https://github.com/HimanshuJ16/Algo-Trading-Skills/tree/main/skills/backtest-determinism-and-reproducibilityを貼り付けてください。TypingMindがSKILL.mdを読み込み、関連ファイルをまとめて、チャットごとに有効にできるスキルとしてインストールします。

Backtest Determinism And Reproducibilityを使えるAIモデルはどれですか?

TypingMindに接続したどのモデルでも利用できます。AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、GPT、Claude、Gemini、Grok、ローカルモデルのいずれも、リクエストの内容がこのスキルの用途に合ったときに、このスキルを読み込めます。

Backtest Determinism And Reproducibilityで使えるAIモデルの数に制限はありますか?

数に制限はありません。スキルに対応したモデルであれば、GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llamaなど、どのモデルでもBacktest Determinism And Reproducibilityを利用できます。すべてTypingMind上で、自分のAPIキーを使って利用できます。

AIスキル「Backtest Determinism And Reproducibility」は無料ですか?

はい。HimanshuJ16がApache-2.0ライセンスのもとでGitHubに公開しています。ご利用のAIプロバイダーに支払うのは、使用したトークンの料金だけです。

AIスキルとは何ですか?

AIスキルは、特定のタスクの実行方法をAIモデルに教える、再利用可能な指示のセットです。オープンなAgent Skills形式に従い、名前と説明を含むSKILL.mdファイルに加え、モデルが必要とするスクリプト、テンプレート、参照ファイルで構成されます。モデルはリクエストがスキルに合致したときだけ指示を読み込むため、インストール済みのスキルも、使うまでは費用がかかりません。

AIスキルは、プラグインやMCPサーバーとどう違いますか?

プラグインやMCPサーバーは、モデルが呼び出せる新しいツールを提供します。ツールとは、どこかで実行され、結果を返すコードです。一方、AIスキルは、タスクへの取り組み方、従うべき手順、望ましい出力の例といった知識や進め方をモデルに伝えます。スキルは通常のMarkdownで記述されているため、サーバー、APIキー、実行環境は不要で、どのモデルでも利用できます。

すべて見る

今すぐ自分専用のAIワークスペースを作成

ニュースレターに登録すると、新機能や今後のプレゼント企画のお知らせが届きます👇