Pro Feature. Requires a Pro or Ultra subscription. Get started at api.mathematicalcompany.com
Graph Analysis
Horizon provides Rust-native graph algorithms for analyzing the structure of prediction market networks. Build correlation graphs, extract minimum spanning trees, detect market communities, and measure contagion risk across interconnected markets.Correlation Graph
Build a weighted graph from pairwise market correlations. Filter by threshold to reveal significant relationships.
Minimum Spanning Tree
Extract the MST to find the most important connections with minimal redundancy.
Community Detection
Identify clusters of related markets using modularity-based community detection.
Contagion Risk
Measure how shocks in one market propagate through the network.
hz.build_correlation_graph
Build a weighted undirected graph from pairwise correlations between market return series. Edges are created between markets whose absolute correlation exceeds the threshold.| Parameter | Type | Default | Description |
|---|---|---|---|
market_ids | list[str] | required | Market identifiers (one per series) |
returns | list[list[float]] | required | Return series for each market (all same length) |
threshold | float | 0.2 | Minimum absolute correlation to create an edge |
MarketGraph Type
| Field | Type | Description |
|---|---|---|
nodes | list[GraphNode] | Market nodes with centrality statistics |
edges | list[GraphEdge] | Undirected edges with correlation weights |
adjacency | list[list[float]] | Full correlation matrix |
density | float | Graph density: edges / max_possible_edges |
GraphNode Type
| Field | Type | Description |
|---|---|---|
market_id | str | Market identifier |
degree | int | Number of connected edges |
strength | float | Sum of absolute edge weights |
centrality | float | Betweenness centrality (0 to 1) |
GraphEdge Type
| Field | Type | Description |
|---|---|---|
source | str | First market ID |
target | str | Second market ID |
weight | float | Correlation value (can be negative) |
distance | float | Correlation distance: sqrt(0.5 * (1 - corr)) |
hz.minimum_spanning_tree
Extract the minimum spanning tree (MST) from a market graph using Kruskal’s algorithm on correlation distances. The MST connects all markets with the minimum total distance, revealing the backbone structure of the correlation network.| Parameter | Type | Description |
|---|---|---|
graph | MarketGraph | A market graph from build_correlation_graph |
MarketGraph containing only the MST edges (N-1 edges for N nodes).
The MST uses correlation distance (smaller = more correlated) as edge weights. Highly correlated markets are connected first. The resulting tree reveals the hierarchical structure of market relationships without cycles.
hz.detect_communities
Detect communities (clusters) of related markets using the Louvain modularity optimization algorithm. Markets within a community are more correlated with each other than with markets in other communities.| Parameter | Type | Default | Description |
|---|---|---|---|
graph | MarketGraph | required | Market correlation graph |
resolution | float | 1.0 | Resolution parameter. Higher = more smaller communities, lower = fewer larger communities. |
Community Type
| Field | Type | Description |
|---|---|---|
id | int | Community index |
members | list[str] | Market IDs in this community |
size | int | Number of markets in the community |
internal_density | float | Fraction of possible internal edges that exist |
modularity_contribution | float | This community’s contribution to total modularity |
hz.contagion_risk
Measure how a shock to one market propagates through the correlation network. Simulates the impact of a large move in a source market on all connected markets using the correlation structure.| Parameter | Type | Default | Description |
|---|---|---|---|
graph | MarketGraph | required | Market correlation graph |
source_market | str | required | Market ID where the shock originates |
shock_size | float | 0.10 | Magnitude of the initial shock |
decay | float | 0.5 | Attenuation factor per network hop (0 to 1) |
Contagion Result Type
| Field | Type | Description |
|---|---|---|
source | str | Source market ID |
impacts | dict[str, float] | Expected impact on each market |
total_impact | float | Sum of all impacts across the network |
max_impact_market | str | Market with the largest expected impact |
max_impact | float | Largest single-market impact |
Pipeline Integration
hz.market_network
Creates a pipeline function that builds and updates a market correlation graph from multiple feeds. Injects network statistics intoctx.params.
| Parameter | Type | Default | Description |
|---|---|---|---|
feeds | list[str] | required | Feed names to include in the network |
lookback | int | 500 | Number of observations to retain per feed |
threshold | float | 0.2 | Minimum correlation for graph edges |
target_feed | str | None | Feed to compute centrality for. None = first feed. |
param_name | str | "network" | Key in ctx.params |
Injected Parameters
| Key | Type | Description |
|---|---|---|
ctx.params["network"]["centrality"] | float | Target market’s betweenness centrality |
ctx.params["network"]["degree"] | int | Target market’s edge count |
ctx.params["network"]["community_size"] | int | Size of the target market’s community |
ctx.params["network"]["density"] | float | Overall graph density |
ctx.params["network"]["n_communities"] | int | Total number of detected communities |
Example: Full Network Analysis
Mathematical Background
Correlation Distance
Correlation Distance
The correlation distance d(i,j) = sqrt(0.5 * (1 - corr(i,j))) maps correlations to a proper metric space. Perfectly correlated assets have distance 0; uncorrelated assets have distance sqrt(0.5) ~ 0.707; perfectly anti-correlated assets have distance 1. This metric satisfies the triangle inequality.
Louvain Community Detection
Louvain Community Detection
The Louvain algorithm optimizes modularity Q = sum over communities [e_c - (a_c)^2], where e_c is the fraction of edges within community c and a_c is the fraction of edge endpoints in c. The algorithm iteratively moves nodes between communities to maximize Q, then aggregates communities and repeats.
Contagion Propagation
Contagion Propagation
Contagion risk is modeled as a breadth-first propagation through the correlation graph. At each hop from the source, the shock is attenuated by the decay factor and scaled by the edge correlation. Markets reachable through multiple paths receive the maximum impact from any single path.