www.pudn.com > ConstrainedEM.zip > calculate_partition_function2.m
%this does not rely on pots, only on alphas but should be updated to handle weights in cases of
%late orcale or negative constraints. The alphas should be raised to the power of W's.
function [ pf , gpf , exp_const]=calculate_partition_function2(p,no_grad_flag,stage)
% p - the current parameters .
% no_grad_flag - when set the gradient isn't caculated and the caculation time reduces by factor of 3*k
% impotant input parameters passed as globals are stage and late_oracle.
% stage - if stage==1 only chunklet pf is calculated ( used only in late oracle model )
% if stage==2 the full pf is calculated
% aa_flag - when set , a rude aproximation to anti chunklets pf is used. it is passed
% as global , not parameter.
% pf - the partition function
% gpf - the partition function gradient
% exp_const - both pf and gpf should be multiplied by exp(log_const) to get the real value.
global nets;
% nets - a set of connected components of a graph as output by organize_constraints
% nets isn't passed as a parameter because line search who calls this function
% can only send 1 parameter. so nets is passed as a global.the same reason applies
% to late_oracle more.
global late_oracle;
global chunklet_sizes;
global anti_chunk_num;
global aa_flag;
if nargin==1 % default is full pf calculation with gradient
no_grad_flag=0;
stage=2;
elseif nargin==2
stage=2;
end
underflow_line=1e-100;
exp_const=0;
% anti chunklett part of pf.
ln=length(nets);
k=length(p);
pf=1;
gpf=zeros(1,k);
if stage==2 % anti-chunklets pf calculation
if ~aa_flag
for i=1:ln
[cur_pf , cur_gpf ]=connected_component_pf(nets{i}.engine.mnet,nets{i}.engine.elim_order,p,no_grad_flag);
if no_grad_flag==0
gpf=cur_gpf*pf+cur_pf*gpf;
end
pf=pf*cur_pf;
% underflow considerations
if no_grad_flag & pf