redsandr/cognifi
GitHub: redsandr/cognifi
CogniFi 是一个基于规则的 NLP 系统,专门用于从印尼散户投资者文本中实时检测 FOMO、损失厌恶和确认偏差等认知偏差,并在交易前提供可解释的行为干预。
Stars: 1 | Forks: 0
# CogniFi
**面向印尼散户投资者的实时认知偏差检测与行为干预。**
[](https://arxiv.org/abs/XXXX.XXXXX)
[](https://www.python.org/)
[](LICENSE)
[](https://huggingface.co/datasets/redsandr/cognifi-investor-bias)
CogniFi 从投资者生成的文本中检测 FOMO、loss aversion 和 confirmation bias,并在交易执行前提供基于摩擦的行为干预。基于规则的设计且完全可解释,每个输出都可追溯到特定的规则和关键词匹配。
## 快速开始
```
git clone https://github.com/redsandr/cognifi.git
cd cognifi
pip install -r requirements.txt
export GEMINI_API_KEY="your-key-here"
streamlit run app.py
```
## 结果
| 系统 | 准确率 | FOMO F1 | LA F1 | CB F1 | NONE F1 |
|--------|----------|---------|-------|-------|---------|
| 多数类基线 | 29.3% | 0.0% | 0.0% | 0.0% | 45.4% |
| Gemini 3 Flash zero-shot (中等推理) | 76.4% | 76.5% | 78.0% | 79.2% | 72.9% |
| **CogniFi (本研究)** | **94.7%** | **94.0%** | **95.8%** | **96.2%** | **93.6%** |
在包含 150 个案例的分层留出集上,**比 Gemini 3 Flash 高出 18.3pp**。
**语码转换发现:** Gemini 的准确率在单语印尼语上从 80.0% 降至语码转换样本的 64.1%(-15.9pp)。CogniFi 在相同样本上保持 91.6% 的准确率。这种差距是分布上的,而非架构上的,像 `masih sempet ga` 和 `serok bareng` 这样的词汇在任何通用训练语料库中都是缺失的。
## 工作原理
CogniFi 运行一个六层基于规则的 pipeline:
```
Input text
│
├── Layer 1: Early exit (analytical/educational text → NONE)
├── Layer 2: Price context (Yahoo Finance real-time data)
├── Layer 3: Keyword scoring (1,307 terms, 13 categories)
├── Layer 4: Contextual adjustments (57 rules)
├── Layer 5: Confidence thresholding
└── Layer 6: Signal construction
│
▼
Bias label + confidence + counter-evidence + friction prompt
```
基于规则的设计是刻意的。在行为干预的情境下,当干预最为需要时,无法解释的输出往往会被直接驳回。
## 数据集
包含 1,193 个来自五个平台、真实的印尼散户投资者帖子的标注样本。
| 平台 | 训练集 | % |
|----------|----------|---|
| Stockbit | 882 | 84.6% |
| X.com | 85 | 8.1% |
| Reddit | 32 | 3.1% |
| Threads | 44 | 4.2% |
**语言分布:**
| 类别 | n | % |
|----------|---|---|
| 单语印尼语 | 844 | 70.7% |
| 句内语码转换 | 307 | 25.7% |
| 单语英语 | 42 | 3.5% |
语言检测使用混合的 FastText LID + 领域词典重叠。
标注者间一致性:**Cohen's Kappa = 0.7815**(实质性一致,Landis & Koch 1977),由两名具备领域资质的标注者完成(n=100 个样本)。
## 仓库结构
```
cognifi/
├── bias_detector.py # 6-layer detection pipeline
├── keywords.py # 1,307-term domain lexicon
├── counter_evidence.py # Historical backtesting engine
├── intervention.py # Friction and Pre-Mortem prompts
├── ticker_extractor.py # Ticker detection
├── price_fetcher.py # Yahoo Finance data layer
├── fundamental_data.py # IDX fundamentals (10 tickers)
├── llm.py # Gemini API wrapper
├── news_context.py # News sentiment layer
├── rag.py # ChromaDB RAG engine
├── app.py # Streamlit web app
├── config.py # Configuration
├── evaluate_final.py # Held-out evaluation
├── gemini_baseline.py # Gemini baseline script
├── test_bias_accuracy.py # Training accuracy script
└── data/
├── training_v2.json # 1,043 training cases with provenance
└── held_out_TEST_ONLY.json # 150 held-out cases (labels withheld)
```
## 复现结果
```
# Training set 准确率
python test_bias_accuracy.py
# Gemini 3 Flash baseline (需要 API key)
python gemini_baseline.py
```
为了防止数据泄露,留出集的答案未包含在内。评估结果记录在论文中。
## 反证引擎
对于 10 个受支持的 IDX 股票代码,该引擎会呈现与检测到的偏差相关的历史先例。输出的是基础概率,而非预测。
| 股票代码 | FOMO 事件 | 修正概率 | LA 事件 | 回复率 | 平均天数 |
|--------|:---:|:---:|:---:|:---:|:---:|
| BBCA.JK | — | — | 75 | 100% | 2 |
| BBRI.JK | 4 | 50% | 76 | 100% | 4 |
| TLKM.JK | — | — | 97 | 100% | 3 |
| ASII.JK | — | — | 95 | 100% | 8 |
| BMRI.JK | — | — | 78 | 100% | 3 |
| UNVR.JK | 10 | 50% | 111 | 95% | 7 |
| GOTO.JK | 10 | 20% | 44 | 95% | 7 |
| BREN.JK | 17 | 18% | 20 | 90% | 5 |
| EMTK.JK | 18 | 22% | 113 | 100% | 1 |
| SIDO.JK | 6 | 50% | 93 | 95% | 10 |
`—` = 没有达到 >20% 的 5 天 FOMO 阈值的事件。系统会返回明确的 `insufficient_data` 响应,而不是从不充分的样本中生成输出。
## License
MIT。详见 [LICENSE](LICENSE)。
数据集已发布用于研究。所有样本均为来自印尼散户投资者社区(Stockbit、X.com、Reddit、Threads)的真实用户生成内容。
标签:Kubernetes, Python, Streamlit, 云计算, 无后门, 行为金融学, 规则引擎, 认知偏差检测, 访问控制, 逆向工具, 金融科技