算法_贝叶斯网络学习_bayesian networks

基本概念

 条件概率 联合概率 边缘概率
  链式法则 随机变量的独立性 条件独立性
  贝叶斯规则、贝叶斯概率推理和贝叶斯网络模型。
stochastic,主要用作形容词,主要意思为“随机的;猜测的”

R语言包

R语言用lme4多层次(混合效应)广义线性模型(GLM),逻辑回归分析
   lme4广义线性混合模型(GLMM)和线性混合模型(LMM) 
gRain包 R包gRain可以用来建立贝叶斯网,表示在模型中概率的传播 
pcalg包做贝叶斯网络结构的学习	

无人驾驶行为决策研究--Intelligent connected vehicles (ICVs)
   Bayesian networks (BNs), 
   artificial neural networks (ANNs), 
   and support vector machines (SVMs)
   在人工智能决策中,贝叶斯定理可以用来计算事件的概率,并作为基础来做出决策
 结构学习算法(结构和参数都是从数据中学习的)。
 参数学习算法(结构由用户提供,参数从数据中学习)

贝叶斯网

 bayesian networks是一种有向无环图模型(DAG),可表示为G=(V,A)。
  其中V是节点的集合,节点表示随机变量;
     A是弧(或称为边)的集合,弧的箭头表示随机变量之间的概率相依性。
     有向无环图DAG定义了一个因子化的V中全体节点的联合概率分布,称为全局概率分布;
     相对的,与每个随机变量关联的,为局部概率分布。
 学习网络结构,然后在此基础上估计局部分布函数的参数
  causal model -- 因果模式

贝叶斯网学习

 BN学习包含结构学习和参数学习--参数学习建立在结构已知的基础上--因此网络的结构学习是关键
     贝叶斯网络结构构造方法
	     专家领域经验结构手工构造
		 通过数据分析构造:推理算法分精确推理和近似推理
 1、确定随机变量间的拓扑关系,形成DAG。
 2、训练贝叶斯网络参数——估计出各节点的条件概率表。这一步也就是要完成条件概率表的构造

网络结构学习

    分连(diverging connection) 顺连(serial connection) 汇连(converging connection)
	  tail-to-tail(共同原因)
	  head-to-tail(因果迹)  tail-to-head(证据迹)
	  head-to-head(共同作用)  
	
    结构学习算法可分为三类:基于约束的算法、基于得分的算法以及混合算法。
       Structure learning algorithms: 
	   constraint-based (PC Stable, Grow-Shrink, IAMB, Fast-IAMB, Inter-IAMB, IAMB-FDR, MMPC, Semi-Interleaved HITON-PC, HPC), 
       score-based (hill climbing and tabu search)  爬山法(hc) 禁忌搜索(tabu)
       hybrid (MMHC, H2PC, RSMAX2).	  MMPC (max-min hill climbing) 算法
	进行本质上的路径分析和研究弧的强度   

网络参数学习方式

参数学习:在给定网络结构时,确定网络参数,即参数估计问题:
      Parameter learning methods: maximum likelihood, Bayesian, hierarchical and Expectation-Maximization parameter estimators.
	  极大似然估计--贝叶斯后验估计

推断问题

 在已知部分变量时,计算其他变量的条件概率分布
  k-fold交叉验证;	hold-out 交叉验证 

数据

预处理和探索性数据分析
   .rds和.rda(.Rdata的简称 )文件都可用于以R本机格式存储R对象。
     2.Rdata 可以保存多个对象,RDS仅处理单个R对象

网络理解

创建一个空网络,节点对应于marks的变量。然后通过指派一个两列的矩阵来添加边。	 
  bnlearn包中管理网络结构。 这个对象包括三个方面的信息:
    (1)learning:结构的学习
	(2)node 节点
	(3)arc 边
 ##可以从邻接矩阵(adjacency matrix)来生成有向图dg。	
 ##手工修改一个已经存在的网络,利用加边(set.arc)、去边(drop.arc)、颠倒(rev.arc)这几个操作,也可以得到一个需要的网
##  节点的邻居(nbr)和Markov毯(mb) (1)节点的拓扑顺序  (3)某个给定节点的子代(child)和父代(parents)和子代的其它父代(o.par)	
平均的网络(avg.diff)

绘制网络

 2.绘制网络
   bnlearn包有两种对bn对象绘制网络结构的方法,
    一种是利用graph包和Rgraphviz包提供的接口给出的一些绘图函数,比如graphviz.plot。
	再一种方法是利用plot函	

案例

#install.packages("bnlearn")
# if (!requireNamespace("BiocManager", quietly = TRUE))
# install.packages("BiocManager")
# BiocManager::install("Rgraphviz")
#of the graph, its adjacency matrix or a model formula(边集、邻接矩阵、模型公式)。
###创建三种网络:空网络、特定网络、随机网络
#创建特定网络网络的三种方法:边集、邻接矩阵、模型公式  结构学习-参数学习-推理-比较两个贝叶斯网络
## 案例: 假设知道DAG和参数--应用与推理
#  assumed to know both the DAG and the parameters of the local distributions defining the BN
# 1.导入依赖
library(bnlearn)

# 通过empty.graph() 和random.graph()函数创建空网络结构或随机网络结构 ,默认只产生一个bn对象,可以指定参数num,使之产生一批网络对象
# 2、创建特定网络结构
dag <- empty.graph(nodes = c("A","S","E","O","R","T"))
# ①特定的边集 要产生边集,首先需要指定一个边集的矩阵-边的标签要和图里的标签一致 
arc.set <- matrix(c("A", "E",
                    "S", "E",
                    "E", "O",
                    "E", "R",
                    "O", "T",
                    "R", "T"),
                  byrow = TRUE, ncol = 2,
                  dimnames = list(NULL, c("from", "to")))
# 将产生的边集arc.set传给bn对象使用arcs()函数
arcs(dag) <- arc.set
###3.查看网络结构
nodes(dag)
arcs(dag)
plot(dag)
graphviz.plot(dag, layout = "dot")

###4.导入数据 以及数据的取值
## 数据的列是factor数据类型-行数要相同
###read_data 通过as.factor方法将字符串向量转化为因子向量
#survey <- read.table("data/survey.txt", header = TRUE)
#Age (A) Residence (R): Education (E)  Occupation (O)  Sex (S) Travel (T)  
survey =data.frame( A= as.factor(c("adult","young","old","old")),
                    R= as.factor(c("big","small","big","big")),
                    E= as.factor(c("high","uni","high","high")),
                    O= as.factor(c("emp","self","emp","emp")), 
                    S= as.factor(c("F","M","F","M")),
                    T= as.factor(c("car","train","train", "other"))
			  )
attributes(survey )
A.lv <- c("young", "adult", "old")
S.lv <- c("M", "F")
E.lv <- c("high", "uni")
O.lv <- c("emp", "self")
R.lv <- c("small", "big")
T.lv <- c("car", "train", "other")
### 数据验概率-根据公式
A.prob <- array(c(0.3,0.5,0.2), dim = 3, dimnames = list(A = A.lv))
S.prob <- array(c(0.6,0.4), dim = 2, dimnames = list(S = S.lv))
E.prob <- array(c(0.75,0.25,0.72,0.28,0.88,0.12,0.64,0.36,0.70,0.30,0.90,0.10), dim = c(2,3,2), dimnames = list(E = E.lv, A = A.lv, S = S.lv))
O.prob <- array(c(0.96,0.04,0.92,0.08), dim = c(2,2), dimnames = list(O = O.lv, E = E.lv))
R.prob <- array(c(0.25,0.75,0.2,0.8), dim = c(2,2), dimnames = list(R = R.lv, E = E.lv))
T.prob <- array(c(0.48,0.42,0.10,0.56,0.36,0.08,0.58,0.24,0.18,0.70,0.21,0.09), dim = c(3,2,2), dimnames = list(T = T.lv, O = O.lv, R = R.lv))
cpt <- list(A = A.prob, S = S.prob, E = E.prob, O = O.prob, R = R.prob, T = T.prob)

## 贝叶斯网络
bn <- custom.fit(dag, cpt)

##估计条件概率的参数
bn.mle <- bn.fit(dag, data = survey, method = "mle")
bn.bayes <- bn.fit(dag, data = survey, method = "bayes", iss = 10)


# predicting a variable in the test set.
# predicting a variable in the test set.
training = bn.fit(model2network("[A][B][E][G][C|A:B][D|B][F|A:D:E:G]"),
                  gaussian.test[1:2000, ])
test = gaussian.test[2001:nrow(gaussian.test), ]
predicted <- predict(training, node = "A", data = test, method = "bayes-lw")


##https://bookdown.org/robertness/causalml/docs/tutorial-probabilistic-modeling-with-bayesian-networks-and-bnlearn.html

参考

 https://cran.r-project.org/web/packages/bnlearn/	
 https://www.bnlearn.com/documentation/man/index.html	
 https://cran.r-project.org/web/packages/bnlearn/bnlearn.pdf	
 Bayesian Networks with Examples in R	 M. Scutari and J.-B. Denis (2021).Texts in Statistical Science, Chapman & Hall/CRC, 2nd edition.
 https://www.bnlearn.com/documentation/topics.html
 https://mp.weixin.qq.com/s/Vg8lyxaDtVLVlhWQ0djk1Q
 https://www.mdpi.com/2079-9292/12/6/1370
 https://bookdown.org/robertness/causalml/docs/tutorial-probabilistic-modeling-with-bayesian-networks-and-bnlearn.html
 https://www.modb.pro/db/634802
posted @ 2023-07-17 17:00  辰令  阅读(131)  评论(0编辑  收藏  举报