当前位置:网站首页>Common evaluation functions for causal models: SHD and FDR
Common evaluation functions for causal models: SHD and FDR
2022-07-20 09:16:00 【ViviranZ】
Structural Hamming distance (Structural Hamming Distance)
Structural Hamming distance (SHD) Is to compare the standard distance of the graph through the adjacency matrix . It includes calculating two ( Binary system ) The difference between adjacency matrices : Every edge that is missing or not in the target graph is counted as an error . Please note that , For digraphs , Two errors can be counted : The side in the wrong direction is false , The side with good direction is missing ;double_for_anticausal The parameter explains this problem . Set it to " false " Will be regarded as an error .
python Of SHD The calling code is :
cdt.metrics.SHD(target, pred, double_for_anticausal=True)
Where the parameters are :
An example :
from cdt.metrics import SHD
from numpy.random import randint
tar, pred = randint(2, size=(10, 10)), randint(2, size=(10, 10))
SHD(tar, pred, double_for_anticausal=False)
Reference resources :
https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/metrics.html#:~:text=The%20Structural%20Hamming%20Distance%20%28SHD%29%20is%20a%20standard,the%20target%20graph%20is%20counted%20as%20a%20mistake.
False discovery rate (False Discovery Rate)
The false discovery rate is to judge all discovery The wrong and reverse proportions in , namely :
among
It's complicated , Save it directly NOTEARS For the implementation process of several common judgment standards
def count_accuracy(B_true, B_est):
"""Compute various accuracy metrics for B_est.
true positive = predicted association exists in condition in correct direction
reverse = predicted association exists in condition in opposite direction
false positive = predicted association does not exist in condition
Args:
B_true (np.ndarray): [d, d] ground truth graph, {0, 1}
B_est (np.ndarray): [d, d] estimate, {0, 1, -1}, -1 is undirected edge in CPDAG
Returns:
fdr: (reverse + false positive) / prediction positive
tpr: (true positive) / condition positive
fpr: (reverse + false positive) / condition negative
shd: undirected extra + undirected missing + reverse
nnz: prediction positive
"""
if (B_est == -1).any(): # cpdag
if not ((B_est == 0) | (B_est == 1) | (B_est == -1)).all():
raise ValueError('B_est should take value in {0,1,-1}')
if ((B_est == -1) & (B_est.T == -1)).any():
raise ValueError('undirected edge should only appear once')
else: # dag
if not ((B_est == 0) | (B_est == 1)).all():
raise ValueError('B_est should take value in {0,1}')
if not is_dag(B_est):
raise ValueError('B_est should be a DAG')
d = B_true.shape[0]
# linear index of nonzeros
pred_und = np.flatnonzero(B_est == -1)
pred = np.flatnonzero(B_est == 1)# The position of the node that is the parent node in the matrix we obtained (flatten In the following matrix )
cond = np.flatnonzero(B_true)# The position of the node that is the parent node in the real adjacency matrix (flatten In the following matrix )
cond_reversed = np.flatnonzero(B_true.T)# This function inputs a matrix , Returns the position of non-zero elements in the flattened matrix
cond_skeleton = np.concatenate([cond, cond_reversed])# It can splice multiple arrays at one time
# true pos
true_pos = np.intersect1d(pred, cond, assume_unique=True)# Returns the sorted 、 Unique value
# treat undirected edge favorably
true_pos_und = np.intersect1d(pred_und, cond_skeleton, assume_unique=True)
true_pos = np.concatenate([true_pos, true_pos_und])
# false pos
false_pos = np.setdiff1d(pred, cond_skeleton, assume_unique=True)
false_pos_und = np.setdiff1d(pred_und, cond_skeleton, assume_unique=True)
false_pos = np.concatenate([false_pos, false_pos_und])
# reverse
extra = np.setdiff1d(pred, cond, assume_unique=True)
reverse = np.intersect1d(extra, cond_reversed, assume_unique=True)
# compute ratio
pred_size = len(pred) + len(pred_und)
cond_neg_size = 0.5 * d * (d - 1) - len(cond)
fdr = float(len(reverse) + len(false_pos)) / max(pred_size, 1)
tpr = float(len(true_pos)) / max(len(cond), 1)
fpr = float(len(reverse) + len(false_pos)) / max(cond_neg_size, 1)
# structural hamming distance
pred_lower = np.flatnonzero(np.tril(B_est + B_est.T))
cond_lower = np.flatnonzero(np.tril(B_true + B_true.T))
extra_lower = np.setdiff1d(pred_lower, cond_lower, assume_unique=True)
missing_lower = np.setdiff1d(cond_lower, pred_lower, assume_unique=True)
shd = len(extra_lower) + len(missing_lower) + len(reverse)
return {'fdr': fdr, 'tpr': tpr, 'fpr': fpr, 'shd': shd, 'nnz': pred_size}
边栏推荐
- 【深度学习】-Imdb数据集情感分析之模型对比(4)- CNN-LSTM 集成模型
- Seata XA 模式示例分析
- 【论文导读】Learning Bayesian Networks: The Combination of Knowledge and Statistical Data
- 10o Polkadot substrate: develop smart contracts
- Golang:容易误解的一些操作
- Write an Aidl
- What if I forget my MySQL password?
- Android studio executes kotlin throwing com android. builder. errors. Solution to evalissueexception problem
- YOLOv2详解
- Array 常用方法原理模拟,以及常用函数高阶
猜你喜欢
Block level element block inline element inline inline block level element inline block and mutual conversion
uniapp中引入自定义图标
The problem of data set CSV coding format in machine learning
Array 常用方法原理模拟,以及常用函数高阶
MYSQL日志模块
Want to try Web3 work? It's enough to read this article
Android Studio 执行 Kotlin 抛出 com.android.builder.errors.EvalIssueException 问题的解决方法
哈希桶的应用
命令行反编译重打包
电源学习(2)——基本元件
随机推荐
Excel 错误含义
float position
机器学习中数据集csv编码格式问题
MPPT电源控制器设计
Static library A documents and Framework file
CPU架构兼容
Create playablead with cocos Creator
What if I forget my MySQL password?
Talk about redis cache deletion strategy
Crudapi add, delete, modify and check interface zero code product success case Golden Tea King voting system
uniapp 微信小程序分享、分享朋友圈功能
说说 Redis 缓存穿透场景与相应的解决方法
One of online problem positioning -- Arthas
How to capture the data of APP websites
js object相加_js 对象数组属性相同的值合并相加
Crack detection of pytoch migration learning Version (resnet50)
Pytorch实现基本模型
Generate crud restful API by configuring zero code without programming
Redis数据类型和底层数据结构
MYSQL日志模块