-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCalculateSunkCostInWZ.m
More file actions
134 lines (109 loc) · 4.64 KB
/
CalculateSunkCostInWZ.m
File metadata and controls
134 lines (109 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function [pEarn,sunkNess] = CalculateSunkCostInWZ(varargin)
%fprintf('Calculating Sunk Cost in WZ...');
% [pEarn,sunkNess] = CalculateSunkCostInWZ(varargin)
%
%this function calculates a time invested X time remaining matrix of earn
%probability (pEarn) along with an overall measure of sunk cost (sunkNess).
%The sunk cost measure is the average difference in pEarn between X s time
%invested (2-29 s invested) and control (1 s invested). Positive values indicate
%evidence of sunk cost (with higher values indiacting stronger sunk cost).
%
%inputs:
%the following variables are needed:
% 1) offer = Offer delay for each WZ entry (1 X nEncounters)
% 2) timeSpentAtQuit = Time to quit for each WZ entry. NaN for earned
% encounters (1 X nEncounters)
% 3) isEarn = Logical indicating whether reward was earned (earn = true)
% for each encounter (1 X nEncounters)
% 4) isQuit = Logican indicating whether a quit occured (quit = true)
% for each encounter (1 X nEncounters)
% 5) maxD = Maximum delay
%
%these variables can be fields within a structure (with field names
%identical to the list above) or they can be separate vectors. IF
%VARIABLES ARE GIVEN AS SEPARATE VECTORS, THE ORDER IN WHICH THEY ARE GIVEN
%IS CRITICAL. 1st input = offer. 2nd input = timeSpentAtQuit. 3rd input =
%isEarn. 4th input = isQuit. 5th input = maxD.
%
%outputs:
% 1) pEarn = maxD X maxD matrix of pEarn. 1st dimension is time invested while
% 2nd dimension is time remaining
% 2) sunkNess = difference in pEarn between X s invested and control (1 s
% invested) averaged across X s invested
% % % % % % % % % % % % % % % % % % %
% %
% Unpack variables & quality check %
% %
% % % % % % % % % % % % % % % % % % %
if nargin == 1 %input was a structure containing variables in fields
R = varargin{1};
fieldNames = {'offer','timeSpentAtQuit','isEarn','isQuit','maxD'};
for iF = 1:length(fieldNames)
curField = fieldNames{iF};
assert(isfield(R,curField),'Incorrect field names in R');
end
offer = R.offer;
timeSpentAtQuit = ceil(R.timeSpentAtQuit);
isEarn = R.isEarn;
isQuit = R.isQuit;
maxD = R.maxD;
elseif nargin == 5 %input was 5 separate vectors (in correct order!)
offer = varargin{1};
timeSpentAtQuit = ceil(varargin{2});
isEarn = varargin{3};
isQuit = varargin{4};
maxD = varargin{5};
else
error('Must input: offer, timeSpentAtQuit, isEarn, isQuit, and maxD variables (separately or as fields in structure)')
end
assert(islogical(isEarn) & islogical(isQuit),'isEarn & isQuit must be logicals');
assert(sum(~isnan(timeSpentAtQuit)) == sum(isQuit),'timeSpentAtQuit & isQuit do not agree on # of quits');
assert(sum(isnan(timeSpentAtQuit)) == sum(isEarn),'timeSpentAtQuit & isEarn do not agree on # of earns');
% % % % % % % % % % % % % % % % % % %
% %
% Get earn probability for offer %
% delay X time invested %
% %
% % % % % % % % % % % % % % % % % % %
nT = length(offer);
didEarn = nan(nT,maxD,maxD); %encounters X offer delay X time invested
for iT = 1:nT
if isEarn(iT)
didEarn(iT,offer(iT),1:offer(iT)) = true;
elseif isQuit(iT)
if timeSpentAtQuit(iT) > 0
didEarn(iT,offer(iT),1:timeSpentAtQuit(iT)) = false;
end
end
end
% % % % % % % % % % % % % % % % % % % % % % %
% %
% Get earn probability for time invested %
% X time waited %
% %
% % % % % % % % % % % % % % % % % % % % % % %
pEarn = nan(maxD,maxD); %time invested X time remaining
for iDelay = 1:maxD
for tWaited = 1:(iDelay-1)
tRemaining = iDelay - tWaited;
pE = squeeze(nanmean(didEarn(:,iDelay,tWaited)));
pEarn(tWaited,tRemaining) = pE;
end
end
if nargout > 1
% % % % % % % % % % %
% %
% Compute SunkNess %
% %
% % % % % % % % % % %
pEarnDiff = nan(maxD,1); %1st element = 1 s invested; 2nd element = 2 s invested...
ctrlLine = pEarn(1,:); %all pEarn differences are taken from 1 s invested as a control line
for iD = 1:(maxD-1) %1 s invested through 29 s invested
curLine = pEarn(iD,:);
curDelayDiff = curLine - ctrlLine;
pEarnDiff(iD) = nanmean(curDelayDiff);
end
pEarnDiff(1) = nan; % this value will always be 0 because of ctrl slope definition
sunkNess = nanmean(pEarnDiff);
end
%fprintf('DONE.\n');