[MPEI] Aula04

Signed-off-by: Tiago Garcia <tiago.rgarcia@ua.pt>
This commit is contained in:
Tiago Garcia 2024-11-03 18:27:10 +00:00
parent 95225420b1
commit 11be924274
Signed by: TiagoRG
GPG Key ID: DFCD48E3F420DB42
5 changed files with 189 additions and 0 deletions

View File

@ -0,0 +1,53 @@
% Joint probability distribution
P_XY = [0.3, 0.2, 0;
0.1, 0.15, 0.05;
0, 0.1, 0.1];
% Values of X and Y
X_vals = [0, 1, 2];
Y_vals = [0, 1, 2];
% Marginal distributions
P_X = sum(P_XY, 2);
P_Y = sum(P_XY, 1);
% Mean of X and Y
E_X = sum(X_vals .* P_X');
E_Y = sum(Y_vals .* P_Y);
% Variance of X and Y
Var_X = sum((X_vals - E_X).^2 .* P_X');
Var_Y = sum((Y_vals - E_Y).^2 .* P_Y);
% Covariance of X and Y
E_XY = sum(sum((X_vals' * Y_vals) .* P_XY));
Cov_XY = E_XY - E_X * E_Y;
% Correlation coefficient
rho_XY = Cov_XY / sqrt(Var_X * Var_Y);
% Display results
fprintf('Marginal PMF of X: \n');
disp(P_X');
fprintf('Marginal PMF of Y: \n');
disp(P_Y);
fprintf('Mean of X: %.2f\n', E_X);
fprintf('Mean of Y: %.2f\n', E_Y);
fprintf('Variance of X: %.2f\n', Var_X);
fprintf('Variance of Y: %.2f\n', Var_Y);
fprintf('Covariance of X and Y: %.2f\n', Cov_XY);
fprintf('Correlation coefficient between X and Y: %.2f\n', rho_XY);
% Plot Marginal PMFs
figure;
subplot(1,2,1);
stem(X_vals, P_X, 'filled');
title('Marginal PMF of X');
xlabel('X');
ylabel('P(X)');
subplot(1,2,2);
stem(Y_vals, P_Y, 'filled');
title('Marginal PMF of Y');
xlabel('Y');
ylabel('P(Y)');

View File

@ -0,0 +1,42 @@
% Joint probability distribution
P_XY = [1/8, 1/8, 1/24;
1/8, 1/4, 1/8;
1/24, 1/8, 1/24];
% Values of X and Y
X_vals = [-1, 0, 1];
Y_vals = [-1, 0, 1];
% Marginal distributions
P_X = sum(P_XY, 2); % Sum across rows for X
P_Y = sum(P_XY, 1); % Sum across columns for Y
% Checking independence
independence = true; % Assume independence initially
for i = 1:length(X_vals)
for j = 1:length(Y_vals)
% Calculate product of marginals
product_marginals = P_X(i) * P_Y(j);
% Compare with joint probability
if abs(P_XY(i, j) - product_marginals) > 1e-10
independence = false;
break;
end
end
if ~independence
break;
end
end
% Display results
fprintf('Marginal PMF of X: \n');
disp(P_X');
fprintf('Marginal PMF of Y: \n');
disp(P_Y);
if independence
fprintf('X and Y are independent.\n');
else
fprintf('X and Y are not independent.\n');
end

View File

@ -0,0 +1,72 @@
%% a)
% Number of students
num_students = 120;
% Mean and variance for N1 and N2
mu_N1 = 14;
var_N1 = 3.5;
mu_N2 = 16.8;
var_N2 = 4.2;
% Standard deviations
sigma_N1 = sqrt(var_N1);
sigma_N2 = sqrt(var_N2);
% Generate N1 and N2 from a normal distribution
N1 = round(normrnd(mu_N1, sigma_N1, [num_students, 1]));
N2 = round(normrnd(mu_N2, sigma_N2, [num_students, 1]));
% Ensure values are within a reasonable range (e.g., scores between 0 and 20)
N1 = max(0, min(20, N1));
N2 = max(0, min(20, N2));
%% b)
% Joint PMF calculation
joint_counts = zeros(21, 21);
for i = 1:num_students
joint_counts(N1(i)+1, N2(i)+1) = joint_counts(N1(i)+1, N2(i)+1) + 1;
end
joint_pmf = joint_counts / num_students;
% Plotting the joint PMF
[X, Y] = meshgrid(0:20, 0:20);
figure;
bar3(joint_pmf);
xlabel('N_1 scores');
ylabel('N_2 scores');
zlabel('Joint Probability');
title('Joint PMF of N_1 and N_2');
%% c)
% Calculate correlation coefficient
correlation_matrix = corrcoef(N1, N2);
correlation_coefficient = correlation_matrix(1, 2);
% Display result
fprintf('Correlation coefficient between N1 and N2: %.2f\n', correlation_coefficient);
%% d)
% Marginal PMFs
marginal_N1 = sum(joint_pmf, 2);
marginal_N2 = sum(joint_pmf, 1);
% Check independence by verifying if joint PMF = product of marginals
independent = true;
for i = 1:21
for j = 1:21
if abs(joint_pmf(i, j) - (marginal_N1(i) * marginal_N2(j))) > 1e-10
independent = false;
break;
end
end
if ~independent
break;
end
end
% Display independence result
if independent
fprintf('N1 and N2 are independent.\n');
else
fprintf('N1 and N2 are not independent.\n');
end

View File

@ -0,0 +1,22 @@
% Given probabilities
P_S = 0.75; % Probability of a sunny day
P_R = 0.25; % Probability of a rainy day
P_C_given_S = 1/3; % Probability meteorologist is correct given sunny day
P_C_given_R = 1; % Probability meteorologist is correct given rainy day
% Part 1: Calculate overall accuracy of the meteorologist
P_C = P_C_given_S * P_S + P_C_given_R * P_R;
% Part 2: Calculate accuracy if the student always predicts sun
P_student_correct = P_S;
% Display the results
fprintf('Meteorologist''s overall accuracy: %.2f%%\n', P_C * 100);
fprintf('Student''s accuracy if they always predict sun: %.2f%%\n', P_student_correct * 100);
% Decision analysis
if P_C > P_student_correct
fprintf('The meteorologist''s balanced prediction method is better.\n');
else
fprintf('The student''s method of always predicting sun is better.\n');
end