-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpcm_generateData.m
executable file
·138 lines (127 loc) · 5.49 KB
/
pcm_generateData.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
function [Y,partVec,condVec] = pcm_generateData(Model,theta,varargin);
% function [Y,part,conditions] =pcm_generateData(Model,theta,varargin);
% pcm_generateData: Simulate multivariate data using the generative model specified in Model
% Noise and Signal strength can be specified for each simulation and voxel
% separately.
% INPUT:
% Model: Model to generate data from
% theta: numParams x 1 vector of parameters for Model
% VARARGIN:
% 'numPart',number of partitions (default 8)
% 'numVox', number of independent voxels (default 50)
% 'numSim', number of simulations,all returned in cell array Y (default 1)
% 'signal', Signal variance: scalar, <numSim x 1>, <1xnumVox>, or <numSim x numVox> (default 0.1)
% 'noise', Noise variance: scalar, <numSim x 1>, <1xnumVox>,<numVox x numVox>, or <numSim x numVox> (default 1)
% option of numVox x numVox generates correlated noise
% 'signalDist',fcnhnd: Functionhandle to distribution function for signal (default normal)
% 'noiseDist',fcnhnd: Functionhandle to distribution function for noise
% 'design',X: - Design matrix (for encoding-style models)
% - Condition vector (for RSA-style models)
% Design matrix and Condition vector are assumed
% to be for 1 partition only.
% If not specified - the function assumes a
% RSA-style model with G being numCond x numCond
% OUTPUT:
% Y: Cell array{numSim} of data
% partVec: Vector indicating the independent partitions
% condVec: Vector of conditions for RSA-style model
% Design matrix for Encoding-style models
% Note that the function uses an "exact" generation of the signal. Thus,
% the true (noiseless) pattern consistent across partitions has exactly
% the representational structure specified by the model
% 2017 [email protected]
% Todo: Implement partition and condition vector as input arguments as
% default
% Defaults:
warning('pcm_generateData is being phased out. Use pcm_makeDesign and pcm_makeDataset instead');
numPart = 8;
numVox = 50;
numSim = 1;
signal = 0.1;
noise = 1;
noiseDist = @(x) norminv(x,0,1); % Standard normal inverse for Noise generation
signalDist = @(x) norminv(x,0,1); % Standard normal inverse for Signal generation
design = [];
pcm_vararginoptions(varargin,{'numPart','numVox','numSim','signal','noise','signalDist','noiseDist','design'});
% Make the overall generative model
if (size(theta,1)~=Model.numGparams)
error('theta needs to be a numParams x 1 vector');
end;
G = pcm_calculateG(Model,theta);
if (isempty(design))
numCond = size(G,1);
N = numPart*numCond; % Number of trials
partVec = kron([1:numPart]',ones(numCond,1)); % Partitions
condVec = kron(ones(numPart,1),[1:numCond]'); % Conditions
Za = kron(ones(numPart,1),eye(numCond));
else
if (size(design,2)==1) % RSA-style condition vector
ZZ = pcm_indicatorMatrix('identity',design);
if (size(G,1)~=size(ZZ,2))
error('For RSA-style models, the design needs to contain as many conditions as G');
end;
condVec = kron(ones(numPart,1),[1:ones(size(ZZ,2))]'); % Conditions
numCond = size(ZZ,2);
N = numPart * size(ZZ,1);
partVec = kron([1:numPart]',ones(size(ZZ,1),1));
Za = kron(ones(numPart,1),ZZ);
else % Encoding-style design matrix
ZZ = design;
numCond = size(ZZ,2);
if (size(G,1)~=size(ZZ,2))
error('For Encoding-style models, the size(G) needs to be equal to the number of columns in design (feature)');
end;
N = numPart * size(ZZ,1);
partVec = kron([1:numPart]',ones(size(ZZ,1),1));
Za = kron(ones(numPart,1),ZZ);
condVec = Za;
end;
end;
% determine signal and noise covariance
[signalRow,signalCol]=size(signal);
[noiseRow,noiseCol]=size(noise);
if (signalRow==numVox && signalCol==numVox)
signalChol = cholcov(signal);
else
signalChol = eye(numVox);
end;
if (noiseRow==numVox && noiseCol==numVox)
noiseChol = cholcov(noise);
else
noiseChol = eye(numVox);
end;
for n = 1:numSim
% Determine signal for this simulation
if (signalRow == numSim)
thisSig = signal(n,:);
elseif (signalRow == numVox && signalCol==numVox)
thisSig = 1;
else
thisSig = signal;
end;
% Determine noise for this simulation
if (noiseRow==numSim)
thisNoi = noise(n,:);
elseif (noiseRow == numVox && noiseCol==numVox)
thisNoi = 1;
else
thisNoi = noise;
end;
% Generate true pattern from specified second moment
K = size(G,1);
pSignal = unifrnd(0,1,numCond,numVox);
U = signalDist(pSignal);
E = (U*U');
Z = E^(-0.5)*U; % Make random orthonormal vectors
A = pcm_diagonalize(G);
if (size(A,2)>numVox)
error('not enough voxels to represent G');
end;
trueU = A*Z(1:size(A,2),:)*signalChol*sqrt(numVox);
trueU = bsxfun(@times,trueU,sqrt(thisSig)); % Multiply by (voxel-specific) signal scaling factor
% Now add the random noise
pNoise = unifrnd(0,1,N,numVox);
Noise = noiseDist(pNoise)*noiseChol;
Noise = bsxfun(@times,Noise,sqrt(thisNoi));
Y{n} = Za*trueU + Noise;
end;