Skip to content

Commit e515343

Browse files
Updated version of HousePrice with Table 8.1
1 parent a10cec1 commit e515343

File tree

2 files changed

+245
-95
lines changed

2 files changed

+245
-95
lines changed

cap8/HousePrice.ipynb

Lines changed: 194 additions & 78 deletions
Large diffs are not rendered by default.

cap8/HousePrice.m

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
%% Windsor House Price data.
2-
% This file creates Figures 8.1-8.7.
2+
% This file creates Figures 8.1-8.7
3+
% and Table 8.1
34

45
%% Beginning of code
56
prin=0;
67

78
load hprice.txt;
89

910
% setup parameters
10-
n=size(hprice,1);
11+
1112
y=hprice(:,1);
1213
X=hprice(:,2:5);
1314

15+
n=size(hprice,1);
16+
p=size(X,2)+1;
17+
1418
bayes=struct;
15-
n0=5;
19+
n0=6;
1620
bayes.n0=n0;
1721

18-
% set \beta components
22+
% beta prior setting
1923
beta0=zeros(5,1);
2024
beta0(2,1)=10;
2125
beta0(3,1)=5000;
2226
beta0(4,1)=10000;
2327
beta0(5,1)=10000;
2428
bayes.beta0=beta0;
2529

26-
% \tau
30+
% tau0 prior setting
2731
s02=1/4.0e-8;
2832
tau0=1/s02;
2933
bayes.tau0=tau0;
@@ -34,28 +38,34 @@
3438
R(3,3)=.15;
3539
R(4,4)=.6;
3640
R(5,5)=.6;
41+
diagR=diag(R);
3742
R=inv(R);
3843
bayes.R=R;
3944

4045
%% Create initial yXplot
4146
% Not given in the book
4247
% yXplot(y,X)
4348

44-
%% Create Figures 8.1 and 8.4
49+
%% Create Table 8.1
50+
% Windsor House Price data: prior and least squares estimates of parameters
4551

46-
outBA=FSRB(y,X,'bayes',bayes', 'plots',1,'xlim',[280 n]);
47-
dout=n-length(outBA.ListOut);
52+
betaLS=regstats(y,X,"linear",{'beta', 'mse'});
53+
betaOLS=betaLS.beta;
54+
tauOLS=1/(betaLS.mse*(n-p)/n);
4855

49-
fig=findobj(0,'tag','fsr_yXplot');
50-
figure(fig(1))
51-
set(gcf,'Tag','fsr_yXplot81')
56+
T1=[[beta0; tau0], [diagR; n0], [betaOLS; tauOLS]];
57+
nam=["beta"+(0:4) "tau"];
58+
T1table=array2table(T1,'RowNames',nam,'VariableNames',["mean" "V0jj" "Least squares"]);
59+
disp('Table 8.1')
60+
disp(T1table)
5261

53-
if prin ==1
54-
print -depsc h4.eps;
55-
else
56-
set(gcf,'Name', 'Figure 8.4');
57-
sgtitle('Figure 8.4')
58-
end
62+
%% Create Figure 8.1
63+
64+
outFSRB=FSRB(y,X,'bayes',bayes', 'plots',1,'xlim',[280 n]);
65+
dout=n-length(outFSRB.ListOut);
66+
67+
fig=findobj(0,'tag','fsr_yXplot');
68+
delete(fig(1))
5969

6070
fig=findobj(0,'tag','pl_fsr');
6171
figure(fig(1))
@@ -234,6 +244,10 @@
234244

235245
drawnow
236246

247+
%% Figure 8.4 will be created at the end of the file
248+
% Note that Figure 8.4 is created later because we need both the output of FSR
249+
% (frequentist forward search) and FSRB (Bayesian forward search)
250+
237251
%% Create Figure 8.5 (frequentist analysis)
238252
% Monitoring of 95 per cent and 99 per cent confidence intervals
239253
% of beta and sigma2
@@ -317,6 +331,7 @@
317331

318332
drawnow
319333

334+
320335
%% Create Figure 8.6
321336
figure
322337
n0=250;
@@ -345,6 +360,25 @@
345360
set(gcf,'Name', 'Figure 8.7');
346361
sgtitle('Figure 8.7')
347362
end
363+
364+
365+
%% Create Figure 8.4
366+
% Note that plot is created later because we need both the output of FSR
367+
% (frequentist forward search) and FSRB (Bayesian forward search)
368+
group=ones(n,1);
369+
commonOutliers=intersect(outFSR.outliers,outFSRB.outliers);
370+
onlyFSRB=setdiff(outFSRB.outliers,outFSR.outliers);
371+
group(commonOutliers)=2;
372+
group(onlyFSRB)=3;
373+
yXplot(y,X,'group',group)
374+
legend(["Normal observations" "Outliers found both by FSR and FSRB" "Outlier found just by FSRB"])
375+
376+
if prin ==1
377+
print -depsc h4.eps;
378+
else
379+
set(gcf,'Name', 'Figure 8.4');
380+
sgtitle('Figure 8.4')
381+
end
348382
%InsideREADME
349383

350384

0 commit comments

Comments
 (0)