Skip to content

Issue with compute_causal_penalty #12

@she3r

Description

@she3r

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)
  1. 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

  2. I think categorical features should be entirely skipped from calculating the causality or we need something other than DirectLiNGAM for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions