forked from andersonwinkler/PALM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
palm_maxshuf.m
142 lines (133 loc) · 4.28 KB
/
palm_maxshuf.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
function maxb = palm_maxshuf(Ptree,stype,uselog)
% Computes the maximum number of possible permutations given
% a tree that specifies the depencence between the observations.
%
% Usage:
% maxb = palm_maxshuf(Ptree,ptype,uselog)
%
% - Ptree : Permutation tree, generated by palm_tree.
% - stype : Shuffling type to count. It can be one of:
% - 'perms' for permutations.
% - 'flips' for sign-flips
% - 'both' for permutations with sign-flips.
% - uselog : A true/false indicating whether compute in logs.
% Default is false.
% - maxb : Maximum number of possible shufflings.
%
% _____________________________________
% Anderson M. Winkler
% FMRIB / University of Oxford
% Oct/2013
% http://brainder.org
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% PALM -- Permutation Analysis of Linear Models
% Copyright (C) 2015 Anderson M. Winkler
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if nargin == 1,
stype = 'perms';
uselog = false;
elseif nargin == 2,
uselog = false;
end
if uselog,
switch lower(stype),
case 'perms',
maxb = lmaxpermnode(Ptree,0);
case 'flips',
maxb = lmaxflipnode(Ptree,0);
maxb = maxb/log2(exp(1));
case 'both',
maxp = lmaxpermnode(Ptree,0);
maxs = lmaxflipnode(Ptree,0);
maxs = maxs/log2(exp(1));
maxb = maxp + maxs;
end
else
switch lower(stype),
case 'perms',
maxb = maxpermnode(Ptree,1);
case 'flips',
maxb = maxflipnode(Ptree,1);
case 'both',
maxp = maxpermnode(Ptree,1);
maxs = maxflipnode(Ptree,1);
maxb = maxp * double(maxs);
end
end
% ==============================================================
function np = maxpermnode(Ptree,np)
% Number of permutations per node, recursive and
% incremental.
for u = 1:size(Ptree,1),
np = np * seq2np(Ptree{u,1}(:,1));
if size(Ptree{u,3},2) > 1,
np = maxpermnode(Ptree{u,3},np);
end
end
% ==============================================================
function np = seq2np(S)
% Takes a sequence of integers and computes the
% number of possible permutations.
U = unique(S);
nU = numel(U);
cnt = zeros(size(U));
for u = 1:nU,
cnt(u) = sum(S == U(u));
end
np = factorial(numel(S))/prod(factorial(cnt));
% ==============================================================
function ns = maxflipnode(Ptree,ns)
% Number of sign-flips per node, recursive and
% incremental.
for u = 1:size(Ptree,1),
if size(Ptree{u,3},2) > 1,
ns = maxflipnode(Ptree{u,3},ns);
end
ns = ns * 2^length(Ptree{u,2});
end
% ==============================================================
function np = lmaxpermnode(Ptree,np)
% Number of permutations per node, recursive and
% incremental.
for u = 1:size(Ptree,1),
np = np + lseq2np(Ptree{u,1}(:,1));
if size(Ptree{u,3},2) > 1,
np = lmaxpermnode(Ptree{u,3},np);
end
end
% ==============================================================
function np = lseq2np(S)
% Takes a sequence of integers and computes the
% number of possible permutations.
nS = numel(S);
U = unique(S);
nU = numel(U);
cnt = zeros(size(U));
for u = 1:nU,
cnt(u) = sum(S == U(u));
end
lfac = palm_factorial(nS);
np = lfac(nS+1) - sum(lfac(cnt+1));
% ==============================================================
function ns = lmaxflipnode(Ptree,ns)
% Number of sign-flips per node, recursive and
% incremental. Note the in/output are base2 logarithm.
for u = 1:size(Ptree,1),
if size(Ptree{u,3},2) > 1,
ns = lmaxflipnode(Ptree{u,3},ns);
end
ns = ns + length(Ptree{u,2});
end