%%%%%%%%%%  July 16, 2003
 
 Mparams=PLM_params
      rep_size: [7 5 1]
       max_act: 0.5000
      rep_gain: 0.8000
     rep_noise: 0.1000
     out_noise: 0.2000
      W_minmax: [-1 1]
        W_init: 0.1700
       W0_seed: [35x1 double]
    learn_rate: 0.0020
    runav_rate: 0.9800
     criterion: 0.0450
       trigger: 0
      blk_size: 300

 reps = reps_from_cache(Mparams);size(reps)
 mnrep=squeeze(mean(reps,1))';  % [12x35]

%% Scatterplot under the initial weights
%
 W0=Mparams.W_init .* Mparams.W0_seed;plot(W0,'.-')
 W0L=-min(0,W0(:));W0R=+max(0,W0(:));
 aL=zeros(5000,12);aR=zeros(5000,12);for k=1:12 aL(:,k)=reps(:,:,k)*W0L;aR(:,k)=reps(:,:,k)*W0R;end

 i1=[1 3 5];idx=[1:200];
 plot(aR(idx,i1),aL(idx,i1),'r.',aR(idx,i1+1),aL(idx,i1+1),'b.',aR(idx,i1+6),aL(idx,i1+6),'k.',aR(idx,i1+7),aL(idx,i1+7),'g.');axis([0 .7 0 .7]);axis square;refline(1,0);xlabel('Evidence for R');ylabel('Evidence for L');



%% Scatterplot under Hebbian weights in Context R
%
 cR=mean(mnrep([8 10 12],:))-mean(mnrep([7 9 11],:));figure(2);plot(reshape(cR,7,5),'.-')
 cR_L=-min(0,cR');cR_R=+max(0,cR');
 uRL=zeros(5000,12);uRR=zeros(5000,12);for k=1:12 uRL(:,k)=reps(:,:,k)*cR_L;uRR(:,k)=reps(:,:,k)*cR_R;end
 figure(2);subplot(1,2,2);plot(uRL(idx,i1),uRR(idx,i1),'r.',uRL(idx,i1+1),uRR(idx,i1+1),'b.',uRL(idx,i1+6),uRR(idx,i1+6),'k.',uRL(idx,i1+7),uRR(idx,i1+7),'g.');axis([0 .5 0 .5]);axis square;refline(1,0);title('Context R');


%% Scatterplot under Hebbian weights in Context L
%
 cL=mean(mnrep([2 4 6],:))-mean(mnrep([1 3 5],:));
 cL_L=-min(0,cL');cL_R=+max(0,cL');
 uLL=zeros(5000,12);uLR=zeros(5000,12);for k=1:12 uLL(:,k)=reps(:,:,k)*cL_L;uLR(:,k)=reps(:,:,k)*cL_R;end
 figure(2);subplot(1,2,1);plot(uLL(idx,i1),uLR(idx,i1),'r.',uLL(idx,i1+1),uLR(idx,i1+1),'b.',uLL(idx,i1+6),uLR(idx,i1+6),'k.',uLL(idx,i1+7),uLR(idx,i1+7),'g.');axis([0 .5 0 .5]);axis square;refline(1,0);title('Context L');


%%%%%%%%%%%%  Ellipses instead of scatterplots
%
 for k=1:12 [descr0(k),ex0(:,k),ey0(:,k)]=elliptic_CI(aR(:,k),aL(:,k));end
 for k=1:12 [descrL(k),exL(:,k),eyL(:,k)]=elliptic_CI(uLR(:,k),uLL(:,k));end
 for k=1:12 [descrR(k),exR(:,k),eyR(:,k)]=elliptic_CI(uRR(:,k),uRL(:,k));end

 subplot(1,2,1);plot(ex0(:,i1),ey0(:,i1),'r-',ex0(:,i1+1),ey0(:,i1+1),'b--',ex0(:,i1+6),ey0(:,i1+6),'r-',ex0(:,i1+7),ey0(:,i1+7),'b--');axis([0 .6 0 .6]);axis square;refline(1,0);title('Initial weights');xlabel('Evidence for R');ylabel('Evidence for L');
% subplot(1,2,1);plot(exL(:,i1),eyL(:,i1),'r-',exL(:,i1+1),eyL(:,i1+1),'b-',exL(:,i1+6),eyL(:,i1+6),'r--',exL(:,i1+7),eyL(:,i1+7),'b--');axis([0 .6 0 .6]);axis square;refline(1,0);title('Context L');xlabel('Evidence for R');ylabel('Evidence for L');
 subplot(1,2,2);plot(exR(:,i1),eyR(:,i1),'r--',exR(:,i1+1),eyR(:,i1+1),'b--',exR(:,i1+6),eyR(:,i1+6),'r-',exR(:,i1+7),eyR(:,i1+7),'b-');axis([0 .6 0 .6]);axis square;refline(1,0);title('Context R');xlabel('Evidence for R');ylabel('Evidence for L');



%%%%%%%%%%%%  Collapse to one dimension:  z = R-L
%
 z0=aR-aL;zL=uLR-uLL;zR=uRR-uRL;  

 describe(z0);
    Mean  Std.dev       Min     Q25  Median     Q75     Max
------------------------------------------------------------
  -0.412    0.046     -0.58   -0.44   -0.41   -0.38   -0.26
  -0.118    0.056     -0.33   -0.15   -0.12   -0.08    0.05
  -0.429    0.048     -0.59   -0.46   -0.43   -0.40   -0.26
  -0.245    0.056     -0.42   -0.28   -0.25   -0.21   -0.05
  -0.438    0.048     -0.59   -0.47   -0.44   -0.41   -0.27
  -0.334    0.054     -0.51   -0.37   -0.34   -0.30   -0.11
   0.117    0.055     -0.05    0.08    0.11    0.15    0.31
   0.411    0.047      0.25    0.38    0.41    0.44    0.60
   0.244    0.057      0.04    0.21    0.24    0.28    0.43
   0.430    0.048      0.25    0.40    0.43    0.46    0.60
   0.336    0.055      0.13    0.30    0.34    0.38    0.49
   0.439    0.048      0.26    0.41    0.44    0.47    0.59
------------------------------------------------------------
   0.000    0.052     -0.18   -0.04    0.00    0.04    0.17

 describe([z0(:) zL(:) zR(:)])

    Mean  Std.dev       Min     Q25  Median     Q75     Max
------------------------------------------------------------
   0.000    0.353     -0.59   -0.36    0.00    0.37    0.60
   0.150    0.191     -0.19   -0.05    0.17    0.33    0.46
  -0.150    0.192     -0.46   -0.33   -0.18    0.05    0.20
------------------------------------------------------------
   0.000    0.245     -0.41   -0.25    0.00    0.25    0.42


 KDE_params=struct('domain_min',-.65,'domain_max',+.65,'step',.005,'bandwidth',.02,'radius',.07,'fold',14)
KDE_params = 
    domain_min: -0.6500
    domain_max: 0.6500
          step: 0.0050
     bandwidth: 0.0200
        radius: 0.0700
          fold: 14

 z=[KDE_params.domain_min:KDE_params.step:KDE_params.domain_max]';
 K0=zeros(length(z),12);for k=1:12 K=build_KDE(z0(:,k),KDE_params);K0(:,k)=K.density;end;plot(z,K0);grid on;
 KL=zeros(length(z),12);for k=1:12 K=build_KDE(zL(:,k),KDE_params);KL(:,k)=K.density;end;plot(z,KL);grid on;
 KR=zeros(length(z),12);for k=1:12 K=build_KDE(zR(:,k),KDE_params);KR(:,k)=K.density;end;plot(z,KR);grid on;
 k=KDE_params.step;K0=K0/k;KL=KL/k;KR=KR/k;clear K k


 subplot(1,2,1);plot(z,sum(K0(:,i1),2),'r-',z,sum(K0(:,i1+1),2),'b--',z,sum(K0(:,i1+6),2),'r-',z,sum(K0(:,i1+7),2),'b--');axis([-.65 +.65 0 40]);set(gca,'xtick',[-.6:.2:+.6]);title('Initial weights');xlabel('z = R - L') ;ylabel('Probability density');
% subplot(1,2,1);plot(z,sum(KL(:,i1),2),'r-',z,sum(KL(:,i1+1),2),'b-',z,sum(KL(:,i1+6),2),'r--',z,sum(KL(:,i1+7),2),'b--');axis([-.65 +.65 0 40]);set(gca,'xtick',[-.6:.2:+.6]);title('Weights from context L');xlabel('z = R - L') ;ylabel('Probability density');
 subplot(1,2,2);plot(z,sum(KR(:,i1),2),'r--',z,sum(KR(:,i1+1),2),'b--',z,sum(KR(:,i1+6),2),'r-',z,sum(KR(:,i1+7),2),'b-');axis([-.65 +.65 0 40]);set(gca,'xtick',[-.6:.2:+.6]);title('Weights from context R');xlabel('z = R - L') ;ylabel('Probability density');


%%%%%%%%  Make figures that can be stitched at 45 degrees with one another
%
 figure(1);plot(ex0,ey0,'k-');axis([0 .6 -.4 .6]);for k=[-.4 -.2 0 .2 .4];h=refline(1,k);set(h,'LineStyle',':','LineWidth',.5,'Color',[.5 .5 .5]);end;axis([0 .6 0 .6]);axis square;xlabel('Evidence for R');ylabel('Evidence for L');
 set(1,'Position',[300 10 525 500]);axis square     % fig 1: W=525, H=500 pix
 get(gca,'Position')
    0.1300    0.1100    0.7750    0.8150             % axes1: W=0.775, H=.815 relative to figure W and H ; There is a little slack in the H dimension due to the 'axis square' command.

% The width of Figure 2 is approximately sqrt(2) greater than that of Fig 1
% The ratio is not exactly 1.41:1 because trial-and-error rotations in Photoshop indicated that the value below works better
 figure(2);plot(z,sum(K0(:,i1),2),'r-',z,sum(K0(:,i1+1),2),'k-',z,sum(K0(:,i1+6),2),'r-',z,sum(K0(:,i1+7),2),'k-');axis([-.65 +.65 0 40]);set(gca,'xtick',[-.6:.2:+.6],'XGrid','on','ytick',[],'box','off');
 set(2,'Position',[200 410, 800 150]);              % fig 2: W=800 pix, H=150 in order to squash the humps
 get(gca,'Position')
    0.1300    0.1100    0.7750    0.8150             % axes2: same as axes1. Therefore, the ratio is determined by the Figure sizes.

% Set LineWidth to 2.5 for all lines in Context R. Make Color='r' for the left targets in Fig 1 (thus matching the colors in Fig 2). In grayscale mode, red translates to dark gray.
% Copy Options: Windows metafile, Match figure screen size, White background
% Paste into a new Photoshop document (RGB, resolution 150 pix/inch). Rotate the layer with Fig 2 by 45 degrees (Edit>Transform>Numeric menu in Photoshop).
% Transform to grayscale mode, enhance contrast by 20%, and save as EPS files, resolution 72 pix/inch.

 figure(1);plot(exR,eyR,'k-');axis([0 .6 -.4 .6]);for k=[-.4 -.2 0 .2 .4];h=refline(1,k);set(h,'LineStyle',':','LineWidth',.5,'Color',[.5 .5 .5]);end;axis([0 .6 0 .6]);axis square;xlabel('Evidence for R');ylabel('Evidence for L');legend('Target L, context L','Target R, context L','Target L, context R','Target R, context R',4);
 figure(2);plot(z,sum(KR(:,i1),2),'r-',z,sum(KR(:,i1+1),2),'k-',z,sum(KR(:,i1+6),2),'r-',z,sum(KR(:,i1+7),2),'k-');axis([-.65 +.65 0 40]);set(gca,'xtick',[-.6:.2:+.6],'XGrid','on','ytick',[],'box','off');


