-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpcm_calculateG.m
executable file
·55 lines (54 loc) · 1.92 KB
/
pcm_calculateG.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
function [G,dGdtheta] = pcm_calculateG(M,theta)
% function [G,dGdtheta] = pcm_calculateG(M,theta)
% This function calculates the predicted second moment matrix (G) and the
% derivate of the second moment matrix in respect to the parameters theta.
% INPUT:
% M: Model structure
% theta: Vector of parameters
% OUTPUT:
% G: Second moment matrix
% dGdtheta: Matrix derivatives in respect to parameters
% Joern Diedrichsen, 2016
if (~isstruct(M))
G=M; % Fixed, parameterless G
dGdtheta = [];
else
if length(theta)~=M.numGparams
error('length of column-vector theta should be equal to number of G parameters');
end;
if (~isfield(M,'type'))
error('M should have a type of fixed / component / feature / nonlinear');
end;
switch (M.type)
case {'fixed','freedirect'}
G = M.Gc(:,:,1);
dGdtheta =[];
case 'component'
if M.numGparams==0
G = M.Gc(:,:,1);
dGdtheta =[];
else
dGdtheta=bsxfun(@times,M.Gc,permute(exp(theta),[3 2 1]));
G = sum(dGdtheta,3);
end;
case {'feature'}
A = bsxfun(@times,M.Ac,permute(theta,[3 2 1]));
A = sum(A,3);
G = A*A';
for i=1:M.numGparams
dA = M.Ac(:,:,i)*A';
dGdtheta(:,:,i) = dA + dA';
end;
case 'freechol'
A = zeros(M.numCond);
A(M.indx) = theta;
G = A*A';
dGdtheta = zeros(M.numCond,M.numCond,M.numGparams);
for i=1:M.numGparams
dGdtheta(M.row(i),:,i) = A(:,M.col(i))';
dGdtheta(:,:,i) = dGdtheta(:,:,i) + dGdtheta(:,:,i)';
end;
case 'nonlinear'
[G,dGdtheta]=M.modelpred(theta(1:M.numGparams),M);
end;
end;