% Pr(X^(1/4) < 1/2) for a uniform X
% Textbook: Part I, "Continuous Random Variable", exercise (2022, Midterm)
%
% X ~ U[0,1]. The event X^(1/4) < 1/2 is the event X < (1/2)^4 = 1/16,
% so Pr = 1/16 = 0.0625. The simulation estimates the probability by the
% fraction of experiments in which the event occurs.

clearvars; close all
rng(0)

N = 1e6;
x = rand(1, N);

p_sim = mean(x.^(1/4) < 1/2);
fprintf('Pr(X^(1/4) < 1/2): theory = %.4f, simulation = %.4f\n', 1/16, p_sim)

% The same estimate as a function of the number of experiments
M = round(logspace(1, 6, 30));
p_M = arrayfun(@(m) mean(x(1:m).^(1/4) < 1/2), M);

figure
semilogx(M, p_M, 'o-', M, ones(size(M))/16, 'r--', 'LineWidth', 1.2)
xlabel('N')
ylabel('Pr(X^{1/4} < 1/2)')
legend('simulation', 'theory, 1/16')
grid on
