Python code for Uplift model with low-rank regularization logistic regression for multiple correlated binary responses. and calculate Individual Treatment Effect (ITE)
时间: 2024-03-12 18:50:16 浏览: 79
Here is an example Python code for Uplift model with low-rank regularization logistic regression for multiple correlated binary responses and calculating Individual Treatment Effect (ITE).
```python
import numpy as np
from sklearn.linear_model import LogisticRegression
from scipy.optimize import minimize
def fit_low_rank_regression(X, Y, Z, r=10, alpha=0.01):
n, p = X.shape
q = Y.shape[1]
U, s, Vt = np.linalg.svd(Z)
U = U[:, :r]
Vt = Vt[:r, :]
S_inv = np.diag(1/s[:r])
X_tilde = U.T @ X @ Vt.T
Y_tilde = U.T @ Y
model = LogisticRegression(penalty='none', solver='lbfgs')
beta = np.zeros((p, q))
for j in range(q):
beta[:, j] = model.fit(X_tilde, Y_tilde[:, j]).coef_
beta = Vt.T @ beta @ S_inv
return beta
def estimate_ite(X, Y, Z, delta):
p = X.shape[1]
beta = fit_low_rank_regression(X, Y, Z)
eta = X @ beta
Y_pred = np.exp(eta)/(1+np.exp(eta))
ite = np.zeros(p)
for i in range(p):
Y_treat = Y_pred.copy()
Y_treat[:, i] = np.exp(eta[:, i] + delta)/(1+np.exp(eta[:, i] + delta))
ite[i] = np.mean(Y_treat - Y_pred)
return ite
```
Input:
- `X`: the matrix of features (n x p)
- `Y`: the matrix of binary responses (n x q)
- `Z`: the matrix of treatment indicators (n x r)
- `delta`: the treatment effect for each feature (p x 1)
Output:
- `ite`: the Individual Treatment Effect for each feature (p x 1)
Example usage:
```python
# generate synthetic data
n = 1000
p = 5
q = 3
r = 2
X = np.random.normal(size=(n, p))
Z = np.random.binomial(n=1, p=0.5, size=(n, r))
beta_true = np.random.normal(size=(p, q))
Y_true = (X @ beta_true) * Z + np.random.normal(size=(n, q))
delta_true = np.random.normal(size=(p, 1))
# estimate ITE
ite = estimate_ite(X, Y_true, Z, delta_true)
print(ite)
```
阅读全文