-
Notifications
You must be signed in to change notification settings - Fork 1
/
logL_CE_w_grad_1.m
173 lines (144 loc) · 4.78 KB
/
logL_CE_w_grad_1.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
%% CURRENTLY DEPRECATED
% function [logL] = logL_CE_w_grad_1(theta,Data,Model,options)
function varargout = logL_CE_w_grad_1(varargin)
%% Load
persistent tau
if isempty(tau)
tau = clock;
end
%% Initialization
theta = varargin{1};
Data = varargin{2};
Model = varargin{3};
options.sign = 'positive';
options.cvo_type = 'diag-matrix-logarithm';
options.grad_ind = [1:length(theta)]';
options.tau_update = 3;
options.plot = 0;
if nargin == 4
options = setdefault(varargin{4},options);
end
if etime(clock,tau) > options.tau_update
options.plot = 1;
tau = clock;
end
%% Evaluation of likelihood function
% Initialization
logL = 0;
dlogLdtheta = zeros(length(theta),1);
% Data types
% - Single-Cell Time Lapse (SCTL)
% - Population Average (PA)
% - Population Snapshot (PS)
data_type = {'SCTL','PA','PS'};
% Assignment of global variables
A = Model.A;
B = Model.B;
n_beta = Model.n_beta;
n_D = Model.n_D;
n_b = Model.n_b;
ind_beta = Model.ind_beta;
ind_D = Model.ind_D;
type_D = Model.type_D;
% Construct fixed effects and Covariance matrix
beta = theta(ind_beta);
[D,invD,dD,dinvD,HD,HinvD] = xi2D(theta(ind_D),type_D);
% Loop: Experimental conditions
for s = 1:length(Data)
%% Construction of time vector
t_s = [];
for dtype = 1:length(data_type)
if isfield(Data{s},data_type{dtype})
t_s = union(eval(['Data{s}.' data_type{dtype} '.time']),t_s);
end
end
%% Single cell time-lapse data
if isfield(Data{s},'SCTL')
% Evaluation of time index set
[~,ind_t] = ismember(Data{s}.SCTL.time,t_s);
% Empty plot
if options.plot, figure(1);
subplot(length(Data),2,2*(s-1)+1); hold off;
subplot(length(Data),2,2*(s-1)+2); hold off;
end
% Loop: Indiviudal cells
for i = 1:size(Data{s}.SCTL.Y,3)
% Load single-cell data
Ym_si = Data{s}.SCTL.Y(:,:,i);
Sigma_si = Data{s}.SCTL.Sigma(:,:,i);
ind = find(~isnan(Ym_si));
% Construct single-cell parameter
ind_b_si = Model.ind_b_si(s,i);
b_si = theta(ind_b_si);
phi_si = A*beta + B*b_si;
% Simulate model
[~,~,~,y,~,sy] = Model.simulation{s}(t_s,phi_si);
% Evaluation of likelihood
Y_si = y(ind_t,:);
logL = logL ...
- 0.5*sum(((Ym_si(ind) - Y_si(ind))./Sigma_si(ind)).^2) ...
- 0.5*log(det(D)) - 0.5*b_si'*invD*b_si;
if nargout >= 2
% beta
for k = 1:length(ind_beta)
sY_si_k = sy(ind_t,:,:)*(A(:,k).*exp(phi_si));
dlogLdtheta(ind_beta(k)) = dlogLdtheta(ind_beta(k)) ...
+ sum(((Ym_si(ind) - Y_si(ind))./Sigma_si(ind).^2).*sY_si_k(ind));
end
% b
for k = 1:length(ind_b_si)
sY_si_k = sy(ind_t,:,:)*(B(:,k).*exp(phi_si));
dlogLdtheta(ind_b_si(k)) = dlogLdtheta(ind_b_si(k)) ...
+ sum(((Ym_si(ind) - Y_si(ind))./Sigma_si(ind).^2).*sY_si_k(ind)) ...
- invD(k,:)*b_si;
end
% D
for k = 1:length(ind_D)
dlogLdtheta(ind_D(k)) = dlogLdtheta(ind_D(k)) ...
- 0.5*trace(invD*dD(:,:,k)) ...
- 0.5*b_si'*dinvD(:,:,k)*b_si;
end
end
% Visulization
if options.plot
subplot(length(Data),2,2*(s-1)+1);
plot(t_s,y,'r'); hold on;
plot(Data{s}.SCTL.time,Data{s}.SCTL.Y(:,:,i),'b-');
subplot(length(Data),2,2*(s-1)+2);
plot(Data{s}.SCTL.time,Data{s}.SCTL.Y(:,:,i)-y,'b-');hold on;
end
end
% Visulization
if options.plot
subplot(length(Data),2,2*(s-1)+1);
xlabel('time'); ylabel('y and y^m');
title(Data{s}.name)
subplot(length(Data),2,2*(s-1)+2);
xlabel('time'); ylabel('y^m-y');
title(Data{s}.name)
drawnow;
end
end
% Population average data
% Population snapshot data
end
%% Output
if nargout <= 1
% One output
switch options.sign
case 'positive'
varargout{1} = logL;
case 'negative'
varargout{1} = -logL;
end
else
% Two outputs
switch options.sign
case 'positive'
varargout{1} = logL;
varargout{2} = dlogLdtheta(options.grad_ind);
case 'negative'
varargout{1} = -logL;
varargout{2} = -dlogLdtheta(options.grad_ind);
end
end