from otree.api import * import numpy as np import random doc = """ Risk-attitude elicitation """ class C(BaseConstants): NAME_IN_URL = 'risk' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 BLANK = False LOTTERY_A_HIGH = cu(2.00) LOTTERY_A_LOW = cu(1.60) LOTTERY_B_HIGH = cu(3.85) LOTTERY_B_LOW = cu(0.10) RISK_INSTRUCTIONS_NEXT = '_templates/risk_instruction_next.html' class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): # Risk-attitude elicitation variables switching_point = models.FloatField(initial=None) random_choice_problem = models.IntegerField(initial=None) random_01 = models.FloatField(initial=None) chose_lottery_a = models.BooleanField(initial=None) # Questionnaire gender = models.IntegerField( blank=C.BLANK, choices=[ [0, "Male"], [1, "Female"], [2, "Other"], [3, "Prefer not to answer"], ], label='', widget=widgets.RadioSelect ) area_of_studies = models.StringField(blank=C.BLANK, label='') year_of_studies = models.IntegerField( blank=C.BLANK, choices=[ [1, "Undergraduate 1st year"], [2, "Undergraduate 2nd year"], [3, "Undergraduate 3rd year"], [4, "Undergraduate 4th or higher year"], [5, "Graduate program"], [6, "Other"]], label='', widget=widgets.RadioSelect ) previous_econ_experiment = models.IntegerField( blank=C.BLANK, choices=[ [1, "Yes"], [0, "No"]], label='', widget=widgets.RadioSelect ) previous_psych_experiment = models.IntegerField( blank=C.BLANK, choices=[ [1, "Yes"], [0, "No"]], label='', widget=widgets.RadioSelect ) stat_prob_course = models.IntegerField( blank=C.BLANK, choices=[ [1, "Yes"], [0, "No"]], label='', widget=widgets.RadioSelect ) easy_instructions = models.IntegerField( blank=C.BLANK, choices=[ [2, "Strongly agree"], [1, "Agree"], [0, "Neither agree nor disagree"], [-1, "Disagree"], [-2, "Strongly disagree"]], label='', widget=widgets.RadioSelect ) email_second_time = models.StringField(blank=C.BLANK, label='') name = models.StringField() email_final = models.StringField(blank=C.BLANK, label='', widget=widgets.RadioSelect) income_final = models.CurrencyField() def creating_session(subsession): for player in subsession.get_players(): player.random_choice_problem = random.randint(0, 10) player.random_01 = random.random() def email_final_choices(player: Player): # qd = current_question(player) return [ player.participant.email, player.email_second_time ] # PAGES class RiskInstruction(Page): pass class RiskChoice(Page): form_model = 'player' form_fields = ['switching_point'] @staticmethod def vars_for_template(player: Player): prob_range = list(np.arange(0, 1.1, 0.1)) prob_range = [round(item, 1) for item in prob_range] return dict(prob_range=prob_range) @staticmethod def before_next_page(player, timeout_happened): if player.random_choice_problem < round(10 * player.switching_point): player.chose_lottery_a = True player.payoff = C.LOTTERY_A_HIGH if player.random_01 < player.switching_point else C.LOTTERY_A_LOW else: player.chose_lottery_a = False player.payoff = C.LOTTERY_B_HIGH if player.random_01 < player.switching_point else C.LOTTERY_B_LOW class Questionnaire(Page): form_model = 'player' form_fields = ['gender', 'area_of_studies', 'year_of_studies', 'previous_econ_experiment', 'previous_psych_experiment', 'stat_prob_course', 'easy_instructions', 'email_second_time'] @staticmethod def before_next_page(player, timeout_happened): if player.participant.email == player.email_second_time: player.email_final = player.email_second_time class EmailCheck(Page): form_model = 'player' form_fields = ['email_final'] @staticmethod def is_displayed(player): return player.participant.email != player.email_second_time class FinalIncome(Page): @staticmethod def vars_for_template(player: Player): player.name = player.participant.name player.income_final = player.participant.payoff_plus_participation_fee() random_choice_problem_plus1 = player.random_choice_problem + 1 random_p = round(player.random_choice_problem / 10, 1) participant = player.participant html = '' for i in list(range(0, 10)): html += f" {i + 1} " \ + "" + str(participant.p[i]) + "" \ + "" + str(participant.t[i]) + "" \ + "" + str(participant.win_prob[i]) + "" \ + "" + str(participant.won[i]) + "" return dict(html=html, random_choice_problem_plus1=random_choice_problem_plus1, random_p=random_p) page_sequence = [ RiskInstruction, RiskChoice, Questionnaire, EmailCheck, FinalIncome ]