Skip to content

feat: 優化暴力搜索,使用 bitmask 向量化加速分析 - #7

Merged
sstklen merged 4 commits into
sstklen:mainfrom
yongjer:main
Mar 17, 2026
Merged

sstklen merged 4 commits into
sstklen:mainfrom
yongjer:main

Conversation

@yongjer

@yongjer yongjer commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI review requested due to automatic review settings March 17, 2026 05:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

此 PR 將「大跌大漲」分析腳本中的暴力搜尋改為以 Python 大整數 bitmask 表示特徵命中集合,透過位元運算(AND / popcount)加速在訓練/驗證集上枚舉 2~3 條件組合並篩選候選規則。

Changes:

  • 新增每日推文文字 _text_cache,避免重複 join/lower 以加速特徵計算。
  • 以 bitmask 預先編碼 train/test 的特徵與目標標籤集合,將組合枚舉改為位元 AND + popcount。
  • 將 2-combo 與 3-combo 搜尋改為巢狀迴圈搭配剪枝與統計輸出。

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread analysis_12_big_moves.py Resolved
Comment thread analysis_12_big_moves.py Outdated
# valid_mask = 所有有 next_td label 的日期

def build_masks(dates, next_labels, feature_names, features_dict):
n = len(dates)
Comment thread analysis_12_big_moves.py Outdated
Comment on lines +415 to +417
# --- 預計算單特徵的 matched mask(AND valid)---
tr_single = [tr_feat_mask[useful[i]] & tr_valid for i in range(n_feat)]
te_single = [te_feat_mask[useful[i]] & te_valid for i in range(n_feat)]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment thread analysis_12_big_moves.py Resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@yongjer

yongjer commented Mar 17, 2026

Copy link
Copy Markdown
Contributor Author

@copilot open a new pull request to apply changes based on the comments in this thread

@sstklen sstklen left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yongjer Welcome back! Two PRs in two days — you're on a roll! 🔥

Overview

Smart approach: bitmask encoding turns O(dates) per combo into a single & + popcount. This is the pure Python path — no GPU needed, no PyTorch dependency. Complements PR #6 nicely (GPU for massive batches, bitmask for quick local runs).

The _text_cache is a great find too — that repeated ' '.join(p['content'].lower() ...) was probably eating more time than anyone realized.

One thing to fix before merge

Result parity concern (also flagged by Copilot):

tr_single = [tr_feat_mask[useful[i]] & tr_valid for i in range(n_feat)]

By ANDing with tr_valid here, dates without a next-trading-day label are excluded from train_total. In the original loop, those dates counted toward total but never toward hits — so the hit rate denominator was larger.

Example: if 10 dates match a feature combo, but 2 have no next-td label:

  • Original: total=10, hits=6 → 60%
  • Bitmask: total=8, hits=6 → 75% ← inflated

This isn't just cosmetic — it changes which rules survive the 50%/40% thresholds. We learned this lesson the hard way in PR #6 (the < 10 vs >= 5 saga 😄).

Fix options:

  1. Don't AND with valid in tr_single — compute tr_total from tr_match directly (without valid), then compute tr_hits from tr_match & tr_valid & tr_target[target]
  2. Or: AND with valid (current behavior) but document it as an intentional change ("exclude unlabeled dates from denominator") and verify result parity

I'd suggest option 1 for exact parity, then we can decide if option 2 is actually better.

Quick wins (from Copilot, agree with all)

# What Fix
1 popcount = int.bit_count ✅ Saw you already asked Copilot to apply this
2 Remove unused n = len(dates) One-liner cleanup
3 Flip build_masks inner loop to iterate feat.keys() instead of all features Nice micro-optimization

Benchmark ask

Once the parity fix is in, could you share before/after timing? Curious how bitmask compares to:

  • Original CPU loop (~60s)
  • Your PyTorch GPU version (0.07s on RTX 4060 Ti)

My guess: bitmask lands somewhere in the 1-5s range? That would make it the best "no dependencies" option.

Looking forward to the fix! 🚀

— TK

@yongjer

yongjer commented Mar 17, 2026

Copy link
Copy Markdown
Contributor Author

@sstklen The latest version should fix, but just in case, could u check the parity for me?

The performance benchmarks will do it asap

@yongjer

yongjer commented Mar 17, 2026

Copy link
Copy Markdown
Contributor Author

@sstklen The latest version should fix, but just in case, could u check the parity for me?

The performance benchmarks will do it asap

On m1 mac, the result as below, io bound. cuz it's not with gpu, I don't think the number on gpu will differ
0.41s user 0.04s system 57% cpu 0.770 total

@sstklen

sstklen commented Mar 17, 2026

Copy link
Copy Markdown
Owner

@yongjer Parity test done! ✅

Metric Original Bitmask (PR #7) Match?
Total combos tested 268,088 268,088 ✅ Exact
Candidates found 0 0 ✅ Exact
Time ~60s 0.25s 🚀 240x faster

The & tr_valid fix in 7539cc05 nailed it — totals now include all matching dates (with or without next-td label), exactly like the original.

Performance summary across all three versions:

Version Time Speedup Deps
Original (CPU loop) ~60s 1x None
This PR (bitmask) 0.25s 240x None
PR #6 (PyTorch GPU) 0.07s 857x torch + CUDA

The bitmask version is the best "zero dependency" option — 240x faster with pure Python. Amazing work! 🚀

Merging now.

— TK

@sstklen
sstklen merged commit a9533a2 into sstklen:main Mar 17, 2026
@sstklen

sstklen commented Mar 17, 2026

Copy link
Copy Markdown
Owner

@yongjer Curious — what was your experience running the brute-force analysis locally?

A few things I'd love to hear:

  1. What caught your eye? When you first ran the original version, what made you think "this could be faster"?
  2. Why bitmask over other approaches? You already did PyTorch GPU in PR feat: 增加 PyTorch GPU 加速支持,優化暴力搜索性能 #6 — what made you go for a pure Python optimization this time?
  3. Any surprises in the data? Running the analysis yourself, did you notice anything interesting about Trump's posting patterns or the big-move predictions?

Two PRs in two days, each attacking the same problem from a completely different angle (GPU vs bitmask). That's rare. 🙏

— TK

@yongjer

yongjer commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor Author

@yongjer Curious — what was your experience running the brute-force analysis locally?

A few things I'd love to hear:

  1. What caught your eye? When you first ran the original version, what made you think "this could be faster"?
  2. Why bitmask over other approaches? You already did PyTorch GPU in PR feat: 增加 PyTorch GPU 加速支持,優化暴力搜索性能 #6 — what made you go for a pure Python optimization this time?
  3. Any surprises in the data? Running the analysis yourself, did you notice anything interesting about Trump's posting patterns or the big-move predictions?

Two PRs in two days, each attacking the same problem from a completely different angle (GPU vs bitmask). That's rare. 🙏

— TK

@sstklen I have zero experience, but with AI, nothing is impossible.

  1. Since there are only 12 analyses, I will run them all once and then identify the slowest one. The current speed is unacceptable. I will then ask the AI if there is any way to speed up the process. The AI may suggest a few options, which you can research to identify the pros and cons of each.

  2. I used Gemini CLI for the first PR and Antigravity with Opus for the second, so the implementation may differ. However, my core concept is that it can be done in under a second, regardless of the method — if it can be done in under a second, it's a good implementation.

  3. Not yet — maybe I need more tokens or a text embedding model to do it. Wait for this to achieve 5,000+ GitHub stars to get a free Claude Code or Codex for open source😆.

By the way, if there's an ELON CODE, it might be necessary to fully optimise the algorithm to save money on the VPS.

LeoLiao2025 pushed a commit to LeoLiao2025/trump-code that referenced this pull request Mar 24, 2026
feat: 優化暴力搜索,使用 bitmask 向量化加速分析
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants