from otree.api import * c = cu doc = '' class C(BaseConstants): NAME_IN_URL = 'Risk_HL' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 PAYOFF_A_HIGH = 2000 PAYOFF_A_LOW = 1600 PAYOFF_B_HIGH = 3850 PAYOFF_B_LOW = 100 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): HL_1 = models.IntegerField(choices=[[1, 'Option A: 10% chance 2000 and 90% chance 1600'], [2, 'Option B: 10% chance 3850 and 90% chance 100']], label='Decision 1:', widget=widgets.RadioSelectHorizontal) HL_2 = models.IntegerField(choices=[[1, 'Option A: 20% chance 2000 and 80% chance 1600'], [2, 'Option B: 20% chance 3850 and 80% chance 100']], label='Decision 2:', widget=widgets.RadioSelectHorizontal) HL_3 = models.IntegerField(choices=[[1, 'Option A: 30% chance 2000 and 70% chance 1600'], [2, 'Option B: 30% chance 3850 and 70% chance 100']], label='Decision 3:', widget=widgets.RadioSelectHorizontal) HL_4 = models.IntegerField(choices=[[1, 'Option A: 40% chance 2000 and 60% chance 1600'], [2, 'Option B: 40% chance 3850 and 60% chance 100']], label='Decision 4:', widget=widgets.RadioSelectHorizontal) HL_5 = models.IntegerField(choices=[[1, 'Option A: 50% chance 2000 and 50% chance 1600'], [2, 'Option B: 50% chance 3850 and 50% chance 100']], label='Decision 5:', widget=widgets.RadioSelectHorizontal) HL_6 = models.IntegerField(choices=[[1, 'Option A: 60% chance 2000 and 40% chance 1600'], [2, 'Option B: 60% chance 3850 and 40% chance 100']], label='Decision 6:', widget=widgets.RadioSelectHorizontal) HL_7 = models.IntegerField(choices=[[1, 'Option A: 70% chance 2000 and 30% chance 1600'], [2, 'Option B: 70% chance 3850 and 30% chance 100']], label='Decision 7:', widget=widgets.RadioSelectHorizontal) HL_8 = models.IntegerField(choices=[[1, 'Option A: 80% chance 2000 and 20% chance 1600'], [2, 'Option B: 80% chance 3850 and 20% chance 100']], label='Decision 8:', widget=widgets.RadioSelectHorizontal) HL_9 = models.IntegerField(choices=[[1, 'Option A: 90% chance 2000 and 10% chance 1600'], [2, 'Option B: 90% chance 3850 and 10% chance 100']], label='Decision 9:', widget=widgets.RadioSelectHorizontal) HL_10 = models.IntegerField(choices=[[1, 'Option A: 100% chance 2000 and 0% chance 1600'], [2, 'Option B: 100% chance 3850 and 0% chance 100']], label='Decision 10:', widget=widgets.RadioSelectHorizontal) HL_choice = models.IntegerField() HL_row = models.IntegerField() HL_random = models.IntegerField() def set_payoff_HL(player: Player): participant = player.participant import random #******************************************* # select random row and random outcome #******************************************* player.HL_row = random.randint(1,10) # select one row randomly for payment (from module random) player.HL_random = random.randint(1,10) # select the number x that defines the outcome of the lottery (if x<=p, outcome is left A_H or A:_, otherwise B_H or B_L) # write it to participant.vars['HL_random'] #******************************************* # select choices in correspondence to random row #******************************************* choices = [player.HL_1,player.HL_2,player.HL_3,player.HL_4,player.HL_5,player.HL_6,player.HL_7,player.HL_8,player.HL_9,player.HL_10] # create a list with all choices of the player player.HL_choice = choices[player.vars['HL_row']-1] # select from the list the choice in correspondence to the randomly drawn row (notice the offset) # write it to participant.vars['HL_choice'] #******************************************* # Compute here the payoffs #******************************************* if player.vars['HL_random'] <= player.vars['HL_row']: # if the random number is smaller equal than the random row if player.vars['HL_choice'] == 1: #A # if the choice was A player.payoff = C.PAYOFF_A_HIGH # because HL_row is the same as p in the MPL else : # if the choice was B player.payoff = C.PAYOFF_B_HIGH else: # if the random number is larger than the random row if player.vars['HL_choice'] == 1 :#A # if the choice was A player.payoff = C.PAYOFF_A_LOW # because HL_row is the same as p in the MPL else : player.payoff = C.PAYOFF_B_LOW #player.payoff = player.vars['payoff_HL'] # write the payoff to player.payoff class Risk_Aversion_HL(Page): form_model = 'player' form_fields = ['HL_1', 'HL_2', 'HL_3', 'HL_4', 'HL_5', 'HL_6', 'HL_7', 'HL_8', 'HL_9', 'HL_10'] @staticmethod def before_next_page(player: Player, timeout_happened): player.set_payoff_HL() class Result(Page): form_model = 'player' @staticmethod def vars_for_template(player: Player): participant = player.participant # retrieve values from participant.vars and store them in a dictionary return dict({ 'payoff_HL': player.participant.vars['payoff_HL'],#payoff 'row':player.participant.vars['HL_row'], # randomly chosen row 'value': participant.vars['HL_random'],# randomly chosen value to define outcome 'choice': participant.vars['HL_choice'],# actual choice # outcomes of the selected row 'p_A_1': player.vars['HL_row'], 'p_A_2': 10-player.vars['HL_row'], 'p_B_1': player.vars['HL_row'], 'p_B_2': 10-player.vars['HL_row']}) page_sequence = [Risk_Aversion_HL, Result]