from otree.api import * import random # from otree.models import player c = Currency doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'test5' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 2 ALPHA = 0.7 PROB = 0.5 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): bet1 = models.BooleanField( choices=[[True, "Democratic Governments"], [False, "Republican Governments"]], widget=widgets.RadioSelect, label="Your belief is:" ) bet2 = models.BooleanField( choices=[[True, "Democratic Governments"], [False, "Republican Governments"]], widget=widgets.RadioSelect, label="Your lottery choice is:" ) reported_prior1 = models.FloatField( label="I believe that the probability the entire US history, " "the average crime rate was higher under the Democratic governments or the Republican governments is (put number between 0 and 100):", max=100, min=0 ) reported_prior2 = models.FloatField( label="At first I believed that the probability is (put number between 0 and 100):", max=100, min=0 ) accuracy = models.IntegerField( choices=[[1, "the given accuracy of the news might be deceptive"], [2, "the given accuracy of the news is trustable"]], widget=widgets.RadioSelect, label="I believe that:" ) accuracy2 = models.FloatField( label="Considering this, I believe that the accuracy is:", max=100, min=0 ) reported_posterior = models.FloatField( label="Now I believe that the probability is (put number between 0 and 100):", max=100, min=0 ) comments = models.StringField( label="And I also consider the following facts:" ) comments2 = models.StringField( label="And I also consider the following facts:" ) assigned_group = models.IntegerField() # Functions def creating_session(subsession): subsession.group_randomly() if subsession.round_number == 1: for player in subsession.get_players(): player.session.signal = random.randint(0, 1) # numberList = [0, 1] # a = random.choices(numberList, weights=(C.PROB, 1 - C.PROB), k=1) # if a == 0: # player. = random.choices(numberList, weights=(C.ALPHA, 1 - C.ALPHA), k=1) # else: # b = random.choices(numberList, weights=(1 - C.ALPHA, C.ALPHA), k=1) # def set_payoffs(group: Group): # players = group.get_players() # feedback = [p.reported_prior1 for p in players] # group.feedback_prior = sum(feedback) # PAGES class Assignment(Page): def is_displayed(player): return player.round_number == 1 form_model = "player" def before_next_page(player, timeout_happened): player.assigned_group = random.randint(0, 1) class Assignment2(Page): def is_displayed(player): return player.round_number == 1 form_model = "player" @staticmethod def vars_for_template(player): group = 'You got admitted to Red University' x = player.assigned_group if x == 1: group = 'You got admitted to Blue University' return dict( group=group ) class Lottery1(Page): def is_displayed(player): return player.round_number == 1 form_model = "player" form_fields = ["bet1"] class Report1(Page): def is_displayed(player): return player.round_number == 1 form_model = "player" form_fields = ["reported_prior1", "comments"] def vars_for_template(player): x = player.bet1 bet1_choice = 'Democratic Governments' if not x: bet1_choice = 'Republican Governments' return dict( bet1_choice=bet1_choice ) class Partner1(Page): def is_displayed(player): return player.round_number == 1 # def vars_for_template(player): # return dict( # prior_others=Group.feedback_prior-player.reported_prior1 # ) class PublicSignal(Page): def is_displayed(player): return player.round_number == 1 class PublicSignal2(Page): def is_displayed(player): return player.round_number == 1 def vars_for_template(player): x = truestate(C.PROB) y = signal(x, C.ALPHA) pub_signal = 'Crime rate has been higher under Democratic Governments' if y == 1: pub_signal = 'Crime rate has been higher under Republican Governments' return dict( news=pub_signal ) class Report2(Page): def is_displayed(player): return player.round_number == 2 form_model = "player" form_fields = ["reported_prior2", "accuracy", "accuracy2", "reported_posterior", "comments2"] def vars_for_template(player): # x = player.bet1 x = player.in_round(1).bet1 bet1_choice = 'Democratic Government' if not x: bet1_choice = 'Republican Government' return dict( bet1_choice=bet1_choice ) class Partner2(Page): def is_displayed(player): return player.round_number == 2 def vars_for_template(player): x = 'Democratic Government' for p in player.get_others_in_group(): if not p.in_round(1).bet1: x = 'Republican Government' return dict( x=x ) # player.in_round(player.participant.task_rounds['L1']).lottery_choice1 class Lottery2(Page): def is_displayed(player): return player.round_number == 2 form_model = "player" form_fields = ["bet2"] class ResultsWaitPage(WaitPage): def is_displayed(player): return player.round_number == 1 class ResultsWaitPage2(WaitPage): def is_displayed(player): return player.round_number == 2 class Results(Page): form_model = "player" page_sequence = [Assignment, Assignment2, Lottery1, Report1, ResultsWaitPage, Partner1, PublicSignal, PublicSignal2, Report2, ResultsWaitPage2, Partner2, Lottery2] def truestate(prob): numberList = [0, 1] return random.choices(numberList, weights=(prob, 1 - prob), k=1) def signal(state, alpha): numberList = [0, 1] if state == 0: return random.choices(numberList, weights=(alpha, 1 - alpha), k=1) else: return random.choices(numberList, weights=(1 - alpha, alpha), k=1)