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