2009年末には、大型5銘柄の最大の方向が分散の80.4%を占め、共分散行列の条件数は45.7でした。2026年8月には同じ方向が30.3%、条件数は3.7です。この測定を6,447個のローリングウィンドウで繰り返すのに、配置がよければ0.043秒、悪ければ11.6秒かかります。このような仕事をどう組み立て、どうコストを見積もるのでしょうか。
考え方をやさしい言葉で
ここまでの流れ。 これが最後のトピックで、他のすべてを使います。仕事の段階は、そのままスキルの段階です。BLASの3レベルが各手順のコストを決め(トピック1)、共分散はタイル化した dgemm(2)で、配置を正直に宣言したデータに対して行い(3)、固有値分解(4)を実運用の流儀で呼び(5)、その答えを条件数で評価し(6)、全体をコアに分散し(7)、ライブラリは表から選び(8)、Cからも呼べます(9)。
- 段階スキルのどこを使うか
- load_returns_matrix価格からリターンへ日付を揃える・素のPython
- covariance_matrixレベル3の積トピック1・2
- eigen_report固有値と条件数トピック4・5・6
- pipeline_report多数のコアでのコストトピック7・8
実際の価格から揃ったリターンへ
data フォルダには、AAPL、JPM、XOM、JNJ、KOの日足が入っています。開始日がそれぞれ違うので、最初の仕事は5つに共通する日付だけを残し、終値を単純リターンに変えることです。PATHS は5つのファイルのリストです。
# Five price files in, one table of aligned daily returns outdates, rows = load_returns_matrix(PATHS)print(len(rows), "days of returns from", dates[0], "to", dates[-1], "|", len(rows[0]), "stocks")1print("last day:", [f"{r * 100:+.2f}%" for r in rows[-1]])
6696 days of returns from 2000-01-04 to 2026-08-21 | 5 stocks last day: ['-0.63%', '+0.01%', '-0.63%', '+1.07%', '+0.66%']
- 16,696日分で、5つのファイルは2000年1月4日から重なり、各行に銘柄ごとのリターンが1つずつあります。
共分散とその形
# The covariance of the last 250 days: one dgemm, charged to the counterblas = Blas()cov = covariance_matrix(blas, rows[-250:])print("flops", blas.flops, "words", blas.words, "| flops per word", round(blas.intensity(), 2))1report = eigen_report(blas, cov)print("share of the variance, smallest to largest direction (%):", [round(s * 100, 1) for s in report["share"]])print("condition number:", round(report["condition"], 2), "| after the eigen step: flops", blas.flops)2print("largest direction (AAPL, JPM, XOM, JNJ, KO):", [round(v, 2) for v in report["top_vector"]])
flops 12500 words 2525 | flops per word 4.95 share of the variance, smallest to largest direction (%): [8.2, 16.3, 19.7, 25.5, 30.3] condition number: 3.67 | after the eigen step: flops 12666 largest direction (AAPL, JPM, XOM, JNJ, KO): [0.64, 0.33, -0.68, -0.07, -0.08]
- 1最初のトピックと同じ、2,525ワードに12,500フロップです。積は安く、強度は約5です。
- 2条件数は4未満で、行列は良条件です。固有値分解は約15桁まで信頼できます。
年を追って形はどう変わるか
選んだ年の最終営業日で終わる250日間で測定を繰り返します。市場が揃って下げると、1つの方向(すべてが市場と一緒に動く)が分散を飲み込み、行列は扱いにくくなります。
# The largest direction's share of the variance, and the condition number, at the end of selected yearslast_day = {}for position, date in enumerate(dates): last_day[date[:4]] = positionfor year in ("2004", "2008", "2009", "2013", "2017", "2020", "2024"): end = last_day[year] window = covariance_matrix(Blas(), rows[end - 249:end + 1]) result = eigen_report(Blas(), window) print(year, f"largest direction {result['share'][-1] * 100:5.1f}% condition {result['condition']:5.1f}")1
2004 largest direction 62.5% condition 10.6 2008 largest direction 64.7% condition 39.5 2009 largest direction 80.4% condition 45.7 2013 largest direction 46.9% condition 10.2 2017 largest direction 39.2% condition 5.0 2020 largest direction 70.8% condition 27.7 2024 largest direction 35.7% condition 6.4
- 12009年と2020年が目立ちます。暴落では銘柄が揃って下がり、1つの方向がリスクの71〜80%を担い、条件数は2017年のような平穏な年の5〜9倍になります。
ローリング計算のコスト見積もり
1ウィンドウは12,666フロップとごく小さく、仕事にはそれが6,447個あります。小さな独立した仕事がたくさんあるときの対処は、トピック7ですでに出ました。コアごとに1スレッドのワーカーを1つです。pipeline_report は配置の探索を行い、8ワーカーがそれぞれ8スレッドを要求する素朴な選択と比べます。
# What the rolling job costs on an 8-core machine (4 billion flops per second per core, 50 microseconds per thread)windows = len(rows) - 249report = pipeline_report(windows, 5, 250, 8, 4e9, 5e-5)print("flops per window:", report["flops_per_window"], "| total:", f"{report['flops_per_window'] * windows:.3g}")print("best layout (workers, threads):", report["layout"], "->", round(report["seconds"], 4), "s")print("8 workers x 8 threads:", round(report["naive_seconds"], 2), "s |", round(report["speedup"]), "times slower")1print("one thread, one worker:", round(batch_time(windows, report["flops_per_window"], 1, 1, 8, 4e9, 5e-5), 3), "s")
flops per window: 12666 | total: 8.17e+07 best layout (workers, threads): (8, 1) -> 0.0429 s 8 workers x 8 threads: 11.62 s | 271 times slower one thread, one worker: 0.343 s
- 1最良の配置は1スレッドのワーカー8つです。素朴な64スレッドを8コアで動かすと271倍かかり、トピック7の予測どおりです。
コストモデルが示すのはどこを見るべきかであり、時計が何と言うかではありません。実機では、NumPyで共分散と固有値の手順を数個のウィンドウに対し、OMP_NUM_THREADS を1とコア数にして、1ワーカーと複数ワーカーで実行し、最も速い配置を採用してください。答えの形(小さな仕事が多いなら1スレッドのワーカーを多数)は頑健ですが、正確な数値はそうではありません。
タスクの内容
- 1`load_returns_matrix`
csv.DictReaderで列名を指定してファイルを読み、共通の日付を残し、日付と単純リターンの表を返します。 - 2`covariance_matrix` と `eigen_report`平均を引いてT − 1で割ったあとの
dgemm1回による共分散と、eighに基づくレポート(固有値、分散の割合、最大の固有ベクトル、条件数)です。 - 3`pipeline_report`スレッドのモデルでローリング計算のコストを見積もります。1ウィンドウのフロップ数、最良の配置、かかる秒数、素朴な配置のコストです。
ラボ:採点される内容
4タスク · 100点load_returns_matrixで揃ったリターンを読み込む25点covariance_matrixで共分散を作る25点eigen_reportで特徴づける25点pipeline_reportで仕事のコストを見積もる25点