# GaussianCopula Model¶

In this guide we will go through a series of steps that will let you
discover functionalities of the `GaussianCopula`

model, including how
to:

Create an instance of a

`GaussianCopula`

.Fit the instance to your data.

Generate synthetic versions of your data.

Use

`GaussianCopula`

to anonymize PII information.Customize the data transformations to improve the learning process.

Specify the column distributions to improve the output quality.

## What is GaussianCopula?¶

The `sdv.tabular.GaussianCopula`

model is based on
copula funtions.

In mathematical terms, a *copula* is a distribution over the unit
cube \({\displaystyle [0,1]^{d}}\) which is constructed from a
multivariate normal distribution over
\({\displaystyle \mathbb {R} ^{d}}\) by using the probability
integral transform. Intuitively, a *copula* is a mathematical function
that allows us to describe the joint distribution of multiple random
variables by analyzing the dependencies between their marginal
distributions.

Let’s now discover how to learn a dataset and later on generate
synthetic data with the same format and statistical properties by using
the `GaussianCopula`

model.

## Quick Usage¶

We will start by loading one of our demo datasets, the
`student_placements`

, which contains information about MBA students
that applied for placements during the year 2020.

```
In [1]: from sdv.demo import load_tabular_demo
In [2]: data = load_tabular_demo('student_placements')
In [3]: data.head()
Out[3]:
student_id gender second_perc high_perc high_spec degree_perc degree_type work_experience experience_years employability_perc mba_spec mba_perc salary placed start_date end_date duration
0 17264 M 67.00 91.00 Commerce 58.00 Sci&Tech False 0 55.0 Mkt&HR 58.80 27000.0 True 2020-07-23 2020-10-12 3.0
1 17265 M 79.33 78.33 Science 77.48 Sci&Tech True 1 86.5 Mkt&Fin 66.28 20000.0 True 2020-01-11 2020-04-09 3.0
2 17266 M 65.00 68.00 Arts 64.00 Comm&Mgmt False 0 75.0 Mkt&Fin 57.80 25000.0 True 2020-01-26 2020-07-13 6.0
3 17267 M 56.00 52.00 Science 52.00 Sci&Tech False 0 66.0 Mkt&HR 59.43 NaN False NaT NaT NaN
4 17268 M 85.80 73.60 Commerce 73.30 Comm&Mgmt False 0 96.8 Mkt&Fin 55.50 42500.0 True 2020-07-04 2020-09-27 3.0
```

As you can see, this table contains information about students which includes, among other things:

Their id and gender

Their grades and specializations

Their work experience

The salary that they were offered

The duration and dates of their placement

You will notice that there is data with the following characteristics:

There are float, integer, boolean, categorical and datetime values.

There are some variables that have missing data. In particular, all the data related to the placement details is missing in the rows where the student was not placed.

Let us use the `GaussianCopula`

to learn this data and then sample
synthetic data about new students to see how well the model captures the
characteristics indicated above. In order to do this you will need to:

Import the

`sdv.tabular.GaussianCopula`

class and create an instance of it.Call its

`fit`

method passing our table.Call its

`sample`

method indicating the number of synthetic rows that you want to generate.

```
In [4]: from sdv.tabular import GaussianCopula
In [5]: model = GaussianCopula()
In [6]: model.fit(data)
```

Note

Notice that the model `fitting`

process took care of transforming the
different fields using the appropriate Reversible Data
Transforms to ensure that the data
has a format that the `GaussianMultivariate`

model can handle.

### Generate synthetic data from the model¶

Once the modeling has finished you are ready to generate new synthetic
data by calling the `sample`

method from your model passing the number
of rows that we want to generate.

```
In [7]: new_data = model.sample(200)
```

This will return a table identical to the one which the model was fitted on, but filled with new data which resembles the original one.

```
In [8]: new_data.head()
Out[8]:
student_id gender second_perc high_perc high_spec degree_perc degree_type work_experience experience_years employability_perc mba_spec mba_perc salary placed start_date end_date duration
0 17476 M 57.47 52.47 Science 58.63 Sci&Tech False 1 78.86 Mkt&Fin 57.92 NaN False NaT NaT NaN
1 17429 M 73.82 66.57 Commerce 55.96 Sci&Tech False 1 73.12 Mkt&HR 60.36 33600.0 True 2020-02-02 2020-10-10 7.0
2 17426 M 45.78 44.95 Commerce 59.04 Comm&Mgmt False 1 56.42 Mkt&Fin 57.19 NaN False NaT NaT NaN
3 17383 M 68.82 63.74 Arts 67.52 Sci&Tech False 1 70.14 Mkt&Fin 74.14 NaN False NaT NaT NaN
4 17403 M 71.56 56.91 Science 59.63 Sci&Tech False 1 70.51 Mkt&HR 51.21 36000.0 True 2020-03-05 2020-09-09 4.0
```

Note

You can control the number of rows by specifying the number of
`samples`

in the `model.sample(<num_rows>)`

. To test, try
`model.sample(10000)`

. Note that the original table only had ~200
rows.

### Save and Load the model¶

In many scenarios it will be convenient to generate synthetic versions
of your data directly in systems that do not have access to the original
data source. For example, if you may want to generate testing data on
the fly inside a testing environment that does not have access to your
production database. In these scenarios, fitting the model with real
data every time that you need to generate new data is feasible, so you
will need to fit a model in your production environment, save the fitted
model into a file, send this file to the testing environment and then
load it there to be able to `sample`

from it.

Let’s see how this process works.

#### Load the model and generate new data¶

The file you just generated can be sent over to the system where the
synthetic data will be generated. Once it is there, you can load it
using the `GaussianCopula.load`

method, and then you are ready to
sample new data from the loaded instance:

```
In [10]: loaded = GaussianCopula.load('my_model.pkl')
In [11]: new_data = loaded.sample(200)
```

Warning

Notice that the system where the model is loaded needs to also have
`sdv`

installed, otherwise it will not be able to load the model and
use it.

### Specifying the Primary Key of the table¶

One of the first things that you may have noticed when looking at the demo
data is that there is a `student_id`

column which acts as the primary
key of the table, and which is supposed to have unique values. Indeed,
if we look at the number of times that each value appears, we see that
all of them appear at most once:

```
In [12]: data.student_id.value_counts().max()
Out[12]: 1
```

However, if we look at the synthetic data that we generated, we observe that there are some values that appear more than once:

```
In [13]: new_data[new_data.student_id == new_data.student_id.value_counts().index[0]]
Out[13]:
student_id gender second_perc high_perc high_spec degree_perc degree_type work_experience experience_years employability_perc mba_spec mba_perc salary placed start_date end_date duration
56 17349 M 87.18 92.75 Commerce 74.82 Comm&Mgmt False 0 61.65 Mkt&Fin 59.94 30100.0 True 2020-01-20 2020-05-29 3.0
61 17349 F 57.56 54.58 Commerce 66.22 Others False 0 62.31 Mkt&Fin 62.69 NaN False NaT NaT NaN
64 17349 M 58.11 58.24 Commerce 58.88 Comm&Mgmt False 0 89.27 Mkt&HR 55.96 27700.0 True 2020-03-07 2020-06-18 3.0
180 17349 M 61.75 59.42 Commerce 56.41 Comm&Mgmt False 0 57.33 Mkt&HR 60.58 26400.0 True 2020-01-30 2020-06-26 4.0
192 17349 F 61.55 57.06 Science 64.29 Comm&Mgmt False 0 55.36 Mkt&HR 54.19 20000.0 True 2020-02-14 2020-06-06 3.0
```

This happens because the model was not notified at any point about the
fact that the `student_id`

had to be unique, so when it generates new
data it will provoke collisions sooner or later. In order to solve this,
we can pass the argument `primary_key`

to our model when we create it,
indicating the name of the column that is the index of the table.

```
In [14]: model = GaussianCopula(
....: primary_key='student_id'
....: )
....:
In [15]: model.fit(data)
In [16]: new_data = model.sample(200)
In [17]: new_data.head()
Out[17]:
student_id gender second_perc high_perc high_spec degree_perc degree_type work_experience experience_years employability_perc mba_spec mba_perc salary placed start_date end_date duration
0 0 M 55.15 68.09 Commerce 62.88 Comm&Mgmt False 0 54.13 Mkt&Fin 58.63 23800.0 True 2020-02-06 2020-11-06 10.0
1 1 M 74.29 79.71 Commerce 75.79 Comm&Mgmt False 0 60.40 Mkt&HR 58.29 21300.0 True 2020-01-16 2020-08-08 6.0
2 2 F 81.73 65.17 Science 62.59 Comm&Mgmt False 0 56.66 Mkt&Fin 76.26 NaN False NaT NaT NaN
3 3 M 68.01 63.47 Science 65.92 Sci&Tech False 1 57.52 Mkt&Fin 57.84 25000.0 True 2020-01-07 2020-08-10 8.0
4 4 F 86.57 91.16 Science 65.24 Sci&Tech False 0 83.35 Mkt&Fin 64.45 25900.0 True 2020-03-26 2020-06-27 3.0
```

As a result, the model will learn that this column must be unique and generate a unique sequence of values for the column:

```
In [18]: new_data.student_id.value_counts().max()
Out[18]: 1
```

### Anonymizing Personally Identifiable Information (PII)¶

There will be many cases where the data will contain Personally Identifiable Information which we cannot disclose. In these cases, we will want our Tabular Models to replace the information within these fields with fake, simulated data that looks similar to the real one but does not contain any of the original values.

Let’s load a new dataset that contains a PII field, the
`student_placements_pii`

demo, and try to generate synthetic versions
of it that do not contain any of the PII fields.

Note

The `student_placements_pii`

dataset is a modified version of the
`student_placements`

dataset with one new field, `address`

, which
contains PII information about the students. Notice that this additional
`address`

field has been simulated and does not correspond to data
from the real users.

```
In [19]: data_pii = load_tabular_demo('student_placements_pii')
In [20]: data_pii.head()
Out[20]:
student_id address gender second_perc high_perc high_spec degree_perc degree_type work_experience experience_years employability_perc mba_spec mba_perc salary placed start_date end_date duration
0 17264 70304 Baker Turnpike\nEricborough, MS 15086 M 67.00 91.00 Commerce 58.00 Sci&Tech False 0 55.0 Mkt&HR 58.80 27000.0 True 2020-07-23 2020-10-12 3.0
1 17265 805 Herrera Avenue Apt. 134\nMaryview, NJ 36510 M 79.33 78.33 Science 77.48 Sci&Tech True 1 86.5 Mkt&Fin 66.28 20000.0 True 2020-01-11 2020-04-09 3.0
2 17266 3702 Bradley Island\nNorth Victor, FL 12268 M 65.00 68.00 Arts 64.00 Comm&Mgmt False 0 75.0 Mkt&Fin 57.80 25000.0 True 2020-01-26 2020-07-13 6.0
3 17267 Unit 0879 Box 3878\nDPO AP 42663 M 56.00 52.00 Science 52.00 Sci&Tech False 0 66.0 Mkt&HR 59.43 NaN False NaT NaT NaN
4 17268 96493 Kelly Canyon Apt. 145\nEast Steven, NC 3... M 85.80 73.60 Commerce 73.30 Comm&Mgmt False 0 96.8 Mkt&Fin 55.50 42500.0 True 2020-07-04 2020-09-27 3.0
```

If we use our tabular model on this new data we will see how the synthetic data that it generates discloses the addresses from the real students:

```
In [21]: model = GaussianCopula(
....: primary_key='student_id',
....: )
....:
In [22]: model.fit(data_pii)
In [23]: new_data_pii = model.sample(200)
In [24]: new_data_pii.head()
Out[24]:
student_id address gender second_perc high_perc high_spec degree_perc degree_type work_experience experience_years employability_perc mba_spec mba_perc salary placed start_date end_date duration
0 0 2707 Maria Parkways Apt. 743\nAlisonview, TN 8... M 76.93 63.75 Science 74.40 Comm&Mgmt False 0 72.39 Mkt&Fin 59.35 33900.0 True 2020-01-06 2020-04-15 4.0
1 1 31846 Alexa Manor Suite 441\nMckinneyville, OH... M 66.84 63.95 Science 73.30 Comm&Mgmt False 2 80.80 Mkt&Fin 65.37 26800.0 True 2020-01-05 2020-09-07 9.0
2 2 2602 Danielle Viaduct Suite 897\nLake Tammybor... F 66.52 63.06 Commerce 69.84 Sci&Tech False 0 90.22 Mkt&HR 55.88 24500.0 True 2020-01-13 2020-07-02 8.0
3 3 2823 Lynn Landing\nNorth Paul, KY 64670 M 62.17 59.96 Commerce 56.87 Comm&Mgmt False 1 64.39 Mkt&HR 59.91 26200.0 True 2020-04-04 2020-07-09 3.0
4 4 21378 Russell Neck\nLake Robertstad, KS 49747 M 56.74 67.40 Commerce 58.60 Comm&Mgmt False 0 86.29 Mkt&HR 61.89 NaN False NaT NaT NaN
```

More specifically, we can see how all the addresses that have been generated actually come from the original dataset:

```
In [25]: new_data_pii.address.isin(data_pii.address).sum()
Out[25]: 200
```

In order to solve this, we can pass an additional argument
`anonymize_fields`

to our model when we create the instance. This
`anonymize_fields`

argument will need to be a dictionary that
contains:

The name of the field that we want to anonymize.

The category of the field that we want to use when we generate fake values for it.

The list complete list of possible categories can be seen in the Faker Providers page, and it contains a huge list of concepts such as:

name

address

country

city

ssn

credit_card_number

credit_card_expire

credit_card_security_code

email

telephone

…

In this case, since the field is an address, we will pass a
dictionary indicating the category `address`

```
In [26]: model = GaussianCopula(
....: primary_key='student_id',
....: anonymize_fields={
....: 'address': 'address'
....: }
....: )
....:
In [27]: model.fit(data_pii)
```

As a result, we can see how the real `address`

values have been
replaced by other fake addresses:

```
In [28]: new_data_pii = model.sample(200)
In [29]: new_data_pii.head()
Out[29]:
student_id address gender second_perc high_perc high_spec degree_perc degree_type work_experience experience_years employability_perc mba_spec mba_perc salary placed start_date end_date duration
0 0 435 Mitchell Grove Apt. 741\nLindafort, NY 70903 M 61.91 63.04 Commerce 56.76 Comm&Mgmt False 1 64.11 Mkt&Fin 62.67 24300.0 True 2020-01-23 2020-07-08 5.0
1 1 9683 Deborah Gateway Apt. 774\nMillerfort, WY ... F 80.45 71.48 Science 64.69 Comm&Mgmt False 0 62.14 Mkt&HR 61.98 24700.0 True 2020-03-08 2020-07-20 3.0
2 2 0517 Frye Cape\nLake Johnshire, CO 31557 M 60.73 57.05 Commerce 58.29 Sci&Tech False 1 68.91 Mkt&HR 56.67 23500.0 True 2020-04-17 2020-08-11 4.0
3 3 064 Owen Stream Suite 902\nWallacestad, MO 95076 M 72.54 64.98 Science 63.98 Sci&Tech False 1 78.95 Mkt&HR 56.84 27100.0 True 2020-02-04 2020-11-16 8.0
4 4 PSC 4586, Box 2071\nAPO AE 60523 M 74.76 65.89 Science 71.87 Sci&Tech False 0 59.66 Mkt&HR 61.63 21500.0 True 2020-03-02 2020-08-04 4.0
```

Which means that none of the original addresses can be found in the sampled data:

```
In [30]: data_pii.address.isin(new_data_pii.address).sum()
Out[30]: 0
```

## Advanced Usage¶

Now that we have discovered the basics, let’s go over a few more
advanced usage examples and see the different arguments that we can pass
to our `GaussianCopula`

Model in order to customize it to our needs.

### How to set transforms to use?¶

One thing that you may have noticed when executing the previous steps is
that the fitting process took much longer on the
`student_placements_pii`

dataset than it took on the previous version
that did not contain the student `address`

. This happens because the
`address`

field is interpreted as a categorical variable, which the
`GaussianCopula`

one-hot
encoded generating 215 new
columns that it had to learn afterwards.

This transformation, which in this case was very inefficient, happens
because the Tabular Models apply Reversible Data
Transforms under the hood to
transform all the non-numerical variables, which the underlying models
cannot handle, into numerical representations which they can properly
work with. In the case of the `GaussianCopula`

, the default
transformation is a One-Hot encoding, which can work very well with
variables that have a small number of different values, but which is
very inefficient in cases where there is a large number of values.

For this reason, the Tabular Models have an additional argument called
`field_transformers`

that let you select which transformer to apply to
each column. This `field_transformers`

argument must be passed as a
`dict`

which contains the name of the fields for which we want to use
a transformer different than the default, and the name of the
transformer that we want to use.

Possible transformer names are:

`integer`

: Uses a`NumericalTransformer`

of dtype`int`

.`float`

: Uses a`NumericalTransformer`

of dtype`float`

.`categorical`

: Uses a`CategoricalTransformer`

without gaussian noise.`categorical_fuzzy`

: Uses a`CategoricalTransformer`

adding gaussian noise.`one_hot_encoding`

: Uses a`OneHotEncodingTransformer`

.`label_encoding`

: Uses a`LabelEncodingTransformer`

.`boolean`

: Uses a`BooleanTransformer`

.`datetime`

: Uses a`DatetimeTransformer`

.

**NOTE**: For additional details about each one of the transformers,
please visit RDT

Let’s now try to improve the previous fitting process by changing the
transformer that we use for the `address`

field to something other
than the default. As an example, we will use the `label_encoding`

transformer, which instead of generating one column for each possible
value, it just replaces each value with a unique integer value.

```
In [31]: model = GaussianCopula(
....: primary_key='student_id',
....: anonymize_fields={
....: 'address': 'address'
....: },
....: field_transformers={
....: 'address': 'label_encoding'
....: }
....: )
....:
In [32]: model.fit(data_pii)
In [33]: new_data_pii = model.sample(200)
In [34]: new_data_pii.head()
Out[34]:
student_id address gender second_perc high_perc high_spec degree_perc degree_type work_experience experience_years employability_perc mba_spec mba_perc salary placed start_date end_date duration
0 0 45954 Karla Wall\nNorth Lorihaven, PA 19709 M 60.94 64.15 Science 63.57 Comm&Mgmt False 0 65.31 Mkt&Fin 54.20 25200.0 True 2020-04-07 2020-06-25 3.0
1 1 852 Rachael Throughway\nTuckerfort, MT 78560 M 82.80 66.42 Science 69.43 Sci&Tech False 1 91.32 Mkt&HR 62.48 33200.0 True 2020-08-11 2020-07-01 3.0
2 2 Unit 4612 Box 4307\nDPO AP 28663 M 72.45 67.18 Science 65.92 Sci&Tech False 0 89.39 Mkt&HR 63.16 25500.0 True 2020-01-20 2020-04-23 3.0
3 3 27421 Turner Way\nEast Kennethville, HI 57379 M 78.02 37.00 Science 69.29 Sci&Tech False 0 81.18 Mkt&HR 58.44 42700.0 True 2020-01-03 2020-08-01 9.0
4 4 5121 Parker Keys Suite 452\nSouth Tammytown, I... F 62.65 74.44 Commerce 63.89 Comm&Mgmt False 0 78.16 Mkt&Fin 58.50 23700.0 True 2020-02-21 2020-08-23 6.0
```

### Setting Bounds and Specifying Rounding for Numerical Columns¶

By default, the model will learn the upper and lower bounds of the
input data, and use that for sampling. This means that all sampled data
will be between the maximum and minimum values found in the original
dataset for each numeric column. This option can be overwritten using the
`min_value`

and `max_value`

model arguments. These values can either
be set to a numeric value, set to `'auto'`

which is the default setting,
or set to `None`

which will mean the column is boundless.

The model will also learn the number of decimal places to round to by default.
This option can be overwritten using the `rounding`

parameter. The value can
be an int specifying how many decimal places to round to, `'auto'`

which is
the default setting, or `None`

which means the data will not be rounded.

Since we may want to sample values outside of the ranges in the original data,
let’s pass the `min_value`

and `max_value`

arguments as None to the model.
To keep the number of decimals consistent across columns, we can set `rounding`

to be 2.

```
In [35]: model = GaussianCopula(
....: primary_key='student_id',
....: min_value=None,
....: max_value=None,
....: rounding=2
....: )
....:
In [36]: model.fit(data)
In [37]: unbounded_data = model.sample(10)
In [38]: unbounded_data
Out[38]:
student_id gender second_perc high_perc high_spec degree_perc degree_type work_experience experience_years employability_perc mba_spec mba_perc salary placed start_date end_date duration
0 0 M 70.79 73.39 Science 75.09 Sci&Tech False 0 73.48 Mkt&HR 67.83 31230.94 True 2020-01-11 2020-09-10 8.27
1 1 F 45.83 59.89 Science 58.63 Comm&Mgmt False 0 86.19 Mkt&Fin 63.55 NaN False NaT NaT NaN
2 2 M 63.57 71.69 Commerce 51.43 Comm&Mgmt False 0 60.94 Mkt&HR 53.49 30773.68 True 2020-06-02 2020-05-07 -1.33
3 3 M 64.09 67.79 Commerce 72.00 Comm&Mgmt False 0 79.27 Mkt&Fin 70.00 25670.81 True 2020-01-10 2020-06-02 7.04
4 4 F 49.86 62.60 Science 53.08 Comm&Mgmt False 0 61.98 Mkt&HR 59.44 NaN False NaT NaT NaN
5 5 M 77.73 77.63 Science 68.31 Sci&Tech False 0 57.21 Mkt&HR 74.75 NaN False NaT NaT NaN
6 6 F 58.44 69.20 Arts 69.65 Comm&Mgmt False 0 88.87 Mkt&Fin 60.08 26246.67 True 2020-01-16 2020-06-24 5.73
7 7 M 68.78 61.68 Science 53.75 Sci&Tech False 0 70.73 Mkt&HR 59.96 NaN False NaT NaT NaN
8 8 M 72.31 59.23 Science 63.08 Sci&Tech False 0 66.84 Mkt&HR 57.79 41502.98 True 2020-03-10 2020-10-25 7.10
9 9 M 65.94 65.03 Science 67.86 Sci&Tech False 0 84.92 Mkt&Fin 53.93 NaN False NaT NaT NaN
```

As you may notice, the sampled data may have values outside the range of the original data.

### Exploring the Probability Distributions¶

During the previous steps, every time we fitted the `GaussianCopula`

it performed the following operations:

Learn the format and data types of the passed data

Transform the non-numerical and null data using Reversible Data Transforms to obtain a fully numerical representation of the data from which we can learn the probability distributions.

Learn the probability distribution of each column from the table

Transform the values of each numerical column by converting them to their marginal distribution CDF values and then applying an inverse CDF transformation of a standard normal on them.

Learn the correlations of the newly generated random variables.

After this, when we used the model to generate new data for our table
using the `sample`

method, it did:

Sample from a Multivariate Standard Normal distribution with the learned correlations.

Revert the sampled values by computing their standard normal CDF and then applying the inverse CDF of their marginal distributions.

Revert the RDT transformations to go back to the original data format.

As you can see, during these steps the *Marginal Probability
Distributions* have a very important role, since the `GaussianCopula`

had to learn and reproduce the individual distributions of each column
in our table. We can explore the distributions which the
`GaussianCopula`

used to model each column using its
`get_distributions`

method:

```
In [39]: model = GaussianCopula(
....: primary_key='student_id',
....: min_value=None,
....: max_value=None
....: )
....:
In [40]: model.fit(data)
In [41]: distributions = model.get_distributions()
```

This will return us a `dict`

which contains the name of the
distribution class used for each column:

```
In [42]: distributions
Out[42]:
{'gender.value0': 'copulas.univariate.gaussian.GaussianUnivariate',
'gender.value1': 'copulas.univariate.gaussian.GaussianUnivariate',
'second_perc.value': 'copulas.univariate.truncated_gaussian.TruncatedGaussian',
'high_perc.value': 'copulas.univariate.log_laplace.LogLaplace',
'high_spec.value0': 'copulas.univariate.gamma.GammaUnivariate',
'high_spec.value1': 'copulas.univariate.gaussian.GaussianUnivariate',
'high_spec.value2': 'copulas.univariate.gaussian.GaussianUnivariate',
'degree_perc.value': 'copulas.univariate.student_t.StudentTUnivariate',
'degree_type.value0': 'copulas.univariate.gaussian.GaussianUnivariate',
'degree_type.value1': 'copulas.univariate.student_t.StudentTUnivariate',
'degree_type.value2': 'copulas.univariate.gaussian.GaussianUnivariate',
'work_experience.value': 'copulas.univariate.student_t.StudentTUnivariate',
'experience_years.value': 'copulas.univariate.gaussian.GaussianUnivariate',
'employability_perc.value': 'copulas.univariate.truncated_gaussian.TruncatedGaussian',
'mba_spec.value0': 'copulas.univariate.gaussian.GaussianUnivariate',
'mba_spec.value1': 'copulas.univariate.gamma.GammaUnivariate',
'mba_perc.value': 'copulas.univariate.gamma.GammaUnivariate',
'salary.value': 'copulas.univariate.gamma.GammaUnivariate',
'salary.is_null': 'copulas.univariate.gaussian.GaussianUnivariate',
'placed.value': 'copulas.univariate.gamma.GammaUnivariate',
'start_date.value': 'copulas.univariate.gamma.GammaUnivariate',
'start_date.is_null': 'copulas.univariate.gaussian.GaussianUnivariate',
'end_date.value': 'copulas.univariate.gamma.GammaUnivariate',
'end_date.is_null': 'copulas.univariate.gaussian.GaussianUnivariate',
'duration.value': 'copulas.univariate.student_t.StudentTUnivariate',
'duration.is_null': 'copulas.univariate.gaussian.GaussianUnivariate'}
```

Note

In this list we will see multiple distributions for each one of the columns that we have in our data. This is because the RDT transformations used to encode the data numerically often use more than one column to represent each one of the input variables.

Let’s explore the individual distribution of one of the columns in our
data to better understand how the `GaussianCopula`

processed them and
see if we can improve the results by manually specifying a different
distribution. For example, let’s explore the `experience_years`

column
by looking at the frequency of its values within the original data:

```
In [43]: data.experience_years.value_counts()
Out[43]:
0 141
1 65
2 8
3 1
Name: experience_years, dtype: int64
In [44]: data.experience_years.hist();
```

By observing the data we can see that the behavior of the values in this column is very similar to a Gamma or even some types of Beta distribution, where the majority of the values are 0 and the frequency decreases as the values increase.

Was the `GaussianCopula`

able to capture this distribution on its own?

```
In [45]: distributions['experience_years.value']
Out[45]: 'copulas.univariate.gaussian.GaussianUnivariate'
```

It seems that it was not, as it rather thought that the behavior was closer to a Gaussian distribution. And, as a result, we can see how the generated values now contain negative values which are invalid for this column:

```
In [46]: new_data.experience_years.value_counts()
Out[46]:
0 104
1 90
2 6
Name: experience_years, dtype: int64
In [47]: new_data.experience_years.hist();
```

Let’s see how we can improve this situation by passing the
`GaussianCopula`

the exact distribution that we want it to use for
this column.

### Setting distributions for individual variables¶

The `GaussianCopula`

class offers the possibility to indicate which
distribution to use for each one of the columns in the table, in order
to solve situations like the one that we just described. In order to do
this, we need to pass a `field_distributions`

argument with `dict`

that indicates the distribution that we want to use for each column.

Possible values for the distribution argument are:

`univariate`

: Let`copulas`

select the optimal univariate distribution. This may result in non-parametric models being used.`parametric`

: Let`copulas`

select the optimal univariate distribution, but restrict the selection to parametric distributions only.`bounded`

: Let`copulas`

select the optimal univariate distribution, but restrict the selection to bounded distributions only. This may result in non-parametric models being used.`semi_bounded`

: Let`copulas`

select the optimal univariate distribution, but restrict the selection to semi-bounded distributions only. This may result in non-parametric models being used.`parametric_bounded`

: Let`copulas`

select the optimal univariate distribution, but restrict the selection to parametric and bounded distributions only.`parametric_semi_bounded`

: Let`copulas`

select the optimal univariate distribution, but restrict the selection to parametric and semi-bounded distributions only.`gaussian`

: Use a Gaussian distribution.`gamma`

: Use a Gamma distribution.`beta`

: Use a Beta distribution.`student_t`

: Use a Student T distribution.`gaussian_kde`

: Use a GaussianKDE distribution. This model is non-parametric, so using this will make`get_parameters`

unusable.`truncated_gaussian`

: Use a Truncated Gaussian distribution.

Let’s see what happens if we make the `GaussianCopula`

use the
`gamma`

distribution for our column.

```
In [48]: from sdv.tabular import GaussianCopula
In [49]: model = GaussianCopula(
....: primary_key='student_id',
....: field_distributions={
....: 'experience_years': 'gamma'
....: },
....: min_value=None,
....: max_value=None
....: )
....:
In [50]: model.fit(data)
```

After this, we can see how the `GaussianCopula`

used the indicated
distribution for the `experience_years`

column

```
In [51]: model.get_distributions()['experience_years.value']
Out[51]: 'copulas.univariate.gaussian.GaussianUnivariate'
```

And, as a result, we can see how the generated data now have a behavior which is closer to the original data and always stays within the valid values range.

```
In [52]: new_data = model.sample(len(data))
In [53]: new_data.experience_years.value_counts()
Out[53]:
0 106
1 91
-1 10
2 8
Name: experience_years, dtype: int64
In [54]: new_data.experience_years.hist();
```

Note

Even though there are situations like the one shown above where manually
choosing a distribution seems to give better results, in most cases the
`GaussianCopula`

will be able to find the optimal distribution on its
own, making this manual search of the marginal distributions necessary
on very little occasions.

### Conditional Sampling¶

As the name implies, conditional sampling allows us to sample from a conditional
distribution using the `GaussianCopula`

model, which means we can generate only values that
satisfy certain conditions. These conditional values can be passed to the `conditions`

parameter in the `sample`

method either as a dataframe or a dictionary.

In case a dictionary is passed, the model will generate as many rows as requested,
all of which will satisfy the specified conditions, such as `gender = M`

.

```
In [55]: conditions = {
....: 'gender': 'M'
....: }
....:
In [56]: model.sample(5, conditions=conditions)
Out[56]:
student_id gender second_perc high_perc high_spec degree_perc degree_type work_experience experience_years employability_perc mba_spec mba_perc salary placed start_date end_date duration
0 0 M 61.62 64.03 Commerce 64.31 Comm&Mgmt False -1 86.47 Mkt&Fin 53.56 37300.0 True 2020-04-28 2020-06-17 0.0
1 1 M 46.28 36.21 Science 58.16 Comm&Mgmt False 0 58.17 Mkt&HR 48.84 NaN False NaT NaT NaN
2 2 M 86.27 66.22 Commerce 71.35 Comm&Mgmt False 2 59.47 Mkt&Fin 64.56 25400.0 True 2020-01-17 2020-11-02 8.0
3 3 M 81.29 78.95 Commerce 77.88 Comm&Mgmt False 0 86.08 Mkt&Fin 70.58 28600.0 True 2020-05-06 2020-07-10 2.0
4 4 M 64.33 63.75 Commerce 59.90 Sci&Tech False 0 65.43 Mkt&Fin 49.92 23500.0 True 2020-01-17 2020-12-31 12.0
```

It’s also possible to condition on multiple columns, such as
`gender = M, 'experience_years': 0`

.

```
In [57]: conditions = {
....: 'gender': 'M',
....: 'experience_years': 0
....: }
....:
In [58]: model.sample(5, conditions=conditions)
Out[58]:
student_id gender second_perc high_perc high_spec degree_perc degree_type work_experience experience_years employability_perc mba_spec mba_perc salary placed start_date end_date duration
0 0 M 67.43 63.58 Science 58.48 Comm&Mgmt False 0 96.46 Mkt&Fin 63.00 NaN False NaT NaT NaN
1 1 M 82.93 64.73 Science 76.92 Sci&Tech False 0 70.38 Mkt&HR 70.20 29400.0 True 2020-01-12 2020-09-12 7.0
2 2 M 76.31 64.33 Science 66.60 Comm&Mgmt False 0 58.79 Mkt&HR 65.99 NaN False NaT NaT NaN
3 3 M 66.92 57.43 Science 62.69 Sci&Tech False 0 53.65 Mkt&Fin 52.63 NaN False NaT NaT NaN
4 4 M 60.65 63.14 Commerce 60.70 Comm&Mgmt False 0 71.58 Mkt&HR 57.08 32200.0 True 2020-02-06 2020-09-18 5.0
```

The `conditions`

can also be passed as a dataframe. In that case, the model
will generate one sample for each row of the dataframe, sorted in the same
order. Since the model already knows how many samples to generate, passing
it as a parameter is unnecessary. For example, if we want to generate three
samples where `gender = M`

and three samples with `gender = F`

, we can do the
following:

```
In [59]: import pandas as pd
In [60]: conditions = pd.DataFrame({
....: 'gender': ['M', 'M', 'M', 'F', 'F', 'F'],
....: })
....:
In [61]: model.sample(conditions=conditions)
Out[61]:
student_id gender second_perc high_perc high_spec degree_perc degree_type work_experience experience_years employability_perc mba_spec mba_perc salary placed start_date end_date duration
0 0 M 79.59 55.24 Science 69.55 Sci&Tech False 0 91.02 Mkt&Fin 56.57 24200.0 True 2020-01-02 2020-05-02 5.0
1 1 M 53.36 67.54 Commerce 64.77 Comm&Mgmt False 1 51.02 Mkt&Fin 69.45 NaN False NaT NaT NaN
2 2 M 65.93 53.96 Science 52.63 Sci&Tech False -1 63.87 Mkt&HR 62.33 NaN False NaT NaT NaN
3 3 F 70.35 63.70 Science 76.73 Sci&Tech False 0 75.46 Mkt&Fin 69.83 39100.0 True 2020-04-24 2020-07-22 2.0
4 4 F 60.23 63.30 Commerce 77.81 Comm&Mgmt False 1 60.45 Mkt&HR 62.71 21500.0 True 2020-01-02 2020-08-20 9.0
5 5 F 80.83 68.38 Commerce 74.83 Comm&Mgmt False 1 93.91 Mkt&Fin 72.57 NaN False NaT NaT NaN
```

`GaussianCopula`

also supports conditioning on continuous values, as long as the values
are within the range of seen numbers. For example, if all the values of the
dataset are within 0 and 1, `GaussianCopula`

will not be able to set this value to 1000.

```
In [62]: conditions = {
....: 'degree_perc': 70.0
....: }
....:
In [63]: model.sample(5, conditions=conditions)
Out[63]:
student_id gender second_perc high_perc high_spec degree_perc degree_type work_experience experience_years employability_perc mba_spec mba_perc salary placed start_date end_date duration
0 0 M 77.63 78.76 Commerce 70.0 Comm&Mgmt False 1 76.21 Mkt&Fin 61.00 25700.0 True 2020-03-06 2021-02-02 10.0
1 1 F 78.15 65.02 Commerce 70.0 Comm&Mgmt False 0 52.05 Mkt&HR 62.24 19400.0 True 2020-02-27 2020-09-06 6.0
2 2 M 65.09 64.79 Science 70.0 Comm&Mgmt False 0 67.55 Mkt&Fin 62.66 25700.0 True 2020-01-06 2020-08-16 7.0
3 3 M 69.23 71.06 Science 70.0 Others False 1 95.55 Mkt&Fin 68.26 37100.0 True 2020-05-30 2020-07-14 2.0
4 4 M 55.95 67.45 Commerce 70.0 Comm&Mgmt False 0 83.65 Mkt&Fin 56.06 27700.0 True 2020-01-08 2020-06-08 5.0
```

Note

Currently, conditional sampling works through a rejection sampling process,
where rows are sampled repeatedly until one that satisfies the conditions is
found. In case you are running into a ```
Could not get enough valid rows within
x trials
```

or simply wish to optimize the results, there are three parameters
that can be fine-tuned: `max_rows_multiplier`

, `max_retries`

and `float_rtol`

.
More information about these parameters can be found in the API section.

### How do I specify constraints?¶

If you look closely at the data you may notice that some properties were
not completely captured by the model. For example, you may have seen
that sometimes the model produces an `experience_years`

number greater
than `0`

while also indicating that `work_experience`

is `False`

.
These types of properties are what we call `Constraints`

and can also
be handled using `SDV`

. For further details about them please visit
the Constraints guide.

### Can I evaluate the Synthetic Data?¶

A very common question when someone starts using **SDV** to generate
synthetic data is: *“How good is the data that I just generated?”*

In order to answer this question, **SDV** has a collection of metrics
and tools that allow you to compare the *real* that you provided and the
*synthetic* data that you generated using **SDV** or any other tool.

You can read more about this in the Synthetic Data Evaluation guide.