first commit

This commit is contained in:
2026-11-20 18:36:00 +08:00
commit cf0ff03d55
92 changed files with 358404 additions and 0 deletions
@@ -0,0 +1,109 @@
# 台股跨學科量化研究 — BDD 行為規格與事前驗屍(Pre-mortem)設計文件
> **記錄日期**:2026-11-20
> **規範依據**:[DBB_Pre_Moreem_template.md](file:///Users/ancel/Desktop/project/eco_analyse/README_Standard_Template/DBB_Pre_Moreem_template.md)
> **同步位置**:`README_exe_commend/report/DBB/DBB_Pre_Moreem_20261120.md` 及 `report/DBB/DBB_Pre_Moreem_20261120.md`
---
## 第一部分:BDD 與 Pre-mortem 融合規格(Gherkin Feature 摘要)
本專案目前鎖定三個核心 Feature,各自對應特定資料階段與 L3 災難防禦邊界(每個 Feature 至多 3 個 `[Pre-mortem]` 情境):
### 1.1 `F01_data_audit_and_repair.feature`(Phase 1:資料修復與零和還原)
- **對應檔案**:[F01_data_audit_and_repair.feature](file:///Users/ancel/Desktop/project/eco_analyse/README_exe_commend/features/F01_data_audit_and_repair.feature)
- **涵蓋情境**:
- `[正例]`:民國年與 `2024-02-29` 例外修復、排序錯位重整、`2597..3219` 游程去重對齊(與大台隱含指數交叉驗證 $r = 0.999463 > 0.998$)。
- `[正例]`:利用期貨市場零和恆等式計算第 4 參與者「非三大法人(隱含散戶淨額)」。
- `[Pre-mortem]`(L3-1):末日(`2025-03-03`)全市場未平倉為 `0` 的快照污染 $\Delta\text{OI}$ 與隱含指數分母除零防護。
- `[Pre-mortem]`(L3-2):原始試算表錯誤滾動公式(`後5日` 除錯分母、`當日高低差異` 誤為實體差)全面攔截與重算隔離。
### 1.2 `F02_twse_taifex_supplement.feature`(Phase 2:TWSE / TAIFEX 官方缺失資料補齊)
- **對應檔案**:[F02_twse_taifex_supplement.feature](file:///Users/ancel/Desktop/project/eco_analyse/README_exe_commend/features/F02_twse_taifex_supplement.feature)
- **涵蓋情境**:
- `[正例]`:解析 TWSE `MI_5MINS_HIST`(每日 OHLC)與 `FMTQIK`(成交股數、成交金額、成交筆數)JSON 並合併。
- `[正例]`:分批(每批 $\le 30$ 日)以 `POST` 擷取 TAIFEX `pcRatioDown` 之 `CP950` CSV,完成跨批去重。
- `[Pre-mortem]`(L3-1):外部補抓收盤價與大台隱含指數交叉比對若相關係數 $< 0.98$ 或 MAE $> 500$ 點,立即阻止覆蓋乾淨主表(防髒資料污染)。
- `[Pre-mortem]`(L3-2):官方 API 回傳非預期 HTML、空欄位或分母為 0,阻斷半殘 CSV 寫入並保留代理序列。
### 1.3 `F03_ptt_hits_sentiment.feature`(Phase 2:PTT Stock 輿論爬蟲與 HITS 演算法)
- **對應檔案**:[F03_ptt_hits_sentiment.feature](file:///Users/ancel/Desktop/project/eco_analyse/README_exe_commend/features/F03_ptt_hits_sentiment.feature)
- **涵蓋情境**:
- `[正例]`:帶入 `over18=1` Cookie 解析 PTT Stock 文章作者與 `.push` 推/噓/箭頭邊,迭代計算收斂的 Authority、Hub 分數與輿論剪刀差 $\Delta_{\text{HA}}$。
- `[Pre-mortem]`(L3-1):水軍自推(`u == v` 自迴圈)與洗板刷推權重截斷(Sybil Defense),防止單一或少數節點壟斷圖中心性。
- `[Pre-mortem]`(L3-2):休市日或無有效推文產生全零鄰接矩陣時,防止幂迭代除零崩潰並安全回傳零向量。
- `[Pre-mortem]`(L3-3):伺服器連續回傳 HTTP `429`/`503` 或斷線超限時,啟動斷路器保護並原子保存已完成分頁。
### 1.4 `F04_ptt_10yr_history_and_industry_topics.feature`(Phase 3:PTT 十年頁碼回溯與產業話題抽取)
- **對應檔案**:[F04_ptt_10yr_history_and_industry_topics.feature](file:///Users/ancel/Desktop/project/eco_analyse/README_exe_commend/features/F04_ptt_10yr_history_and_industry_topics.feature)
- **涵蓋情境**:
- `[正例]`:由文章 URL 內嵌之 `M.<TIMESTAMP>.A` 轉換為台北時間西元日期,透過內插二分搜尋定位近十年各年度里程碑頁碼。
- `[正例]`:抽取 12 大台股產業標籤與 6 大宏觀籌碼話題標籤(英文邊界守護防止 `Taiwan` 誤判為 `AI`),並按日期彙算熱門文章 HITS 剪刀差序列。
- `[Pre-mortem]`(L3-1):遇到歷史刪文空頁與 `r-list-sep` 後方置底舊公告時,必須排除置底雜訊並強制收縮二分搜尋步數避免死循環。
- `[Pre-mortem]`(L3-2):長時段歷史抓取中斷或觸發斷路器時,必須透過原子暫存檔保存 Checkpoint 進度與去重集合。
### 1.5 `F05_cross_market_forum_sampling_and_taxonomy.feature`(Phase 4:跨國取樣、語料分類與跨時區對齊)
- **對應檔案**:[F05_cross_market_forum_sampling_and_taxonomy.feature](file:///Users/ancel/Desktop/project/eco_analyse/README_exe_commend/features/F05_cross_market_forum_sampling_and_taxonomy.feature)
- **涵蓋情境**:
- `[正例]`:分離 `data/market_data`、`data/corpus` 與 `data/features_aligned` 目錄,以五語辭典統一標記 12 產業 × 6 話題。
- `[正例]`:分別解析美(Stocktwits/Reddit/RSS)、韓(Naver)、日(Yahoo 揭示板)、中(股吧/雪球)並建構跨市場 HITS 指標。
- `[Pre-mortem]`(L3-1):台北時間 `13:30` 收盤後發表之貼文強制歸入下一個有效交易日(防跨時區未來函數洩漏)。
- `[Pre-mortem]`(L3-2):多語編碼(`CP949`/`EUC-KR`/`UTF-8`)相容處理與反爬蟲驗證頁攔截,防止污染語料庫。
### 1.6 `F06_graph_laozi_lhs_and_falsification.feature`(Phase 5:圖論 × 老子四時 × LHS-MC × 反證公式)
- **對應檔案**:[F06_graph_laozi_lhs_and_falsification.feature](file:///Users/ancel/Desktop/project/eco_analyse/README_exe_commend/features/F06_graph_laozi_lhs_and_falsification.feature)
- **涵蓋情境**:
- `[正例]`:跨商品 Kruskal MST、Fiedler $\lambda_2$、Forman-Ricci 曲率與有效轉移熵 $\text{TE}_{X \to Y}$。
- `[正例]`:老子「四時循環」馬可夫狀態機、「可道」IC 衰減率與「常道」反轉張力;LHS + 平穩區塊自助法假設檢定。
- `[Pre-mortem]`(L3-1):動態剔除晚期上市缺值商品與零變異節點,並以 Tikhonov 正則化防止特徵值分解崩潰。
- `[Pre-mortem]`(L3-2):嚴格因果計算與 Purged + Embargoed Walk-Forward 隔離,杜絕未來資訊滲漏。
- `[Pre-mortem]`(L3-3):三層聯立反證公式觸發後套用冷卻期與雙閾值遲滯恢復機制,兼顧極端回撤控制與防止頻繁抖動。
### 1.7 `F07_visual_compendium_and_citations.feature`(Phase 6:圖表文分章總覽與五大類出處稽核)
- **對應檔案**:[F07_visual_compendium_and_citations.feature](file:///Users/ancel/Desktop/project/eco_analyse/README_exe_commend/features/F07_visual_compendium_and_citations.feature)
- **涵蓋情境**:
- `[正例]`:由 Phase 1–5 成果自動繪製 6 張繁體中文 PNG 圖表,並驗證分章與附錄五大出處連結完整性。
- `[Pre-mortem]`(L3-1):攔截零位元組/非 PNG 魔數損毀圖檔,避免輸出空白或壞掉視覺報告。
- `[Pre-mortem]`(L3-2):攔截失效圖片路徑或遺漏「資料/學說/公式/演算法/研究方法」出處標題,確保報告零斷鏈。
### 1.8 `F08_taifex_futures_live_update_202611.feature`(Phase 7:官方源頭更新至 `2026/11/20` 與重算驗證)
- **對應檔案**:[F08_taifex_futures_live_update_202611.feature](file:///Users/ancel/Desktop/project/eco_analyse/README_exe_commend/features/F08_taifex_futures_live_update_202611.feature)
- **涵蓋情境**:
- `[正例]`:分批抓補 TAIFEX `futContractsDateDown`、`pcRatioDown` 與 TWSE 至 `2026/11/20` 並重算圖論/老子/LHS/反證消融與 `CH05`。
- `[Pre-mortem]`(L3-1):偵測期交所契約金額單位突變(元 vs 千元)自動除 `1000.0` 標準化,並攔截 HTML/DateTime 錯誤頁。
- `[Pre-mortem]`(L3-2):強制以官方完整數據覆蓋 `futures_oi.xlsx` 殘留的 `2025-03-03` 全零未平倉列,杜絕污染 $\Delta\text{OI}$。
---
## 第二部分:程式碼層級高追溯性註解映射表(`src/` 全函式對照)
所有實作程式碼皆集中於 `src/`,並強制符合 9 欄位 Docstring 格式(`[使用工具]`、`[設計目的]`、`[程式說明]`、`[修改日期]`、`[修改概要]`(10–40 字)、`[輸入參數]`、`[輸出結果]`、`[關聯測試]`、`[工作日誌]`):
| 模組 | Class / Function / Method | 關聯 BDD Feature & Scenario | 關聯工作日誌 |
| :--- | :--- | :--- | :--- |
| [phase1_data_audit_and_eda.py](file:///Users/ancel/Desktop/project/eco_analyse/src/phase1_data_audit_and_eda.py) | `parse_roc_date` | `F01 / [正例] 民國與閏年解析` | `work_record_20261120.md` |
| `phase1_data_audit_and_eda.py` | `repair_taiex_spot` | `F01 / [正例] 去重修復 & [Pre-mortem] 公式污染` | `work_record_20261120.md` |
| `phase1_data_audit_and_eda.py` | `enrich_taifex_futures` | `F01 / [正例] 零和散戶 & [Pre-mortem] 零未平倉` | `work_record_20261120.md` |
| `phase1_data_audit_and_eda.py` | `build_daily_feature_table` | `F01 / [Pre-mortem] 末日未平倉代理切換` | `work_record_20261120.md` |
| `phase1_data_audit_and_eda.py` | `audit_merged_workbook`, `safe_corr`, `compute_summary_stats`, `main` | `F01 / [正例] 交叉驗證與關聯統計` | `work_record_20261120.md` |
| [phase2_data_supplement_and_hits.py](file:///Users/ancel/Desktop/project/eco_analyse/src/phase2_data_supplement_and_hits.py) | `TwseSpotSupplementer` (`parse_roc_date_str`, `parse_twse_number`, `fetch_month_ohlc`, `fetch_month_fmtqik`, `fetch_range_and_merge`) | `F02 / [正例] TWSE解析 & [Pre-mortem] 髒資料攔截` | `work_record_20261120.md` |
| `phase2_data_supplement_and_hits.py` | `TaifexPcRatioSupplementer` (`parse_pc_csv`, `fetch_chunk`, `fetch_full_range`) | `F02 / [正例] CP950分批解析 & [Pre-mortem] 欄位異常` | `work_record_20261120.md` |
| `phase2_data_supplement_and_hits.py` | `PttStockHitsPipeline` (`fetch_board_page`, `parse_board_list`, `parse_article_pushes`, `build_interaction_graph`, `compute_hits_scores`, `run_offline_verification`, `run_live_pipeline`) | `F03 / [正例] HITS計算 & [Pre-mortem] 水軍/零矩陣/斷路器` | `work_record_20261120.md` |
| [phase3_ptt_10yr_history_and_topics.py](file:///Users/ancel/Desktop/project/eco_analyse/src/phase3_ptt_10yr_history_and_topics.py) | `PttTenYearExplorer`, `_PttRichBoardParser`, `compile_keyword_patterns` | `F04 / [正例] 十年頁碼定位與話題抽取 & [Pre-mortem] 二分防迴圈與斷點續傳` | `work_record_20261120.md` |
| [phase4_cross_market_sampler_and_taxonomy.py](file:///Users/ancel/Desktop/project/eco_analyse/src/phase4_cross_market_sampler_and_taxonomy.py) | `MultilingualCorpusTaxonomy`, `CausalTimezoneAligner`, `UsStocktwitsRedditSampler`, `KrNaverBoardSampler`, `JpYahooBbsSampler`, `CnEastmoneyXueqiuSampler`, `CrossMarketSamplingPipeline` | `F05 / [正例] 雙軸分層與四國取樣 & [Pre-mortem] 跨時區防洩漏與編碼驗證` | `work_record_20261120.md` |
| [phase5_core_models_and_falsification.py](file:///Users/ancel/Desktop/project/eco_analyse/src/phase5_core_models_and_falsification.py) | `MarketGraphEngine`, `LaoziDynamicsEngine`, `LhsMonteCarloValidator`, `PopperFalsificationEngine`, `WalkForwardBenchmarkSuite` | `F06 / [正例] 圖論老子LHS檢定 & [Pre-mortem] 退化正則化/防未來洩漏/反證遲滯` | `work_record_20261120.md` |
| [phase6_visual_report_generator.py](file:///Users/ancel/Desktop/project/eco_analyse/src/phase6_visual_report_generator.py) | `ResearchFigureGenerator`, `CompendiumIntegrityAuditor`, `run_phase6_visual_compendium_pipeline`, `main` | `F07 / [正例] 六大圖表生成與分章總覽稽核 & [Pre-mortem] 損毀圖檔/斷鏈出處攔截` | `work_record_20261120.md` |
| [phase7_live_202611_update_and_reanalysis.py](file:///Users/ancel/Desktop/project/eco_analyse/src/phase7_live_202611_update_and_reanalysis.py) | `TaifexFuturesInstitutionalSupplementer`, `LiveMarketDatasetUpdater`, `run_offline_verification`, `run_live_202611_pipeline`, `main` | `F08 / [正例] 官方補齊至2026年底與重算 & [Pre-mortem] 單位防呆與20250303零OI覆蓋` | `work_record_20261120.md` |
| [test_bdd_scenarios.py](file:///Users/ancel/Desktop/project/eco_analyse/src/test_bdd_scenarios.py) | `TestDocstringCompliance`, `TestF01DataAudit` ~ `TestF08TaifexFuturesLiveUpdate202611` | `F01`–`F08` 全情境自動化驗證 | `work_record_20261120.md` |
---
## 第三部分:Pre-mortem 錯誤分級與防禦邊界矩陣(SOP)
依據決策樹「Q1 是否導致內外狀態不一致? $\to$ Q2 能否簡單重試自癒? $\to$ Q3 是否造成不可逆髒資料或重大模型失真?」,本專案之錯誤分級邊界定義如下:
| 等級 | 具體錯誤類型(本專案實務案例) | 處理手段 | BDD 文件化要求 |
| :--- | :--- | :--- | :--- |
| **L1 基礎例外<br>(Routine)** | 單次 HTTP 讀取超時(Timeout)、SSL 暫時握手失敗、單篇文章已遭刪文回傳 404 | **程式碼層級解決**<br>`try-except` 搭配 Exponential Backoff 重試與 404 跳過 | **不寫入 Pre-mortem** |
| **L2 業務邊界<br>(Alternate)** | 該月份為農曆年期間僅少量交易日、TWSE 當日資料尚未更新回傳空列表、PTT 某篇新聞無推文 | **常規條件判斷**<br>跳過空日並記錄警示日誌,不計入特徵均值 | **一般 Scenario 涵蓋即可** |
| **L3 毀滅災難<br>(Pre-mortem)** | ① **髒資料覆蓋**(如 `tw_stock.xlsx` 3 倍重複或外部 API 抓錯指數);<br>② **未來函數洩漏與除零崩潰**(末日全市場 `OI=0` 或誤差 7,285 點之 `VLOOKUP`);<br>③ **圖拓樸被水軍污染或零矩陣除零**(Sybil 自推壟斷 Authority/Hub 權重) | **架構層級防禦**<br>交叉驗證相關性守門($r \ge 0.98$)、原子寫入(寫入前暫存校驗)、對角線自連歸零+交互權重上限 Clip、斷路器熔斷 | **必須撰寫 `[Pre-mortem]`**<br>各 Feature 嚴格收斂於 2–3 個致命情境 |