
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">
<html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <!--
This HTML is auto-generated from an M-file.
To make changes, update the M-file and republish this document.
      -->
      <title>fit_ANCHOR_ARL2_part2.m</title>
      <meta name="generator" content="MATLAB 7.4">
      <meta name="date" content="2007-08-18">
      <meta name="m-file" content="fit_ANCHOR_ARL2_part2"><style>

body {
  background-color: white;
  margin:10px;
}

h1 {
  color: #990000; 
  font-size: x-large;
}

h2 {
  color: #990000;
  font-size: medium;
}

/* Make the text shrink to fit narrow windows, but not stretch too far in 
wide windows. */ 
p,h1,h2,div.content div {
  max-width: 600px;
  /* Hack for IE6 */
  width: auto !important; width: 600px;
}

pre.codeinput {
  background: #EEEEEE;
  padding: 10px;
}
@media print {
  pre.codeinput {word-wrap:break-word; width:100%;}
} 

span.keyword {color: #0000FF}
span.comment {color: #228B22}
span.string {color: #A020F0}
span.untermstring {color: #B20000}
span.syscmd {color: #B28C00}

pre.codeoutput {
  color: #666666;
  padding: 10px;
}

pre.error {
  color: red;
}

p.footer {
  text-align: right;
  font-size: xx-small;
  font-weight: lighter;
  font-style: italic;
  color: gray;
}

  </style></head>
   <body>
      <div class="content">
         <h1>fit_ANCHOR_ARL2_part2.m</h1>
         <introduction>
            <p>Continued from fit_ANCHOR_ARL2_part1.m</p>
            <p>Fit the ANCHOR2 model to the group ARL2 profiles of the Fdbk1 Experiment.</p>
            <p>The main tool is .../work/models/anchor/fit_SSE/anchor_ARL2.m</p><pre>File: work/CMUExper/fdbk1/data/fit_ANCHOR_ARL2_part2.m
Date: 2007-08-18
Alexander Petrov, http://alexpetrov.com</pre></introduction>
         <h2>Contents</h2>
         <div>
            <ul>
               <li><a href="#1">Load the data</a></li>
               <li><a href="#2">Collapse across groups</a></li>
               <li><a href="#3">Fit INST on the same stimulus sequences, 11 per group</a></li>
               <li><a href="#4">Search parameters</a></li>
               <li><a href="#5">"Chance event" sets</a></li>
               <li><a href="#6">Cutoff parameter grid</a></li>
               <li><a href="#7">Main work</a></li>
               <li><a href="#8">Plot the SSE</a></li>
               <li><a href="#9">Plot the memory noise parameter</a></li>
               <li><a href="#10">Plot the temperature parameter</a></li>
               <li><a href="#11">Clean up</a></li>
            </ul>
         </div>
         <h2>Load the data<a name="1"></a></h2>
         <p>Start from scratch, generate new chev's, etc. The only thing used from fit_ANCHOR_ARL2_part1.m are the new c_ratio=.75 parameter
            setting and the new starting point for the searches.
         </p><pre class="codeinput">clear <span class="string">all</span>
cd(fullfile(work_pathstr,<span class="string">'CMUExper'</span>,<span class="string">'fdbk1'</span>,<span class="string">'data'</span>)) ;
load(<span class="string">'S2.mat'</span>) ;
ARL2 = [S2(:).ARL2] ;    <span class="comment">% 9x55</span>
gr=[S2(:).group]' ;
ugr = unique(gr) ;
N_groups = length(ugr) ;
ugr_descr = {<span class="string">'U1'</span> <span class="string">'L1'</span> <span class="string">'H1'</span>, <span class="string">'L2'</span> <span class="string">'H2'</span> } ;   <span class="comment">% [1 2 3, 5 6]</span>
</pre><h2>Collapse across groups<a name="2"></a></h2><pre class="codeinput">grARL2 = zeros(size(ARL2,1),N_groups) ;   <span class="comment">% 9x5</span>
gr_idx = cell(1,N_groups) ;
<span class="keyword">for</span> k = 1:N_groups
    gr_idx{k} = find(gr==ugr(k))' ;
    grARL2(:,k) = mean(ARL2(:,gr_idx{k}),2) ;
<span class="keyword">end</span>
</pre><h2>Fit INST on the same stimulus sequences, 11 per group<a name="3"></a></h2><pre class="codeinput">stim = [S2(:).dist] ;   <span class="comment">% 476x55</span>
fdbk = [S2(:).fdbk] ;
</pre><h2>Search parameters<a name="4"></a></h2>
         <p>Same search settings as in fit_ANCHOR_ARL2_part1.m Search strategy:   1. Fix c_ratio = .75 once and for all   1. Explore cutoff
            on a grid   2. Explore history on a grid   3. Search for mem_k and temper using FMINCON as before but start from         mem_k
            = .085, temper = .030    % perc_k = .040 as always
         </p><pre class="codeinput">fminconp = 1 ;
flags = [0 1 1 0 0 0 0 0] ;   <span class="comment">% [perc_k mem_k temper hist cutoff xx xx c_ratio]</span>
Sparams = anchor_search_params2(flags,fminconp) ;
optns = optimset(Sparams.optns,<span class="string">'TolX'</span>,1e-2,<span class="string">'Display'</span>,<span class="string">'off'</span>) ;
Sparams.optns = optns ;
Sparams.target_vals = grARL2
c_ratio = .75 ;
Sparams.params.cutoffs = .65 .* [-3 -1 c_ratio 3*c_ratio] ;
Sparams.params.mem_k = .085 ;
Sparams.params.temper = .030 ;
Sparams.params.history = .040 ;
Sparams.params
</pre><pre class="codeoutput">Sparams = 
     model_name: 'anchor_ARL2'
         params: [1x1 struct]
      p2v_templ: {'VAL = PARAMS.mem_k * 10 ;'  'VAL = PARAMS.temper * 10 ;'}
      v2p_templ: {'PARAMS.mem_k = VAL / 10 ;'  'PARAMS.temper = VAL / 10 ;'}
         bounds: [2x2 double]
     ARL_params: [1x1 struct]
    funfun_name: 'fmincon'
          optns: [1x1 struct]
     lsfun_name: 'sumsqerr'
    target_vals: [9x5 double]
ans = 
       scale: 'LINEAR'
       N_cat: 7
     SM_conv: 1.0000e-03
      cat_sz: 0.0500
      perc_k: 0.0400
       mem_k: 0.0850
       avail: [7x1 double]
     anchors: [7x6 double]
     cutoffs: [-1.9500 -0.6500 0.4875 1.4625]
      temper: 0.0300
     history: 0.0400
       alpha: 0.3000
       decay: 0.5000
         ITI: 4
    M_raster: 7
    A_raster: [5 5 3 3]
    mnfieldp: 1
</pre><h2>"Chance event" sets<a name="5"></a></h2><pre class="codeinput">N_chevs = 2 ;

<span class="comment">% Pack into "model data" structures suitable for passing as arguments.</span>
Mdata = cell(1,N_chevs) ;
<span class="keyword">for</span> k = 1:N_chevs
    Mdata{k}.stim = stim ;
    Mdata{k}.fdbk = fdbk ;
    Mdata{k}.group_idx = gr_idx ;
    <span class="comment">%Mdata{k}.params = Sparams.params ;    % will be set by SUMSQERR</span>
    Mdata{k}.chev = make_anchor_chev(size(stim,1),size(stim,2),7) ;
    Mdata{k}.ARL_params = Sparams.ARL_params ;
<span class="keyword">end</span>
</pre><h2>Cutoff parameter grid<a name="6"></a></h2><pre class="codeinput">cutoff_levels = [.55 .60 .65 .70]' ;        <span class="comment">% c- in categ_sz units</span>
N_cutoff_levels = length(cutoff_levels) ;
history_levels = [.020 .030 .040 .050 .060] ;
N_history_levels = length(history_levels) ;
</pre><h2>Main work<a name="7"></a></h2><pre class="codeinput">fname = <span class="string">'grid_search_ANCHOR_ARL2_part2.mat'</span> ;
<span class="keyword">if</span> (file_exists(fname))
    load(fname) ;
<span class="keyword">else</span>
  grid_SSE = zeros([N_cutoff_levels,N_history_levels,N_chevs]) ;
  grid_optX = cell([N_cutoff_levels,N_history_levels,N_chevs]) ;
  grid_det = cell([N_cutoff_levels,N_history_levels,N_chevs]) ;
  tic
  <span class="keyword">for</span> k1 = 1:N_cutoff_levels
      c = cutoff_levels(k1) ;
      cutoffs = c .* [-3 -1 c_ratio 3*c_ratio] ;
      Sparams.params.cutoffs = cutoffs ;
      fprintf(<span class="string">'\n cutoff %4.2f: '</span>,c) ;
      <span class="keyword">for</span> k2 = 1:N_history_levels
          H = history_levels(k2) ;
          Sparams.params.history = H ;
          <span class="keyword">for</span> k = 1:N_chevs
              [par,SSE,optX,det] = paramsearch(Mdata{k},Sparams) ;  <span class="comment">% &lt;-- Sic!</span>
              grid_SSE(k1,k2,k) = SSE ;
              grid_optX{k1,k2,k} = optX ;
              grid_det{k1,k2,k} = det ;
              fprintf(<span class="string">'%5.3f '</span>,SSE) ;
              save(fname,<span class="string">'grid_SSE'</span>,<span class="string">'grid_optX'</span>,<span class="string">'grid_det'</span>,<span class="string">'Sparams'</span>,<span class="keyword">...</span>
                   <span class="string">'cutoff_levels'</span>,<span class="string">'history_levels'</span>) ;
          <span class="keyword">end</span>
          fprintf(<span class="string">', '</span>) ;
      <span class="keyword">end</span>
  <span class="keyword">end</span>
  fprintf(<span class="string">'\n'</span>) ;
  toc
<span class="keyword">end</span> <span class="comment">% if file_exists ...</span>
</pre><pre class="codeoutput">
 cutoff 0.55: 1.819 1.798 , 1.110 1.022 , 0.999 0.995 , 1.087 0.970 , 1.066 0.890 , 
 cutoff 0.60: 1.621 1.732 , 1.016 0.998 , 1.088 1.046 , 1.066 0.969 , 1.056 0.954 , 
 cutoff 0.65: 1.603 1.698 , 1.018 1.084 , 1.108 1.028 , 1.190 0.986 , 1.204 0.950 , 
 cutoff 0.70: 1.384 1.768 , 1.132 1.012 , 1.099 0.984 , 1.066 1.050 , 1.149 0.990 , 
Elapsed time is 13840.380774 seconds.
</pre><h2>Plot the SSE<a name="8"></a></h2><pre class="codeinput">SSE = mean(grid_SSE,3)   <span class="comment">% average across the chev's</span>
[X,Y] = meshgrid(c_ratio_levels,cutoff_levels) ;
contourf(X,Y,SSE) ; colorbar ; title(<span class="string">'SSE'</span>) ;
xlabel(<span class="string">'c-/c+ ratio'</span>) ; ylabel(<span class="string">'c- cutoff'</span>) ;
</pre><pre class="codeoutput">Error using ==&gt; evalin
Undefined function or variable 'c_ratio_levels'.</pre><h2>Plot the memory noise parameter<a name="9"></a></h2><pre class="codeinput">mem_k = zeros(size(grid_optX)) ;
temper = zeros(size(grid_optX)) ;
<span class="keyword">for</span> k1 = 1:N_cutoff_levels
    <span class="keyword">for</span> k2 = 1:N_c_ratio_levels
        <span class="keyword">for</span> k = 1:N_chevs
            mem_k(k1,k2,k) = grid_optX{k1,k2,k}(1) / 10 ;
            temper(k1,k2,k) = grid_optX{k1,k2,k}(2) / 10 ;
        <span class="keyword">end</span>
    <span class="keyword">end</span>
<span class="keyword">end</span>

mem_k = mean(mem_k,3)
contourf(X,Y,mem_k) ; colorbar ; title(<span class="string">'Memory noise'</span>) ;
xlabel(<span class="string">'c-/c+ ratio'</span>) ; ylabel(<span class="string">'c- cutoff'</span>) ;
</pre><h2>Plot the temperature parameter<a name="10"></a></h2><pre class="codeinput">temper = mean(temper,3)
contourf(X,Y,temper) ; colorbar ; title(<span class="string">'Softmax temperature'</span>) ;
xlabel(<span class="string">'c-/c+ ratio'</span>) ; ylabel(<span class="string">'c- cutoff'</span>) ;
</pre><h2>Clean up<a name="11"></a></h2><pre class="codeinput">clear <span class="string">k</span> <span class="string">k1</span> <span class="string">k2</span> <span class="string">X</span> <span class="string">Y</span> ;
</pre><p class="footer"><br>
            Published with MATLAB&reg; 7.4<br></p>
      </div>
      <!--
##### SOURCE BEGIN #####
%% fit_ANCHOR_ARL2_part2.m
%
% Continued from fit_ANCHOR_ARL2_part1.m
%
% Fit the ANCHOR2 model to the group ARL2 profiles of the Fdbk1 Experiment.
%
% The main tool is .../work/models/anchor/fit_SSE/anchor_ARL2.m
%
%  File: work/CMUExper/fdbk1/data/fit_ANCHOR_ARL2_part2.m
%  Date: 2007-08-18
%  Alexander Petrov, http://alexpetrov.com


%% Load the data
% Start from scratch, generate new chev's, etc.
% The only thing used from fit_ANCHOR_ARL2_part1.m are the new c_ratio=.75
% parameter setting and the new starting point for the searches.
clear all
cd(fullfile(work_pathstr,'CMUExper','fdbk1','data')) ;
load('S2.mat') ;
ARL2 = [S2(:).ARL2] ;    % 9x55
gr=[S2(:).group]' ;
ugr = unique(gr) ;
N_groups = length(ugr) ;
ugr_descr = {'U1' 'L1' 'H1', 'L2' 'H2' } ;   % [1 2 3, 5 6]

%% Collapse across groups
grARL2 = zeros(size(ARL2,1),N_groups) ;   % 9x5
gr_idx = cell(1,N_groups) ;
for k = 1:N_groups
    gr_idx{k} = find(gr==ugr(k))' ;
    grARL2(:,k) = mean(ARL2(:,gr_idx{k}),2) ;
end

%% Fit INST on the same stimulus sequences, 11 per group
stim = [S2(:).dist] ;   % 476x55
fdbk = [S2(:).fdbk] ;

%% Search parameters
% Same search settings as in fit_ANCHOR_ARL2_part1.m
% Search strategy:
%   1. Fix c_ratio = .75 once and for all
%   1. Explore cutoff on a grid
%   2. Explore history on a grid
%   3. Search for mem_k and temper using FMINCON as before but start from
%         mem_k = .085, temper = .030    % perc_k = .040 as always
fminconp = 1 ;
flags = [0 1 1 0 0 0 0 0] ;   % [perc_k mem_k temper hist cutoff xx xx c_ratio]
Sparams = anchor_search_params2(flags,fminconp) ;
optns = optimset(Sparams.optns,'TolX',1e-2,'Display','off') ;
Sparams.optns = optns ;
Sparams.target_vals = grARL2
c_ratio = .75 ;
Sparams.params.cutoffs = .65 .* [-3 -1 c_ratio 3*c_ratio] ;
Sparams.params.mem_k = .085 ;
Sparams.params.temper = .030 ;
Sparams.params.history = .040 ;
Sparams.params

%% "Chance event" sets
N_chevs = 2 ;

% Pack into "model data" structures suitable for passing as arguments.
Mdata = cell(1,N_chevs) ;
for k = 1:N_chevs
    Mdata{k}.stim = stim ;
    Mdata{k}.fdbk = fdbk ;
    Mdata{k}.group_idx = gr_idx ;
    %Mdata{k}.params = Sparams.params ;    % will be set by SUMSQERR
    Mdata{k}.chev = make_anchor_chev(size(stim,1),size(stim,2),7) ;
    Mdata{k}.ARL_params = Sparams.ARL_params ;
end

%% Cutoff parameter grid
cutoff_levels = [.55 .60 .65 .70]' ;        % c- in categ_sz units
N_cutoff_levels = length(cutoff_levels) ;
history_levels = [.020 .030 .040 .050 .060] ;
N_history_levels = length(history_levels) ;

%% Main work
fname = 'grid_search_ANCHOR_ARL2_part2.mat' ;
if (file_exists(fname))
    load(fname) ;
else
  grid_SSE = zeros([N_cutoff_levels,N_history_levels,N_chevs]) ;
  grid_optX = cell([N_cutoff_levels,N_history_levels,N_chevs]) ;
  grid_det = cell([N_cutoff_levels,N_history_levels,N_chevs]) ;
  tic
  for k1 = 1:N_cutoff_levels
      c = cutoff_levels(k1) ;
      cutoffs = c .* [-3 -1 c_ratio 3*c_ratio] ;
      Sparams.params.cutoffs = cutoffs ;
      fprintf('\n cutoff %4.2f: ',c) ;
      for k2 = 1:N_history_levels
          H = history_levels(k2) ;
          Sparams.params.history = H ;
          for k = 1:N_chevs
              [par,SSE,optX,det] = paramsearch(Mdata{k},Sparams) ;  % <REPLACE_WITH_DASH_DASH Sic!
              grid_SSE(k1,k2,k) = SSE ;
              grid_optX{k1,k2,k} = optX ;
              grid_det{k1,k2,k} = det ;
              fprintf('%5.3f ',SSE) ;
              save(fname,'grid_SSE','grid_optX','grid_det','Sparams',...
                   'cutoff_levels','history_levels') ;
          end
          fprintf(', ') ;
      end
  end
  fprintf('\n') ;
  toc
end % if file_exists ...

%% Plot the SSE
SSE = mean(grid_SSE,3)   % average across the chev's
[X,Y] = meshgrid(c_ratio_levels,cutoff_levels) ; 
contourf(X,Y,SSE) ; colorbar ; title('SSE') ;
xlabel('c-/c+ ratio') ; ylabel('c- cutoff') ;

%% Plot the memory noise parameter
mem_k = zeros(size(grid_optX)) ;
temper = zeros(size(grid_optX)) ;
for k1 = 1:N_cutoff_levels
    for k2 = 1:N_c_ratio_levels
        for k = 1:N_chevs
            mem_k(k1,k2,k) = grid_optX{k1,k2,k}(1) / 10 ;
            temper(k1,k2,k) = grid_optX{k1,k2,k}(2) / 10 ;
        end
    end
end

mem_k = mean(mem_k,3)
contourf(X,Y,mem_k) ; colorbar ; title('Memory noise') ;
xlabel('c-/c+ ratio') ; ylabel('c- cutoff') ;

%% Plot the temperature parameter
temper = mean(temper,3)
contourf(X,Y,temper) ; colorbar ; title('Softmax temperature') ;
xlabel('c-/c+ ratio') ; ylabel('c- cutoff') ;


%% Clean up
clear k k1 k2 X Y ;

##### SOURCE END #####
-->
   </body>
</html>