from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) from treatment.config import * import random from random import randrange # ******************************************************************************************************************** # # *** CLASS SUBSESSION # ******************************************************************************************************************** # class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: n = Constants.num_choices for p in self.get_players(): # create list of lottery indices # ---------------------------------------------------------------------------------------------------- indices = [j for j in range(1, n)] indices.append(n) if Constants.certain_choice else None # create list of lottery indices # ---------------------------------------------------------------------------------------------------- choices = [0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30] # create list corresponding to form_field variables including all choices # ---------------------------------------------------------------------------------------------------- form_fields = ['choice_' + str(k) for k in indices] # create list of choices # ---------------------------------------------------------------------------------------------------- p.participant.vars['mpl_choices_test'] = list( zip(indices, form_fields, choices) ) # randomly determine index/choice of binary decision to pay # ---------------------------------------------------------------------------------------------------- p.participant.vars['mpl_index_to_pay'] = random.choice(indices) p.participant.vars['mpl_choice_to_pay'] = 'choice_' + str(p.participant.vars['mpl_index_to_pay']) # initiate list for choices made # ---------------------------------------------------------------------------------------------------- p.participant.vars['mpl_choices_made_test'] = [None for j in range(1, n + 1)] # randomly assign the player to one of the three groups # ---------------------------------------------------------------------------------------------------- p.participant.vars['treated'] = random.randint(1, 10) if (p.participant.vars['treated'] == 11) or (p.participant.vars['treated'] == 12): p.participant.vars['treatment'] = 1 elif (p.participant.vars['treated'] > 0) and (p.participant.vars['treated'] < 6): p.participant.vars['treatment'] = 2 else: p.participant.vars['treatment'] = 3 p.participant.vars['payoff'] = 0 # generate random switching point for PlayerBot in tests.py # -------------------------------------------------------------------------------------------------------- for participant in self.session.get_participants(): participant.vars['mpl_switching_point'] = random.randint(1, n) # ******************************************************************************************************************** # # *** CLASS GROUP # ******************************************************************************************************************** # class Group(BaseGroup): pass # ******************************************************************************************************************** # # *** CLASS PLAYER # ******************************************************************************************************************** # class Player(BasePlayer): # add model fields to class player # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: if Constants.certain_choice: for j in range(1, Constants.num_choices + 1): locals()['choice_' + str(j)] = models.StringField() del j else: for j in range(1, Constants.num_choices): locals()['choice_' + str(j)] = models.StringField() del j random_draw = models.IntegerField() choice_to_pay = models.StringField() value_to_pay = models.IntegerField() option_to_pay = models.StringField() inconsistent = models.IntegerField() switching_row = models.IntegerField() brisk_five_test = models.FloatField( label="What is your estimated percent chance of being diagnosed with breast cancer in the next 5 years? (%)", ) brisk_life_test = models.FloatField( label="What is your estimated percent chance of being diagnosed with breast cancer in the next 50 years? (%)", ) page_pass_time = models.FloatField() # set player's payoff # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: def set_payoffs(self): # random draw to determine whether to pay the "high" or "low" outcome of the randomly picked lottery # ------------------------------------------------------------------------------------------------------------ self.random_draw = randrange(1, len(self.participant.vars['mpl_choices_test'])) # set to participant.var['choice_to_pay'] determined creating_session # ------------------------------------------------------------------------------------------------------------ self.choice_to_pay = self.participant.vars['mpl_choice_to_pay'] self.value_to_pay = Constants.payoff[self.participant.vars['mpl_index_to_pay']] # elicit whether lottery "A" or "B" was chosen for the respective choice # ------------------------------------------------------------------------------------------------------------ self.option_to_pay = getattr(self, self.choice_to_pay) # set player's payoff # ------------------------------------------------------------------------------------------------------------ if self.option_to_pay == 'A': if self.random_draw <= 1: self.participant.vars['payoff'] = self.value_to_pay else: self.participant.vars['payoff'] = 0 else: if self.random_draw <= 1: self.participant.vars['payoff'] = 10 else: self.participant.vars['payoff'] = 0 # set payoff as global variable # ------------------------------------------------------------------------------------------------------------ self.participant.vars['mpl_payoff'] = self.payoff # determine consistency # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: def set_consistency(self): n = Constants.num_choices # replace A's by 1's and B's by 0's self.participant.vars['mpl_choices_made_test'] = [ 1 if j == 'A' else 0 for j in self.participant.vars['mpl_choices_made_test'] ] # check for multiple switching behavior for j in range(1, n): choices = self.participant.vars['mpl_choices_made_test'] self.inconsistent = 1 if choices[j] < choices[j - 1] else 0 if self.inconsistent == 1: break # determine switching row # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: def set_switching_row(self): # set switching point to row number of first 'B' choice if self.inconsistent == 0: self.switching_row = Constants.num_choices - sum(self.participant.vars['mpl_choices_made_test']) + 1