Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range constraint does not produce cases of missing values & may create violative data #1393

Closed
npatki opened this issue Apr 26, 2023 · 0 comments · Fixed by #1404
Closed
Assignees
Labels
bug Something isn't working
Milestone

Comments

@npatki
Copy link
Contributor

npatki commented Apr 26, 2023

  • SDV version: 1.0.1

Error Description

In my dataset, I have a Range constraint such that column A < B < C. Any of the values may be null in different scenarios.

Expected:

  • When present: A < B, B < C and A < C
  • There may be missing values in any of the columns in any combination

Observed:

Fix

The fix is also similar to #1392:

  1. Keep the lowest column A and then replace each other column with the diffs (Replace B with (B-A) and replace C with (C-B))
  2. Create a new categorical column that stores which columns are missing
  3. Replace any originally missing values with the average

image

On the reversal, we can compute the values in a cascading way using the diffs (B = A + diff1, C = B + diff2), and then set the missing values using the categorical column.

Steps to reproduce

import pandas as pd
import numpy as np
from sdv.metadata import SingleTableMetadata
from sdv.single_table import GaussianCopulaSynthesizer

data = pd.DataFrame(data={
    'low':    [1, 4, np.nan, 0,      4,      np.nan, np.nan, 5,      np.nan],
    'middle': [2, 5, 3,      np.nan, 5,      np.nan, 5,      np.nan, np.nan],
    'high':   [3, 7, 8,      4,      np.nan, 9,      np.nan, np.nan, np.nan]
})

metadata_dict = {
    'columns': {
        'low': { 'sdtype': 'numerical' },
        'middle': { 'sdtype': 'numerical'},
        'high': { 'sdtype': 'numerical'}
    }
}

metadata = SingleTableMetadata.load_from_dict(metadata_dict)
synth = GaussianCopulaSynthesizer(metadata)

my_constraint = {
    'constraint_class': 'Range',
    'constraint_parameters': {
        'low_column_name': 'low',
        'middle_column_name': 'middle',
        'high_column_name': 'high'
    }
}

synth.add_constraints(constraints=[my_constraint])
synth.fit(data)

synth_data = synth.sample(2000)
synth_data[synth_data['low'] > synth_data['high']]

image

@npatki npatki added the bug Something isn't working label Apr 26, 2023
@npatki npatki changed the title Range constraint does not produce cases of missing values & may create inaccurate data Range constraint does not produce cases of missing values & may create violative data Apr 26, 2023
@amontanez24 amontanez24 added this to the 1.2.0 milestone Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants