|
| 1 | +from typing import Callable, List, Optional |
| 2 | + |
| 3 | +import networkx as nx |
| 4 | +import numpy as np |
| 5 | + |
| 6 | + |
| 7 | +def make_graph_linear_gaussian( |
| 8 | + G: nx.DiGraph, |
| 9 | + node_mean_lims: Optional[List[float]] = None, |
| 10 | + node_std_lims: Optional[List[float]] = None, |
| 11 | + edge_functions: List[Callable[[float], float]] = None, |
| 12 | + edge_weight_lims: Optional[List[float]] = None, |
| 13 | + random_state=None, |
| 14 | +): |
| 15 | + r"""Convert an existing DAG to a linear Gaussian graphical model. |
| 16 | +
|
| 17 | + All nodes are sampled from a normal distribution with parametrizations |
| 18 | + defined uniformly at random between the limits set by the input parameters. |
| 19 | + The edges apply then a weight and a function based on the inputs in an additive fashion. |
| 20 | + For node :math:`X_i`, we have: |
| 21 | +
|
| 22 | + .. math:: |
| 23 | +
|
| 24 | + X_i = \\sum_{j \in parents} w_j f_j(X_j) + \\epsilon_i |
| 25 | +
|
| 26 | + where: |
| 27 | +
|
| 28 | + - :math:`\\epsilon_i \sim N(\mu_i, \sigma_i)`, where :math:`\mu_i` is sampled |
| 29 | + uniformly at random from `node_mean_lims` and :math:`\sigma_i` is sampled |
| 30 | + uniformly at random from `node_std_lims`. |
| 31 | + - :math:`w_j \sim U(\\text{edge_weight_lims})` |
| 32 | + - :math:`f_j` is a function sampled uniformly at random |
| 33 | + from `edge_functions` |
| 34 | +
|
| 35 | + Parameters |
| 36 | + ---------- |
| 37 | + G : NetworkX DiGraph |
| 38 | + The graph to sample data from. The graph will be modified in-place |
| 39 | + to get the weights and functions of the edges. |
| 40 | + node_mean_lims : Optional[List[float]], optional |
| 41 | + The lower and upper bounds of the mean of the Gaussian random variable, by default None, |
| 42 | + which defaults to a mean of 0. |
| 43 | + node_std_lims : Optional[List[float]], optional |
| 44 | + The lower and upper bounds of the std of the Gaussian random variable, by default None, |
| 45 | + which defaults to a std of 1. |
| 46 | + edge_functions : List[Callable[float]], optional |
| 47 | + The set of edge functions that take in an iid sample from the parent and computes |
| 48 | + a transformation (possibly nonlinear), such as ``(lambda x: x**2, lambda x: x)``, |
| 49 | + by default None, which defaults to the identity function ``lambda x: x``. |
| 50 | + edge_weight_lims : Optional[List[float]], optional |
| 51 | + The lower and upper bounds of the edge weight, by default None, |
| 52 | + which defaults to a weight of 1. |
| 53 | + random_state : int, optional |
| 54 | + Random seed, by default None. |
| 55 | +
|
| 56 | + Returns |
| 57 | + ------- |
| 58 | + G : NetworkX DiGraph |
| 59 | + NetworkX graph with the edge weights and functions set with node attributes |
| 60 | + set with ``'parent_functions'``, and ``'gaussian_noise_function'``. Moreover |
| 61 | + the graph attribute ``'linear_gaussian'`` is set to ``True``. |
| 62 | + """ |
| 63 | + if not nx.is_directed_acyclic_graph(G): |
| 64 | + raise ValueError("The input graph must be a DAG.") |
| 65 | + rng = np.random.default_rng(random_state) |
| 66 | + |
| 67 | + if node_mean_lims is None: |
| 68 | + node_mean_lims = [0, 0] |
| 69 | + elif len(node_mean_lims) != 2: |
| 70 | + raise ValueError("node_mean_lims must be a list of length 2.") |
| 71 | + if node_std_lims is None: |
| 72 | + node_std_lims = [1, 1] |
| 73 | + elif len(node_std_lims) != 2: |
| 74 | + raise ValueError("node_std_lims must be a list of length 2.") |
| 75 | + if edge_functions is None: |
| 76 | + edge_functions = [lambda x: x] |
| 77 | + if edge_weight_lims is None: |
| 78 | + edge_weight_lims = [1, 1] |
| 79 | + elif len(edge_weight_lims) != 2: |
| 80 | + raise ValueError("edge_weight_lims must be a list of length 2.") |
| 81 | + |
| 82 | + # Create list of topologically sorted nodes |
| 83 | + top_sort_idx = list(nx.topological_sort(G)) |
| 84 | + |
| 85 | + for node_idx in top_sort_idx: |
| 86 | + # get all parents |
| 87 | + parents = sorted(list(G.predecessors(node_idx))) |
| 88 | + |
| 89 | + # sample noise |
| 90 | + mean = rng.uniform(low=node_mean_lims[0], high=node_mean_lims[1]) |
| 91 | + std = rng.uniform(low=node_std_lims[0], high=node_std_lims[1]) |
| 92 | + |
| 93 | + # sample weight and edge function for each parent |
| 94 | + node_function = dict() |
| 95 | + for parent in parents: |
| 96 | + weight = rng.uniform(low=edge_weight_lims[0], high=edge_weight_lims[1]) |
| 97 | + func = rng.choice(edge_functions) |
| 98 | + node_function.update({parent: {"weight": weight, "func": func}}) |
| 99 | + |
| 100 | + # set the node attribute "functions" to hold the weight and function wrt each parent |
| 101 | + nx.set_node_attributes(G, {node_idx: node_function}, "parent_functions") |
| 102 | + nx.set_node_attributes(G, {node_idx: {"mean": mean, "std": std}}, "gaussian_noise_function") |
| 103 | + G.graph["linear_gaussian"] = True |
| 104 | + return G |
0 commit comments