Coursera概率图模型(Probabilistic Graphical Models)第一周编程作业分析

Computing probability queries in a Bayesian network

计算贝叶斯网络中的概率查询

 

1.基础因子操作

 

这一周的作业主要是熟悉一下基础操作。作业中因子的结构如下:

 

phi = struct('var', [3 1 2], 'card', [2 2 2], 'val', ones(1, 8));

其中:var表示因子中变量的标签及顺序,card代表基数,描述了各变量的状态数量,val表示各变量取不同值时对应的概率分布,其向量长度等于prod(card)。

 

FactorProduct.m 计算两个因子的积

 

输入:

FACTORS.INPUT(1) = struct('var', [1], 'card', [2], 'val', [0.11, 0.89]);

FACTORS.INPUT(2) = struct('var', [2, 1], 'card', [2, 2], 'val', [0.59, 0.41, 0.22, 0.78]);

FACTORS.PRODUCT = FactorProduct(FACTORS.INPUT(1), FACTORS.INPUT(2));

 

期望输出:

FACTORS.PRODUCT = struct('var', [1, 2], 'card', [2, 2], 'val', [0.0649, 0.1958, 0.0451, 0.6942]);

 

我们知道,对贝叶斯网络而言,因子积其实就是表示贝叶斯链式法则。比如若FACTORS.INPUT(1) 表示学生的智力是否正常的分布,即,FACTORS.INPUT(2)表示学生在其智力是否正常的条件下考试是否及格的分布,即,则其联合概率分布可记为FACTORS.PRODUCT = FactorProduct(FACTORS.INPUT(1), FACTORS.INPUT(2)),即

 

计算步骤很简单,就是贝叶斯链式法则的步骤:

 

参考代码如下:

 

 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 2 
 3 % YOUR CODE HERE:
 4 
 5 % Correctly populate the factor values of C
 6 
 7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 8 
 9 for ii = 1 : length(C.val)
10 
11     C.val(ii) = A.val(indxA(ii)) * B.val(indxB(ii));
12 
13 end
14 
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

 

FactorMarginalization.m 计算因子的边缘分布

 

输入:

FACTORS.INPUT(2) = struct('var', [2, 1], 'card', [2, 2], 'val', [0.59, 0.41, 0.22, 0.78]);

FACTORS.MARGINALIZATION = FactorMarginalization(FACTORS.INPUT(2), [2]);

 

期望输出:

FACTORS.MARGINALIZATION = struct('var', [1], 'card', [2], 'val', [1 1]);

 

本质上,求边缘分布就是一个求和的过程。对相应变量的值求和就可以了。

 

参考代码如下:

 

 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 2 
 3 % YOUR CODE HERE
 4 
 5 % Correctly populate the factor values of B
 6 
 7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 8 
 9 for ii = 1 : length(unique(indxB))
10 
11     B.val(ii) = sum(A.val(indxB == ii));
12 
13 end
14 
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

 

ObserveEvidence.m 变量观测

 

输入:

FACTORS.INPUT(1) = struct('var', [1], 'card', [2], 'val', [0.11, 0.89]);

FACTORS.INPUT(2) = struct('var', [2, 1], 'card', [2, 2], 'val', [0.59, 0.41, 0.22, 0.78]);

FACTORS.INPUT(3) = struct('var', [3, 2], 'card', [2, 2], 'val', [0.39, 0.61, 0.06, 0.94]);

FACTORS.EVIDENCE = ObserveEvidence(FACTORS.INPUT, [2 1; 3 2]);

 

期望输出:

FACTORS.EVIDENCE(1) = struct('var', [1], 'card', [2], 'val', [0.11, 0.89]);

FACTORS.EVIDENCE(2) = struct('var', [2, 1], 'card', [2, 2], 'val', [0.59, 0, 0.22, 0]);

FACTORS.EVIDENCE(3) = struct('var', [3, 2], 'card', [2, 2], 'val', [0, 0.61, 0, 0]);

 

在ObserveEvidence函数中,第二个参数为一个的矩阵,第一列表示所观测的变量,第二列表示对应变量的取值。要求只保留因子中被观测变量所对应取值的概率,被观测变量的其他取值对应概率置0。未被观测变量不受影响。

 

参考代码如下:

 

 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 2 
 3 % YOUR CODE HERE
 4 
 5 % Adjust the factor F(j) to account for observed evidence
 6 
 7 % Hint: You might find it helpful to use IndexToAssignment
 8 
 9 % and SetValueOfAssignment
10 
11 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 
13 assignments = IndexToAssignment(1 : length(F(j).val), F(j).card);
14 
15 F(j).val(assignments(:, indx) ~= x) = 0;
16 
17 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

 

2.计算联合分布

 

ComputeJointDistribution.m 计算贝叶斯网络的联合概率分布

 

输入:

FACTORS.INPUT(1) = struct('var', [1], 'card', [2], 'val', [0.11, 0.89]);

FACTORS.INPUT(2) = struct('var', [2, 1], 'card', [2, 2], 'val', [0.59, 0.41, 0.22, 0.78]);

FACTORS.INPUT(3) = struct('var', [3, 2], 'card', [2, 2], 'val', [0.39, 0.61, 0.06, 0.94]);

FACTORS.JOINT = ComputeJointDistribution(FACTORS.INPUT);

 

期望输出:

FACTORS.JOINT = struct('var', [1, 2, 3], 'card', [2, 2, 2], 'val', [0.025311, 0.076362, 0.002706, 0.041652, 0.039589, 0.119438, 0.042394, 0.652548]);

 

 

如前所述,在贝叶斯网络中,联合概率分布就是其因子积。下面是不同的表述:

 

 

参考代码如下:

 

 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 2 
 3 % YOUR CODE HERE:
 4 
 5 % Compute the joint distribution defined by F
 6 
 7 % You may assume that you are given legal CPDs so no input checking is required.
 8 
 9 %
10 
11 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 
13 Joint = F(1);
14 
15 for ii = 2 : length(F)
16 
17     Joint = FactorProduct(Joint, F(ii));
18 
19 end
20 
21 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

 

3.计算边缘分布

 

ComputeMarginal.m 计算贝叶斯网络的边缘概率分布

 

输入:

FACTORS.INPUT(1) = struct('var', [1], 'card', [2], 'val', [0.11, 0.89]);

FACTORS.INPUT(2) = struct('var', [2, 1], 'card', [2, 2], 'val', [0.59, 0.41, 0.22, 0.78]);

FACTORS.INPUT(3) = struct('var', [3, 2], 'card', [2, 2], 'val', [0.39, 0.61, 0.06, 0.94]);

FACTORS.MARGINAL = ComputeMarginal([2, 3], FACTORS.INPUT, [1, 2]);

 

期望输出:

FACTORS.MARGINAL = struct('var', [2, 3], 'card', [2, 2], 'val', [0.0858, 0.0468, 0.1342, 0.7332]);

 

相比之前计算因子的边缘分布,这里主要多了归一化的要求,同时还要注意合并相同变量的问题。

 

参考代码如下:

 

 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 2 
 3 % YOUR CODE HERE:
 4 
 5 % M should be a factor
 6 
 7 % Remember to renormalize the entries of M!
 8 
 9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 
11 Joint = ComputeJointDistribution(F);
12 
13 M = FactorMarginalization(ObserveEvidence(Joint, E), setdiff(Joint.var, V));
14 
15 M.val = M.val ./ sum(M.val);
16 
17 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

转载于:https://www.cnblogs.com/polaris-shi/p/9498750.html

Statistical learning refers to a set of tools for modeling and understanding complex datasets. It is a recently developed area in statistics and blends with parallel developments in computer science and, in particular, machine learning. The field encompasses many methods such as the lasso and sparse regression, classification and regression trees, and boosting and support vector machines. With the explosion of “Big Data” problems, statistical learning has be- come a very hot field in many scientific areas as well as marketing, finance, and other business disciplines. People with statistical learning skills are in high demand. One of the first books in this area—The Elements of Statistical Learning (ESL) (Hastie, Tibshirani, and Friedman)—was published in 2001, with a second edition in 2009. ESL has become a popular text not only in statis- tics but also in related fields. One of the reasons for ESL’s popularity is its relatively accessible style. But ESL is intended for individuals with ad- vanced training in the mathematical sciences. An Introduction to Statistical Learning (ISL) arose from the perceived need for a broader and less tech- nical treatment of these topics. In this new book, we cover many of the same topics as ESL, but we concentrate more on the applications of the methods and less on the mathematical details. We have created labs illus- trating how to implement each of the statistical learning methods using the popular statistical software package R . These labs provide the reader with valuable hands-on experience.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值