from otree.api import * from secrets import SystemRandom, token_urlsafe from timed_response import response_payload_error, store_timed_response from withdrawal import ExperimentPage secure_random = SystemRandom() doc = """ Ambiguity task. Participants choose between a sure 5-point payment and an ambiguous lottery. A short, uniformly jittered fixation interval follows each non-final trial. """ class C(BaseConstants): NAME_IN_URL = 'game-04' PLAYERS_PER_GROUP = None NUM_BLOCKS = 1 TRIAL_TYPES_PER_BLOCK = 15 NUM_ROUNDS = NUM_BLOCKS * TRIAL_TYPES_PER_BLOCK SAFE_POINTS = 5 JITTER_MIN_MS = 200 JITTER_MAX_MS = 600 # The underlying composition is always 50/50. Only information visibility varies. # (underlying high-payoff probability, occluder percentage, high payoff) AMBIGUITY_TRIALS = tuple( (50, ambiguity, payoff) for ambiguity in (24, 50, 74) for payoff in (5, 8, 20, 50, 125) ) class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): chosen_side = models.StringField( choices=['Left', 'Right'], blank=True, ) rt = models.FloatField(blank=True) response_payload = models.LongStringField() response_token = models.StringField() block_number = models.IntegerField() probability_high = models.IntegerField() ambiguity_percent = models.IntegerField() high_payoff = models.IntegerField() low_payoff = models.IntegerField(initial=0) safe_side = models.StringField() jitter_ms = models.IntegerField() missed = models.BooleanField(initial=False) check1 = models.StringField( choices=[ ('A', 'Spin the lottery wheel on every trial'), ('B', 'Predict how the colours are divided behind the grey sector'), ('C', 'Choose between a sure payment of 5 points and a lottery'), ], label='1. What do you need to do on each trial?', widget=widgets.RadioSelect, ) check2 = models.StringField( choices=[ ('A', 'My average point outcome across all trials'), ('B', 'My choice in the final trial'), ('C', 'One trial is randomly selected, and my choice in that trial determines my points'), ], label='2. What determines your point earnings for this game?', widget=widgets.RadioSelect, ) check3 = models.StringField( choices=[ ('A', '0 points only'), ('B', '20 points only'), ('C', 'Either 0 points or 20 points'), ('D', 'A third outcome represented by grey'), ], label=( '3. Suppose that in the trial shown below, you chose the lottery and ' 'the wheel lands in the grey sector. What could your outcome be?' ), widget=widgets.RadioSelect, ) def creating_session(subsession: Subsession): """Create one shuffled block containing all 15 ambiguity trials.""" for player in subsession.get_players(): player.response_token = token_urlsafe(16) if player.round_number == 1: order = list(range(C.TRIAL_TYPES_PER_BLOCK)) secure_random.shuffle(order) player.participant.vars['ambiguity_order'] = order safe_sides = ['Left'] * (C.NUM_ROUNDS // 2) + ['Right'] * (C.NUM_ROUNDS // 2) safe_sides.append(secure_random.choice(['Left', 'Right'])) secure_random.shuffle(safe_sides) player.participant.vars['ambiguity_safe_sides'] = safe_sides order = player.participant.vars['ambiguity_order'] safe_sides = player.participant.vars['ambiguity_safe_sides'] trial_index = order[player.round_number - 1] probability, ambiguity, payoff = C.AMBIGUITY_TRIALS[trial_index] player.block_number = 1 + (player.round_number - 1) // C.TRIAL_TYPES_PER_BLOCK player.probability_high = probability player.ambiguity_percent = ambiguity player.high_payoff = payoff player.low_payoff = 0 player.safe_side = safe_sides[player.round_number - 1] player.jitter_ms = secure_random.randint(C.JITTER_MIN_MS, C.JITTER_MAX_MS) class AmbiguityInstructions(ExperimentPage): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class AmbiguityExample(ExperimentPage): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class AmbiguityCheck(ExperimentPage): form_model = 'player' form_fields = ['check1', 'check2', 'check3'] @staticmethod def is_displayed(player: Player): return player.round_number == 1 @staticmethod def error_message(player: Player, values): if any(values[field] != 'C' for field in ('check1', 'check2', 'check3')): return 'At least one answer is incorrect. Please review the instructions and try again.' class AmbiguityPracticeIntro(ExperimentPage): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class AmbiguityPractice(ExperimentPage): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class AmbiguityPracticeEnd(ExperimentPage): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class AmbiguityTask(ExperimentPage): form_model = 'player' form_fields = ['response_payload'] @staticmethod def vars_for_template(player: Player): return dict( known_probability=(100 - player.ambiguity_percent) // 2, response_token=player.response_token, ) @staticmethod def error_message(player, values): return response_payload_error(values['response_payload'], player.response_token) @staticmethod def before_next_page(player: Player, timeout_happened): store_timed_response(player) class Fixation(ExperimentPage): @staticmethod def is_displayed(player: Player): return player.round_number < C.NUM_ROUNDS class AmbiguityEnd(ExperimentPage): @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS page_sequence = [ AmbiguityInstructions, AmbiguityExample, AmbiguityCheck, AmbiguityPracticeIntro, AmbiguityPractice, AmbiguityPracticeEnd, AmbiguityTask, Fixation, AmbiguityEnd, ]