Welcome to Fuzzy Causal’s documentation!

Causal Estimation Example on IHDP :

import numpy as np
import pandas as pd
from source.utils import Score
from source.Fuzzy import Fuzzy
from sklearn.model_selection import train_test_split



df= pd.read_csv('https://raw.githubusercontent.com/AMLab-Amsterdam/CEVAE/master/datasets/IHDP/csv/ihdp_npci_1.csv', header = None)

df.dataframeName = 'data'

cols =  ["treatment", "y_factual", "y_cfactual", "mu0", "mu1"] + [i for i in range(25)]

df.columns = cols
print(df.head())


#precising variables type
binfeats = [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
contfeats = [i for i in range(25) if i not in binfeats]
perm = binfeats + contfeats
df = df.reset_index(drop=True)
df.head()

X = df[perm].values
treatment = df['treatment'].values
y = df['y_factual'].values
y_cf = df['y_cfactual'].values
tau = df.apply(lambda y: y['y_factual'] - y['y_cfactual'] if y['treatment']==1
               else y['y_cfactual'] - y['y_factual'],
               axis=1)
mu_0 = df['mu0'].values
mu_1 = df['mu1'].values

# train and test
itr, ite = train_test_split(np.arange(X.shape[0]), test_size=0.2, random_state=1)
X_train, treatment_train, y_train, y_cf_train, tau_train, mu_0_train, mu_1_train = X[itr], treatment[itr], y[itr], y_cf[itr], tau[itr], mu_0[itr], mu_1[itr]
X_val, treatment_val, y_val, y_cf_val, tau_val, mu_0_val, mu_1_val = X[ite], treatment[ite], y[ite], y_cf[ite], tau[ite], mu_0[ite], mu_1[ite]

m=min(len(y_val),len(y_train))

test=Fuzzy()
new=test.fuzzify(df,[1,2],0,1)
eval=Score(y_train[:m],treatment_train[:m],y_cf_train[:m],mu_0_train[:m],mu_1_train[:m])
print('ATE ',eval.evaluate(y_val,y_cf_val)[1])
y = new['y_factual_low'].values
y_cf = new['y_cfactual_low'].values
y_val, y_cf_val=y[ite], y_cf[ite]
low=eval.evaluate(y_val,y_cf_val)[1]
print('ATE low ',low)
y = new['y_factual_average'].values
y_cf = new['y_cfactual_average'].values
y_val, y_cf_val=y[ite], y_cf[ite]
medium=eval.evaluate(y_val,y_cf_val)[1]
print('ATE medium ',medium)
y = new['y_factual_high'].values
y_cf = new['y_cfactual_high'].values
y_val, y_cf_val=y[ite], y_cf[ite]
high=eval.evaluate(y_val,y_cf_val)[1]
print('ATE high',high)
print('The average value of low, medium and high :', np.mean([low,medium,high]))
ATE low 3.8494642946001822
ATE medium 4.091268219603135
ATE high 4.255198638764754
The average value of low, medium and high : 4.06531038432269

Indices and tables

Fuzzy Input

class source.Input.Fuzzy_input[source]

class aiming to manipulate vectors and tensors and return the fuzzified version of it’s input

fuzzify(data: FrameOrSeries, distribution: str, plot=False)FrameOrSeries[source]

Class method aiming to fuzzify only on data frame serie at once The output of every single serie will be appended to one fuzzified dataframe containing all equivalent fuzzified dataframe series.

data : Pandas DataFrame, serie to fuzzify. distribution : string ,to precise the fuzzy function distribution triangular or gaussian. plot : boolean to plot , False by default .

out : list of fuzzy sets.

Notes

The process of precising the Closure of the given data and the number of fuzzy sets is automated , Thus the User is left to input only the distribution of the fuzzy membership function.

Examples

>>> df= pd.DataFrame({'Data':np.arange(5)})
# Generate fuzzy membership
>>> Test=Fuzzy_only()
>>> out = Test.fuzzify(df,'gaussian')
>>> out
[array([[1.        ],
   [0.81873075],
   [0.44932896],
   [0.16529889],
   [0.0407622 ]]), array([[0.44932896],
   [0.81873075],
   [1.        ],
   [0.81873075],
   [0.44932896]]), array([[0.0407622 ],
   [0.16529889],
   [0.44932896],
   [0.81873075],
   [1.        ]])]
fuzzify_all(data: FrameOrSeries, distribution: str)FrameOrSeries[source]

A function that fuzzifies mutliple variables at once

datapandas DataFrame , The tensor to fuzzify containing the

variables columns names and values.

distribution : string , to precise the fuzzy function distribution triangular or gaussian.

A fuzzified DataFrame of the input data

See also

fuzzify

Fuzzy Fuzzification

class source.Fuzzy.Fuzzy[source]

intialize the fuzzy class

fuzzify(data: FrameOrSeries, cont_feats: list, potencial_index: int, Target_index: int)FrameOrSeries[source]

Class method aiming to fuzzify only on data frame serie at once The output of every single serie will be appended to one fuzzified dataframe containing all equivalent fuzzified dataframe series.

data : Pandas DataFrame, serie to fuzzify. cont_feats: list , precising which variables are continuous to an index list. potencial_index : int , index of the variable to to estimate it’s effect(eq. to treatment) Target_index : int, index of the variable to search it’s causes(eq. to outcome)

out : list of fuzzy sets.

Notes

The process of precising the Closure of the given data and the number of fuzzy sets is automated , This class adds also the automoation of numbers of fuzzy sets using fuzzy C-means Clustering testing multiple outcomes with a number of clusters ranging from 2 to 9 and using the optimal number of fuzzy sets corresponding to the highest fuzzy partition coefficient(In case if the input data has more than two continuous variables so it is possible to apply the clustering).

Fuzzy Score

class source.utils.Score(y, t, y_cf=None, mu0=None, mu1=None)[source]

Class aiming to evaluate the causal effect metrics

ate(ypred1, ypred0)[source]

ypred1 ypred0

out : float , the real ate corresponding to mu1-mu0

evaluate(ypred1, ypred0)tuple[source]

method aiming to compute the causal effect metrics ate : average treatment effect ite : individual treatment effect

ypred1 : list ,outcome when treatment is given ypred0 : list , outcome of the control group

output : tuple , (ite, ate)