File size: 13,963 Bytes
b6c95bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
classdef gymGame
    % Defines the outcomes of a T period game with a commitment contract
    % that imposes penalty p if a_star attendances are not achieved, or
    % instead with a piece-rate incentive of r per attendance
    
    properties
        
        % Game properties
        T;                  % number of periods in game
        a_star;             % commitment contract attendance threshold
        p;                  % commitment contract penalty
        r;                  % per attendance reward
        
        % Parameters for individuals taking up the contract
        lambda;             % 1/mean of exponential cost distribution
        min_cost;           % lower bound on support of cost distribution 
        beta;               % actual present focus
        betatilde;          % perceived present focus
        b;                  % health benefit
        mu;                 % fraction of people taking up the contract
        
        % Outcomes under commitment contract
        utilities;          % utilities in each period for each g
        t1_utility;         % period 1 utility
        utilities_tilde;    % perceived utilities in each future period
        att;                % distribution of attendances in simulation
        avg_att;            % average attendance in simulation
        avg_costs;          % average aggregate costs incurred
        avg_t1_att;         % average first period attendance
        prob_success;       % likelihood of meeting threshold in simulation
        prob_att_c_b;       % likelihood of attending when cost > benefit
        prob_att_day;       % likelihood of attending each day
        
        % Outcomes under piece-rate incentive(s)
        prob_att_r;         % probability of attending on a given day
        att_r;              % expected attendance under incentive r
        change_att_r;       % expected increase in attendance due to r
        benefits_r;         % expected health benefits under incentive r
        costs_r;            % expected costs under incentive r
        net_surplus_r;      % surplus under incentive r, net of incentive
        surplus_r;          % total surplus under incentive r
        
        % Identifier
        spec;
        
    end
    
    properties (Constant)
        
        seed = 12345;       % seed for simulation
        rounds = 10000;     % number of iterations of the simulation
        
    end
    
    
    methods
        
        function obj = gymGame(properties, parameters, min_cost, title)
            
            % input properties
            obj.T = properties.T;
            obj.a_star = properties.a_star;
            obj.p = properties.p;
            
            % set parameters from data
            obj.lambda = parameters.lambda;
            obj.min_cost = min_cost;
            obj.beta = parameters.beta;
            obj.betatilde = parameters.betatilde;
            obj.b = parameters.b;
            obj.mu = parameters.mu;
            
            % set identifier
            obj.spec = title;
            
            % compute matrix of utilities
            obj = obj.compute_utilities();
            
            % simulate behavior of participants
            obj = obj.simulate_behavior();
            
        end
        
        function obj = compute_utilities(obj)
            
            V_n = NaN([obj.a_star+1 obj.T]);
            V_tilde = NaN([obj.a_star+1 obj.T]);
            
            % set last period utilities
            for g = 0:obj.a_star
                g1 = g + 1; % 1-indexed
                V_n(g1,obj.T) = obj.compute_T_utility(g, 'actual');
                V_tilde(g1,obj.T) = obj.compute_T_utility(g, 'perceived');
            end
            
            % set all other period utilities
            for period = 1:obj.T-1
                
                t = obj.T - period; % go backwards
                start = max(obj.a_star - t + 1, 0);
                
                % iterate through remaining attendances
                for g = start:obj.a_star 
                    g1 = g + 1; % 1-indexed
                    
                    % expected utility difference from attending
                    delta_V_tilde = V_tilde(max(g1-1,1),t+1) - V_tilde(g1,t+1);
                    delta_V_n =  V_n(max(g1-1,1),t+1) - V_n(g1,t+1);
                    
                    % perceived expected utility at period t
                    V_tilde(g1,t) = (obj.F(obj.betatilde.*(obj.b + delta_V_tilde)).* ...
                        (obj.b + delta_V_tilde)) + V_tilde(g1,t+1) - ...
                        obj.C(obj.betatilde.*(obj.b + delta_V_tilde));
                    
                    % actual expected utility at period t
                    V_n(g1,t) = (obj.F(obj.beta.*(obj.b + delta_V_tilde)).* ...
                        (obj.b + delta_V_n)) + V_n(g1,t+1) - ...
                        obj.C(obj.beta.*(obj.b + delta_V_tilde));
                    
                end
                
            end
            
            obj.utilities = V_n;
            obj.t1_utility = V_n(obj.a_star+1,1); % period 1 utility
            obj.utilities_tilde = V_tilde;
            
        end
        
        function V_T = compute_T_utility(obj, g, focus)
            % compute utility in the last period
                
            % set present focus parameter
            if strcmp(focus, 'actual')
                
                B = obj.beta;
                
            elseif strcmp(focus, 'perceived')
                
                B = obj.betatilde;
                
            end
            
            if g == 0 % no attendances left
                
                V_T = (obj.F(B.*obj.b).*obj.b) - obj.C(B.*obj.b);
                
            elseif g == 1 % one attendance left
                
                V_T = (obj.F(B.*(obj.b + obj.p)).*obj.b) - ...
                    obj.C(B.*(obj.b + obj.p)) - ...
                    ((1 - obj.F(B.*(obj.b + obj.p))).*obj.p);
                
            else % more than one attendance left
                
                V_T = (obj.F(B.*obj.b).*obj.b) - obj.C(B.*obj.b) - obj.p;
            end
            
        end
        
        function obj = simulate_behavior(obj)
            % simulate behavior through T periods for a number of rounds to
            % generate estimates of average attendance and the likelihood
            % of meeting the contract threshold
            
            rng(obj.seed); % set seed
            obj.att = zeros(1,obj.rounds); % default no attendances
            t1_att = obj.att;
            att_day = zeros(obj.T,obj.rounds); % default no attendances
            costs_day = zeros(obj.T,obj.rounds); % default no costs
            met_goal = zeros(1,obj.rounds); % default failed to meet goal
            obj.prob_att_c_b = zeros(1,obj.rounds); % default never attend
            
            for round = 1:obj.rounds
                
                c = obj.cost_draws();   % generate cost draws
                g = obj.a_star;         % all attendances initially left
                a = 0;                  % total number of attendances
                c_b = 0;                % events where cost > benefit
                att_c_b = 0;            % attendance in those events
                
                % check if the DM wants to go to the gym in each period
                for t = 1:obj.T
                    g1 = g + 1; % 1-indexed
                    
                    if t == obj.T && (g == 0 || g >= 2) % last period, case i
                        
                        if c(t) <= (obj.beta * obj.b)
                            
                            % update attendances & costs incurred
                            g = max(g - 1, 0);
                            a = a + 1;
                            att_day(t,round) = 1;
                            costs_day(t,round) = c(t);
                            
                            if c(t) > obj.b
                                % cost is greater than benefit
                                att_c_b = att_c_b + 1;
                            end
                            
                        end
                        
                    elseif t == obj.T && g == 1 % last period, case ii
                        
                        if c(t) <= (obj.beta * (obj.b + obj.p))
                            
                            % update attendances & costs incurred
                            g = max(g - 1, 0);
                            a = a + 1;
                            att_day(t,round) = 1;
                            costs_day(t,round) = c(t);
                            
                            if c(t) > obj.b
                                % cost is greater than benefit
                                att_c_b = att_c_b + 1;
                            end
                            
                        end
                        
                    else % all other periods
                        
                        if c(t) <= (obj.beta * (obj.b + ...
                                obj.utilities_tilde(max(g1-1,1),t+1) - ...
                                obj.utilities_tilde(g1,t+1)))
                            
                            % update attendances & costs incurred
                            g = max(g - 1, 0);
                            a = a + 1;
                            att_day(t,round) = 1;
                            costs_day(t,round) = c(t);
                            
                            if c(t) > obj.b
                                % cost is greater than benefit
                                att_c_b = att_c_b + 1;
                            end
                            
                        end
                        
                    end
                    
                    if c(t) > obj.b
                        % cost is greater than benefit
                        
                        c_b = c_b + 1;
                        
                    end
                    
                    if t == 1 && g < obj.a_star
                        % period one attendances
                    
                        t1_att(round) = 1;
                    
                    end
                    
                end
                
                if g == 0
                    
                    met_goal(round) = 1; % input outcome
                    
                end
                
                obj.att(round) = a; % input attendances
                obj.prob_att_c_b(round) = att_c_b ./ c_b;  
                
            end
            
            % return means
            obj.avg_att = mean(obj.att);
            obj.avg_t1_att = mean(t1_att);
            obj.prob_success = mean(met_goal);
            obj.prob_att_c_b = mean(obj.prob_att_c_b);
            obj.prob_att_day = sum(att_day,2) ./ obj.rounds;
            obj.avg_costs = mean(sum(costs_day,1));
            
        end
        
        function obj = compute_incentive_behavior(obj, r)
            % compute expected attendance and total surplus given
            % piece-rate incentive r
            
            obj.r = r;
            
            % compute expected attendance on a given day
            obj.prob_att_r = obj.F(obj.beta .* (obj.b + r));
            prob_att_no_r = obj.F(obj.beta .* (obj.b)); % without incentive
            
            % compute expected attendance
            obj.att_r = obj.T .* obj.prob_att_r;
            
            % compute expected increase in attendance
            obj.change_att_r = obj.T .* (obj.prob_att_r - prob_att_no_r);
            
            % compute expected benefits
            obj.benefits_r = obj.att_r .* obj.b;
            
            % compute expected costs
            obj.costs_r = obj.T .* obj.C(obj.beta .* (obj.b + r));
            
            % compute surplus net of incentive
            obj.net_surplus_r = obj.benefits_r - obj.costs_r;
            
            % compute total surplus
            obj.surplus_r = obj.net_surplus_r + (obj.att_r .* r);
            
        end
        
        function probability = F(obj, x)
            % cumulative distribution function of costs
            
            z = x - obj.min_cost; 
            z = gymGame.minzero(z);
            probability = 1 - exp(-obj.lambda.*z);
            
        end
        
        function density = f(obj, x)
            % cost density function
            % returns zero when given x < lower bound on costs
           
            z = x - obj.min_cost;
            density = obj.lambda.*exp(-obj.lambda.*z);
            density = density.*heaviside(heaviside(z));

        end
        
        function expectation = C(obj, x)
            % expected cost function
            
            z = x - obj.min_cost;
            z = gymGame.minzero(z);
            expectation = (1 ./ obj.lambda).*(1 - exp(-obj.lambda.*z)) ...
                - (z.*exp(-obj.lambda.*z)) + obj.min_cost.*obj.F(x);
            
        end
        
        function c = cost_draws(obj)
            % vector of random cost draws for each period
            
            mean = 1 ./ obj.lambda;
            c = exprnd(mean, [1 obj.T]) + obj.min_cost;
            
        end
        
    end
    
    methods(Static)
        
        function y = minzero(x)
            % returns x if x is positive, 0 otherwise
            
            y = x .* heaviside(heaviside(x));
            
        end
        
    end
    
end