forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagnostics.m
114 lines (87 loc) · 2.38 KB
/
diagnostics.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
%calculate some diagnostics using generated data
clear;
clc;
randn('seed',sum(100*clock));
rand('seed',sum(100*clock));
beta0 = 1;
beta2 = .5;
beta_true = [1 .5]';
sigsq = .4;
nobs = 2000;
xmat = [ones(nobs,1) randn(nobs,1)];
%prior hyperparms
mu_beta = zeros(2,1);
V_beta = 100*eye(2);
a = 3;
b = (1/(2*.2));
iter = 2000;
burn = 100;
skew_final = zeros(iter-burn,3);
kurt_final = skew_final;
sigmasq = 1;
for j = 1:3;
%NOW, GENERATE Y. CHANGE EACH TIME
%normal errors
if j ==1;
y = xmat*beta_true + sqrt(sigsq)*randn(nobs,1);
disp('doing Normal');
elseif j==2;
%UNIFORM ERRORS (here uniform from -5 to 5
error_vec = -5 + 10*rand(nobs,1);
y = xmat*beta_true + error_vec;
disp('doing uniform');
elseif j==3;
%CHISQUARE ERRORS
error_vec = chi2rnd(5,nobs,1) - 5;
y = xmat*beta_true + error_vec;
disp('doing chisquare')
end;
for i = 1:iter
%------------
%sample beta
%------------
D_beta = inv(xmat'*xmat/sigmasq + inv(V_beta));
d_beta = xmat'*y/sigmasq + inv(V_beta)*mu_beta;
H_beta = chol(D_beta);
betas = D_beta*d_beta + H_beta'*randn(2,1);
%--------------
%sample sigma^2
%--------------
sigmasq = invgamrnd((nobs/2) + a, inv( inv(b) + .5*(y - xmat*betas)'*(y - xmat*betas)),1,1);
%calculate diagnostic measures - skewness and kurtosis
resid = y - xmat*betas;
skew = (mean( (resid.^3)))/( (sqrt(sigmasq))^(3/2) );
kurt = (mean( (resid.^4)))/( ((sigmasq))^2 );
if i > burn;
skew_final(i-burn,j) = skew;
kurt_final(i-burn,j) = kurt;
end;
end;
end;
subplot(3,2,1);
[dom,ran] = epanech2(skew_final(:,1));
plot(dom,ran);
xlabel('Skewness - Normal');
%axis([-.4 1.6 0 6.5]);
subplot(3,2,3);
[dom ran] = epanech2(skew_final(:,2));
plot(dom,ran);
xlabel('Skewness - Uniform');
%axis([-.4 1.6 0 6.5]);
subplot(3,2,5);
[dom ran] = epanech2(skew_final(:,3));
plot(dom,ran);
xlabel('Skewness - Chisquare');
%axis([-.4 1.6 0 6.5]);
subplot(3,2,2);
[dom,ran] = epanech2(kurt_final(:,1));
plot(dom,ran);
xlabel('Kurtosis - Normal');
subplot(3,2,4);
[dom ran] = epanech2(kurt_final(:,2));
plot(dom,ran);
xlabel('Kurtosis - Uniform');
subplot(3,2,6);
[dom ran] = epanech2(kurt_final(:,3));
plot(dom,ran);
xlabel('Kurtosis - Chisquare');