from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import csv import random author = 'Daphne Baldassari' doc = """ Group phase of the knowledge contribution study. Individuals are randomly assigned to a group of three at every odd round. They perform two rounds of 3 multiple choice questions with the same group and are being asked to elicit their beliefs. """ class Constants(BaseConstants): name_in_url = 'group_knowledge' players_per_group = 3 num_rounds = 10 with open('group_knowledge/GroupPhase_Task.csv') as questions_file: questions = list(csv.DictReader(questions_file)) paying_round_a = 0 paying_round_b = 0 payoff_if_null = c(0) class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: self.session.vars['questions'] = Constants.questions self.group_randomly() for p in self.get_players(): question_data = p.current_question_set() p.q_id1 = int(question_data[0]['q_id']) p.q_id2 = int(question_data[1]['q_id']) p.q_id3 = int(question_data[2]['q_id']) p.question1 = question_data[0]['question'] p.question2 = question_data[1]['question'] p.question3 = question_data[2]['question'] p.solution1 = question_data[0]['solution'] p.solution2 = question_data[1]['solution'] p.solution3 = question_data[2]['solution'] p.id_of_group = p.group.id_in_subsession choice_round = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] paying_round_a = random.choice(choice_round) self.session.vars['paying_round_a'] = paying_round_a print('set the group paying round for answers to', paying_round_a) # Paying Round for the beliefs (pick a different round than the round for answers) choice_round.remove(paying_round_a) # paying_round_a = random.randint(1, Constants.num_rounds) paying_round_b = random.choice(choice_round) self.session.vars['paying_round_b'] = paying_round_b print('set the group paying round for beliefs to', paying_round_b) elif self.round_number == 2: self.group_like_round(1) for p in self.get_players(): question_data = p.current_question_set() p.q_id1 = int(question_data[0]['q_id']) p.q_id2 = int(question_data[1]['q_id']) p.q_id3 = int(question_data[2]['q_id']) p.question1 = question_data[0]['question'] p.question2 = question_data[1]['question'] p.question3 = question_data[2]['question'] p.solution1 = question_data[0]['solution'] p.solution2 = question_data[1]['solution'] p.solution3 = question_data[2]['solution'] p.id_of_group = p.group.id_in_subsession elif self.round_number == 4: self.group_like_round(3) for p in self.get_players(): question_data = p.current_question_set() p.q_id1 = int(question_data[0]['q_id']) p.q_id2 = int(question_data[1]['q_id']) p.q_id3 = int(question_data[2]['q_id']) p.question1 = question_data[0]['question'] p.question2 = question_data[1]['question'] p.question3 = question_data[2]['question'] p.solution1 = question_data[0]['solution'] p.solution2 = question_data[1]['solution'] p.solution3 = question_data[2]['solution'] p.id_of_group = p.group.id_in_subsession elif self.round_number == 6: self.group_like_round(5) for p in self.get_players(): question_data = p.current_question_set() p.q_id1 = int(question_data[0]['q_id']) p.q_id2 = int(question_data[1]['q_id']) p.q_id3 = int(question_data[2]['q_id']) p.question1 = question_data[0]['question'] p.question2 = question_data[1]['question'] p.question3 = question_data[2]['question'] p.solution1 = question_data[0]['solution'] p.solution2 = question_data[1]['solution'] p.solution3 = question_data[2]['solution'] p.id_of_group = p.group.id_in_subsession elif self.round_number == 8: self.group_like_round(7) for p in self.get_players(): question_data = p.current_question_set() p.q_id1 = int(question_data[0]['q_id']) p.q_id2 = int(question_data[1]['q_id']) p.q_id3 = int(question_data[2]['q_id']) p.question1 = question_data[0]['question'] p.question2 = question_data[1]['question'] p.question3 = question_data[2]['question'] p.solution1 = question_data[0]['solution'] p.solution2 = question_data[1]['solution'] p.solution3 = question_data[2]['solution'] p.id_of_group = p.group.id_in_subsession elif self.round_number == 10: self.group_like_round(9) for p in self.get_players(): question_data = p.current_question_set() p.q_id1 = int(question_data[0]['q_id']) p.q_id2 = int(question_data[1]['q_id']) p.q_id3 = int(question_data[2]['q_id']) p.question1 = question_data[0]['question'] p.question2 = question_data[1]['question'] p.question3 = question_data[2]['question'] p.solution1 = question_data[0]['solution'] p.solution2 = question_data[1]['solution'] p.solution3 = question_data[2]['solution'] p.id_of_group = p.group.id_in_subsession elif self.round_number == 3: self.group_randomly() print(self.get_group_matrix()) for p in self.get_players(): question_data = p.current_question_set() p.q_id1 = int(question_data[0]['q_id']) p.q_id2 = int(question_data[1]['q_id']) p.q_id3 = int(question_data[2]['q_id']) p.question1 = question_data[0]['question'] p.question2 = question_data[1]['question'] p.question3 = question_data[2]['question'] p.solution1 = question_data[0]['solution'] p.solution2 = question_data[1]['solution'] p.solution3 = question_data[2]['solution'] p.id_of_group = p.group.id_in_subsession elif self.round_number == 5: self.group_randomly() for p in self.get_players(): question_data = p.current_question_set() p.q_id1 = int(question_data[0]['q_id']) print(p.q_id1) p.q_id2 = int(question_data[1]['q_id']) p.q_id3 = int(question_data[2]['q_id']) p.question1 = question_data[0]['question'] p.question2 = question_data[1]['question'] p.question3 = question_data[2]['question'] p.solution1 = question_data[0]['solution'] p.solution2 = question_data[1]['solution'] p.solution3 = question_data[2]['solution'] p.id_of_group = p.group.id_in_subsession elif self.round_number == 7: self.group_randomly() for p in self.get_players(): question_data = p.current_question_set() p.q_id1 = int(question_data[0]['q_id']) p.q_id2 = int(question_data[1]['q_id']) p.q_id3 = int(question_data[2]['q_id']) p.question1 = question_data[0]['question'] p.question2 = question_data[1]['question'] p.question3 = question_data[2]['question'] p.solution1 = question_data[0]['solution'] p.solution2 = question_data[1]['solution'] p.solution3 = question_data[2]['solution'] p.id_of_group = p.group.id_in_subsession else: self.group_randomly() for p in self.get_players(): question_data = p.current_question_set() p.q_id1 = int(question_data[0]['q_id']) p.q_id2 = int(question_data[1]['q_id']) p.q_id3 = int(question_data[2]['q_id']) p.question1 = question_data[0]['question'] p.question2 = question_data[1]['question'] p.question3 = question_data[2]['question'] p.solution1 = question_data[0]['solution'] p.solution2 = question_data[1]['solution'] p.solution3 = question_data[2]['solution'] p.id_of_group = p.group.id_in_subsession pass class Group(BaseGroup): group_answer1 = models.StringField() group_answer2 = models.StringField() group_answer3 = models.StringField() lowest_place_in_line1 = models.IntegerField() lowest_place_in_line2 = models.IntegerField() lowest_place_in_line3 = models.IntegerField() group_is_correct1 = models.BooleanField() group_is_correct2 = models.BooleanField() group_is_correct3 = models.BooleanField() def set_contributor(self): players = self.get_players() self.lowest_place_in_line1 = min([p.place_in_line1 for p in players]) self.lowest_place_in_line2 = min([p.place_in_line2 for p in players]) self.lowest_place_in_line3 = min([p.place_in_line3 for p in players]) players_with_lowest_place1 = [p for p in players if p.place_in_line1 == self.lowest_place_in_line1] players_with_lowest_place2 = [p for p in players if p.place_in_line2 == self.lowest_place_in_line2] players_with_lowest_place3 = [p for p in players if p.place_in_line3 == self.lowest_place_in_line3] contributor1 = random.choice( players_with_lowest_place1) # if tie, winner is chosen at random contributor2 = random.choice( players_with_lowest_place2) contributor3 = random.choice( players_with_lowest_place3) contributor1.is_contributor1 = True contributor2.is_contributor2 = True contributor3.is_contributor3 = True def pick_group_answer(self): players = self.get_players() for p in players: if p.is_contributor1: self.group_answer1 = p.submitted_answer1 if p.is_contributor2: self.group_answer2 = p.submitted_answer2 if p.is_contributor3: self.group_answer3 = p.submitted_answer3 def check_group_answer(self): p1 = self.get_player_by_id(1) self.group_is_correct1 = (self.group_answer1 == p1.solution1) self.group_is_correct2 = (self.group_answer2 == p1.solution2) self.group_is_correct3 = (self.group_answer3 == p1.solution3) def set_group_bonus(self): group_is_correct = [self.group_is_correct1, self.group_is_correct2, self.group_is_correct3] if self.subsession.round_number == self.session.vars['paying_round_a']: for p in self.get_players(): if p.payoff == c(0): print(p.payoff) print(group_is_correct.count(True)) p.payoff = c(1.25 * group_is_correct.count(True) - 0.75) print(p.payoff) else: p.payoff = p.payoff + c(1.25 * group_is_correct.count(True) - 0.75) pass def make_line(): return models.IntegerField(label='Choose your place in line from 1 to 5 :', min=1, max=5) class Player(BasePlayer): quiz_player1 = models.StringField(choices=['Player 1', 'Player 2', 'Player 3'], widget=widgets.RadioSelectHorizontal) quiz_player2 = models.StringField(choices=['Player 1', 'Player 2', 'Player 3'], widget=widgets.RadioSelectHorizontal) id_of_group = models.IntegerField() q_id1 = models.IntegerField() q_id2 = models.IntegerField() q_id3 = models.IntegerField() question1 = models.StringField() solution1 = models.StringField() question2 = models.StringField() solution2 = models.StringField() question3 = models.StringField() solution3 = models.StringField() submitted_answer1 = models.StringField(widget=widgets.RadioSelect) submitted_answer2 = models.StringField(widget=widgets.RadioSelect) submitted_answer3 = models.StringField(widget=widgets.RadioSelect) own_belief_number = models.IntegerField(min=0, max=3, label='Provide a guess on how many of your answers are correct :') place_in_line1 = make_line() place_in_line2 = make_line() place_in_line3 = make_line() is_correct1 = models.BooleanField() is_correct2 = models.BooleanField() is_correct3 = models.BooleanField() id_partner1 = models.IntegerField() id_partner2 = models.IntegerField() # gender = models.StringField( # choices=['Male', 'Female', 'Other'], # label='What is your gender?', # widget=widgets.RadioSelect # ) gender = models.StringField(initial='Female') gender_partner1 = models.StringField() gender_partner2 = models.StringField() is_contributor1 = models.BooleanField( initial=False, doc="""Indicates whether the player is the contributor of the question 1""" ) is_contributor2 = models.BooleanField( initial=False, doc="""Indicates whether the player is the contributor of the question 2""" ) is_contributor3 = models.BooleanField( initial=False, doc="""Indicates whether the player is the contributor of the question 2""" ) participant_vars_dump = models.StringField() belonging1 = models.StringField( label='Sometimes I feel that I could belong in the finance industry, and' ' sometimes I will that I could not belong in' ' the finance industry.', choices=['strongly disagree', 'disagree', 'moderately disagree', 'neutral', 'moderately agree', 'agree', 'strongly agree' ] ) belonging2 = models.StringField( label="If something bad happens in this finance study," "I would feel that I wouldn't belong in the finance industry.", choices=['strongly disagree', 'disagree', 'moderately disagree', 'neutral', 'moderately agree', 'agree', 'strongly agree' ] ) belonging3 = models.StringField( label="If something good happens in this finance study," "I would feel that I would belong in the finance industry.", choices=['strongly disagree', 'disagree', 'moderately disagree', 'neutral', 'moderately agree', 'agree', 'strongly agree' ] ) def current_question_set(self): self.session.vars['questions'] = Constants.questions[3 * (self.round_number - 1):3 * self.round_number] return self.session.vars['questions'] def check_correct(self): self.is_correct1 = (self.submitted_answer1 == self.solution1) self.is_correct2 = (self.submitted_answer2 == self.solution2) self.is_correct3 = (self.submitted_answer3 == self.solution3) def bsr_number(self, number, is_correct1, is_correct2, is_correct3): realized_value = sum([is_correct1, is_correct2, is_correct3]) guessed_value = number mse = (realized_value - guessed_value) ^ 2 k = random.uniform(0, 9) if mse < k: return 1 else: return 0 def set_payoff(self): self.payoff = c(0) if self.subsession.round_number == self.session.vars['paying_round_a']: is_correct = [self.is_correct1, self.is_correct2, self.is_correct3] self.payoff = self.payoff + + c(1 * is_correct.count(True)) if self.subsession.round_number == self.session.vars['paying_round_b']: self.payoff = c(self.bsr_number(self.own_belief_number, self.is_correct1, self.is_correct2, self.is_correct3 ) ) def get_other_partners_info(self): if (self.round_number % 2) == 1: for p in (self.subsession.get_players()): p.gender = p.participant.vars.get('gender') list_group = self.get_others_in_group() self.id_partner1 = list_group[0].id_in_group self.gender_partner1 = list_group[0].gender self.id_partner2 = list_group[1].id_in_group self.gender_partner2 = list_group[1].gender else: u = self.round_number self.id_partner1 = self.in_round(u - 1).id_partner1 self.gender_partner1 = self.in_round(u - 1).gender_partner1 self.id_partner2 = self.in_round(u - 1).id_partner2 self.gender_partner2 = self.in_round(u - 1).gender_partner2 pass