csci5521/assignments/hwk01/MLE_Learning.m
2023-10-01 18:09:50 -05:00

25 lines
929 B
Matlab

% implements MLE_Learning, returns the outputs (p1: learned Bernoulli
% parameters of the first class, p2: learned Bernoulli parameters of the
% second class; pc1: prior of the first class, pc2: prior of the
% second class
function [p1,p2,pc1,pc2] = MLE_Learning(training_data)
[train_row_size, column_size] = size(training_data); % dimension of training data
X = training_data(1:train_row_size, 1:column_size-1); %Training data
y = training_data(:,column_size); % training labels
% (1) TODO: find label counts of class 1 and class 2
class1_rows = find(y == 1);
class1_count = length(class1_rows);
class2_rows = find(y == 2);
class2_count = length(class2_rows);
% (2) TODO: compute priors pc1, pc2
pc1 = class1_count / train_row_size;
pc2 = class2_count / train_row_size;
% (3) TODO: compute maximum likelihood estimate (MLE) p1, p2
p1 = sum(X(class1_rows, :)) / class1_count;
p2 = sum(X(class2_rows, :)) / class2_count;