Programming lesson
Graph Algorithms in Finance and Transportation: A Summer 2025 Guide
Explore graph theory through stock market correlations and Uber traffic data. Learn MST, community detection, and sector prediction with real-world examples.
Graph Algorithms in Finance and Transportation: A Summer 2025 Guide
Graph theory is everywhere in 2026—from social networks to AI models like ChatGPT's knowledge graph. This tutorial builds on the ECE 232E Summer 2025 project, applying graph algorithms to stock market correlations and Uber traffic data. You'll learn how to construct correlation graphs, extract minimum spanning trees (MST), run community detection, and predict stock sectors. We'll also connect these concepts to current trends like algorithmic trading and ride-sharing optimization.
1. Stock Market Correlation Graphs
In finance, investors often buy or sell stocks in the same sector together—for example, tech stocks during an AI boom. The project uses log-normalized returns to measure correlation between stocks. Why log-normalized? Because it makes the distribution more symmetric and reduces the impact of extreme price changes, similar to how AI models normalize data for better performance.
The correlation coefficient ρij ranges from -1 to 1. A value of 1 means perfect positive correlation (both stocks move together), -1 means perfect negative correlation (one goes up, the other down), and 0 means no linear relationship. Edge weights are computed as wij = √(2(1-ρij)), which maps correlations to distances: high correlation gives small weight, low correlation gives large weight.
2. Minimum Spanning Tree (MST) and Community Detection
The MST connects all stocks with the minimum total edge weight, revealing the strongest correlations. In 2026, MSTs are used in network design for 5G and data centers. For the stock graph, you'll find that stocks from the same sector often cluster together—these are called vine clusters. For example, energy stocks might form a cluster when oil prices fluctuate.
Community detection algorithms like walktrap can identify these clusters automatically. The homogeneity and completeness metrics (from the clevr library) tell you how well the predicted clusters match true sectors. A high homogeneity means most stocks in a cluster share the same sector; high completeness means most stocks of a sector are in one cluster.
3. Sector Prediction with Neighborhood Voting
To predict an unknown stock's sector, you can use its neighbors in the MST. The metric α measures the average probability that a stock's sector matches its neighbors' sectors. Compare this to a baseline where you randomly guess based on sector size. In practice, MST-based prediction often outperforms random guessing, especially with daily data where correlations are strong.
4. Impact of Time Granularity
The project compares daily, weekly, and monthly data. Daily data captures short-term noise (e.g., news events), while monthly data smooths out fluctuations, revealing long-term trends. In 2026, high-frequency trading uses millisecond data, but for sector prediction, weekly data often gives the best balance: it reduces noise without losing too much information. You'll notice that MST structures become more stable with coarser granularity, but community detection may become less precise.
5. Uber Traffic Network Analysis
The second part uses Uber Movement data from Los Angeles (Winter 2019) to analyze travel times. This dataset contains pairwise travel times between zones. By constructing a graph where nodes are zones and edge weights are travel times, you can find the MST—the most efficient routes for Santa's gift delivery! In 2026, similar algorithms optimize delivery routes for Amazon and DoorDash.
You'll also run community detection to find neighborhoods with similar traffic patterns. For example, downtown LA might form a dense cluster with short travel times, while suburbs are more spread out. Comparing MSTs from different times of day can reveal rush hour effects.
6. Putting It All Together
Graph algorithms are powerful tools for real-world data. Whether you're analyzing stock correlations or traffic patterns, the same principles apply: construct a graph, extract its MST, detect communities, and evaluate predictions. In 2026, these skills are in high demand for roles in fintech, logistics, and AI. By completing this project, you'll be ready to tackle similar challenges in your career.
Remember to cite any code or libraries you use, and always validate your results with proper metrics. Good luck!