from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import csv import random author = 'Daphne Baldassari' doc = """ Individual phase of the knowledge contribution study. Individuals answer multiple choice questions and elicit their beliefs. """ class Constants(BaseConstants): name_in_url = 'ind_knowledge' players_per_group = None num_rounds = 2 with open('ind_knowledge/IndividualPhase_Task.csv') as questions_file: questions = list(csv.DictReader(questions_file)) # randomized_questions = random.sample(questions, len(questions)) 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 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.q_id4 = int(question_data[3]['q_id']) p.q_id5 = int(question_data[4]['q_id']) p.question1 = question_data[0]['question'] p.question2 = question_data[1]['question'] p.question3 = question_data[2]['question'] p.question4 = question_data[3]['question'] p.question5 = question_data[4]['question'] p.solution1 = question_data[0]['solution'] p.solution2 = question_data[1]['solution'] p.solution3 = question_data[2]['solution'] p.solution4 = question_data[3]['solution'] p.solution5 = question_data[4]['solution'] # Paying Round for the answers paying_round_a = random.randint(1, Constants.num_rounds) # Paying Round for the beliefs if paying_round_a == 1: paying_round_b = 2 else: paying_round_b = 1 self.session.vars['paying_round_a'] = paying_round_a print('set the paying round for answers to', paying_round_a) self.session.vars['paying_round_b'] = paying_round_b print('set the paying round for beliefs to', paying_round_b) # self.session.vars['questions'] = Constants.randomized_questions[0:4] if self.round_number == 2: self.session.vars['questions'] = Constants.questions 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.q_id4 = int(question_data[3]['q_id']) p.q_id5 = int(question_data[4]['q_id']) p.question1 = question_data[0]['question'] p.question2 = question_data[1]['question'] p.question3 = question_data[2]['question'] p.question4 = question_data[3]['question'] p.question5 = question_data[4]['question'] p.solution1 = question_data[0]['solution'] p.solution2 = question_data[1]['solution'] p.solution3 = question_data[2]['solution'] p.solution4 = question_data[3]['solution'] p.solution5 = question_data[4]['solution'] pass class Group(BaseGroup): pass def make_belief(): return models.FloatField(label='Confidence that my answer is correct (in %):', initial=0, widget=widgets.Slider(attrs={'step': '1', 'min': 0, 'max': 100}, show_value=True) ) class Player(BasePlayer): q_id1 = models.IntegerField() q_id2 = models.IntegerField() q_id3 = models.IntegerField() q_id4 = models.IntegerField() q_id5 = models.IntegerField() question1 = models.StringField() solution1 = models.StringField() question2 = models.StringField() solution2 = models.StringField() question3 = models.StringField() solution3 = models.StringField() question4 = models.StringField() solution4 = models.StringField() question5 = models.StringField() solution5 = models.StringField() submitted_answer1 = models.StringField(widget=widgets.RadioSelect) submitted_answer2 = models.StringField(widget=widgets.RadioSelect) submitted_answer3 = models.StringField(widget=widgets.RadioSelect) submitted_answer4 = models.StringField(widget=widgets.RadioSelect) submitted_answer5 = models.StringField(widget=widgets.RadioSelect) belief_answer1 = make_belief() belief_answer2 = make_belief() belief_answer3 = make_belief() belief_answer4 = make_belief() belief_answer5 = make_belief() is_correct1 = models.BooleanField() is_correct2 = models.BooleanField() is_correct3 = models.BooleanField() is_correct4 = models.BooleanField() is_correct5 = models.BooleanField() pre_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' ] ) pre_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' ] ) pre_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): if self.round_number == 1: self.session.vars['questions'] = Constants.questions[0:5] return self.session.vars['questions'] if self.round_number == 2: self.session.vars['questions'] = Constants.questions[5:10] 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) self.is_correct4 = (self.submitted_answer4 == self.solution4) self.is_correct5 = (self.submitted_answer5 == self.solution5) def bsr(self, b, is_correct): if is_correct: mse = (1 - b / 100) ** 2 else: mse = (b / 100) ** 2 k = random.uniform(0, 1) if mse < k: return 1 else: return 0 def set_payoff(self): if self.subsession.round_number == self.session.vars['paying_round_a']: is_correct = [self.is_correct1, self.is_correct2, self.is_correct3, self.is_correct4, self.is_correct5] self.payoff = self.payoff + c(is_correct.count(True)*1) if self.subsession.round_number == self.session.vars['paying_round_b']: self.payoff = self.payoff + c(self.bsr(self.belief_answer1, self.is_correct1)*1) self.payoff = self.payoff + c(self.bsr(self.belief_answer2, self.is_correct2)*1) self.payoff = self.payoff + c(self.bsr(self.belief_answer3, self.is_correct3)*1) self.payoff = self.payoff + c(self.bsr(self.belief_answer4, self.is_correct4)*1) self.payoff = self.payoff + c(self.bsr(self.belief_answer5, self.is_correct5)*1) pass