% main program for Ch 2 nr = 10; type = 2; noise = 1; cutoff = .2; mod = 1; [XAC XBC XAR XBR P] = stimuli(type,noise); Max = round(max(max(XAC))); % sig = 1; sig = 5; aa = 25; bb = 15; AV = 1*((1:aa)/aa); BV = (1:bb); MP = []; MT=[]; MP = zeros(aa,bb); for i = 1:aa for j = 1:bb alf = AV(i) b = BV(j) if mod == 1 [mP mT] = proto(alf,b,sig,XAC,XBC,XAR,XBR,P,nr,type,cutoff); else [mP mT] = exemplar(alf,b,sig,XAC,XBC,XAR,XBR,P,nr,type,cutoff); end MP(i,j) = mP; MT(i,j) = mT; end end if 1==2 mesh(BV,AV,MP) xlabel('Sensitivity') ylabel('Learning Rate') zlabel('Proportion Correct') if mod == 1 & type == 2 title('Prototype, XOR Generalization Test') elseif mod ==2 & type ==2 title('Exemplar, XOR Generalization Test') elseif mod == 1 & type ==1 title('Prototype, Linear') else title('Exemplar, Linear') end else spy(MT) end mP = max(max(MP)); mT = max(max(MT)); [mP mT] %%%%%%%%%%%%%%%%%%%%%%%% functions %%%%%%%%%%%%%%%% function [In , Out] = InOut(I1,I2,X,W,sig,st) % function [In,Out] = InOut(I1,I2,XAC,W,sig,st) in1 = 1./exp(((I1-X(st,1))/sig).^2); in2 = 1./exp(((I2-X(st,2))/sig).^2); In = kron(in1,in2); In = (In./sum(In))'; Out = W*In; function [XAC,XBC,XAR,XBR,P] = stimuli(type,noise) % function [XAC,XBC,XAR,XBR,P] = stimuli(type,noise) % stimuli randn('state', 97882315) Max = 10; n = 2*10; P = [ 1 1 ; ... 1+1 Max ; ... Max-1 1 ; ... Max Max-1 ] ; X = kron(P, ones(n,1))+ noise*randn(n*4,2); if type==2 % crossover XB = X( [ (1:n) ; ((3*n)+1):(4*n)],: ); XA = X( [ ((n+1):(2*n)) ; ((2*n)+1):(3*n)],: ); XAC = XA( [ 1:(n/2) ; (n+1):(3*n/2) ], :); XBC = XB( [ 1:(n/2) ; (n+1):(3*n/2) ], :); XAR = XA( [ ((n/2)+1):n ; ((3*n/2)+1):(2*n) ], :); XBR = XB( [ ((n/2)+1):n ; ((3*n/2)+1):(2*n) ], :); if 1==2 subplot(1,2,1) plot(XAC(:,1),XAC(:,2),'+',XBC(:,1),XBC(:,2),'o'); legend('A','B') title('Categorization') subplot(1,2,2) plot(XAR(:,1),XAR(:,2),'+',XBR(:,1),XBR(:,2),'o',P(:,1),P(:,2),'x'); title('Generalization') r = input('wait') end else % linear XA = X( [ (1:n) ; (n+1):(2*n)],: ); XB = X( [ (((2*n)+1):(3*n)) ; ((3*n)+1):(4*n)],: ); XAC = XA( [ 1:(n/2) ; (n+1):(3*n/2) ], :); XBC = XB( [ 1:(n/2) ; (n+1):(3*n/2) ], :); XAR = XA( [ ((n/2)+1):n ; ((3*n/2)+1):(2*n) ], :); XBR = XB( [ ((n/2)+1):n ; ((3*n/2)+1):(2*n) ], :); if 1==2 plot(XAC(:,1),XAC(:,2),'+',XBC(:,1),XBC(:,2),'o'); legend('A','B') r = input('wait') end end function [mP, mT, W] = proto(alf,b,sig,XAC,XBC,XAR,XBR,P,nr,type,cutoff) % function [mP, mT, W] = proto(alf,b,sg,XAC,XBC,XAR,XBR,P,nr,type,cutoff) % prototype model Max = round(max(max(XAC))); mm = Max; I1 = 1*(0:mm); I2 = 1*(0:mm); Len = size(I1,2); PA =[]; PB = []; TA = []; TB = []; ns = size(XAC,1); W = 0*randn(2,2*Len); for rep = 1:nr for st = 1:ns in1 = 1./exp(((I1-XAC(st,1))/sig).^2); in1 = in1/sum(in1); in2 = 1./exp(((I2-XAC(st,2))/sig).^2); in2 = in2/sum(in2); In = [in1 in2]'; Out = W*In; WA = alf*([1 0]'-Out)*In'; % pa = exp(b*Out); pa = pa(1)/(sum(pa)); % PA = [PA ; pa]; in1 = 1./exp(((I1-XBC(st,1))/sig).^2); in1 = in1/sum(in1); in2 = 1./exp(((I2-XBC(st,2))/sig).^2); in2 = in2/sum(in2); In = [in1 in2]'; Out = W*In; WB = alf*([0 1]'-Out)*In'; % pb = exp(b*Out); pb = pb(2)/(sum(pb)); % PB = [PB ; pb]; W = W + WA + WB; end end for st = 1:ns in1 = 1./exp(((I1-XAR(st,1))/sig).^2); in1 = in1/sum(in1); in2 = 1./exp(((I2-XAR(st,2))/sig).^2); in2 = in2/sum(in2); In = [in1 in2]'; Out = W*In; pa = exp(b*Out); pa = pa(1)/(sum(pa)); PA = [PA ; pa]; in1 = 1./exp(((I1-XBR(st,1))/sig).^2); in1 = in1/sum(in1); in2 = 1./exp(((I2-XBR(st,2))/sig).^2); in2 = in2/sum(in2); In = [in1 in2]'; Out = W*In; pb = exp(b*Out); pb = pb(2)/(sum(pb)); PB = [PB ; pb]; end for st = 1:4 if st == 2 | st == 3 in1 = 1./exp(((I1-P(st,1))/sig).^2); in1 = in1/sum(in1); in2 = 1./exp(((I2-P(st,2))/sig).^2); in2 = in2/sum(in2); In = [in1 in2]'; Out = W*In; pa = exp(b*Out); pa = pa(1)/(sum(pa)); TA = [TA ; pa]; else in1 = 1./exp(((I1-P(st,1))/sig).^2); in1 = in1/sum(in1); in2 = 1./exp(((I2-P(st,2))/sig).^2); in2 = in2/sum(in2); In = [in1 in2]'; Out = W*In; pb = exp(b*Out); pb = pb(1)/(sum(pb)); TB = [TB ; pb]; end end % T = 1:(nr*ns); % plot(T,PA,'o',T,PB,'+') % prob choose A given each type of category % PA = PA((nr*ns)-(1:ns)+1); % PB = PB((nr*ns)-(1:ns)+1); % mP1 = sum(PA > cutoff); % mP2 = sum(PB > cutoff); % mP = (mP1+mP2)/(2*ns); mP1 = mean(PA(1:(ns/2))) + mean(PB(1:(ns/2))); mP2 = mean(PA(((ns/2)+1):ns)) + mean(PB(((ns/2)+1):ns)); mP = (mP1 + mP2)/4; mT = ((TA(1)-TB(1)) > cutoff)*((TA(2) - TB(2)) > cutoff); function [mP, mT, W] = exemplar(alf,b,sig,XAC,XBC,XAR,XBR,P,nr,type,cutoff) % function [mP, mT, W] = exemplar(alf,b,sg,XAC,XBC,XAR,XBR,P,nr,type,cutoff) % exemplar model Max = round(max(max(XAC))); I1 = 0:Max; I2 = 0:Max; PA =[]; PB = []; TA = []; TB = []; ns = size(XAC,1); W = 0*randn(2,(Max+1).^2); for rep = 1:nr for st = 1:ns [In Out] = InOut(I1,I2,XAC,W,sig,st); WA = alf*([1 0]'-Out)*In'; % pa = exp(b*Out); pa = pa(1)/sum(pa); % PA = [PA ; pa]; [In Out] = InOut(I1,I2,XBC,W,sig,st); WB = alf*([0 1]'-Out)*In'; % pb = exp(b*Out); pb = pb(2)/sum(pb); % PB = [PB ; pb]; W = W + WA + WB; end end for st = 1:ns [In Out] = InOut(I1,I2,XAR,W,sig,st); pa = exp(b*Out); pa = pa(1)/sum(pa); PA = [PA ; pa]; [In Out] = InOut(I1,I2,XBR,W,sig,st); pb = exp(b*Out); pb = pb(2)/sum(pb); PB = [PB ; pb]; end for st = 1:4 if st == 2 | st == 3 [In Out] = InOut(I1,I2,P,W,sig,st); pa = exp(b*Out); pa = pa(1)/(sum(pa)); TA = [TA ; pa]; else [In Out] = InOut(I1,I2,P,W,sig,st); pb = exp(b*Out); pb = pb(1)/(sum(pb)); TB = [TB ; pb]; end end % T = 1:(nr*ns); % plot(T,PA,'o',T,PB,'+') % prob choose A given each type of category % PA = PA((nr*ns)-(1:ns)+1); % PB = PB((nr*ns)-(1:ns)+1); % mP1 = sum(PA > cutoff); % mP2 = sum(PB > cutoff); % mP = (mP1+mP2)/(2*ns); mP1 = mean(PA(1:(ns/2))) + mean(PB(1:(ns/2))); mP2 = mean(PA(((ns/2)+1):ns)) + mean(PB(((ns/2)+1):ns)); mP = (mP1 + mP2)/4; mT = ((TA(1)-TB(1)) > cutoff)*((TA(2) - TB(2)) > cutoff);