-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
This is the implementation of computation of causal penalty i received in the draft notebook:
def compute_causal_penalty(samples, adjacency_matrix, sample_order, categorical=None):
"""
Calculate the inconsistency of samples with the given adjacency matrix.
Parameters:
- adjacency_matrix: np.ndarray, the adjacency matrix from DirectLiNGAM
- samples: np.ndarray, the samples to evaluate (num_samples x num_features)
Returns:
- inconsistency: float, a measure of how inconsistent the samples are with the adjacency matrix
"""
num_samples, num_features = samples.shape
inconsistency = 0.0
if categorical is None:
categorical = [False] * len(sample_order)
# Iterate through each feature and its causal parents
for i in sample_order:
parents = np.where(adjacency_matrix[i, :] != 0)[0]
if len(parents) > 0:
# Predicted values based on parents
predicted_values = np.dot(samples[:, parents], adjacency_matrix[i, parents])
# Calculate the inconsistency as the mean squared error
mse = (samples[:, i] - predicted_values) ** 2
if categorical[i]:
mse = min((1, mse))
inconsistency += mse
return np.sqrt(np.mean(inconsistency)) / len(sample_order)
-
I suggest different calculation for categorical features:
if categorical[i]:
mse = (samples[:, i].round() != predicted_values.round()).astype(float)
else:
mse = (samples[:, i] - predicted_values) ** 2 -
I think categorical features should be entirely skipped from calculating the causality or we need something other than DirectLiNGAM for this.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels