0% found this document useful (0 votes)
7 views16 pages

Tinywow Matlabworkbookstathw4 83108852

Uploaded by

100030182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Tinywow Matlabworkbookstathw4 83108852

Uploaded by

100030182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Untitled file:///C:/Users/chees/Documents/MatLab_WorkBook_3.

html

% Q1
data = [240 205 225 275 300 320 280 210 215 240];

lambda_hat = mean(data);

n = length(data);
z = norminv(0.975);
se = sqrt(lambda_hat / n);
ci = [lambda_hat - z * se, lambda_hat + z * se];

fprintf('MLE for lambda: %.2f\n', lambda_hat);

MLE for lambda: 251.00

fprintf('95%% Confidence Interval: [%.2f, %.2f]\n', ci(1), ci(2));

95% Confidence Interval: [241.18, 260.82]

1 of 4 8/6/2025, 9:28 PM
Untitled file:///C:/Users/chees/Documents/MatLab_WorkBook_3.html

% Data
data = xlsread('data.xlsx');
train_data = data(1:6000, :);
test_data = data(6001:9000, :);

% Training
X = train_data(:, 1:6);
Y = train_data(:, 7);
X_Y1 = X(Y == 1, :);
X_Y0 = X(Y == 0, :);

PX_given_Y1 = zeros(6, 5);


PX_given_Y0 = zeros(6, 5);

for i = 1:6
for j = 1:5
PX_given_Y1(i,j) = sum(X_Y1(:,i) == j) / size(X_Y1, 1);
PX_given_Y0(i,j) = sum(X_Y0(:,i) == j) / size(X_Y0, 1);
end
end

P_Y1 = sum(Y == 1) / length(Y);


P_Y0 = 1 - P_Y1;

% pred
X_test = test_data(:, 1:6);
Y_true = test_data(:, 7);
Y_pred = zeros(size(Y_true));

for idx = 1:size(X_test,1)


x = X_test(idx, :);

p1 = P_Y1;
p0 = P_Y0;

for i = 1:6
p1 = p1 * PX_given_Y1(i, x(i));
p0 = p0 * PX_given_Y0(i, x(i));
end

Y_pred(idx) = p1 > p0;


end

% test
accuracy = sum(Y_pred == Y_true) / length(Y_true);
fprintf('Prediction accuracy: %.2f%%\n', accuracy * 100);

2 of 4 8/6/2025, 9:28 PM
Untitled file:///C:/Users/chees/Documents/MatLab_WorkBook_3.html

data = [151320, 149832, 150458, ...


150020, 149263, 149908, ...
149395, 150956, 150003]; % sample data

n = length(data);
xbar = mean(data);
s = std(data, 0); % sample std dev
se = s / sqrt(n); % standard error

% t critical values
t_crit_95 = tinv(1 - 0.05/2, n - 1);
t_crit_99 = tinv(1 - 0.01/2, n - 1);

% Confidence intervals for mean


mu_ci_95 = xbar + [-1, 1] * t_crit_95 * se;
mu_ci_99 = xbar + [-1, 1] * t_crit_99 * se;

% Variance and Std Dev CI using chi-squared distribution


s2 = var(data, 0); % sample variance

% 95% CI for variance


alpha = 0.05;
chi2_low_95 = chi2inv(1 - alpha/2, n - 1);
chi2_high_95 = chi2inv(alpha/2, n - 1);
var_ci_95 = [(n-1)*s2/chi2_low_95, (n-1)*s2/chi2_high_95];
std_ci_95 = sqrt(var_ci_95);

% 99% CI for variance


alpha = 0.01;
chi2_low_99 = chi2inv(1 - alpha/2, n - 1);
chi2_high_99 = chi2inv(alpha/2, n - 1);
var_ci_99 = [(n-1)*s2/chi2_low_99, (n-1)*s2/chi2_high_99];
std_ci_99 = sqrt(var_ci_99);

% Display results
fprintf('95%% CI for Mean: [%.2f, %.2f]\n', mu_ci_95(1), mu_ci_95(2));

95% CI for Mean: [149608.12, 150648.55]

fprintf('99%% CI for Mean: [%.2f, %.2f]\n', mu_ci_99(1), mu_ci_99(2));

99% CI for Mean: [149371.39, 150885.28]

fprintf('95%% CI for Std Dev: [%.2f, %.2f]\n', std_ci_95(1), std_ci_95(2));

95% CI for Std Dev: [457.13, 1296.54]

fprintf('99%% CI for Std Dev: [%.2f, %.2f]\n', std_ci_99(1), std_ci_99(2));

99% CI for Std Dev: [408.53, 1650.90]

fprintf('95%% CI for Variance: [%.2f, %.2f]\n', var_ci_95(1), var_ci_95(2));

95% CI for Variance: [208968.40, 1681017.71]

fprintf('99%% CI for Variance: [%.2f, %.2f]\n', var_ci_99(1), var_ci_99(2));

99% CI for Variance: [166894.72, 2725476.30]

s2

s2 = 4.5802e+05

3 of 4 8/6/2025, 9:28 PM
Untitled file:///C:/Users/chees/Documents/MatLab_WorkBook_3.html

4 of 4 8/6/2025, 9:28 PM

You might also like