SLIDE 14 Example 3: Confidence Intervals MATLAB
function [] = ConfidenceIntervals(); close all; rand(’state’,1); randn(’state’,3); N = 50; % Number of points P = 2; % Number of parameters x = rand(N,1); % Data set inputs y = 2*x - 1 + randn(N,1); % Data set outputs A = [ones(N,1) x]; % A matrix w = pinv(A)*y; % Estimated weights yh = A*w; % Model outputs SSE = sum((y -yh).^2); % Error sum of squares ASE = SSE/(N-P); % Average squared error s2 = ASE*inv(A’*A); % Covariance matrix of weight vector xt = (-1:0.01:2)’; % Test points NT = length(xt); % No. test points At = [ones(NT,1) xt]; % Test matrix yh = w(1) + w(2)*xt; % Model output at test points yt = -1 + 2 *xt; % Process output at test points sy2 = zeros(NT,1); % s^2[y_hat] for cnt = 1:NT, sy2(cnt) = At(cnt,:)*s2*At(cnt,:)’; end; sy = sy2.^(1/2); % s[y_hat] sp2 = sy2 + ASE; % s^2[y_hat + e] sp = sp2.^(1/2); % s[y_hat + e] %======================================================== % Expected Point Prediction Boundaries
Portland State University ECE 4/557 Linear Models
55
Expected Prediction Confidence Intervals
- Ideally we would like our model to
– Generate good estimated outputs ˆ y for new inputs – Give us a confidence interval on the estimate
var(ˆ y) = σ2
εxT(ATA)−1x = xT
σ2
ε(ATA)−1
x = x
1×p Tvar(w) p×p
x
p×1
- The estimated variance is then given by
s2[ˆ y] = ASE xT(ATA)−1x = x
1×p Ts2[w] p×p
x
p×1
- The 1 − α confidence limits for E[y] are
ˆ y ± t(1 − α/2; n − p)s[ˆ y]
Portland State University ECE 4/557 Linear Models
53
%======================================================== figure; FigureSet(1,4.5,2.8); yl95 = yh - tinv(1-0.05/2,N-P)*sy; % Lower 95% confidence interval yu95 = yh + tinv(1-0.05/2,N-P)*sy; % Upper 95% confidence interval yl99 = yh - tinv(1-0.01/2,N-P)*sy; % Lower 95% confidence interval yu99 = yh + tinv(1-0.01/2,N-P)*sy; % Upper 95% confidence interval h = plot(x,y,’b.’,xt,yt,’b’,xt,yh,’c’,... xt,yl95,’g’,xt,yl99,’r’,... xt,yu95,’g’,xt,yu99,’r’); set(h(1),’MarkerSize’,15); set(h([4 6]),’Color’,[0.0 0.7 0.0]); set(h,’LineWidth’,1.5); xlabel(’Input x’); ylabel(’Output y’); title(’Confidence Intervals for E[y]’); set(gca,’Box’,’Off’); grid on; axis([min(xt) max(xt) -5 5]); AxisSet(8); legend(’Data Set’,’True’,’Model’,’95% CI’,’99% CI’,2); print -depsc ExpectedConfidenceInterval; %======================================================== % Expected Point Prediction Boundaries %======================================================== figure; FigureSet(1,4.5,2.8); yl95 = yh - tinv(1-0.05/2,N-P)*sp; % Lower 95% confidence interval yu95 = yh + tinv(1-0.05/2,N-P)*sp; % Upper 95% confidence interval yl99 = yh - tinv(1-0.01/2,N-P)*sp; % Lower 95% confidence interval yu99 = yh + tinv(1-0.01/2,N-P)*sp; % Upper 95% confidence interval h = plot(x,y,’b.’,xt,yt,’b’,xt,yh,’c’,... xt,yl95,’g’,xt,yl99,’r’,... xt,yu95,’g’,xt,yu99,’r’);
Portland State University ECE 4/557 Linear Models
56
Expected Prediction Confidence Interval Comments The 1 − α confidence limits for E[y] are ˆ y ± t(1 − α/2; n − p)s[ˆ y]
y = xTω + ε E[y] = xTω
- The above confidence interval is for the expected value of y, not
for y itself
- In words, if all of the normal error model assumptions hold, the
expected value of y will fall in the above confidence interval 100(1 − α) percent of the time
- As α decreases, the confidence interval gets larger
- J. McNames
Portland State University ECE 4/557 Linear Models
54