贝叶斯网的R实现( Bayesian networks in R)bnlearn(3)

4.参数学习

得到贝叶斯网的网络结构之后,可以对局部分布的参数进行参数估计了,这称作参数学习。

4.1参数学习的基本方法

bnlearn包的参数学习函数是bn.fit,其参数method给出了两种具体的方法:“mle”为极大似然估计;"bayes"为贝叶斯后验估计(采用无信息先验分布)。

4.2对marks数据集的参数学习

marks是个连续数据集,所以参数采用的是回归系数的形式。下面对其中的一个节点计算参数的极大似然估计:

library(bnlearn)

## Warning: package 'bnlearn' was built under R version 3.0.1

data(marks)
marks.bn <- gs(marks, undirected = FALSE)
marks.bn1 <- hc(marks)


marks.fit <- bn.fit(marks.bn1, data = marks)
marks.fit <- bn.fit(marks.bn, data = marks)

## Error: the graph is only partially directed.


marks.fit$ALG

## 
## Parameters of node ALG (Gaussian distribution)
## 
## Conditional density: ALG | MECH + VECT
## Coefficients:
## (Intercept) MECH VECT 
## 25.3620 0.1834 0.3577 
## Standard deviation of the residuals: 7.987



## Pearson's Linear Correlation
## 
## data: ALG ~ STAT | ANL
## cor = 0.4172, df = 85, p-value = 5.827e-05
## alternative hypothesis: true value is not equal to 0

#在参数估计的时候,由gs算法得到的mark.bn是个部分有向图,会报错。

参数估计bn.fit的结果得到一个bn.fit对象。利用这个可以修改局部分布。

marks.fit$ALG <- list(coef = c(`(Intercept)` = 26, MECH = 0.18, VECT = 0.36), 
    sd = 7.99)
marks.fit$ALG

## 
## Parameters of node ALG (Gaussian distribution)
## 
## Conditional density: ALG | MECH + VECT
## Coefficients:
## (Intercept) MECH VECT 
## 26.00 0.18 0.36 
## Standard deviation of the residuals: 7.99

在marks这个连续数据例子中,生成的网络是高斯贝叶斯网,在上面对参数的修改中,包括了一个回归系数的完整集合(就是coef),以及残差的标准差(sd)

另外,利用custom.fit函数也可以使用上面的语法生成bn.fit对象。

5.离散化

下面考虑在保持相依结构的前提下把marks数据集进行离散化。这种离散化会如何改变网络的形式呢?discretize可以实现离散化的功能。下面是discretize采用interval方法,按照中位数分为两个区间(breaks=2),数据按相应的区间归到对应的类中。 离散化的数据也可以采用结构学习的方法,由结构学习算法得到相应的网络结构。

marks.d <- discretize(marks, method = "interval", breaks = 2)

marks.dgs <- gs(marks.d)

plot(marks.dgs, radius = 200, arrow = 30)


marks.dhc <- hc(marks.d)
plot(marks.dhc, radius = 160, arrow = 40)


all.equal(cpdag(marks.dgs), cpdag(marks.dhc))

## [1] TRUE

可以看到离散化之后的网络结构依然保持着连续数据marks生成的部分网络结构。

离散化的网络参数就构成了CPT(条件概率表)。下面是参数学习的结果:

marks.fit2 <- bn.fit(marks.dhc, data = marks.d)
marks.fit2$ALG

## 
## Parameters of node ALG (multinomial distribution)
## 
## Conditional probability table:
## 
## VECT
## ALG [8.93,45.5] (45.5,82.1]
## [14.9,47.5] 0.5806 0.2281
## (47.5,80.1] 0.4194 0.7719

用python写的一段贝叶斯络的程序 This file describes a Bayes Net Toolkit that we will refer to now as BNT. This version is 0.1. Let's consider this code an "alpha" version that contains some useful functionality, but is not complete, and is not a ready-to-use "application". The purpose of the toolkit is to facilitate creating experimental Bayes nets that analyze sequences of events. The toolkit provides code to help with the following: (a) creating Bayes nets. There are three classes of nodes defined, and to construct a Bayes net, you can write code that calls the constructors of these classes, and then you can create links among them. (b) displaying Bayes nets. There is code to create new windows and to draw Bayes nets in them. This includes drawing the nodes, the arcs, the labels, and various properties of nodes. (c) propagating a-posteriori probabilities. When one node's probability changes, the posterior probabilities of nodes downstream from it may need to change, too, depending on firing thresholds, etc. There is code in the toolkit to support that. (d) simulating events ("playing" event sequences) and having the Bayes net respond to them. This functionality is split over several files. Here are the files and the functionality that they represent. BayesNetNode.py: class definition for the basic node in a Bayes net. BayesUpdating.py: computing the a-posteriori probability of a node given the probabilities of its parents. InputNode.py: class definition for "input nodes". InputNode is a subclass of BayesNetNode. Input nodes have special features that allow them to recognize evidence items (using regular-expression pattern matching of the string descriptions of events). OutputNode.py: class definition for "output nodes". OutputBode is a subclass of BayesNetNode. An output node can have a list of actions to be performed when the node's posterior probability exceeds a threshold ReadWriteSigmaFiles.py: Functionality for loading and saving Bayes nets in an XML format. SampleNets.py: Some code that constructs a sample Bayes net. This is called when SIGMAEditor.py is started up. SIGMAEditor.py: A main program that can be turned into an experimental application by adding menus, more code, etc. It has some facilities already for loading event sequence files and playing them. sample-event-file.txt: A sequence of events that exemplifies the format for these events. gma-mona.igm: A sample Bayes net in the form of an XML file. The SIGMAEditor program can read this type of file. Here are some limitations of the toolkit as of 23 February 2009: 1. Users cannot yet edit Bayes nets directly in the SIGMAEditor. Code has to be written to create new Bayes nets, at this time. 2. If you select the File menu's option to load a new Bayes net file, you get a fixed example: gma-mona.igm. This should be changed in the future to bring up a file dialog box so that the user can select the file. 3. When you "run" an event sequence in the SIGMAEditor, the program will present each event to each input node and find out if the input node's filter matches the evidence. If it does match, that fact is printed to standard output, but nothing else is done. What should then happen is that the node's probability is updated according to its response method, and if the new probability exceeds the node's threshold, then its successor ("children") get their probabilities updated, too. 4. No animation of the Bayes net is performed when an event sequence is run. Ideally, the diagram would be updated dynamically to show the activity, especially when posterior probabilities of nodes change and thresholds are exceeded. To use the BNT, do three kinds of development: A. create your own Bayes net whose input nodes correspond to pieces of evidence that might be presented and that might be relevant to drawing inferences about what's going on in the situation or process that you are analyzing. You do this by writing Python code that calls constructors etc. See the example in SampleNets.py. B. create a sample event stream that represents a plausible sequence of events that your system should be able to analyze. Put this in a file in the same format as used in sample-event-sequence.txt. C. modify the code of BNT or add new modules as necessary to obtain the functionality you want in your system. This could include code to perform actions whenever an output node's threshold is exceeded. It could include code to generate events (rather than read them from a file). And it could include code to describe more clearly what is going on whenever a node's probability is updated (e.g., what the significance of the update is -- more certainty about something, an indication that the weight of evidence is becoming strong, etc.)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值