% Empirical PDF of a fair die
% Textbook: Part I, "Discrete Random Variable"
%
% The relative frequency of each face estimates p_X[k] = 1/6, and the sample
% mean estimates E[X] = 3.5. Increase N to see both estimates converge.

clearvars; close all
rng(0)

N = 1e5;                    % number of rolls
x = randi(6, [1 N]);        % die outcomes 1..6

k = 1:6;
p_emp = sum(x == k', 2)'/N; % relative frequency of each face

figure
bar(k, p_emp)
hold on
plot([0.5 6.5], [1 1]/6, 'r--', 'LineWidth', 1.5)
hold off
xlabel('k')
ylabel('p_X[k]')
legend('simulation', 'theory, 1/6')
grid on

fprintf('E[X]: theory = 3.5, simulation = %.4f\n', mean(x))
fprintf('max |p_emp - 1/6| = %.4f\n', max(abs(p_emp - 1/6)))
