Newer
Older
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* glmrand.cc
Mark Woolrich, FMRIB Image Analysis Group
Copyright (C) 1999-2000 University of Oxford */
/* Part of FSL - FMRIB's Software Library
WWW: http://www.fmrib.ox.ac.uk/fsl
Email: fsl@fmrib.ox.ac.uk
Developed at FMRIB (Oxford Centre for Functional Magnetic Resonance
Imaging of the Brain), Department of Clinical Neurology, Oxford
University, Oxford, UK
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 2 of the License, or
(at
your option) any later version.
This program is distributed in the hope that it will be useful, but
in
order that the University as a charitable foundation protects its
assets for the benefit of its educational and research purposes, the
University makes clear that no condition is made or to be implied,
nor
is any warranty given or to be implied, as to the accuracy of FSL,
or
that it will be suitable for any particular purpose or for use under
any specific conditions. Furthermore, the University disclaims all
responsibility for the use which is made of FSL. 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA */
#include "glmrand.h"
#include "miscmaths.h"
#include "ols.h"
#include "Log.h"
#include "histogram.h"
#include "t2z.h"
#define __STL_NO_DRAND48
#include <vector.h>
#include <algo.h>
#ifndef NO_NAMESPACE
using namespace MISCMATHS;
using namespace TACO;
using namespace UTILS;
namespace SIGPROC {
#endif
void GlmRand::addData(ColumnVector& p_y, Matrix& p_x)
{
Tracer ts("GlmRand::addData");
yorig = p_y;
x = &p_x;
sizeTS = p_y.Nrows();
randomise();
}
void GlmRand::randomise()
{
Tracer ts("GlmRand::randomise");
y.ReSize(sizeTS, numrand+1);
vector<float> yorigvec;
// put in origy:
y.getSeries(1) = yorig.AsColumn();
//// void columnVector2Vector(const ColumnVector& cvec, vector<float>& vec)
ColumnVector cvec = yorig;
vector<float>& vec = yorigvec;
for(int j=1; j<=sizeTS; j++)
{
vec.push_back(cvec(j));
}
////////
// put in num randomised versions of yorig:
for(int i=1; i<=numrand; i++)
{
random_shuffle(yorigvec.begin(), yorigvec.end());
//// void vector2ColumnVector(const vector<float>& vec, ColumnVector& cvec)
vec = yorigvec;
for(int j=1; j<=sizeTS; j++)
{
cvec(j) = vec[j-1];
}
////////
y.getSeries(i+1) = cvec.AsColumn();
}
ComputeResids();
Computecb();
ComputeVar();
ComputeTStats();
}
void GlmRand::ComputeTStats()
{
Tracer ts("GlmRand::ComputeTStats");
datats(datatscount) = cb(1)/sqrt(var(1));
for(int i=1; i<=numrand; i++)
{
randts(((datatscount-1)*numrand)+i) = cb(i+1)/sqrt(var(i+1));
}
datatscount++;
}
const Volume& GlmRand::ComputeZStats()
{
Tracer ts("GlmRand::ComputeZStats");
Log::getInstance().out("randts",randts);
Log::getInstance().out("datats",datats);
Histogram hist(randts, randts.getVolumeSize());
hist.generate();
Volume logprob(numTS);
float logtotal = log((float)hist.integrateAll());
cerr << logtotal << endl;
T2z& t2z = T2z::getInstance();
for(int i=1; i<=numTS; i++)
{
float numtoinf = (float)hist.integrateToInf(datats(i));
if(!(numtoinf>0))
numtoinf = 1;
logprob(i) = log(numtoinf)-logtotal;
datazs(i) = t2z.convertlogp2z(logprob(i));
}
Log::getInstance().out("logprob",logprob);
Log::getInstance().out("datazs",datazs);
return datazs;
}
void GlmRand::ComputeResids()
{
Tracer ts("GlmRand::ComputeResids");
int batch_pos = 1;
Matrix& d = *x;
pinv_x = (d.t()*d).i()*d.t();
// R = I - x*pinv(x)
Matrix I(sizeTS, sizeTS);
Identity(I);
RMat = I-d*pinv_x;
r.ReSize(sizeTS, numrand+1);
while(batch_pos <= numrand+1)
{
if(batch_pos+batch_size - 1 > numrand+1)
r.Columns(batch_pos, numrand+1) = RMat*y.Columns(batch_pos, numrand+1);
else
r.Columns(batch_pos, batch_pos+batch_size-1) = RMat*y.Columns(batch_pos, batch_pos+batch_size-1);
batch_pos += batch_size;
}
}
void GlmRand::Computecb()
{
Tracer ts("Computecb");
int batch_pos = 1;
cb.ReSize(numrand+1);
while(batch_pos <= numrand+1)
{
if(batch_pos+batch_size - 1 > numrand+1)
cb.Rows(batch_pos, numrand+1) = (c.t()*pinv_x*y.Columns(batch_pos, numrand+1)).t();
else
cb.Rows(batch_pos, batch_pos+batch_size-1) = (c.t()*pinv_x*y.Columns(batch_pos, batch_pos+batch_size-1)).t();
batch_pos += batch_size;
}
}
void GlmRand::ComputeVar()
{
Tracer ts("ComputeVar");
int batch_pos = 1;
var.ReSize(numrand+1);
Matrix varmatfull(batch_size, batch_size);
ColumnVector vartempfull(batch_size);
Matrix& d = *x;
// inv_xx = inv(x'x)
float var_on_e = (c.t()*((d.t()*d).i())*c).AsScalar();
while(batch_pos <= numrand+1)
{
if(batch_pos+batch_size - 1 > numrand+1)
{
// var = e*var_on_e
// e is the estimate of the variance of the timeseries, sigma^2
Matrix varmat = (r.Columns(batch_pos, numrand+1).t()*r.Columns(batch_pos, numrand+1))*var_on_e/sizeTS;
ColumnVector vartemp;
getdiag(vartemp, varmat);
var.Rows(batch_pos, numrand+1) = vartemp;
}
else
{
varmatfull = (r.Columns(batch_pos, batch_pos+batch_size-1).t()*r.Columns(batch_pos, batch_pos+batch_size-1))*var_on_e/sizeTS;
getdiag(vartempfull, varmatfull);
var.Rows(batch_pos, batch_pos+batch_size-1) = vartempfull;
}
batch_pos += batch_size;
}
}
#ifndef NO_NAMESPACE
}
#endif