-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitDebiasedPC.m
132 lines (106 loc) · 3.56 KB
/
fitDebiasedPC.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
function dataOut = fitDebiasedPC(data)
alpha = 0.1;
idx = data.finished & data.random;
optiStim = data.optiStim;
contrast = data.contrast;
behavior = data.behavior;
session = data.iSession;
idxNone = idx & ~optiStim(:, 1) & ~optiStim(:,2);
idxLeft = idx & optiStim(:, 1) & ~optiStim(:,2);
idxRight = idx & ~optiStim(:, 1) & optiStim(:,2);
idxBoth = idx & optiStim(:, 1) & optiStim(:,2);
idx = {idxNone; idxLeft; idxRight; idxBoth};
groupName = {'none'; 'left'; 'right'; 'both'};
LineStyle = {'.k'; '.r'; '.b'; '.m'};
pcLineStyle = {'k'; 'r'; 'b'; 'm'};
nGroups = length(idx);
nSessions = max(session);
%%
for iGroup = 1:nGroups
for iSession = 1:nSessions
idxSession = idx{iGroup} & session == iSession;
cc{iGroup, iSession} = unique(contrast(idxSession));
nn{iGroup, iSession} = nan(size(cc{iGroup, iSession}));
nr{iGroup, iSession} = nan(size(cc{iGroup, iSession}));
for iC = 1:length(cc{iGroup, iSession})
idxC = idxSession & contrast == cc{iGroup, iSession}(iC);
nn{iGroup, iSession}(iC) = sum(idxC);
nr{iGroup, iSession}(iC) = sum(behavior(idxC) == 'R');
end
% [pp{iGroup}, ci{iGroup}]= binofit(nr{iGroup}, nn{iGroup}, alpha);
end
ccAll{iGroup} = unique(contrast(idx{iGroup}));
nnAll{iGroup} = nan(size(ccAll{iGroup}));
nrAll{iGroup} = nan(size(ccAll{iGroup}));
for iC = 1:length(ccAll{iGroup})
idxC = idx{iGroup} & contrast == ccAll{iGroup}(iC);
nnAll{iGroup}(iC) = sum(idxC);
nrAll{iGroup}(iC) = sum(behavior(idxC) == 'R');
end
end
%% Fit logit psychometric curve to pooled data
x0 = [0, 0.2, 0.1, 0.1];
figure
for iGroup = 1:nGroups
ccFit = ccAll{iGroup};
nnFit = nnAll{iGroup};
nrFit = nrAll{iGroup};
pOut{iGroup} = fminsearch(@logLikFun, x0);
xx = -50:50;
yy = pcCurve(xx, pOut{iGroup});
plot(ccFit, nrFit./nnFit, LineStyle{iGroup}, 'MarkerSize', 20);
hold on;
plot(xx, yy, pcLineStyle{iGroup}, 'LineWidth', 3);
end
%% Fit logit psychometric curve session-by-session correcting for bias
% figure
for iGroup = 1:nGroups
x0 = [pOut{iGroup}(1)*ones(1, nSessions), pOut{iGroup}(2:4)];
ccFit = cc(iGroup, :);
nnFit = nn(iGroup, :);
nrFit = nr(iGroup, :);
pOutBiased{iGroup} = fminsearch(@logLikBiasedFun, x0);
% xx = -50:50;
% yy = pcCurve(xx, pOut{iGroup});
% plot(ccFit, nrFit./nnFit, LineStyle{iGroup}, 'MarkerSize', 20);
% hold on;
% plot(xx, yy, pcLineStyle{iGroup}, 'LineWidth', 3);
end
%% plot debiased results
figure;
for iGroup = 1:nGroups
xx = -50:50;
yy = pcCurve(xx, pOut{iGroup});
plot(xx, yy, pcLineStyle{iGroup}, 'LineWidth', 3);
hold on;
yy = pcCurve(xx, [0, pOutBiased{iGroup}(end-2:end)]);
plot(xx, yy, pcLineStyle{iGroup}, 'LineWidth', 1);
xlim([-50 50]);
ylim([0 1]);
plot(xlim, [0.5 0.5], 'k:', [0 0], ylim, 'k:');
end
plotDebiased();
%%
dataOut = pOut;
function logL = logLikFun(p)
f = pcCurve(ccFit, p);
logL = sum(log(f).*nrFit + log(1-f).*(nnFit-nrFit));
logL = -logL; % find minimum, not maximum
end
function logL = logLikBiasedFun(pAll)
logL = 0;
for iS = 1:length(ccFit)
p = [pAll(iS), pAll(end-2:end)];
f = pcCurve(ccFit{iS}, p);
logL = logL + sum(log(f).*nrFit{iS} + log(1-f).*(nnFit{iS}-nrFit{iS}));
end
logL = -logL; % find minimum, not maximum
end
end
function out = pcCurve(x, p)
b = p(1);
s = p(2);
upL = p(3);
lowL = p(4);
out = (1-upL-lowL)./(1+exp(-s*(x-b)))+lowL;
end