% compute ML estimate of K given MM

% from NOTES/Kx9-4.txt
%
% Pmiss = 0.1;
% false_alarm_rate = 1;
% MM = [4  3  3  4  2  2  5  2  4  3  2  2  3  2  2  2  3  2  2  3  4];
% N = size(MM,2);
% K_max = 4;

% from NOTES/Kx9-5.txt
%
% Pmiss = 0.2;
% false_alarm_rate = 2;
% MM = [4  6  3  2  3  2  3  2  4  6  3  4  2  4  3  4  3  3  4  4  2];
% N = size(MM,2);
% K_max = 4;

% from track/x23.m
%
% Pmiss = 0.4;
% false_alarm_rate = 4;
% MM = [3 3];
% N = size(MM,2);
% K_max = 3;

% test case
%
Pmiss = 0.2;
false_alarm_rate = 2;
MM = 3;
N = 1;
K_max = 3;

% PK(K+1) = P(K) apriori
%
% try uniform PK
%
PK = ones(K_max+1,1)/(K_max+1)

% PMMK(K+1) = P(MM|K), K = 0..K_max
%
PMMK = zeros(K_max+1,1);

for k = 0:K_max
  p = 1;
  for j = 1:N
    p = p * p_MM_K( MM(j), k, Pmiss, false_alarm_rate);
  end
  PMMK(k+1) = p;
end

PMMK

% PMM = P(MM)
%
PMM = PMMK' * PK

% PKMM(K+1) = P(K|MM)
%
PKMM = (PMMK .* PK) / PMM
