% hw2_2012

% APMA 0410
% September 30, 2012 

clear

% We will arrange our posterior probabilities in 4 rows, one for each direction.
% Each row will contain 5 values, one for each of the 5 observed firings.

y_values = [20 30 40 50 60];            % a row containing the 5 observed firings
y_values_rep = repmat(y_values, 4,1);   % this row is replicated 4 times to create a 4x5 matrix 
f_values = [.032 .044 .027 .052]';      % a column containing the 4 learned firing rates
f_values_rep = repmat(f_values, 1,5);   % this column is replicated 5 times to create a 4x5 matrix 

% The conditional probability is given by the binomial distribution:
conditional_p = binopdf(y_values_rep,1000,f_values_rep);

% We shall use Bayes formula.

%% Problem 2.11

% Here, all four directions are equally likely.
% Therefore, the prior probability in Bayes formula
% (equal to 0.25 for all directions), cancels out.
% The posterior probability is therefore simply
% the conditional probability,
% normalized by the sum over directions, i.e., the column sum.

denominator = sum(conditional_p,1);
denominator_rep = repmat(denominator, 4,1);
posterior_p = conditional_p./denominator_rep;

disp(' ')
disp('y values:')
disp(y_values)
disp('posterior probability under uniform prior:')
disp(posterior_p)
disp(' ')

%% Problem 2.12

% Now the prior probabilities are given by: 
prior_values = [.3 .3 .2 .2]';

prior_values_rep = repmat(prior_values, 1,5);   % this column is replicated
                                                % 5 times to create a 4x5 matrix 

% The numerator in Bayes formula is the conditional times the prior:
conditional_times_prior = conditional_p.*prior_values_rep;

% And, again, the denominator is the column sum:
denominator = sum(conditional_times_prior,1);
denominator_rep = repmat(denominator, 4,1);

% And so:
posterior_p = conditional_times_prior./denominator_rep;

disp(y_values)
disp('posterior probability under learned prior:')
disp(posterior_p)
disp(' ')
