# Coursera概率图模型（Probabilistic Graphical Models）第四周编程作业分析

Decision Making

1.记号和定义

2.已知决策规则的期望效用

SimpleCalcExpectedUtility.m 简单计算期望效用

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

V = unique([F(:).var]);

Z = setdiff(V, U.var);

Fnew = VariableElimination(F, Z);

Ffinal = Fnew(1);

for ii = 2 : length(Fnew)

Ffinal = FactorProduct(Ffinal, Fnew(ii));

end

Ffinal.val = Ffinal.val / sum(Ffinal.val);

U_reorder_val = zeros(size(U.val));

for ii = 1 : prod(U.card)

U_reorder_val(ii) = GetValueOfAssignment(U, IndexToAssignment(ii, U.card), Ffinal.var);

end

EU = Ffinal.val * U_reorder_val';

2.基于期望效用因子最大化期望效用

CalculateExpectedUtilityFactor.m 计算期望效用因子

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

F = [I.RandomFactors, I.UtilityFactors];

V = unique([F(:).var]);

Z = setdiff(V, I.DecisionFactors(1).var);

Fnew = VariableElimination(F, Z);

EUF = Fnew(1);

for ii = 2 : length(Fnew)

EUF = FactorProduct(EUF, Fnew(ii));

end

OptimizeMEU.m 优化最大期望效用

OptimalDecisionRule = CalculateExpectedUtilityFactor(I);

if length(OptimalDecisionRule.var) == 1

[MEU, index] = max(OptimalDecisionRule.val);

OptimalDecisionRule.val = zeros(size(OptimalDecisionRule.val));

OptimalDecisionRule.val(index) = 1;

else

assignments = IndexToAssignment(1 : prod(OptimalDecisionRule.card(1 : end - 1)), OptimalDecisionRule.card(1 : end - 1));

MEU = 0;

for ii = 1 : OptimalDecisionRule.card(end)

indices1 = AssignmentToIndex([assignments, ii * ones(size(assignments, 1), 1)], OptimalDecisionRule.card);

[meu, indices2] = max(OptimalDecisionRule.val(indices1));

MEU = MEU + meu;

OptimalDecisionRule.val(indices1) = 0;

OptimalDecisionRule.val(indices1(indices2)) = 1;

end

end

3.多效用因子

FactorSum.m 因子求和

function C = FactorSum(A, B)

if (isempty(A.var)), C = B; return; end;

if (isempty(B.var)), C = A; return; end;

[dummy iA iB] = intersect(A.var, B.var);

if ~isempty(dummy)

assert(all(A.card(iA) == B.card(iB)), 'Dimensionality mismatch in factors');

end

C.var = union(A.var, B.var);

[dummy, mapA] = ismember(A.var, C.var);

[dummy, mapB] = ismember(B.var, C.var);

C.card = zeros(1, length(C.var));

C.card(mapA) = A.card;

C.card(mapB) = B.card;

C.val = zeros(1,prod(C.card));

assignments = IndexToAssignment(1:prod(C.card), C.card);

indxA = AssignmentToIndex(assignments(:, mapA), A.card);

indxB = AssignmentToIndex(assignments(:, mapB), B.card);

C.val = A.val(indxA) + B.val(indxB);

end

OptimizeWithJointUtility.m 联合效用优化

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

U = I.UtilityFactors(1);

for ii = 2 : length(I.UtilityFactors)

U = FactorSum(U, I.UtilityFactors(ii));

end

I.UtilityFactors = U;

[MEU, OptimalDecisionRule] = OptimizeMEU(I);

OptimizeLinearExpectations.m 线性期望优化

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

%

% A decision rule for D assigns, for each joint assignment to D's parents,

% probability 1 to the best option from the EUF for that joint assignment

% to D's parents, and 0 otherwise. Note that when D has no parents, it is

% a degenerate case we can handle separately for convenience.

%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

OptimalDecisionRule = struct('var', [], 'card', [], 'val', []);

for ii = 1 : length(I.UtilityFactors)

I_ = I;

I_.UtilityFactors = I.UtilityFactors(ii);

EUF_ = CalculateExpectedUtilityFactor(I_);

OptimalDecisionRule = FactorSum(OptimalDecisionRule, EUF_);

end

if length(OptimalDecisionRule.var) == 1

[MEU, index] = max(OptimalDecisionRule.val);

OptimalDecisionRule.val = zeros(size(OptimalDecisionRule.val));

OptimalDecisionRule.val(index) = 1;

else

assignments = IndexToAssignment(1 : prod(OptimalDecisionRule.card(1 : end - 1)), OptimalDecisionRule.card(1 : end - 1));

MEU = 0;

for ii = 1 : OptimalDecisionRule.card(end)

indices1 = AssignmentToIndex([assignments, ii * ones(size(assignments, 1), 1)], OptimalDecisionRule.card);

[meu, indices2] = max(OptimalDecisionRule.val(indices1));

MEU = MEU + meu;

OptimalDecisionRule.val(indices1) = 0;

OptimalDecisionRule.val(indices1(indices2)) = 1;

end

end

posted @ 2018-09-09 23:13  小石学CS  阅读(...)  评论(... 编辑 收藏