% Matlab script for fitting MDFT lBounds = [.1, .1, .1, .1, .1, .1, .001, .001, .001, .001, .001, .001, .001, .1, 1]; uBounds = [.9, .9, .9, .9, .9, .9, 100, 100, 100, 100, 100, 100, .5, 1000, 100]; wJitter = .15; % mean of bias jitter sig2Jitter = 15; % mean of noise jitter paramGrid.w = [.4 .5 .6]; % params #1-6 paramGrid.sig2 = [20 45 70]; % params #1-6 paramGrid.phi2 = [.03 .06 .1]; % param #13 paramGrid.phi1 = [100 200 300 400]; % param #14 paramGrid.wgt = [10 20 30]; % param #15 maxIter = 1000; loop = 1; for a = paramGrid.w for b = paramGrid.sig2 for c = paramGrid.phi2 for d = paramGrid.phi1 for e = paramGrid.wgt startParams(loop,:) = [a+((rand(1,6)-.5)*wJitter), b+((rand(1,6)-.5)*sig2Jitter) c d e]; loop = loop + 1; end end end end end subs = 1:48; % subs to fit for s = 1:length(subs) sub = subs(s); fitMDFT(sub,startParams,lBounds,uBounds,maxIter,crossValSet) end ------------------------------------------------------------------------------ lBounds = [.1, .1, .1, .1, .1, .1, .5, .5, .5, .5, .5, .5, .3, .001, .001]; uBounds = [10, 10, 10, 10, 10, 10, 50, 50, 50, 50, 50, 50, 20, .999, .999]; bJitter = .2; % mean of bias jitter mJitter = 2; % mean of noise jitter paramGrid.beta = [.75 1 1.33]; % params #1-6 paramGrid.m = [1 10 20]; % params #1-6 paramGrid.I_0 = [1 8 15]; % param #13 paramGrid.lambda1 = [.2 .4 .6 .8]; % param #14 paramGrid.lambda2 = [.2 .4 .6 .8]; % param #15 maxIter = 2000; loop = 1; for a = paramGrid.beta for b = paramGrid.m for c = paramGrid.I_0 for d = paramGrid.lambda1 for e = paramGrid.lambda2 startParams(loop,:) = [a+((rand(1,6)-.5)*bJitter), b+((rand(1,6)-.5)*mJitter) c d e]; loop = loop + 1; end end end end end subs = 1:48; % subs to fit for s = 1:length(subs) sub = subs(s); fitMLBA(sub,startParams,lBounds,uBounds,maxIter) end ------------------------------------------------------------------------------ function fitMLBA(sub,newStartParams,lBounds,uBounds,newMaxIter,crossValSet) % This function takes stimuli and uses fminsearchbnd to do many bounded % searches from a grid start points. bestErr = []; bestParams = []; maxIter = newMaxIter; startParams = newStartParams; loop = 1; % Read in the .mat Data load('exp2data','-mat') maxTrials = size(data,1); % max (assuming no missing data) # trials/sub % Pull out individual data to pass into fitting functions prodCol = 11; % column in data that codes product choiceCols = 12:14; % columns coding chosen alternative subData = data(:,:,sub); % pull out subject's attribute data products = subData(:,prodCol); choices = subData(:,choiceCols)*[1:3]'; % find column w/ a 1 % Create an nAlt X nAttr X nTrials matrix of attribute values; % each column is a dimension, each 2D plane in a trial for trial = 1:maxTrials M(:,:,trial) = [subData(trial,1:2); subData(trial,3:4); subData(trial,5:6)]*10; % multiply by 10 to match stimuli from Trueblood, et al. (2014) if isnan(subData(trial,1)) || any(subData(trial,1:6)<0) missing(trial) = 1; % 1 for missing data else missing(trial) = 0; % 0 for good data end end while loop <= size(startParams,1) disp(['Subject: ' num2str(sub) ', Loop: ' num2str(loop)]) options = optimset('Display','off','MaxIter', maxIter); try [bestParams(end+1,:), bestErr(end+1)] = fminsearchbnd(@(x)... compErrMLBA(x, M, choices, products, missing),... startParams(loop,:), lBounds, uBounds, options); if bestErr(end) > 1000 bestParams = bestParams(1:end-1,:); bestErr = bestErr(1:end-1); end save([num2str(crossValSet) '_MLBA_' num2str(sub) '.mat'], 'bestErr', 'bestParams', 'startParams', 'maxIter', 'loop','lBounds','uBounds','crossValSet'); loop = loop+1; catch loop = loop+1; continue end end save(['final_' num2str(crossValSet) '_MLBA_' num2str(sub) '.mat'], 'bestErr', 'bestParams', 'startParams', 'maxIter', 'loop','lBounds','uBounds','crossValSet'); ------------------------------------------------------------------------------ function [LL_MLBA_sum, allProbs] = compErrMLBA(params,M,choices,products,missing) % This function takes in parameters and loops through products to compute % predictions for MLBA across the whole exp. Returns the negative % log-likelihood of the data, given the model parameters allTrials = length(missing); % total # of trials, including missing % Products-independent params I_0 = params(13); lambda1 = params(14); lambda2 = params(15); pMLBA_chosenAlt = []; for prod = 1:6 % Pull out product data prodTrials = (products==prod); goodTrials = find(~missing' & prodTrials); nTrials = length(goodTrials); % # of prod trials if nTrials == 0 continue end prodStim = M(:,:,goodTrials); prodChoices = choices(goodTrials); % prod choices % Get prod-specific params and put all into a vector beta = params(prod); m = params(6+prod); preds = []; for trial = 1:nTrials preds(trial,:) = getProb(prodStim(:,:,trial), I_0, m, lambda1, lambda2, beta); end if sum(sum(isnan(preds))) disp('MLBA drift rate error') end pMLBA_allAlt = adjustProb(preds); % adjust probabilities away from 1 and 0 ind = sub2ind(size(pMLBA_allAlt),1:nTrials,prodChoices'); pMLBA_chosenAlt = [pMLBA_chosenAlt pMLBA_allAlt(ind)]; allProbs(goodTrials,:) = pMLBA_allAlt; end % calculate the summed Log-Likelihood (LL) % multiply by -1 to minimize positive values LL_MLBA_sum = -sum(log(pMLBA_chosenAlt)); if size(allProbs,1) < allTrials allProbs((end+1):allTrials,:) = zeros(allTrials-size(allProbs,1),3); end end function prob = getProb(stimuli, I_0, m, lambda1, lambda2, beta) % Returns the choice probabilities from MLBA % Set other parameters for LBA t = Inf; x0max = 1; % start point varibility (i.e., A) chi = 2; % threshold (i.e., b) sdI = 1; % drift rate variance st0 = 0; % non-decision time truncdrifts = true; % sets whether that part of the multi-variate normal distribution on drift rates which would otherwise lead to non-terminating trials is truncated % Get mean drift rates d = drifts(stimuli, I_0, m, lambda1, lambda2, beta); if max(d) < 0 prob(1:3) = nan; % error code else % Get probabilities prob(1) = n1CDF(t,x0max,chi,d,sdI,st0,truncdrifts); prob(2) = n1CDF(t,x0max,chi,d([2,1,3]),sdI,st0,truncdrifts); prob(3) = n1CDF(t,x0max,chi,d([3,1,2]),sdI,st0,truncdrifts); if sum(prob<0) prob(1:3) = nan; % error code end end end ------------------------------------------------------------------------------ function [d] = drifts(stimuli, I_0, m, lambda1, lambda2, beta) % Calculates the mean drift rates for MLBA. Stimuli is a 3x2 matrix u1 = CurveFun(stimuli(1,1:2), m); u2 = CurveFun(stimuli(2,1:2), m); u3 = CurveFun(stimuli(3,1:2), m); d(1) = Valuation(u1, u2, lambda1, lambda2, beta) + Valuation(u1, u3, lambda1, lambda2, beta) + I_0; d(2) = Valuation(u2, u1, lambda1, lambda2, beta) + Valuation(u2, u3, lambda1, lambda2, beta) + I_0; d(3) = Valuation(u3, u1, lambda1, lambda2, beta) + Valuation(u3, u2, lambda1, lambda2, beta) + I_0; end function [u] = CurveFun(option, m) % Mapping objective to subjective values (Appendix C) % x and y intercepts of the line of indifference a = option(1) + option(2); b = a; % angle between x-axis and the vector angle = atan(option(2)/option(1)); % subjective values for the option on the curve (x/a)^m + (y/b)^m u(1) = b/((tan(angle))^m+(b/a)^m)^(1/m); u(2) = b*(1-(u(1)/a)^m)^(1/m); end function [wt] = Weight(A, B, lambda1, lambda2, beta, attribNum) % Attention weights (equation 5) if attribNum == 1 if(A >= B) wt =exp(-lambda1*abs(A-B)); else wt =exp(-lambda2*abs(A-B)); end elseif attribNum == 2 if(A >= B) wt =exp(-lambda1*abs(A-B)); else wt =exp(-lambda2*beta*abs(A-B)); end end end function [v] = Valuation(option1, option2, lambda1, lambda2, beta) % Valuation function (equation 3) v = Weight(option1(1), option2(1), lambda1, lambda2, beta, 1) * (option1(1) - option2(1)) + ... Weight(option1(2), option2(2), lambda1, lambda2, beta, 2) * (option1(2) - option2(2)); end ------------------------------------------------------------------------------ function result = n1CDF(t,x0max,chi,drift,sdI,st0,truncdrifts) % n1CDF - port of Scott Brown's R code % Generates defective CDF for responses on node #1. if nargin<6 || isempty(st0) st0 = 0; end if nargin<7 || isempty(truncdrifts) truncdrifts = true; end N=length(drift); % Number of responses if length(x0max)=bounds(i+1)) outs(i)=0; break; end tmp=quadgk(@(t) n1PDF(t,x0max,chi,drift,sdI,st0,truncdrifts),bounds(i),bounds(i+1)); if ~isnan(tmp) outs(i)=tmp; break; end % Try smart lower bound. if bounds(i)<=0 firstpart = (chi-0.98*x0max)./(max(mean(drift),drift(1))+2*sdI); bounds(i)=max([firstpart(1),0]); continue; end % Try smart upper bound. if bounds(i+1)==Inf bounds(i+1)=0.02.*max(chi)./(mean(drift)-2*mean(sdI)); continue; end end end result = cumsum(outs); end function result = n1PDF(t,x0max,chi,drift,sdI,st0,truncdrifts) % n1PDF - port of Scott Brown's code if nargin<6 || isempty(st0) st0 = 0; end if nargin<7 || isempty(truncdrifts) truncdrifts = true; end N=length(drift); % Number of responses if length(x0max)1, st0=st0(1); end; % Only ONE non-decision time. if st0==0 result = n1PDFfixedt0(t,x0max,chi,drift,sdI,truncdrifts); return; end result=zeros(size(drift)); for i = 1:length(result) result(i)=quad(@(t) tmpf(t,x0max,chi,drift,sdI,st0,truncdrifts),t(i)-st0/2,t(i)+st0/2); end end function result=tmpf(t,x0max,chi,drift,sdI,st0,truncdrifts) result = n1PDFfixedt0(t,x0max,chi,drift,sdI,truncdrifts)./st0; end function result = n1PDFfixedt0(t,x0max,chi,drift,sdI,truncdrifts) % n1PDFfixedt0 - port of Scott Brown's R code % Generates defective PDF for responses on node #1. % "truncdrifts" sets whether that part of the multi-variate % normal distribution on drift rates which would otherwise % lead to non-terminating trials is truncated. if nargin<6 truncdrifts = true; end N=length(drift); % Number of responses. if (N>2) % For three or more accumulators (i.e., responses) tmp=zeros(length(t),N-1); for i=2:N tmp(:,i-1)=fptcdf(t,x0max(i),chi(i),drift(i),sdI(i)); % Get cdf for accumulators 2-N G=prod(1-tmp,2); % Product of cfds (i.e., (1-cdft2)*(1-cdft3)*...*(1-cdfN)) G = G'; % Fix dimensions end else % For 2 accumulators (i.e., responses) G=1-fptcdf(t,x0max(2),chi(2),drift(2),sdI(2)); end result=G.*fptpdf(t,x0max(1),chi(1),drift(1),sdI(1)); % Product of cdfs times pdf of first accumulator: (f1(t)*(1-F2(t))*(1-F3(t))*...*(1-FN(t)) if (truncdrifts) result=result./(1-prod(normcdf(-drift./sdI))); result(t<=0)=0; end end function result = fptcdf(z,x0max,chi,driftrate,sddrift) % FPTCDF - port of Scott Brown's code if (x0max==0) result = 1 - normcdf(chi./z,driftrate,sddrift); return; end zs=z.*sddrift; zu=z.*driftrate; chiminuszu=chi-zu; xx=chiminuszu-x0max; chizu=chiminuszu./zs; chizumax=xx./zs; tmp1=zs.*(normpdf(chizumax)-normpdf(chizu)); tmp2=xx.*normcdf(chizumax)-chiminuszu.*normcdf(chizu); result = 1+(tmp1+tmp2)./x0max; end function result = fptpdf(z,x0max,chi,driftrate,sddrift) % FPTPDF - port of Scott Brown's R code if (x0max==0) result = (chi./z.^2) * normpdf(chi./z,driftrate,sddrift); return; end zs=z.*sddrift; zu=z.*driftrate; chiminuszu=chi-zu; chizu=chiminuszu./zs; chizumax=(chiminuszu-x0max)./zs; result = (driftrate.*(normcdf(chizu)-normcdf(chizumax)) + ... sddrift*(normpdf(chizumax)-normpdf(chizu)))./x0max; end ------------------------------------------------------------------------------