The English version of this documentation was translated and refined with the assistance of Large Language Models (LLMs) to ensure clarity and accuracy.
This project aims to analyze the sentiment of financial news using Natural Language Processing (NLP) techniques and investigate the correlation between these sentiment indicators and major U.S. stock indices (S&P 500, NASDAQ, Dow Jones, Russell 2000).
It combines Data Engineering (automated scraping pipeline), Deep Learning (FinBERT), and Econometrics (Dynamic Logit Model) to verify the predictive power of market sentiment.
Using a Dynamic Logit Model, we found that news sentiment significantly improves prediction accuracy for tech-heavy indices.
-
NASDAQ-100 (QQQ): Prediction accuracy improved by +10% (Baseline 55%
$\to$ Model 65%). -
Statistical Significance: The sentiment coefficient was significant (
$p=0.091$ ), confirming that yesterday's news sentiment positively correlates with today's market rise.
Sentiment indices show strong correlation with market movements, especially in volatile periods.
| NASDAQ-100 (QQQ) | S&P 500 (SPY) |
|---|---|
![]() |
![]() |
- Language: Python 3.8+
- NLP Framework: Hugging Face Transformers (FinBERT), PyTorch
- Data Engineering: Selenium, BeautifulSoup, yfinance API
- Statistical Analysis: Statsmodels (Dynamic Logit), NumPy, Pandas
- Visualization: Matplotlib, Seaborn
01_scraper.ipynb: Scrapes headlines/content from Google News (handles IP rotation).02_bert_training.ipynb: Loads/Fine-tunes the FinBERT model.03_inference.ipynb: Calculates daily Sentiment Scores.04_analysis.ipynb: Integrates data, runs Logit regressions, and generates plots.
-
Environment Setup:
git clone [https://github.com/YOUR_USERNAME/REPO_NAME.git](https://github.com/YOUR_USERNAME/REPO_NAME.git) pip install -r requirements.txt
-
Execution Flow: Run the notebooks in numerical order (
01->04).💡 Pro Tip: It is highly recommended to run
01_scraper.ipynbon Google Colab. Since Colab assigns a new Dynamic IP each session, this effectively prevents IP blocking from target news websites.
Click to view: Sentiment Index Construction (Math)
1. Discrete Classification (Bullish Ratio)
Forces headlines into Positive/Negative/Neutral.
2. Continuous Probability Score
Uses Softmax probabilities from FinBERT logits to preserve confidence levels.
Click to view: Dynamic Logit Model Results (Full Table)
We utilized a Dynamic Logit Model to predict market direction (Up/Down):
Summary Statistics:
| Ticker | Model Accuracy | Lift | Sentiment Coeff ( |
P-value |
|---|---|---|---|---|
| NASDAQ-100 (QQQ) | 65.00% | +10% | 14.61 | 0.091* |
| S&P 500 (SPY) | 65.00% | +5% | 14.62 | 0.107 |
| Dow Jones (DIA) | 60.00% | +5% | 9.06 | 0.197 |
| Small Cap (IWM) | 60.00% | 0% | 7.99 | 0.281 |
Note: P-value < 0.1 indicates significance at the 10% confidence level.
本專案利用自然語言處理 (NLP) 技術分析財經新聞情緒,並結合計量經濟模型(Dynamic Logit Model),探討情緒指標與美股主要指數(S&P 500, NASDAQ, Dow Jones)之預測相關性。
本專案實作了從自動化爬蟲、BERT 模型推論到時間序列分析的完整資料管線。
透過動態 Logit 模型驗證,我們發現加入情緒指標能顯著提升對科技股指數的預測準確率。
-
NASDAQ-100 (QQQ):預測準確率提升 10% (基準 55%
$\to$ 模型 65%)。 -
統計顯著性:情緒係數顯著 (
$p=0.091$ ),證實昨日新聞情緒與今日市場上漲機率呈正相關。
| NASDAQ-100 (QQQ) | S&P 500 (SPY) |
|---|---|
![]() |
![]() |
- 語言: Python 3.8+
- NLP 模型: FinBERT (Hugging Face Transformers)
- 資料工程: Selenium, BeautifulSoup, Google Colab (IP Rotation)
- 統計分析: Statsmodels (Dynamic Logit), Pandas
- 視覺化: Matplotlib, Seaborn
01_scraper.ipynb: 爬取 Google News 財經新聞(處理反爬蟲機制)。02_bert_training.ipynb: 載入 FinBERT 預訓練模型進行微調。03_inference.ipynb: 計算每日新聞情緒分數。04_analysis.ipynb: 整合股價數據,執行回歸分析與視覺化。
-
安裝依賴:
pip install -r requirements.txt
-
執行順序: 請依序執行
notebooks/中的 Jupyter Notebook (01->04)。💡 實戰技巧: 強烈建議在 Google Colab 上執行
01_scraper.ipynb。由於 Colab 每次啟動都會分配新的動態 IP,這能有效規避目標新聞網站的 IP 封鎖 (IP Blocking) 機制。
點擊展開:情緒指標建構公式 (Math)
模式一:離散標籤 (Bullish Ratio)
模式二:連續機率分數 (Continuous Probability)
利用 Softmax 機率保留模型信心程度:
點擊展開:Dynamic Logit 模型詳細報表
我們使用時間序列 Logit 模型預測市場漲跌:
實證結果摘要:
| 指數 | 模型準確率 | 提升幅度 (Lift) | 情緒係數 ( |
P-value |
|---|---|---|---|---|
| NASDAQ-100 (QQQ) | 65.00% | +10% | 14.61 | 0.091* |
| S&P 500 (SPY) | 65.00% | +5% | 14.62 | 0.107 |
| Dow Jones (DIA) | 60.00% | +5% | 9.06 | 0.197 |
| Small Cap (IWM) | 60.00% | 0% | 7.99 | 0.281 |
註:P-value < 0.1 代表在 10% 信心水準下顯著。

