0

I am new to MATLAB's environment and no matter how much I have struggled it just seems that I cannot get the concept of how to construct a ML algorithm for a multivariate Bernoulli.

I have a dataset of N variables (x1,x2,...,xN) and each variable is a vector of D dimensions (Dx1), with a parameter vector in the form p=(p1,p2,...,pD) . So the Bernoulli distribution should have the form:

Pr(X|p)=Πp(d)^x(nd)*(1-p(d))^(1-x(nd))

The code that I created uses MATLAB's mle function:

for n=1:D
    prob(n)=mle(dataset(:,n),'distribution', 'bernoulli');  
end

which gives me a D vector of estimated probabilities from the dataset. But, what I am really interested in is how to implement the ML on a step-by-step MATLAB process and not just use the mle.

Thank you very much.

Jespar
  • 1,017
  • 5
  • 16
  • 29

1 Answers1

0

phat for a Bernoulli distribution is proportion of successes to the number of trials. If you'd like to do it manually, you can just count the number of successes (either 1 or 0) in each of your vectors then divide it by the length of the vector. Here's a quick way assuming 1's are successes stored vertically in the matrix.

bern_mat = [0 0 1 0 1 1; 1 1 0 1 0 0 ; 1 0 1 0 1 1]; % 3x6 matrix of 1's and 0's
phat = sum(bern_mat,1)/size(bern_mat,1); % sum across the first dim then divide by size of first dim.
Brad Day
  • 400
  • 2
  • 9