from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random author = 'Nishtha' doc = """ Treatment Beta and Bias Change - A gets 10% of Round 2 payoff. When A'a bid multiplier = 3 and B's bid multiplier = 1 in period 1 but can change in period 2 depending on who wins in period 1". """ class Constants(BaseConstants): name_in_url = 'TreatmentBetaBias_grad' players_per_group = 2 num_rounds = 8 endowment = 100 A_multiplier = 3 B_multiplier = 1 Prize = 100 class Subsession(BaseSubsession): def creating_session(self): self.group_randomly(fixed_id_in_group=True) class Group(BaseGroup): total_investment = models.FloatField() effective_investment_A = models.FloatField() effective_investment_B = models.FloatField() total_effective_investment = models.FloatField() pwin_A = models.FloatField() pwin_B = models.FloatField() chance_A = models.FloatField() chance_B = models.FloatField() Winner_A = models.BooleanField() guess_A = models.BooleanField() guess_B = models.BooleanField() def set_payoffs_1(self): A = self.get_player_by_id(1) B = self.get_player_by_id(2) self.effective_investment_A = Constants.A_multiplier * A.investment self.effective_investment_B = B.investment self.total_investment = A.investment + B.investment self.total_effective_investment = self.effective_investment_A + self.effective_investment_B if self.total_effective_investment > 0: self.pwin_A = (self.effective_investment_A / self.total_effective_investment) self.pwin_B = (self.effective_investment_B / self.total_effective_investment) else: self.pwin_A = 0.75 self.pwin_B = 0.25 if random.random() < self.pwin_A: self.Winner_A = True self.chance_A = round(100 * self.pwin_A, 2) self.chance_B = round(100 * self.pwin_B, 2) if A.Perceived_investment < (B.investment - 2) or A.Perceived_investment > (B.investment + 2): self.guess_A = False else: self.guess_A = True if B.Perceived_investment < (A.investment - 2) or B.Perceived_investment > (A.investment + 2): self.guess_B = False else: self.guess_B = True if self.guess_A: A.is_guess = True else: A.is_guess = False if self.guess_B: B.is_guess = True else: B.is_guess = False for player in [A, B]: if player.is_guess: player.guesspayoff1 = 25 else: player.guesspayoff1 = 0 if self.Winner_A: A.is_winner = True B.is_winner = False else: A.is_winner = False B.is_winner = True for player in [A, B]: if player.is_winner: player.payoff1 = 200 - player.investment else: player.payoff1 = 100 - player.investment for player in [A, B]: player.Totalpayoff1 = player.payoff1 + player.guesspayoff1 effective_investment_A_2 = models.FloatField() effective_investment_B_2 = models.FloatField() total_effective_investment_2 = models.FloatField() pwin_A_2 = models.FloatField() pwin_B_2 = models.FloatField() chance_A_2 = models.FloatField() chance_B_2 = models.FloatField() Winner_A_2 = models.BooleanField() guess_A_2 = models.BooleanField() guess_B_2 = models.BooleanField() def set_payoffs_2(self): A = self.get_player_by_id(1) B = self.get_player_by_id(2) if self.Winner_A: self.effective_investment_A_2 = Constants.A_multiplier * A.investment_2 else: self.effective_investment_A_2 = A.investment_2 self.effective_investment_B_2 = B.investment_2 self.total_effective_investment_2 = self.effective_investment_A_2 + self.effective_investment_B_2 self.pwin_A_2 = (self.effective_investment_A_2 / self.total_effective_investment_2) self.pwin_B_2 = (self.effective_investment_B_2 / self.total_effective_investment_2) if random.random() < self.pwin_A_2: self.Winner_A_2 = True self.chance_A_2 = round(100 * self.pwin_A_2, 2) self.chance_B_2 = round(100 * self.pwin_B_2, 2) if A.Perceived_investment_2 < (B.investment_2 - 2) or A.Perceived_investment_2 > (B.investment_2 + 2): self.guess_A_2 = False else: self.guess_A_2 = True if B.Perceived_investment_2 < (A.investment_2 - 2) or B.Perceived_investment_2 > (A.investment_2 + 2): self.guess_B_2 = False else: self.guess_B_2 = True if self.guess_A_2: A.is_guess_2 = True else: A.is_guess_2 = False if self.guess_B_2: B.is_guess_2 = True else: B.is_guess_2 = False for player in [A, B]: if player.is_guess_2: player.guesspayoff2 = 25 else: player.guesspayoff2 = 0 if self.Winner_A_2: A.is_winner_2 = True B.is_winner_2 = False else: A.is_winner_2 = False B.is_winner_2 = True for player in [A, B]: if player.is_winner_2: player.payoff2 = 200 - player.investment_2 else: player.payoff2 = 100 - player.investment_2 for player in [A, B]: player.Totalpayoff2 = player.payoff2 + player.guesspayoff2 def set_payoffs_total(self): A = self.get_player_by_id(1) B = self.get_player_by_id(2) for player in [A, B]: A.payoff_total = A.Totalpayoff1 + (0.1 * player.Totalpayoff2) B.payoff_total = B.Totalpayoff1 + (5 * player.Totalpayoff2) player.in_round(7).payoff = player.in_round(7).payoff_total player.in_round(1).payoff = 0 player.in_round(2).payoff = 0 player.in_round(3).payoff = 0 player.in_round(4).payoff = 0 player.in_round(5).payoff = 0 player.in_round(6).payoff = 0 player.in_round(8).payoff = 0 class Player(BasePlayer): investment = models.FloatField( min=0, max=Constants.endowment, doc="""Bid in contest""" ) Perceived_investment = models.FloatField( min=0, max=Constants.endowment, doc="""Perceived bid of other player in contest""" ) payoff1 = models.CurrencyField() payoff2 = models.CurrencyField() payoff_total = models.CurrencyField() guesspayoff1 = models.CurrencyField() guesspayoff2 = models.CurrencyField() Totalpayoff1 = models.CurrencyField() Totalpayoff2 = models.CurrencyField() investment_2 = models.FloatField( min=0, max=Constants.endowment, doc="""Bid in Pd2""" ) Perceived_investment_2 = models.FloatField( min=0, max=Constants.endowment, doc="""Perceived bid of other player in Pd2""" ) rounds = models.IntegerField() periods = models.IntegerField() slidera = models.IntegerField() sliderb = models.IntegerField() codepena = models.IntegerField() codepenc = models.IntegerField() codepenf = models.IntegerField() demo1 = models.IntegerField() demo2 = models.IntegerField() is_winner = models.BooleanField() is_winner_2 = models.BooleanField() is_guess = models.BooleanField() is_guess_2 = models.BooleanField() def role(self): if self.id_in_group == 1: return 'A' if self.id_in_group == 2: return 'B' def other_player(self): return self.get_others_in_group()[0]