arXiv:2610.24147stat.MEOct 2026

Manuscript Offprint

arXiv:2610.24147

lepidoptera: A Domain-Agnostic Engine for Causal Simulation with Mixed Variable Types, Vine Copula Dependence, and Individual-Level Counterfactual Propagation

Danilo Augusto Sarti

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

lepidoptera is an open-source R and Python package for simulation from structural causal models with heterogeneous variable types. The package is built around a typed mechanism architecture, which separates the causal graph from the generating rule at each node and exposes each rule as an inspectable, serialisable object. A pre-built mechanism library covers all nine combinations of continuous, categorical, and ordinal parent and child types, removing the need for users to implement type-specific sampling from scratch. A vine copula mechanism estimates the full joint dependence structure from observed data using pair-copula constructions (Bedford and Cooke, 2002; Aas et al., 2009) and the Rosenblatt conditional-simulation algorithm (Rosenblatt, 1952), supporting over twenty copula families through rvinecopulib (Nagler and Vatter, 2022). Individual-level counterfactual queries are supported through partial propagation, which freezes the random draws of unaffected nodes and replays only the downstream consequences of an intervention, thereby separating the individual treatment effect (ITE) estimand from the population-level average treatment effect (ATE) without additional model assumptions. An extension layer allows any pre-fitted predictive model to serve as a generative mechanism via a unified callable interface. Three worked illustrations spanning clinical trial planning, emergency-ward capacity analysis, and epidemiological mediation analysis demonstrate the package's scope.

1 Introduction

Simulation from structural causal models (SCMs) plays a central role across applied statistics. Monte Carlo power calculations for clinical trials routinely demand realistic synthetic patient populations that honour complex dependencies among continuous biomarkers, ordinal performance scores, and categorical treatment assignments (Morris et al., 2019; Burton et al., 2006). Digital twinning for hospital operations and personalised medicine requires the ability to intervene on individual pathways and measure downstream consequences at the level of individual units rather than population averages (Laubenbacher et al., 2024). Epidemiological mediation analyses call for joint interventions on both exposures and mediators, where the individual-level quantities needed to decompose effects into direct and indirect components (VanderWeele, 2015; Pearl, 2001) are conceptually distinct from the population-level estimands produced by simple rerandomisation (Robins and Greenland, 1992). Despite this diversity of applications, the computational problem underlying all three scenarios is the same: simulate from a directed acyclic graph (DAG) (Pearl, 2009; Spirtes et al., 2000) whose nodes span heterogeneous measurement scales, apply one or more interventions, and distinguish between what happens on average across a population and what would have happened to a specific individual (Rubin, 1974; Neyman, 1990).

Existing software addresses parts of this problem, but no single package handles all of it. simcausal (Sofrygin et al., 2017) and simDAG (Denz and Timmesfeld, 2026) offer the most comprehensive DAG-based simulation facilities currently available in R, and both packages have been widely adopted. Their designs, however, treat a mechanism primarily as a named node type or a formula: a string or expression whose generating logic is baked into the package internals and cannot be replaced in isolation. This precludes the clean intervention semantics that Pearl's modularity assumption (Pearl, 2009) demands and makes it cumbersome to substitute a data-fitted copula or a random-forest mechanism in place of a parametric equation without re-specifying the entire node. Neither package natively supports vine copula dependence modelling or individual-level (counterfactual) propagation. DagSim (Al Hajj et al., 2023) addresses the restriction on variable types and functional forms in its Python implementation by routing all generating logic through user-supplied functions, but provides no pre-built mechanism library, no vine copula backend, and no partial propagation. The R package R6causal (Tikka, 2024) supports counterfactual simulation for parametric SCMs with discrete variables but does not cover the mixed-type workflow described above.

lepidoptera is designed to fill this gap. The package name reflects the modular, compositional logic of biological taxonomy: just as a lepidopterist builds an identification key from composable, observable features, lepidoptera builds a data-generating process from composable, observable mechanisms—each typed, replaceable, and independently auditable. The central design decision is the strict separation of graph (which variables exist and who depends on whom) from mechanism (how each variable is generated given its parents), a separation grounded in Pearl's modularity assumption for SCMs (Pearl, 2009; Peters et al., 2017). This separation has four direct consequences. First, a pre-built mechanism library covers all nine parent-type/child-type combinations arising from the Cartesian product of {continuous, categorical, ordinal}; users specify parameters and the package handles type coercion, domain validation, and the correct sampling distribution. Second, a vine copula mechanism learns the full joint dependence structure from observed data and registers a conditional-simulation rule at the specified node, so that data-driven dependence can be incorporated without replacing the rest of the system. Third, propagate() with mode = "partial" freezes all unaffected random draws and recomputes only the downstream subgraph, enabling ITE estimation from the same synthetic population as the ATE. Fourth, any R or Python callable can serve as a mechanism through three wrapper functions that add type validation and serialisation support, making it straightforward to incorporate random forests, gradient-boosted trees, or language-model outputs into a causal simulation without any adapter code. Systems serialise to JSON for cross-language portability, so a model defined in R can be re-simulated in Python and vice versa.

The remainder of this paper is organised as follows. Section 2 surveys related software and positions lepidoptera within the existing landscape. Section 3 introduces the formal framework. The mechanism library and its R interface are described in Section 4, followed by the simulation and propagation algorithms in Section 5. The vine copula mechanism is treated in depth in Section 6, and the predictive model integration layer is described in Section 7. Three illustrations are given in Section 8. Computational details and package availability are covered in Section 9, and the paper closes with a discussion in Section 10.

References (39)