Class TrainedDagSimulatorANM

java.lang.Object
edu.cmu.tetrad.sem.TrainedDagSimulatorANM

public final class TrainedDagSimulatorANM extends Object

Trained DAG simulator (ANM): learn local mechanisms from a real dataset and resimulate

This class is a “train-then-resimulate” simulator: given an observed DataSet and a user-supplied acyclic Graph (treated as a DAG), it learns a local conditional mechanism for each node and then generates new samples by running the DAG forward in topological order.

Primary motivation. This simulator is designed to demonstrate that (i) marginal and pairwise plots can often be made to look “realistic” under substantially different DAGs when the mechanisms are flexible and noise is bootstrapped, yet (ii) likelihood/score-based evaluation should systematically penalize fitting the wrong graph to data generated from the learned mechanisms. In other words: visual realism is cheap; causal correctness should still matter for scoring.

Model family (ANM)

For each node Y with parents Pa(Y), the fitted mechanism is an additive-noise model

  Y = f(Pa(Y)) + e

where f is a small neural regressor/classifier and e is drawn by bootstrap from training residuals (continuous) or via sampling from fitted class probabilities (discrete).

Mixed data support

  • Continuous child: 1-hidden-layer MLP regressor trained by SGD; during simulation, generate yHat = f(x) and add a bootstrapped residual e from the training rows used for that node.
  • Discrete child: 1-hidden-layer MLP softmax classifier; during simulation, sample a category from the predicted class probabilities.

Parent encoding

  • Continuous parents enter as z-scored scalars (mean/sd computed from available rows).
  • Discrete parents enter as one-hot blocks (levels inferred from DiscreteVariable metadata, with a configurable cap).

Missingness

Training is done node-by-node using only rows where the child and all of its parents are observed (rows with any missing among {Y} ∪ Pa(Y) are skipped for that node). Simulation produces complete samples given the learned mechanisms.

Notes and limitations

  • This is intentionally a lightweight baseline (single hidden layer, tanh activation, SGD, L2 decay), aimed at robustness and ease of packaging rather than state-of-the-art prediction accuracy.
  • The quality of the resimulation depends on the supplied DAG and on support coverage of parent values; extrapolation may occur when simulated parent configurations fall outside the training support.
  • Constructor Details

    • TrainedDagSimulatorANM

      public TrainedDagSimulatorANM(DataSet data, Graph dag, TrainedDagSimulatorANM.Params params)
      Constructs a TrainedDagSimulatorANM
      Parameters:
      data - Training dataset
      dag - Directed Acyclic Graph (DAG) representing the causal structure
      params - Simulation parameters
  • Method Details

    • safeGetInt

      public static int safeGetInt(DataSet data, int row, int col)
      Safely retrieves an integer value from a DataSet, handling missing or non-integer values gracefully.
      Parameters:
      data - DataSet containing the data
      row - Row index
      col - Column index
      Returns:
      Integer value or -1 if missing or non-integer
    • getFitReportText

      public String getFitReportText()
      Human-readable report (one block per node).
      Returns:
      String containing the fit report text.
    • getNodeReports

      public List<TrainedDagSimulatorANM.NodeReport> getNodeReports()
      Structured per-node reports.
      Returns:
      List of NodeReport objects containing detailed simulation results for each node
    • writeFitReportTxt

      public void writeFitReportTxt(File file) throws IOException
      Writes a human-readable fit report to a specified text file. The fit report provides per-node details on the training process and its results, including error metrics and parent information.
      Parameters:
      file - The output file where the fit report will be written.
      Throws:
      IOException - If an I/O error occurs during writing.
    • fit

      public void fit()
      Fit one mechanism per node given its parents in the supplied DAG.
    • simulate

      public TrainedDagSimulatorANM.SimResult simulate(int nSamples)
      Simulates the DAG with the given number of samples and random seed.
      Parameters:
      nSamples - Number of samples to generate
      Returns:
      SimResult object containing simulation results
      Throws:
      IllegalArgumentException - if nSamples is less than 1
    • simulate

      public TrainedDagSimulatorANM.SimResult simulate(int nSamples, long seed)
      Simulates the DAG with the given number of samples and random seed.
      Parameters:
      nSamples - Number of samples to generate
      seed - Random seed for the simulation
      Returns:
      SimResult object containing simulation results
      Throws:
      IllegalArgumentException - if nSamples is less than 1