<jupyter_start><jupyter_text>Functions<jupyter_code>def gen_X(N, n_ftr=3):
    X = np.random.normal(0, 1, (N, n_ftr))
    return X

def gen_S(X, thr=0):
    N = len(X)
    S = np.zeros(N)
    S[X[:, 0] < thr] = -1
    S[X[:, 0] >= thr] = 1
    return S
    
def gen_y(X):
    N, n_ftr = X.shape
    return np.dot(X, np.ones(n_ftr) * 3) + np.random.normal(0, 1, N)  

def plot_pred_dist(model, X, S, data = 'test (out of sample)', fair=True):   
    
    y_pred = model.predict(X)

    plt.figure('1')
    plt.hist(y_pred[S == -1], label='s=-1', density=True, stacked=True, bins=20)
    plt.hist(y_pred[S == 1], label='s=+1', alpha=0.3, density=True, stacked=True, bins=20)
    
    if fair:
        plt.title('Distribution of prediction on '+ str(data) +' data with fairness adjustment')
    else:
        plt.title('Distribution of prediction on '+ str(data) +' data without fairness adjustment')
        
    plt.legend()<jupyter_output><empty_output><jupyter_text>Data generation.<jupyter_code>N_train = 500
X_train = gen_X(N_train)
S_train = gen_S(X_train)
y_train = gen_y(X_train)
N_unlab = 1000
X_unlab = gen_X(N_unlab)
N_test = 1000
X_test = gen_X(N_test)
S_test = gen_S(X_test)<jupyter_output><empty_output><jupyter_text>Training regression model on labeled data.<jupyter_code>reg = LinearRegression(fit_intercept=False)
reg.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Training sensitive attribute classifier on labeled data.<jupyter_code>clf = LogisticRegression()
clf.fit(X_train,S_train)<jupyter_output><empty_output><jupyter_text>Training fairness-adjusted regression model on unlabeled data.<jupyter_code>fair_reg = RegSensOut(reg, clf)
fair_reg.fit(X_unlab)<jupyter_output><empty_output><jupyter_text>Plots<jupyter_code>plot_pred_dist(fair_reg, X_test, S_test, data = 'test (out of sample)', fair=True)
plot_pred_dist(reg, X_test, S_test, data = 'test (out of sample)', fair=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1433710174717369, 'fraction_numerical': 0.01695786228160329, 'mean_word_length': 5.122641509433962, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '371216', 'license': 'MIT', 'n_tokens_mistral': 825, 'n_tokens_neox': 774, 'n_words': 147, 'path': 'examples/Discretization-RegSensOut.ipynb', 'repo_name': 'taturyan/fairness', 'size': '1946'}