Package: causalgen
CAUSALGEN: A Causal-based Utility for Data GenerationCausalgenis a utility that allows to generate data which is based on a certain causal graph structure.1. Build A New GeneratorIn order to generate your random data, you first need to create a Generator instance as:fromcausalgenimportGeneratordg=Generator(seed=42)The seed parameter controls the random number operations of the generator, it can be:an integer, in which case a new numpy random number generator (rng) is createdan already existingrng, in which case thisrngis used within the generatorNone, in which case thenp.randommodule is used instead2. Add Source VariablesOnce you have your generator, you can add your random variables.
Generator objects already provide some random distributions, which you can use to build a new variable.
When a new variable is added, the generator automatically appends it inside its data structure and returns it.
Moreover, each variable must have a unique name within the graph.A=dg.normal(mu=0,sigma=1,name='a')Note: if no name is passed, one is automatically attached to a new nodeIn case you want to use a custom random variable which is not provided, you can do that with thecustommethod.
This method works as all the other distribution methods, but you can pass a custom function to it.B=dg.custom(lambdas:dg.random.weibull(a=1,size=s),name='b')Note: the function must match the signaturef(size: int) -> np.ndarray, wheresizerepresent the length of the output vectorAdditionally, you can create hidden variables using thehiddenparameter.
Nodes marked as hidden should represent all those variables that cannot be measured in a real-world scenario.
These nodes will be displayed with a different color, and optionally removed from the generated dataframe.C=dg.integers(low=0,high=10,endpoint=False,hidden=True,name='c')3. Add Child VariablesOnce you have build your source variables, you can add new descendant variables using thedescendantmethod.
This method works similarly to the previous ones, but you can pass the input function in four different ways:you can pass a custom function whose input parameters match the name of nodes already in the generator, in which case the generator automatically retrieves the parent nodes and build a new child nodedg.descendant(lambdaa,b,c:np.sin(a)+np.cos(b)-3*c,name='d1')you can pass a function with a given number of parameters and then use the 'parents' argument to pass an (ordered) list of node instances that are already in the generator and whose values will be used as inputsdg.descendant(lambdax,y,z:np.sin(x)+np.cos(y)-3*z,parents=[A,B,C],name='d2')you can use the same syntax as in 2. but passing the parents as strings instead of node objects, in which case the generator automatically retrieves the parent nodes and build a new child nodedg.descendant(lambdax,y,z:np.sin(x)+np.cos(y)-3*z,parents=['a','b','c'],name='d3')you can perform operations directly on nodes, as they support a wide number of numpy-based arithmetical and logical operations which are eventually transformed into a new node by the generatorD4=dg.descendant(np.sin(A)+np.cos(B)-3*C,name='d4')4. Introduce NoiseAnother important aspect of data generation is the presence of noise.
When adding descendant nodes, you can introduce noise as part of the node's function:you can use the 'noise' parameter in the 'descendant' method in order to add additive gaussian noise of the given amountdg.descendant(lambdad1:d1,noise=0.1,name='e1')Note: this is usually enough, but if your noise should be either non-gaussian or non-additive you will have to leverage one of the other two methodsif you create the node by passing a user-defined function, you can add noise using the internal random number generator which is stored in therandomfield;
please be careful to use the internal rng instead of other random number generators (or thenp.randompackage) in order to get reproducible resultsdg.descendant(lambdad2:d2+0.1*dg.random.normal(),name='e2')if you create the node via explicit variable's operations, you can simply create a new source within the operation itself;
this is equivalent to adding a new node and then use it within the equation, hence the resulting object will appear both in visualization and in the generated dataframedg.descendant(D4+0.1*dg.noise(),name='e4')5. Generate Data And Visualize GraphNow that you have built the causal graph, you can sample some instances using thegeneratemethod.
By default, the method returns a dataframe with the given number of instances and with visible variables only:df=dg.generate(num=10)If you want to have access to hidden variables as well, you just need to use thehiddenparameter.df=dg.generate(num=10,hidden=True)Finally, you can visualize the casual graph structure using thevisualizemethod.dg.visualize()