You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: mixBernEm.m
    path of file: ./repos/PRMLT/chapter09
    
    the code of the file until where you have to start completion: function [label, model, llh] = mixBernEm(X, k)
% Perform EM algorithm for fitting the Bernoulli mixture model.
% Input: 
%   X: d x n binary (0/1) data matrix 
%   k: number of cluster
% Output:
%   label: 1 x n cluster label
%   model: trained model structure
%   llh: loglikelihood
% Written by Mo Chen (sth4nth@gmail.com).

    
function [label, model, llh] = mixBernEm(X, k)
% Perform EM algorithm for fitting the Bernoulli mixture model.
% Input: 
%   X: d x n binary (0/1) data matrix 
%   k: number of cluster
% Output:
%   label: 1 x n cluster label
%   model: trained model structure
%   llh: loglikelihood
% Written by Mo Chen (sth4nth@gmail.com).
%% initialization
fprintf('EM for mixture model: running ... \n');
X = sparse(X);
n = size(X,2);
label = ceil(k*rand(1,n));  % random initialization
R = full(sparse(1:n,label,1));
tol = 1e-8;
maxiter = 500;
llh = -inf(1,maxiter);
for iter = 2:maxiter
    model = maximization(X,R);
    [R, llh(iter)] = expectation(X,model);
    if abs(llh(iter)-llh(iter-1)) < tol*abs(llh(iter)); break; end