Files
WeHub Mirror ba5b1d8398
Tests / changes (push) Has been cancelled
Tests / lint (push) Has been cancelled
Tests / guards (push) Has been cancelled
Tests / test-unit (push) Has been cancelled
Tests / ${{ matrix.name }} (push) Has been cancelled
Tests / cs-${{ matrix.case-study }} (push) Has been cancelled
Tests / test-py312 (push) Has been cancelled
Tests / test-neo4j (push) Has been cancelled
Tests / test-benchmark (push) Has been cancelled
WeHub snapshot of c2c7b6ca46223f2522af92054ac1c53327ee2799
2026-08-07 17:42:00 +08:00
..

Chapter 23: Knowledge Graphs for Financial AI

Vector retrieval made evidence-grounded financial question answering practical, but similarity search still operates on isolated chunks. Questions that depend on ownership chains, supplier networks, contagion paths, or the timing of relationship changes require explicit structure rather than fuzzy matches across text. Knowledge graphs represent entities as nodes, relationships as typed edges, and provenance as properties — giving analytical queries something to traverse, audit, and reuse.

This chapter covers the construction, query, feature-engineering, and evaluation discipline that make financial knowledge graphs useful rather than novel. It treats graph building as a governance problem (identity, schema, provenance), introduces Graph RAG as deterministic relational retrieval, turns graph structure into leakage-aware ML features, connects the statistical-financial-networks literature to the explicit-KG workflow, and closes with a three-timestamp model for temporal integrity and the engineering choices that keep systems auditable.

Learning Objectives

  • Distinguish financial questions that genuinely require graph structure from those better served by tabular databases or vector retrieval
  • Design a compact, typed, and auditable financial knowledge graph with stable entity identity, finite relationship vocabularies, and edge-level provenance
  • Build and validate LLM-assisted extraction pipelines that convert disclosures into replayable graph objects while controlling schema, duplication, and temporal consistency
  • Explain how Graph RAG differs from vector retrieval and implement safe relational query workflows using constrained text-to-query generation and deterministic database execution
  • Transform graph structure into leakage-aware machine learning features — topology, crowding, concentration, and cross-graph interaction terms
  • Evaluate explicit knowledge graphs, statistical financial networks, and learned graph representations pragmatically for forecasting, portfolio construction, and risk analysis
  • Apply a three-timestamp framework and disclosure-time cutoff rules to prevent temporal leakage in graph queries, feature generation, and backtests
  • Make sound engineering choices about graph databases, ontology scope, query safety, and schema evolution for production-oriented financial systems

Chapter Sections

# Title Core Idea
23.1 When Relational Structure Unlocks Financial Insight Graphs earn their overhead when the question is multi-hop, structurally crowded, or temporally evolving
23.2 Constructing Financial Knowledge Graphs LLM extraction plus identity, schema, and provenance contracts that make the graph replayable and auditable
23.3 Graph RAG: Deterministic Relational Reasoning A five-stage architecture that delegates relational logic to the database and language generation to the LLM
23.4 From Graphs to Machine Learning Features Centrality, crowding, co-ownership, and cross-graph interactions become tabular features for downstream models
23.5 Financial Networks: From Correlations to Portfolios How classic MST and correlation-network methods complement explicit KGs, and where GNNs fit in practice
23.6 Temporal Integrity and Leakage-Safe Evaluation The three-timestamp model (event, disclosure, extraction) and a protocol for cutoff-safe features
23.7 Building a KG-Ready Pipeline: Engineering Decisions Engine selection, ontology scope, text-to-Cypher safety, and schema versioning for production

Notebooks

Graph Construction

Notebook What It Teaches
01_sp100_sec_download Loads and inspects 10-K and 8-K filings for S&P 100 companies from SEC EDGAR, verifies coverage and text quality before graph extraction.
02_supply_chain_kg_construction Extracts supplier, customer, and competitor relationships from 10-K filings using Qwen2.5-7B, resolves entities, loads triples to Neo4j, and visualizes the resulting supply-chain graph.
05_institutional_holdings_kg Builds an institutional-holdings property graph from EDGAR 13F filings, demonstrates shared-holding and crowding queries, and computes Jaccard co-ownership similarity.
08_8k_event_extraction Extracts structured event quadruples from SEC 8-K filings with the FinReflectKG critic-corrector reflection loop, then loads the event graph to Neo4j.

Retrieval and Evaluation

Notebook What It Teaches
03_graph_rag_qa Implements controlled text-to-Cypher question answering over the 13F holdings graph, enforcing read-only schema validation, dated queries, and row limits on every generated query.
04_rag_comparison_benchmark Benchmarks graph retrieval against embedding-based retrieval on real 13F holdings, measuring support recall and retrieval-token cost across direct-lookup and multi-entity question types.

Graph-Derived Features and Networks

Notebook What It Teaches
09_knowledge_graph_features Combines supply-chain topology (PageRank, betweenness, HHI) with 13F crowding and cross-graph interactions to produce a tabular feature matrix suitable for gradient-boosted models.
10_network_portfolio_construction Builds correlation networks and minimum spanning trees from US equities, ranks assets by centrality, constructs network-diversified portfolios, and runs a contagion simulation.
06_gnn_feature_engineering Trains a simplified GAT on a stock correlation network to generate relational embeddings, then compares a tabular-only ridge regression to a hybrid model that concatenates the learned embeddings.

Temporal Integrity

Notebook What It Teaches
07_dynamic_kg_temporal Builds leakage-safe temporal snapshots from the 8-K event graph by separating event, disclosure, and extraction time, then measures relationship churn and centrality drift while verifying no post-cutoff leakage.

Running the Notebooks

# From the repository root
uv run python 23_knowledge_graphs/<notebook>.py

# Test mode (reduced data via Papermill)
uv run pytest tests/test_notebooks.py -v -k "23_knowledge_graphs"

# Headless (no display)
MPLBACKEND=Agg PLOTLY_RENDERER=json uv run python 23_knowledge_graphs/<notebook>.py

A running Neo4j instance is required for 02_supply_chain_kg_construction, 05_institutional_holdings_kg, 08_8k_event_extraction, 03_graph_rag_qa, and 07_dynamic_kg_temporal. Local LLM extraction for the supply-chain and 8-K pipelines uses Qwen2.5-7B via a vLLM-compatible endpoint.

Dependencies

Upstream

  • Chapter 4 (Fundamental and Alternative Data) — 13F institutional-holdings loader
  • Chapter 10 (Text Feature Engineering) — NER and filing parsing patterns
  • Chapter 22 (RAG for Financial Research) — retrieval-augmented generation foundation

Downstream

  • Chapter 12 (Advanced Models for Tabular Data) — consumes cross-graph features
  • Chapter 14 (Latent Factor Models) — consumes co-ownership and crowding features
  • Chapter 24 (Autonomous Agents) — uses structured retrieval, memory, and evidence tracking

Chapter-specific libraries

  • neo4j — graph database driver (Cypher client)
  • networkx — graph analytics and MST construction
  • torch_geometric — Graph Attention Network implementation
  • edgartools — SEC EDGAR filings ingestion
  • sentence-transformers — embedding baseline for the RAG comparison

References