-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogLikelihood.m
54 lines (37 loc) · 1.13 KB
/
LogLikelihood.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
function L = LogLikelihood(tree, mu, a, nu, lambda, Y)
% Calculate log-likelihood of data given a tree.
% Input
% tree: an object of Tree class
% mu, a, nu, lambda: hyperparamaters. Refer to definition in MCMC function
% Y: target gene expression matrix: nsamples x ngenes
% Output
% L: loglikelihood
% if Y is a row vector, convert to col vector
if size(Y,1) == 1
Y = Y(:);
end
% Find leaves
allID = cell2mat(tree.lchild_ids.keys);
leafIDs = allID(isnan(cell2mat(tree.lchild_ids.values)));
L = 0;
for i = 1:length(leafIDs)
leafID = leafIDs(i);
dataID = tree.nodes(leafID).sample_ids;
% target value in this leaf
y = Y(dataID,:);
if isempty(y)
continue
end
% convert data into vector for easier calculation of sufficient stats
y = y(:);
n = length(y);
s = (n-1) * var(y);
t = n*a/(n+a)*(mean(y) - mu)^2;
L = L + ...
(-n*0.5)*log(pi) + ...
nu*0.5 * log(lambda*nu) + ...
0.5*(log(a)-log(n+a)) + ...
gammaln((n+nu)*0.5) - ... % change to log base 2
gammaln(nu*0.5) - ...
((n+nu)*0.5)*log(s+t+nu*lambda);
end