function bnet = mk_bnet_from_noisyfn(pfail, F, child_size, prior)
% MK_BNET_FOR_NOISYFN Make a mini Bayes net representing a noisy-fn node.
% bnet = mk_bnet_for_noisyfn(pfail, F, child_size, prior)
%
% prior is the equivalent sample size of the uniform Dirichlet prior on the Xi nodes.

if size(pfail,2)==1 % symmetric channel
  pfail = [pfail pfail];
end
np = size(pfail, 1); % num. parents
psize = size(pfail, 2); % num. states in the channel
N = 2*np + 1;
Cs = 1:np;
Xs = np+1:2*np;
E = 2*np+1;
dag = zeros(N);
for i=1:np
  dag(Cs(i), Xs(i)) = 1;
end
dag(Xs,E) = 1;
dnodes = 1:N;
bnet = mk_bnet(dag, [psize*ones(1,2*np) child_size], dnodes);
for i=1:np
  % We would make the Cs input nodes, except that we have to add soft evidence to them.
  bnet.bnodes{i} = mk_tabular_node(bnet, i, 0);
  bnet.bnodes{i}.CPT(:) = normalise(ones(1,psize));  % uniform prior
  bnet.bnodes{i}.adjustable = 0;
end
for i=1:np
  x = Xs(i);
  bnet.bnodes{x} = mk_tabular_node(bnet, x, prior);
  bnet.bnodes{x}.CPT = mk_CPT_from_noisy_channel(pfail(i,:), psize);
end
bnet.bnodes{E} = mk_tabular_node(bnet, E, 0);
bnet.bnodes{E}.CPT = mk_CPT_from_deterministic_fn(F, np, psize, child_size);
bnet.bnodes{E}.adjustable = 0;

bnet = compile_params(bnet);

