我在bnlearn
中使用R
软件包,我想知道该软件包如何计算BIC-g(高斯分布中的BIC)。
让我们建立一个结构,我可以找到如下的BIC分数
library(bnlearn)
X = iris[, 1:3]
names(X) = c("A", "B", "C")
Network = empty.graph(names(X))
bnlearn::score(Network, X, type="bic-g")
bnlearn
为我提供了有关如何计算此分数的更详细的信息,
bnlearn::score(Network, X, type="bic-g", debug=TRUE)
这导致
----------------------------------------------------------------
* processing node A.
> loglikelihood is -184.041441.
> penalty is 2.505318 x 2 = 5.010635.
----------------------------------------------------------------
* processing node B.
> loglikelihood is -87.777815.
> penalty is 2.505318 x 2 = 5.010635.
----------------------------------------------------------------
* processing node C.
> loglikelihood is -297.588727.
> penalty is 2.505318 x 2 = 5.010635.
[1] -584.4399
我知道如何计算贝叶斯网络中离散数据的BIC,请参阅here。但是我不知道如何将其推广到联合高斯(多元正态)情况。
肯定地,它可能与近似似然和惩罚项有关,并且看来打包过程计算每个节点的似然和惩罚,然后将它们相加。
bnlearn::score(Network, X, type="loglik-g", debug=TRUE)
但是我想知道如何在给定数据的情况下具体计算可能性和惩罚。
我发现解释Laplace Approximation
的{{3}}(请参阅第57页),但是我无法将其关联。
有人帮我吗?
答案 0 :(得分:1)
BIC计算为
BIC = -2 * logLik + nparams * log(nobs)
但是在bnlearn
中,它被重新缩放了-2(请参见?score
)以得到
BIC = logLik -0.5 * nparams * log(nobs)
因此,在您的示例中,没有边缘时,使用边际均值计算似然性,并且误差(或更笼统地说,对于每个节点,参数数量由1(截距)+1(残差)+父母人数),例如
library(bnlearn)
X = iris[, 1:3]
names(X) = c("A", "B", "C")
Network = empty.graph(names(X))
(ll = sum(sapply(X, function(i) dnorm(i, mean(i), sd(i), log=TRUE))))
#[1] -569.408
(penalty = 0.5* log(nrow(X))* 6)
#[1] 15.03191
ll - penalty
#[1] -584.4399
如果存在边,则使用拟合值和残差计算对数似然。对于网络:
Network = set.arc(Network, "A", "B")
我们需要来自节点A和C的对数似然组件
(llA = with(X, sum(dnorm(A, mean(A), sd(A), log=TRUE))))
#[1] -184.0414
(llC = with(X, sum(dnorm(C, mean(C), sd(C), log=TRUE))))
#[1] -297.5887
然后我们通过线性回归获得B的条件概率
m = lm(B ~ A, X)
(llB = with(X, sum(dnorm(B, fitted(m), stats::sigma(m), log=TRUE))))
#[1] -86.73894
给予
(ll = llA + llB + llC)
#[1] -568.3691
(penalty = 0.5* log(nrow(X))* 7)
#[1] 17.53722
ll - penalty
#[1] -585.9063
# bnlearn::score(Network, X, type="bic-g", debug=TRUE)
# ----------------------------------------------------------------
# * processing node A.
# loglikelihood is -184.041441.
# penalty is 2.505318 x 2 = 5.010635.
# ----------------------------------------------------------------
# * processing node B.
# loglikelihood is -86.738936.
# penalty is 2.505318 x 3 = 7.515953.
# ----------------------------------------------------------------
# * processing node C.
# loglikelihood is -297.588727.
# penalty is 2.505318 x 2 = 5.010635.
# [1] -585.9063