-
Notifications
You must be signed in to change notification settings - Fork 0
/
ay_deviance.m
132 lines (127 loc) · 3.67 KB
/
ay_deviance.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function [DEV_C,DEV_D]=ay_deviance(DISTR,In,Ib,Yn,Yb,Param,obs_valid,XSmt,SSmt)
% This function calculates the deviance for both continuous and discrete observations
% number of samples over censored points
Ns = 10;
K = max(length(Yn),length(Yb));
%% Here, you need the function to replace the censored/missing data with a sampleed one
if ~isempty(Yn)
tYn = repmat(Yn,1,Ns);
end
if ~isempty(Yb)
tYb = repmat(Yb,1,Ns);
end
for k=1:K
if obs_valid(k)==0 % missing
for s=1:Ns
[samp_yn,samp_yb] = ay_post_sampling(DISTR,[],In(k,:),Ib(k,:),Param,XSmt{k},SSmt{k});
if DISTR(1), tYn(k,s)= samp_yn; end
if DISTR(2), tYb(k,s)= samp_yb; end
end
end
if obs_valid(k)==2 % censored
for s=1:Ns
[samp_yn,samp_yb] = ay_post_sampling(DISTR,Param.censor_time,In(k,:),Ib(k,:),Param,XSmt{k},SSmt{k});
if DISTR(1), tYn(k,s)= samp_yn; end
if DISTR(2), tYb(k,s)= samp_yb; end
end
end
end
%% CONTINUOUS PART DEVIANCE
% Normal
DEV_C = [];
if DISTR(1) == 1
xM = Param.xM;
[MCk,MDk] = ay_Tk(In,Param);
Vk = Param.Vk;
DTk = Param.Dk.*MDk;
Ck = Param.Ck;
% deviance calculation
N = 1000;
DEV_C = 0;
for k=1:K
% draw samples
Xs = mvnrnd(XSmt{k},SSmt{k},N)' ;
CTk = (Ck.*MCk{k})*xM;
Mx = CTk * Xs + DTk * In(k,:)';
Sx = Vk;
if obs_valid(k)==1
DEV_C = DEV_C -2*sum(log(pdf('normal',tYn(k,1),Mx,sqrt(Sx))))/N;
end
if obs_valid(k)==2
avg_log_ll = 0;
for s=1:Ns
avg_log_ll = avg_log_ll -2*sum(log(pdf('normal',tYn(k,s),Mx,sqrt(Sx))))/N;
end
avg_log_ll = avg_log_ll/Ns;
DEV_C = DEV_C + avg_log_ll;
end
end
end
% Gamma
if DISTR(1)==2
xM = Param.xM;
[MCk,MDk] = ay_Tk(In,Param);
DTk = Param.Dk.*MDk;
% model parameters
Ck = Param.Ck;
Vk = Param.Vk;
S = Param.S;
% draw samples per trial
N = 1000;
DEV_C = 0;
for k=1:K
Xs = mvnrnd(XSmt{k},SSmt{k},N)' ;
CTk = (Ck.*MCk{k})*xM;
Mx = exp(CTk * Xs + DTk * In(k,:)');
if obs_valid(k)==1
DEV_C = DEV_C -2*sum(log(gampdf(tYn(k,1)-S,Vk,Mx/Vk)))/N;
end
if obs_valid(k)==2
avg_log_ll = 0;
for s=1:Ns
avg_log_ll = avg_log_ll -2*sum(log(gampdf(tYn(k,s)-S,Vk,Mx/Vk)))/N;
end
avg_log_ll = avg_log_ll/Ns;
DEV_C = DEV_C + avg_log_ll;
end
end
end
%% Discrete Part DEVIANCE
% we assume fully observed data
DEV_D = [];
if DISTR(2)==1
xM = Param.xM;
[MEk,MFk] = ay_Qk(Ib,Param);
% model parameters
Ek = Param.Ek;
FTk = Param.Fk.*MFk;
% draw samples per trial
N = 1000;
% map to larger space
DEV_D = 0;
for k=1:K
Xs = mvnrnd(XSmt{k},SSmt{k},N)' ;
ETk = (Ek.*MEk{k})*xM;
st = ETk * Xs + FTk * Ib(k,:)' ;
pk = exp(st)./(1+exp(st));
if obs_valid(k)== 1
if tYb(k,1)
DEV_D = DEV_D -2*sum(log(pk))/N;
else
DEV_D = DEV_D -2*sum(log(1-pk))/N;
end
end
if obs_valid(k)== 2
avg_log_ll = 0;
for s=1:Ns
if tYb(k,s)
avg_log_ll = avg_log_ll -2*sum(log(pk))/N;
else
avg_log_ll = avg_log_ll -2*sum(log(1-pk))/N;
end
end
avg_log_ll = avg_log_ll/Ns;
DEV_D = DEV_D + avg_log_ll;
end
end
end