Skip to main content

Causalgen: a causal-based utility for data generation

Project description

CAUSALGEN: A Causal-based Utility for Data Generation

Causalgen is a utility that allows to generate data which is based on a certain causal graph structure.

1. Build A New Generator

In order to generate your random data, you first need to create a Generator instance as:

from causalgen import Generator

dg = 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 created
  • an already existing rng, in which case this rng is used within the generator
  • None, in which case the np.random module is used instead

2. Add Source Variables

Once 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 node

In case you want to use a custom random variable which is not provided, you can do that with the custom method. This method works as all the other distribution methods, but you can pass a custom function to it.

B = dg.custom(lambda s: dg.random.weibull(a=1, size=s), name='b')

Note: the function must match the signature f(size: int) -> np.ndarray, where size represent the length of the output vector

Additionally, you can create hidden variables using the hidden parameter. 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 Variables

Once you have built your source variables, you can add new descendant variables using the descendant method. This method works similarly to the previous ones, but you can pass the input function in four different ways:

  1. 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 node
dg.descendant(lambda a, b, c: np.sin(a) + np.cos(b) - 3 * c, name='d1')
  1. 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 inputs
dg.descendant(lambda x, y, z: np.sin(x) + np.cos(y) - 3 * z, parents=[A, B, C], name='d2')
  1. 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 node
dg.descendant(lambda x, y, z: np.sin(x) + np.cos(y) - 3 * z, parents=['a', 'b', 'c'], name='d3')
  1. 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 generator
D4 = dg.descendant(np.sin(A) + np.cos(B) - 3 * C, name='d4')

4. Introduce Noise

Another 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:

  1. you can use the 'noise' parameter in the 'descendant' method in order to add additive gaussian noise of the given amount
dg.descendant(lambda d1: 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 methods

  1. if you create the node by passing a user-defined function, you can add noise using the internal random number generator which is stored in the random field; please be careful to use the internal rng instead of other random number generators (or the np.random package) in order to get reproducible results
dg.descendant(lambda d2: d2 + 0.1 * dg.random.normal(), name='e2')
  1. 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 dataframe
dg.descendant(D4 + 0.1 * dg.noise(), name='e4')

5. Generate Data And Visualize Graph

Now that you have built the causal graph, you can sample some instances using the generate method. 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 the hidden parameter.

df = dg.generate(num=10, hidden=True)

Finally, you can visualize the casual graph structure using the visualize method.

dg.visualize()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

causalgen-0.1.4.tar.gz (83.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

causalgen-0.1.4-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file causalgen-0.1.4.tar.gz.

File metadata

  • Download URL: causalgen-0.1.4.tar.gz
  • Upload date:
  • Size: 83.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for causalgen-0.1.4.tar.gz
Algorithm Hash digest
SHA256 1870c679496e7a305d11184be8e7df372d86c8def256c693cdf9743973591eb4
MD5 902900a1243a0f6979bf5832c9df9e75
BLAKE2b-256 4744479e86b847df94c4a35cf4757c903007411e4c1a3011f6279f5a1ddc5e0f

See more details on using hashes here.

File details

Details for the file causalgen-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: causalgen-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for causalgen-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 aa6e3cfcb18a8116d0c718aceb0468f821d1b0a253da50f940642f25ba5a738f
MD5 a050471395309582f296fcd3b3e9e8b1
BLAKE2b-256 0ab39283baa8c922a4044bdd9233d8bd08e09bd78cb810c4df7dbd13967e8638

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page