|
@@ -0,0 +1,89 @@
|
|
|
+function [likelihood eventprobs forward backward]=calcEEG50CondshAllD(bumps,conds,mags,params,x, y,map)
|
|
|
+spacing=5;
|
|
|
+offset = 2;
|
|
|
+%offset is how soon the first peak can be or how late the last
|
|
|
+%space is the distance between peaks
|
|
|
+nconds=max(conds);
|
|
|
+nstates=size(mags,2);
|
|
|
+lens=y-x+1;
|
|
|
+maxDur = max(lens);
|
|
|
+ndims=size(bumps,2);
|
|
|
+nsamples=size(bumps,1);
|
|
|
+ntrials=length(x);
|
|
|
+gains=zeros(nsamples,nstates);
|
|
|
+for i = 1:ndims
|
|
|
+ %gains=gains+(2*bumps(:,i)*mags(i,:)-repmat(mags(i,:).^2,nsamples,1))/(2/5);
|
|
|
+ gains=gains+bumps(:,i)*mags(i,:)-repmat(mags(i,:).^2,nsamples,1)/2;
|
|
|
+end
|
|
|
+gains=exp(gains);
|
|
|
+probs=zeros(maxDur,ntrials,nstates);
|
|
|
+probsB=zeros(maxDur,ntrials,nstates);
|
|
|
+for i = 1:ntrials
|
|
|
+ probs(offset+1:y(i)-x(i)+1-offset,i,:)=gains(x(i)+offset:y(i)-offset,:);
|
|
|
+ for j = 1:nstates
|
|
|
+ probsB(offset+1:y(i)-x(i)+1-offset,i,j)=wrev(gains(x(i)+offset:y(i)-offset,nstates+1-j));
|
|
|
+ end
|
|
|
+end
|
|
|
+LP=zeros(maxDur,nstates+1,nconds);
|
|
|
+for j = 1:nstates+1
|
|
|
+ for i = 1:nconds
|
|
|
+ if j==nstates+1 || map(i,j)~=0
|
|
|
+ % do gamma PDF for last stage and requested stage/condition (map)
|
|
|
+ LP(:,j,i)=gammaEEG(params(j,1,i),params(j,2,i),maxDur);
|
|
|
+ end
|
|
|
+ end
|
|
|
+end
|
|
|
+BLP=LP(:,nstates+1:-1:1,:);
|
|
|
+forward = zeros(maxDur,ntrials,nstates);
|
|
|
+backward = zeros(maxDur,ntrials,nstates);
|
|
|
+for j = 1:nconds % iterate conditions
|
|
|
+ % it does forward & backward by conditions. If a stage=S in a condition is not
|
|
|
+ % requested, it assign to stage=S forward and backward from stage=S-1
|
|
|
+ nj = sum(conds==j); % number of trials of condition
|
|
|
+ lensT=lens(conds==j);
|
|
|
+ forwardT=zeros(maxDur,nj,nstates);
|
|
|
+ forwardBT=zeros(maxDur,nj,nstates);
|
|
|
+ backwardT=zeros(maxDur,nj,nstates);
|
|
|
+ forwardBT(offset+1:maxDur,:,1)=repmat(BLP(1:maxDur-offset,1,j),1,nj); % reversed gamma PDF
|
|
|
+ forwardT(offset+1:maxDur,:,1)=repmat(LP(1:maxDur-offset,1,j),1,nj).*probs(offset+1:maxDur,conds==j,1); % Gamma PDF * Gains of condition
|
|
|
+ probsT=probs(:,conds==j,:);
|
|
|
+ probsBT=probsB(:,conds==j,:);
|
|
|
+ for i = 2:nstates % iterate bumps
|
|
|
+ if map(j,i)>0 % if condition/current stage estimation requested
|
|
|
+ next=cat(1,zeros(spacing,1),LP(1:maxDur-spacing,i,j)); % gamma PDF (shifted by offset)
|
|
|
+ for k = 1:nj % iterate trials of condition
|
|
|
+ temp=conv(forwardT(:,k,i-1),next); % convolution with previous stage ~~ conv(gammaPDF(i-1)*gains(i-1), gammaPDF(i))
|
|
|
+ forwardT(:,k,i)=temp(1:maxDur);
|
|
|
+ end
|
|
|
+ forwardT(:,:,i)=forwardT(:,:,i).*probsT(:,:,i); % forwardT ~~ conv(gammaPDF(i-1)*gains(i-1), gammaPDF(i)) * gains(i)
|
|
|
+ else % if not requested: assign to current stage forwardT of previous stage/state
|
|
|
+ forwardT(:,:,i)=forwardT(:,:,i-1);
|
|
|
+ end
|
|
|
+ if map(j,nstates+2-i)>0 % if condition/last minus current stage estimation requested
|
|
|
+ nextB=cat(1,zeros(spacing,1),BLP(1:maxDur-spacing,i,j));
|
|
|
+ addB=forwardBT(:,:,i-1).*probsBT(:,:,i-1); % add ~~ gammaPDF_b(i-1)*gains_b(i-1) ; _b = reverted back to forth
|
|
|
+ for k=1:nj % iterate trials of condition
|
|
|
+ temp=conv(addB(:,k),nextB); % temp ~~ conv(gammaPDF_b(i-1)*gains_b(i-1), gammaPDF_b(i))
|
|
|
+ forwardBT(:,k,i)=temp(1:maxDur);
|
|
|
+ end
|
|
|
+ else % if not requested: assign to current stage forwardBT of previous stage/state
|
|
|
+ forwardBT(:,:,i)=forwardBT(:,:,i-1);
|
|
|
+ end
|
|
|
+ end
|
|
|
+ forwardBT=forwardBT(:,:,nstates:-1:1);
|
|
|
+ for k=1:nj
|
|
|
+ for i = 1:nstates
|
|
|
+ backwardT(1:lensT(k),k,i)=wrev(forwardBT(1:lensT(k),k,i)); % revers forwardBT by trial and stage
|
|
|
+ end
|
|
|
+ end
|
|
|
+ backwardT(1:offset,:,:)=0;
|
|
|
+ backward(:,conds==j,:)=backwardT;
|
|
|
+ forward(:,conds==j,:)=forwardT;
|
|
|
+end
|
|
|
+temp=forward.*backward;
|
|
|
+likelihood=sum(log(sum(temp(:,:,1))));
|
|
|
+eventprobs=temp./repmat(sum(temp),[maxDur,1,1]);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|