-
Notifications
You must be signed in to change notification settings - Fork 317
/
copulas.py
441 lines (358 loc) · 16.9 KB
/
copulas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
"""Wrappers around copulas models."""
import inspect
import logging
import warnings
from copy import deepcopy
import copulas
import copulas.univariate
import numpy as np
import pandas as pd
import scipy
from copulas import multivariate
from rdt.transformers import OneHotEncoder
from sdv.errors import NonParametricError
from sdv.single_table.base import BaseSingleTableSynthesizer
from sdv.single_table.utils import (
flatten_dict,
log_numerical_distributions_error,
unflatten_dict,
validate_numerical_distributions,
)
LOGGER = logging.getLogger(__name__)
class GaussianCopulaSynthesizer(BaseSingleTableSynthesizer):
"""Model wrapping ``copulas.multivariate.GaussianMultivariate`` copula.
Args:
metadata (sdv.metadata.Metadata):
Single table metadata representing the data that this synthesizer will be used for.
* sdv.metadata.SingleTableMetadata can be used but will be deprecated.
enforce_min_max_values (bool):
Specify whether or not to clip the data returned by ``reverse_transform`` of
the numerical transformer, ``FloatFormatter``, to the min and max values seen
during ``fit``. Defaults to ``True``.
enforce_rounding (bool):
Define rounding scheme for ``numerical`` columns. If ``True``, the data returned
by ``reverse_transform`` will be rounded as in the original data. Defaults to ``True``.
locales (list or str):
The default locale(s) to use for AnonymizedFaker transformers.
Defaults to ``['en_US']``.
numerical_distributions (dict):
Dictionary that maps field names from the table that is being modeled with
the distribution that needs to be used. The distributions can be passed as either
a ``copulas.univariate`` instance or as one of the following values:
* ``norm``: Use a norm distribution.
* ``beta``: Use a Beta distribution.
* ``truncnorm``: Use a truncnorm distribution.
* ``uniform``: Use a uniform distribution.
* ``gamma``: Use a Gamma distribution.
* ``gaussian_kde``: Use a GaussianKDE distribution. This model is non-parametric,
so using this will make ``get_parameters`` unusable.
default_distribution (str):
Copulas univariate distribution to use by default. Valid options are:
* ``norm``: Use a norm distribution.
* ``beta``: Use a Beta distribution.
* ``truncnorm``: Use a Truncated Gaussian distribution.
* ``uniform``: Use a uniform distribution.
* ``gamma``: Use a Gamma distribution.
* ``gaussian_kde``: Use a GaussianKDE distribution. This model is non-parametric,
so using this will make ``get_parameters`` unusable.
Defaults to ``beta``.
"""
_DISTRIBUTIONS = {
'norm': copulas.univariate.GaussianUnivariate,
'beta': copulas.univariate.BetaUnivariate,
'truncnorm': copulas.univariate.TruncatedGaussian,
'gamma': copulas.univariate.GammaUnivariate,
'uniform': copulas.univariate.UniformUnivariate,
'gaussian_kde': copulas.univariate.GaussianKDE,
}
@classmethod
def get_distribution_class(cls, distribution):
"""Return the corresponding distribution class from ``copulas.univariate``.
Args:
distribution (str):
A string representing a copulas univariate distribution.
Returns:
copulas.univariate:
A copulas univariate class that corresponds to the distribution.
"""
if not isinstance(distribution, str) or distribution not in cls._DISTRIBUTIONS:
error_message = f"Invalid distribution specification '{distribution}'."
raise ValueError(error_message)
return cls._DISTRIBUTIONS[distribution]
def __init__(
self,
metadata,
enforce_min_max_values=True,
enforce_rounding=True,
locales=['en_US'],
numerical_distributions=None,
default_distribution=None,
):
super().__init__(
metadata,
enforce_min_max_values=enforce_min_max_values,
enforce_rounding=enforce_rounding,
locales=locales,
)
validate_numerical_distributions(numerical_distributions, self.metadata.columns)
self.default_distribution = default_distribution or 'beta'
self._default_distribution = self.get_distribution_class(self.default_distribution)
self._set_numerical_distributions(numerical_distributions)
self._num_rows = None
def _set_numerical_distributions(self, numerical_distributions):
self.numerical_distributions = numerical_distributions or {}
self._numerical_distributions = {
field: self.get_distribution_class(distribution)
for field, distribution in self.numerical_distributions.items()
}
def _fit(self, processed_data):
"""Fit the model to the table.
Args:
processed_data (pandas.DataFrame):
Data to be learned.
"""
log_numerical_distributions_error(
self.numerical_distributions, processed_data.columns, LOGGER
)
self._num_rows = self._learn_num_rows(processed_data)
numerical_distributions = self._get_numerical_distributions(processed_data)
self._model = self._initialize_model(numerical_distributions)
self._fit_model(processed_data)
def _learn_num_rows(self, processed_data):
return len(processed_data)
def _get_numerical_distributions(self, processed_data):
numerical_distributions = deepcopy(self._numerical_distributions)
for column in processed_data.columns:
if column not in numerical_distributions:
numerical_distributions[column] = self._numerical_distributions.get(
column, self._default_distribution
)
return numerical_distributions
def _initialize_model(self, numerical_distributions):
return multivariate.GaussianMultivariate(distribution=numerical_distributions)
def _fit_model(self, processed_data):
with warnings.catch_warnings():
warnings.filterwarnings('ignore', module='scipy')
self._model.fit(processed_data)
def _warn_quality_and_performance(self, column_name_to_transformer):
"""Raise warning if the quality/performance may be impacted.
Args:
column_name_to_transformer (dict):
Dict mapping column names to transformers to be used for that column.
"""
for column, transformer in column_name_to_transformer.items():
if isinstance(transformer, OneHotEncoder):
warnings.warn(
f"Using a OneHotEncoder transformer for column '{column}' "
'may slow down the preprocessing and modeling times.'
)
def _sample(self, num_rows, conditions=None):
"""Sample the indicated number of rows from the model.
Args:
num_rows (int):
Amount of rows to sample.
conditions (dict):
If specified, this dictionary maps column names to the column
value. Then, this method generates ``num_rows`` samples, all of
which are conditioned on the given variables.
Returns:
pandas.DataFrame:
Sampled data.
"""
return self._model.sample(num_rows, conditions=conditions)
def _get_valid_columns_from_metadata(self, columns):
valid_columns = []
for column in columns:
for valid_column in self.metadata.columns:
if column.startswith(valid_column):
valid_columns.append(column)
break
return valid_columns
def get_learned_distributions(self):
"""Get the marginal distributions used by the ``GaussianCopula``.
Return a dictionary mapping the column names with the distribution name and the learned
parameters for those.
Returns:
dict:
Dictionary containing the distributions used or detected for each column and the
learned parameters for those.
"""
if not self._fitted:
raise ValueError(
"Distributions have not been learned yet. Please fit your model first using 'fit'."
)
parameters = self._model.to_dict()
columns = parameters['columns']
univariates = deepcopy(parameters['univariates'])
learned_distributions = {}
valid_columns = self._get_valid_columns_from_metadata(columns)
for column, learned_params in zip(columns, univariates):
if column in valid_columns:
distribution = self.numerical_distributions.get(column, self.default_distribution)
learned_params.pop('type')
learned_distributions[column] = {
'distribution': distribution,
'learned_parameters': learned_params,
}
return learned_distributions
def _get_parameters(self):
"""Get copula model parameters.
Compute model ``correlation`` and ``distribution.std``
before it returns the flatten dict.
Returns:
dict:
Copula parameters.
Raises:
NonParametricError:
If a non-parametric distribution has been used.
"""
for univariate in self._model.univariates:
univariate_type = type(univariate)
if univariate_type is copulas.univariate.Univariate:
univariate = univariate._instance
if univariate.PARAMETRIC == copulas.univariate.ParametricType.NON_PARAMETRIC:
raise NonParametricError('This GaussianCopula uses non parametric distributions')
params = self._model.to_dict()
correlation = []
for index, row in enumerate(params['correlation'][1:]):
correlation.append(row[: index + 1])
params['correlation'] = correlation
params['univariates'] = dict(zip(params.pop('columns'), params['univariates']))
params['num_rows'] = self._num_rows
return flatten_dict(params)
@staticmethod
def _get_nearest_correlation_matrix(matrix):
"""Find the nearest correlation matrix.
If the given matrix is not Positive Semi-definite, which means
that any of its eigenvalues is negative, find the nearest PSD matrix
by setting the negative eigenvalues to 0 and rebuilding the matrix
from the same eigenvectors and the modified eigenvalues.
After this, the matrix will be PSD but may not have 1s in the diagonal,
so the diagonal is replaced by 1s and then the PSD condition of the
matrix is validated again, repeating the process until the built matrix
contains 1s in all the diagonal and is PSD.
After 10 iterations, the last step is skipped and the current PSD matrix
is returned even if it does not have all 1s in the diagonal.
Insipired by: https://stackoverflow.com/a/63131250
"""
eigenvalues, eigenvectors = scipy.linalg.eigh(matrix)
negative = eigenvalues < 0
identity = np.identity(len(matrix))
iterations = 0
while np.any(negative):
eigenvalues[negative] = 0
matrix = eigenvectors.dot(np.diag(eigenvalues)).dot(eigenvectors.T)
if iterations >= 10:
break
matrix = matrix - matrix * identity + identity
max_value = np.abs(np.abs(matrix).max())
if max_value > 1:
matrix /= max_value
eigenvalues, eigenvectors = scipy.linalg.eigh(matrix)
negative = eigenvalues < 0
iterations += 1
return matrix
@classmethod
def _rebuild_correlation_matrix(cls, triangular_correlation):
"""Rebuild a valid correlation matrix from its lower half triangle.
The input of this function is a list of lists of floats of size 1, 2, 3...n-1:
[[c_{2,1}], [c_{3,1}, c_{3,2}], ..., [c_{n,1},...,c_{n,n-1}]]
Corresponding to the values from the lower half of the original correlation matrix,
**excluding** the diagonal.
The output is the complete correlation matrix reconstructed using the given values
and scaled to the :math:`[-1, 1]` range if necessary.
Args:
triangle_correlation (list[list[float]]):
A list that contains lists of floats of size 1, 2, 3... up to ``n-1``,
where ``n`` is the size of the target correlation matrix.
Returns:
numpy.ndarray:
rebuilt correlation matrix.
"""
zero = [0.0]
size = len(triangular_correlation) + 1
left = np.zeros((size, size))
right = np.zeros((size, size))
for idx, values in enumerate(triangular_correlation):
values = values + zero * (size - idx - 1)
left[idx + 1, :] = values
right[:, idx + 1] = values
correlation = left + right
max_value = np.abs(correlation).max()
if max_value > 1:
correlation /= max_value
correlation += np.identity(size)
return cls._get_nearest_correlation_matrix(correlation).tolist()
def _rebuild_gaussian_copula(self, model_parameters, default_params=None):
"""Rebuild the model params to recreate a Gaussian Multivariate instance.
Args:
model_parameters (dict):
Sampled and reestructured model parameters.
default_parameters (dict):
Fall back parameters if sampled params are invalid.
Returns:
dict:
Model parameters ready to recreate the model.
"""
if default_params is None:
default_params = {}
columns = []
univariates = []
for column, univariate in model_parameters['univariates'].items():
columns.append(column)
if column in self._numerical_distributions:
univariate_type = self._numerical_distributions[column]
else:
univariate_type = self.get_distribution_class(self.default_distribution)
univariate['type'] = univariate_type
model = univariate_type.MODEL_CLASS
if hasattr(model, '_argcheck'):
to_check = {
parameter: univariate[parameter]
for parameter in inspect.signature(model._argcheck).parameters.keys()
if parameter in univariate
}
if not model._argcheck(**to_check):
if column in default_params.get('univariates', []):
LOGGER.info(
f"Invalid parameters sampled for column '{column}', "
'using default parameters.'
)
univariate = default_params['univariates'][column]
univariate['type'] = univariate_type
else:
LOGGER.debug(f"Column '{column}' has invalid parameters.")
else:
LOGGER.debug(f"Univariate for col '{column}' does not have _argcheck method.")
if 'scale' in univariate:
univariate['scale'] = max(0, univariate['scale'])
univariates.append(univariate)
model_parameters['univariates'] = univariates
model_parameters['columns'] = columns
correlation = model_parameters.get('correlation')
if correlation:
model_parameters['correlation'] = self._rebuild_correlation_matrix(correlation)
else:
model_parameters['correlation'] = [[1.0]]
return model_parameters
def _get_likelihood(self, table_rows):
return self._model.probability_density(table_rows)
def _set_parameters(self, parameters, default_params=None):
"""Set copula model parameters.
Args:
params [dict]:
Copula flatten parameters.
default_params [list]:
Flattened list of parameters to fall back to if `params` are invalid.
"""
if default_params is not None:
default_params = unflatten_dict(default_params)
else:
default_params = {}
parameters = unflatten_dict(parameters)
if 'num_rows' in parameters:
num_rows = parameters.pop('num_rows')
self._num_rows = 0 if pd.isna(num_rows) else max(0, int(round(num_rows)))
if parameters:
parameters = self._rebuild_gaussian_copula(parameters, default_params)
self._model = multivariate.GaussianMultivariate.from_dict(parameters)