from otree.api import * c = cu doc = 'The same procedure for all participants' class C(BaseConstants): NAME_IN_URL = 'choice_task_1' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 NUM_GRAPH = 20 CHOICE_BONUS = 0.5 INSTRUCTIONS_TEMPLATE = 'choice_task_1/instructions.html' class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): session = subsession.session if subsession.round_number == 1: ps = subsession.get_players() ps_choice_first = [p for p in ps if p.participant.first_task == 'choice'] ps_ambiguity_first = [p for p in ps if p.participant.first_task == 'ambiguity'] group_matrix = [ps_choice_first, ps_ambiguity_first] for s in subsession.in_rounds(1, C.NUM_ROUNDS): s.set_group_matrix(group_matrix) class Group(BaseGroup): pass class Player(BasePlayer): q1 = models.StringField(choices=[['€13', '€13'], ['€6.5', '€6.5'], ['I do not know', 'I do not know']], widget=widgets.RadioSelect) q2 = models.StringField(choices=[['€5', '€5'], ['€4.5', '€4.5'], ['I do not know', 'I do not know']], widget=widgets.RadioSelect) num_errors = models.IntegerField(initial=0) choice = models.IntegerField() choice = models.IntegerField(choices=[[1, 'Option A: Predictions made by the human analyst. The human analyst is an equity analyst employed by a brokerage house or investment bank.'], [0, 'Option B: Predictions made by the algorithmic analyst. The algorithmic analyst is a machine learning algorithm trained by historical data to predict stock prices.'], [2, 'I am indifferent between the two types of analysts.']], label='', widget=widgets.RadioSelect) def set_payoff(player: Player): participant = player.participant print(participant.task_to_pay) if participant.task_to_pay == 'choice': participant.vars['choice_player_to_pay'] = player.choice print(participant.vars['choice_player_to_pay']) records = participant.vars['records'] human_accuracy = sum([1 if r[-3] == 'TRUE' else 0 for r in records]) algorithmic__accuracy = sum([1 if r[-1] == 'TRUE' else 0 for r in records]) # Option A: Predictions made by the human analyst if player.choice == 1: accuracy = human_accuracy # Option B: Predictions made by the algorithmic analyst elif player.choice == 0: accuracy = algorithmic__accuracy # Remaining choice is Option C: I am indifferent between the two types of analyst else: # the remaining option is player.choice == 2 accuracy = min(human_accuracy, algorithmic__accuracy) participant.payoff = C.CHOICE_BONUS * accuracy class WaitingTask(WaitPage): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Instruction(Page): form_model = 'player' form_fields = ['q1', 'q2'] @staticmethod def error_message(player: Player, values): if values["q1"] != "€6.5" or values["q2"] != "€4.5": player.num_errors += 1 return "There is at least one mistake in your responses. Please try again." class CorrectAnswer(Page): form_model = 'player' class Decision(Page): form_model = 'player' form_fields = ['choice'] @staticmethod def vars_for_template(player: Player): participant = player.participant return dict( graphs=[f'stock/record_id_{n}.png' for n in participant.graphs] ) @staticmethod def before_next_page(player: Player, timeout_happened): set_payoff(player) page_sequence = [Instruction, CorrectAnswer, Decision]