This repository contains a reproducible, human-in-the-loop benchmark for auditing how multiple language models explain fraud vs. no-fraud decisions on real SEC EDGAR filings. The focus is no longer on generic model training; instead we provide:
- Dataset preparation that samples balanced excerpts from EDGAR statements.
- Multi-model inference + verification that runs FinBERT variants (and future API models) while checking every prediction against the ground truth.
- Rich explainability artifacts (SHAP, LIME, attention, permutation feature importance, counterfactual search) saved as JSON + composite PNGs.
- Blind human-evaluation surveys—single-file HTML packets with embedded visualizations so reviewers judge explanations without seeing labels.
- Which explanation style helps humans most when deciding whether a disclosure is fraudulent?
- How do legacy vs. modern FinBERT-class models differ in both accuracy and explanation quality?
- Can the verification layer + survey feedback highlight when models over-trust spurious cues?
├── data_ingestion/ # EDGAR parsing & dataset utilities
├── models/ # FinBERT classifiers + shared prompt templates + CV tooling
├── scripts/ # Small CLIs (e.g., hold-out splitter)
├── xai/ # Comparator, explanation runner, visualization helpers
├── evaluation/ # Survey template + HTML generator
├── archive/legacy_gpt4/ # Archived GPT‑4 comparison framework + analyzer
├── explanations/ # (Generated) per-model artifacts: sample_i.json/.png, metrics, reports
├── results/ # Optional analysis outputs / notebooks
├── research_proposal.md # Living design document for the study
└── README.md # You are here
Legacy training notebooks and the original LLM comparison framework live under archive/ so the main root stays focused on the XAI experiment.
-
Dataset Prep –
python evaluation/create_evaluation_dataset.py- Draws a stratified sample (default 10 fraud / 10 non-fraud) from
real_edgar_data/real_edgar_financial_statements.csv. - Output:
evaluation_dataset.csv
- Draws a stratified sample (default 10 fraud / 10 non-fraud) from
-
FinBERT Fine-tuning (CV) –
python -m models.finbert_finetune_cv- Stratified K-fold CV with class-weighted trainer, partial unfreezing, and full metrics per fold.
- Produces checkpoints under
finbert_finetune_weighted*/fold_<k>pluscv_metrics.csv&cv_predictions.csv.
-
Hold-out Split (optional) –
python scripts/create_holdout.py- Carves out 30 samples (stratified) for unbiased XAI evaluation while leaving the remainder for CV.
- Outputs
evaluation_dataset_cv.csv+evaluation_holdout.csv.
-
Explanation Generation –
python xai/generate_explanations.py- Iterates over
MODEL_CONFIGS(FinBERT fine-tuned checkpoints by default). - Logs prediction correctness per sample (verification layer) and aggregates accuracy/precision/recall/F1 + confusion matrix counts in both per-model JSON and
explanations/model_metrics_summary.csv. - Saves explanations to
explanations/<model_id>/sample_{i}.json/.png.
- Iterates over
-
Blind Survey Creation –
python evaluation/generate_survey.py- For each model folder inside
explanations/, randomly selects ~10 samples, base64-embeds the visualization, and writes a self-contained HTML file (surveys/xai_survey_<model_id>.html). - Each card asks participants to make a fraud/no-fraud decision, rate confidence, score clarity/actionability/financial relevance, describe evidence, and state trust delta—without revealing the true label.
- For each model folder inside
-
Human Evaluation & Analysis
- Distribute HTML packets (per model or combined if desired).
- Compare responses against
verification_report.csvand explanation metadata to reason about human vs. model alignment.
python -m venv xai_env # or your preferred env name
# PowerShell
xai_env\Scripts\Activate.ps1
pip install -r requirements_xai.txtIf you plan to run any API models (e.g., GPT-4o or Llama 3.1 70B via a hosted endpoint), set the corresponding credentials via environment variables before executing the explanation script.
# 0. (Optional) Recreate the hold-out split
python scripts/create_holdout.py \
--dataset evaluation_dataset.csv \
--holdout-size 30
# 1. Run FinBERT CV on the training portion
python -m models.finbert_finetune_cv \
--dataset evaluation_dataset_cv.csv \
--output-dir finbert_finetune_weighted_cv130 \
--freeze-base --train-last-n-layers 6 \
--class-weight-no-fraud 1.0 \
--class-weight-fraud 3.0
# 2. Generate explanations + surveys on the hold-out set
python pipeline.py \
--skip-dataset \
--dataset-output evaluation_holdout.csv \
--explanations-dir explanations_holdout_fold2 \
--samples-per-class 200 \
--survey-sample-count 10Artifacts you should see:
explanations/
├── finbert_base/ # points to the best CV fold (e.g., finbert_finetune_weighted/fold_2)
│ ├── sample_0.json
│ ├── sample_0.png
│ ├── ...
│ ├── verification_report.csv
│ └── metrics.json
├── verification_report_all_models.csv
└── model_metrics_summary.csv
surveys/
└── xai_survey_finbert_base.html
| Step | Command / Script | Notes |
|---|---|---|
| CV fine-tune | python -m models.finbert_finetune_cv |
Handles stratified folds, class weights, partial unfreezing, metrics CSV/JSON. |
| Hold-out creation | python scripts/create_holdout.py |
Stratified 30-sample set (15/15) for unbiased explanation review. |
| Pipeline run | python pipeline.py --skip-dataset --dataset-output evaluation_holdout.csv ... |
Loads the best CV checkpoint (configured in xai/generate_explanations.py) and regenerates explanations/surveys on the hold-out samples. |
By default xai/generate_explanations.py points to finbert_finetune_weighted/fold_2, the top-performing fold from the most recent CV run. Update MODEL_CONFIGS if you select a different checkpoint.
| Metric | How it is computed | Why it matters here |
|---|---|---|
| Accuracy | (TP + TN) / Total |
Overall fraction of correct calls (fraud + no-fraud). |
| Precision | TP / (TP + FP) |
Of the disclosures flagged as fraud, how many were actually fraud? High precision prevents overwhelming analysts with false alarms. |
| Recall | TP / (TP + FN) |
Of the truly fraudulent disclosures, how many did we catch? High recall is critical for fraud detection coverage. |
| F1 | Harmonic mean of precision and recall | Single score balancing false positives vs. false negatives—useful when classes are imbalanced and we cannot sacrifice one side. |
These metrics appear in:
finbert_finetune_weighted*/cv_metrics.csvfor each CV fold.explanations_holdout_fold2/*/verification_report.csvfor hold-out verification.- README tables detailing the workflow so reviewers interpret the numbers correctly.
- Every prediction stores
is_prediction_correct, numeric confusion counts, and probabilities. - CSV + JSON metrics let you spot regressions quickly before involving human annotators.
- Combined summaries (
verification_report_all_models.csv) make cross-model comparison trivial. - Pass
--auto-survey(default) toxai/generate_explanations.pyor usepipeline.pyto automatically refresh HTML survey packets after explanations finish.
| Method | Purpose |
|---|---|
| SHAP | Token-level additive contributions for local faithfulness |
| LIME | Interpretable surrogate model with finance-aware kernel |
| Attention Visualization | Layer/head averages projected onto the input tokens |
| Permutation Importance | Mask tokens/groups to measure probability drop |
| Counterfactual Search | Remove sentences/tokens until the prediction flips (TextFooler-style) |
All methods are orchestrated via xai/xai_comparator.py, which also renders a unified PNG grid for survey consumption.
The HTML template (evaluation/xai_survey_template.html) embeds each visualization as a base64 image so participants only need a single file. Questions per card:
- Fraud decision (binary radio, required).
- Confidence slider (0–100).
- Likert ratings for Clarity, Actionability, Financial Relevance.
- Free-text justification of the cues they used.
- Trust delta (Higher / Lower / Unchanged).
Responses can be collected via email attachment, shared drive, Qualtrics, or any workflow that supports HTML uploads.
- Automate the dataset → explanations → survey orchestration under a single CLI entrypoint.
- Integrate GPT-4o or Llama 3.1 70B API client once access is available.
- Add combined survey mode (mixed models) for fully blind cross-model studies if needed.
- Document recommended analysis scripts for aggregating survey responses.
See LICENSE for usage terms. For questions or collaboration proposals, please open an issue or reach out directly.