Untitled

 avatar
unknown
python
5 months ago
1.1 kB
2
Indexable
def permutation_test_auc_roc(y_true_labels, y_scores, n_permutations=1000, random_seed=42):
    """
    Perform a permutation test for the AUC-ROC score.
    
    Parameters:
    - y_true_labels: True labels for the dataset.
    - y_scores: Predicted scores for the dataset.
    - n_permutations: Number of permutations to perform (default is 1000).
    - random_seed: Seed for the permutation randomness, kept constant across experiments 
                   to ensure consistent permutation results (this seed is independent of 
                   the experimental seed used for model training).
    
    Returns:
    - p_value: The p-value of the permutation test.
    """
    np.random.seed(random_seed)

    original_auc_roc = roc_auc_score(y_true_labels, y_scores)

    # Permutation test for original AUC-ROC
    permuted_aucs = np.array([roc_auc_score(y_true_labels, np.random.permutation(y_scores)) for _ in range(n_permutations -1)])
    p_value = (np.sum(permuted_aucs >= original_auc_roc) + 1) / (n_permutations -1)

    logging.info(f"AUC-ROC - permutation test: {original_auc_roc:.4f}, p-value: {p_value:.4f}")

    return p_value
Editor is loading...
Leave a Comment