Skip to content

Instantly share code, notes, and snippets.

@Higgcz
Created January 3, 2014 20:33
Show Gist options
  • Save Higgcz/8246057 to your computer and use it in GitHub Desktop.
Save Higgcz/8246057 to your computer and use it in GitHub Desktop.
%% -------------------------------------------------------------------------
% ( 9 ) Inference & missing values
% -------------------------------------------------------------------------
% Create inference engine
engine = jtree_inf_engine ( bnet );
% Sort brokenData to right order
brokenDataSorted = brokenData ( bnet.ordering, : )';
% Find nanvalues in broken data
[nanrow, nancol] = find(isnan(brokenDataSorted));
% First we need to tranform brokenData into cell array
cbroken = num2cell ( brokenDataSorted );
[broken_num, ~] = size ( cbroken );
% Delete the NaN values from cell array
[cbroken{nanrow + broken_num .* (nancol-1)}] = deal([]);
% For all broken samples
for i = 1:broken_num
% Get one sample
csample = cbroken ( i, : );
% Create evidence from sample
evidence = csample;
% Print evidence
fprintf('\nAll evidence for %d-nth sample:\n', i);
for e = 1:N
cEvid = evidence { e };
if (~isnan(cEvid))
fprintf ( '%15s = %-9s\n', data.names{bnet.ordering(e)}, getLabel( bnet.ordering(e), cEvid ));
end
end
fprintf ( '\n' );
% Add evidence to the engine
engine = enter_evidence ( engine, evidence );
% All nan variables
varIdxs = nancol ( nanrow == i );
% Count all nan variables
countNanVar = size ( varIdxs, 1 );
% For all nan variables
for j = 1:countNanVar
% Find index of current nan variable
varIdx = varIdxs ( j );
% Result - Marginal distribution
marg = marginal_nodes ( engine, varIdx );
fprintf('\n');
% Print all result
for v = 1:size(marg.T)
fprintf('Pr (%s = %-9s | all_others) = %f\n', data.names{bnet.ordering(varIdx)}, getLabel ( bnet.ordering(varIdx), v ), marg.T(v) );
end
% Maximum
[mmax, midx] = max ( marg.T );
fprintf('\n');
fprintf( 'The value of "%s" most probably is: %-9s\n', data.names{bnet.ordering(varIdx)}, getLabel ( bnet.ordering(varIdx), midx ) );
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment