Equity Penetration Analysis with LLMs: Technical Implementation Path

    This article explains how to implement equity penetration analysis, tracing ownership relationships layer by layer to compute effective ownership ratios. It covers graph-based data modeling, cycle-safe recursive traversal, batch performance optimization, result validation, and common pitfalls to avoid.

    1. Problem Definition: What Is Equity Penetration?

    Equity penetration means starting from an entity and tracing ownership relationships downward (or upward) layer by layer until a termination condition is met, then calculating the target entity’s effective equity ratio in the bottom-most entity. It differs from a “direct ownership query” in two essential ways: the relationships are recursive, and the ratios are multiplicative.

    Suppose entity A holds a ratio p1 of B, and B holds a ratio p2 of C. Then A’s indirect equity in C is p1 × p2. If multiple paths exist, the equity between the same pair of entities is the sum of the products along each path. This model looks simple, but cycles, duplicate edges, and missing values in real data can quickly make a naive implementation spiral out of control.

    2. Data Modeling: Representing Equity Relationships as a Weighted Directed Graph

    An adjacency list is recommended over an adjacency matrix, because the equity graph is extremely sparse and a matrix would waste a large amount of space.

    1. from collections import defaultdict
    2. # Edge: (holder -> target, ratio)
    3. graph = defaultdict(list)
    4. def add_edge(holder, target, ratio: float):
    5. assert 0.0 < ratio <= 1.0, "Ratio must fall within (0, 1]"
    6. graph[holder].append((target, ratio))

    Three constraints must be enforced during modeling: ratio value validation, direct rejection of self-loops (A holding A), and merging duplicate edges between the same pair of entities or explicitly keeping them as multiple paths. Without these constraints, all subsequent calculation results are untrustworthy.

    3. Core Algorithm: Recursive Penetration with Cycles

    The most basic implementation is depth-first search. The key is to use a path set for cycle detection rather than a global visited marker—a global marker misses the independent contribution of the same node under different paths.

    1. def penetrate(start, graph, max_depth=20):
    2. result = defaultdict(float)
    3. stack = [(start, 1.0, frozenset([start]), 0)]
    4. while stack:
    5. node, ratio, path, depth = stack.pop()
    6. if depth >= max_depth:
    7. continue
    8. for nxt, w in graph.get(node, []):
    9. if nxt in path: # Cycle: truncate this branch
    10. continue
    11. new_ratio = ratio * w
    12. result[nxt] += new_ratio
    13. stack.append((nxt, new_ratio, path | {nxt}, depth + 1))
    14. return dict(result)

    Engineering practice requires three additions: set a maximum depth to prevent overly long chains; prune extremely small ratios (for example, below 1e-9); and switch to an explicit stack instead of recursion for very deep graphs to avoid call stack overflow.

    4. Performance Optimization: From Single Queries to Batch Serving

    When batch computation is needed for millions of entities, point-by-point traversal is unacceptable. Viable strategies include: first condensing the graph by strongly connected components, compressing cycles into single points before penetration; building result caches for high-frequency query entities with an invalidation policy; sharding graph data into object storage and loading it in parallel by compute tasks on a container platform; and using a message queue to smooth peaks, turning online requests into asynchronous tasks.

    1. # Pseudo-flow for penetration after condensation
    2. # 1. Compute strongly connected components to obtain a DAG
    3. # 2. Accumulate ratios in topological order on the DAG
    4. # 3. Distribute ratios within a component evenly among members or by a secondary rule

    5. Result Validation and Observability

    Penetration results must be verifiable. A common approach is conservation checks: the sum of all direct ratios of an entity should not exceed 1; sample comparisons between “layer-by-layer expansion” and “one-shot penetration” results; and recording the graph version number for each computation to ensure reproducibility. Logging services and monitoring alerts should also be integrated, with threshold alerts on computation time, cycle hit counts, and pruning ratios.

    6. Common Pitfalls

    First, treating a global visited set as cycle detection, which causes missed calculations. Second, accumulating directly with floating point without considering precision, so errors accumulate over long chains. Third, ignoring data time points and mixing ownership relationships from different dates into one graph. Fourth, lacking a depth limit, causing direct timeouts on dirty data. Avoiding these problems costs far less than investigating them after the fact.

    7. Summary

    The engineering difficulty of equity penetration lies not in the algorithm itself, but in balancing data quality, cycle handling, and batch performance. Solidifying the graph model and validation rules first, then discussing optimization, is a more reliable order for implementation.