% 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 = 2000; 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, .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 ------------------------------------------------------------------------------ function fitMDFT(sub,newStartParams,lBounds,uBounds,newMaxIter) % This function takes stimuli and uses fminsearchbnd to do many bounded % searches from a grid of 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) nAlt = 3; % # of alternatives nAttr = 2; % # of attributes/alternative prodCol = 11; % column in data that codes product choiceCols = 12:14; % columns coding chosen alternative sig1 = 1; % fixed param createCandL; % Pull out individual data to pass into fitting functions 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)]; 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)... compErrMDFT(x,sig1,nAlt,nAttr,M,C,L,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) '_MDFT_' num2str(sub) '_test.mat'], 'bestErr', 'bestParams', 'startParams', 'maxIter', 'loop', 'crossValSet','lBounds','uBounds','crossValSet'); loop = loop+1; catch loop = loop+1; continue end end save(['final_' num2str(crossValSet) '_MDFT_' num2str(sub) '_test.mat'], 'bestErr', 'bestParams', 'startParams', 'maxIter', 'loop', 'crossValSet','lBounds','uBounds','crossValSet'); ------------------------------------------------------------------------------ % create the C-Matrix and the L-array, based on the number of alternatives C = -1/(nAlt-1) * ones(nAlt,nAlt); ind = sub2ind(size(C),1:nAlt,1:nAlt) ; C(ind) =1; counter = 1; L=nan(nAlt-1,nAlt,nAlt); for k = 1:nAlt for i = 1:nAlt-1 for j = 1:nAlt if counter==k && counter==j counter=counter+1; end if j==k L(i,j,k)=1; else if j==counter L(i,j,k)=-1; else L(i,j,k)=0; end end end counter=counter+1; end counter=1; end ------------------------------------------------------------------------------ function [LL_MDFT_sum allProbs] = compErrMDFT(params,sig1,nAlt,nAttr,M,C,L,choices,products,missing) % This function takes in parameters and loops through products to compute % predictions for MDFT 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 phi2 = params(13); phi1 = params(14); wgt = params(15); pMDFT_chosenAlt = []; for prod = 1:6 % Pull out product data in M prodTrials = (products==prod); goodTrials = find(~missing' & prodTrials); nTrials(prod) = length(goodTrials); % # of prod trials if nTrials == 0 continue end M2 = M(:,:,goodTrials); prodChoices = choices(goodTrials); % prod choices % Get prod-specific params and put all into a vector w = params(prod); sig2 = params(6+prod); par = [w, sig2, phi2, phi1, wgt]; % Check that attribute weights sum to 1 Wtemp = sort(par(1:(nAttr-1))); Wtemp2 = [0,Wtemp,1]; W = diff(Wtemp2); pMDFT_allAlt = adjustProb(pMDFT_choiceSet_fun(W,C,L,sig1,sig2,phi2,phi1,nAlt,nAttr,nTrials(prod),M2,wgt)); % adjust probabilities away from 1 and 0 ind = sub2ind(size(pMDFT_allAlt),1:nTrials(prod),prodChoices'); pMDFT_chosenAlt = [pMDFT_chosenAlt pMDFT_allAlt(ind)]; allProbs(goodTrials,:) = pMDFT_allAlt; end % calculate the summed Log-Likelihood (LL) % multiply by -1 to minimize positive values LL_MDFT_sum = -sum(log(pMDFT_chosenAlt)); if size(allProbs,1) < allTrials allProbs((end+1):allTrials,:) = zeros(allTrials-size(allProbs,1),3); end end ------------------------------------------------------------------------------ function pMDFT = pMDFT_choiceSet_fun (W,C,L,sig1,sig2,phi2,phi1,nAlt,nAttr,nTrials,M,wgt) pMDFT = nan(nTrials,nAlt); % Calculate the feedback array S and its eigenvalues S = zeros(nAlt,nAlt,size(M,3)); eigenValS = zeros(nAlt,size(M,3)); for i = 1: size(M,3) D = distfunct(M(:,:,i),phi1,phi2,W,wgt); S(:,:,i) = D; eigenValS(:,i) = eig(D); end pMDFT = zeros(nAlt,nTrials); for j = 1:nTrials pMDFT(:,j) = pMDFT_fun(squeeze(M(:,:,j)),squeeze(S(:,:,j)),W,C,L,sig1,sig2,nAlt,nAttr,nTrials); end pMDFT = pMDFT.'; % calculated the choice probabilities end ------------------------------------------------------------------------------ % generalized distance function for n dimensions and unequal attribute weights function D = distfunct (X,phi1,phi2,W,wgt) n = size(X,1); D = nan(n,n); for u = 1:n for j = 1:n D(u,j)= distance_allgemein(X(u,:),X(j,:),W,wgt); end end D=.99*eye(n) - phi2 * exp(-phi1 * D.^2); end function Distance = distance_allgemein(u,v,w,wgt) n = length(u); B = zeros(n,n); %Calculate the indifference vectors for i = 1:(n-1) B(1,i) = -w(i+1)/w(1); B(i+1,i) = w(1)/w(1); end %calculate the doominance vector for j = 1:n B(j,n) = w(j)/w(1); end for k = 1:n B(:,k) = B(:,k)/sqrt(sum((B(:,k).^2))); end P = inv(B); abstand = u-v; Distance = P * abstand(:); A = eye(n); A(n,n) = wgt; Distance = Distance.'* A * Distance; end ------------------------------------------------------------------------------ function p = pMDFT_fun(M, S, W, C,L,sig1,sig2,nAlt) if(any(isnan([W(:);sig2])) || any([W<0, sig2<0]) ) p = repmat(1/1000,nAlt,1); % assign a bad fit else EV = C*M*W(:); % expected value for each option psi = diag(W) - W(:)*W(:).'; % variance-covariance matrix for the primary weights nM = M / norm(M,1); % prevents a very small value nC = C / norm(C,1); % prevents a very small value % variance-covariance matrix of eta phi = (sig1*nC*nM*psi*nM.'*nC.')*norm(M,1)^2*norm(C,1)^2 + sig2*eye(nAlt); % t goes to infinity omega = omegaNoLoop(S,phi,nAlt); if(all(isnan(omega(:))) == true) p = repmat(1e-10,nAlt,1); %assign a bad fit else eta = (eye(nAlt)-S)\EV; p = zeros(nAlt,1); for i = 1: nAlt p(i) = choice_fun(L(:,:,i),eta,omega); end end end end ------------------------------------------------------------------------------ function p = choice_fun (L, eta, omega) Leta = L*eta; % mean difference in preference Lomega = L*omega*L.'; % varance-covariance matrix of the mean difference in preference Lomega = topdm(Lomega); % must be symmetric p = mvncdf(Leta,[],Lomega); end ------------------------------------------------------------------------------ function [sigma] = topdm(sig) % FUNCTION THAT TRANSFORMS A NON POSITIVE DEFINITE SYMMETRIC % MATRIX -E.G. A COVARIANCE MATRIX- TO A % POSITIVE DEFINITE SYMMETRIC MATRIX. EPS = 10^-6; %SET THE VALUE TO PLACE INSTEAD OF ZERO OR NEGATIVE EIGENVALUES ZERO = 10^-10; %SET THE VALUE TO LOOK FOR sigma = sig; [~, err] = cholcov(sigma, 0); %ERR<>0 MEANS SOME EIGENVALUES <=0 %So... SIGMA IS NOT POSITIVE DEFINITE %So... SIGMA IS NOT INVERTIBLE if (err ~= 0) [v d] = eig(sigma); %CALCULATE EIGENVECTOR AND EIGENVALUES d=diag(d); %GET A VECTOR OF EIGENVALUES d(d<=ZERO)=EPS; %FIND ALL EIGENVALUES<=ZERO AND CHANGE THEM FOR EPS d=diag(d); %CONVERT VECTOR d INTO A MATRIX sigma = v*d*v'; %RECOMPOSE SIGMA MATRIX USING EIGENDECOMPOSITION %WHY? SIGMA IS SIMETRIC AND V IS ORTHONORMAL end ------------------------------------------------------------------------------ function OmegaDirect = omegaNoLoop(S,Phi,nAlt) A = nan(nAlt^2,nAlt^2); for i = 1:nAlt for j = 1:nAlt A((1+(i-1)*nAlt):(nAlt*i),(1+(j-1)*nAlt):(nAlt*j))=S(i,j)*S; end end I = eye(nAlt^2); A = (I-A); Phi = reshape(Phi.',[],1); if(abs(det(A))<10e-17) OmegaDirect = nan(nAlt,nAlt); else Omega = A\Phi; OmegaDirect = reshape(Omega.',nAlt,nAlt); end end ------------------------------------------------------------------------------