from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import itertools import numpy as np import math author = 'Zheng Li' doc = """ Direct Elicitation for money experiment """ class Constants(BaseConstants): name_in_url = 'direct_elicitation_load' players_per_group = None # pairs of tau2 and tau1# decisions = [ # [0:7] choose 2 and repeat 1 from the chosen 2 ['In 1 week', 'Today'], ['In 1 month', 'Today'], ['In 2 months', 'Today'], ['In 2 years', 'Today'], ['In 3 years', 'Today'], ['In 4 years', 'Today'], ['In 7 years', 'Today'], # [7:14] choose 2 ['In 1 month and 1 week', 'In 1 month'], ['In 2 months', 'In 1 month'], ['In 6 months', 'In 1 month'], ['In 1 year', 'In 1 month'], ['In 2 years', 'In 1 month'], ['In 4 years', 'In 1 month'], ['In 7 years', 'In 1 month'], # [14:17] sub-additivity set 1 ['In 6 months', 'Today'], ['In 1 year', 'In 6 months'], ['In 1 year', 'Today'], # [17:20] sub-additivity set 2 ['In 4 months', 'Today'], ['In 8 months', 'In 4 months'], ['In 8 months', 'Today'], ] amounts = range(36, 58, 2) steps = 2 num_rounds = 6 # to generate the certainty-uncertainty spectrum bar list1 = np.arange(1 * 20, - 1, -1) list2 = [] list2.append(0) for x in range(20): list2.append((x+1)*5) measure_set = [('In ?', 'Today', 30)] class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: for p in self.get_players(): p.participant.vars['tests_passed'] = True p.participant.vars['sub_'] = random.choice([1, 2]) datessub1 = Constants.decisions[14:17] dates_sub1 = list() amount_tmp = random.choice(Constants.amounts) for date in datessub1: date = (date, amount_tmp, 1) dates_sub1.append(date) datessub2 = Constants.decisions[17:20] dates_sub2 = list() amount_tmp = random.choice(Constants.amounts) for date in datessub2: date = (date, amount_tmp, 2) dates_sub2.append(date) if p.participant.vars['sub_'] == 1: dates_sub = dates_sub1 elif p.participant.vars['sub_'] == 2: dates_sub = dates_sub2 dates_1 = list() dates1 = random.sample(Constants.decisions[0:7], 2) for date in dates1: amount_tmp = random.choice(Constants.amounts) date = (date, amount_tmp, 0) dates_1.append(date) dates_1_extra = random.choice(dates_1) dates_1.append(dates_1_extra) tmp = random.sample(dates_sub + dates_1, 6) p.participant.vars['dates'] = [] for time_dates, pay_amount, sub_id in tmp[0:3]: p.participant.vars['dates'].append((time_dates, pay_amount, sub_id, 1)) for time_dates, pay_amount, sub_id in tmp[3:6]: p.participant.vars['dates'].append((time_dates, pay_amount, sub_id, 0)) p.participant.vars['dates'] = random.sample(p.participant.vars['dates'], 6) for p in self.get_players(): p.set_current_task_number() p.participant.vars['load_num_correct'] = 0 class Group(BaseGroup): pass class Player(BasePlayer): # whether the participant has been passing all tests so far tests_passed = models.BooleanField(initial=True) # for load load_identifier = models.IntegerField(initial=1) load_guess = models.IntegerField( min=0, max=1000, blank=False, label="Enter the sum of red numbers that were flashed on the choice list screen" ) load_sum = models.IntegerField(initial=0) load_correct = models.IntegerField(initial=0) load_num_correct = models.IntegerField(initial=0) timeout_load = models.IntegerField(initial=0) # current set sub_identifier = models.IntegerField() # parameters in the optimization problem pay_amount = models.IntegerField() # benchmark payment at tau2 time_date_lhs = models.StringField() # tau2 time_date_rhs = models.StringField() # tau1 # indicator_never_always_switcher = models.IntegerField() # indifferent payment task: switching_point = models.IntegerField(min=0) def switching_point_max(self): return self.pay_amount confidence = models.FloatField() # higher -> more confident; from 0 to 20 respectively corresponding to from 0% to 100%; step = $5% def set_current_task_number(self): setattr(self, 'time_date_lhs', self.participant.vars['dates'][self.round_number - 1][0][0]) setattr(self, 'time_date_rhs', self.participant.vars['dates'][self.round_number - 1][0][1]) setattr(self, 'pay_amount', self.participant.vars['dates'][self.round_number - 1][1]) setattr(self, 'sub_identifier', self.participant.vars['dates'][self.round_number - 1][2]) setattr(self, 'load_identifier', self.participant.vars['dates'][self.round_number - 1][3]) def current_choice(self): iet_choice = [(self.time_date_lhs, self.time_date_rhs, self.pay_amount)] return iet_choice