from otree.api import *
import random
class C(BaseConstants):
NAME_IN_URL = 'survey'
PLAYERS_PER_GROUP = None
NUM_ROUNDS = 1
question_correct_answers = {
'one': 'More',
'two': 'Exactly',
'three': 'Never',
'four': 'Lost',
'five': 29,
'six': 20,
}
class Subsession(BaseSubsession):
pass
class Group(BaseGroup):
pass
class Player(BasePlayer):
q_one = models.StringField(
choices=[['More', 'More than €102'],
['Exactly', 'Exactly €102'],
['Less', 'Less than €102']
],
label='Question 1.
Suppose you had €100 in a savings account and the interest rate is 2% per year. '
'How much '
'would you have in the account after 5 years if you would leave the money to grow?',
widget=widgets.RadioSelect,
)
guess1_one = models.IntegerField(
label='1st Guess: What percentage of '
'the participants in this '
'experiment do you think '
'answered Question 1 correctly? ',
min=0,
max=100
)
guess2_one = models.IntegerField(
label='2nd Guess : Consider a randomly chosen participant in this session. '
'What do you think is the 1st guess of this participant? ',
min=0,
max=100
)
q_two = models.StringField(
choices=[['More', 'More than today'],
['Exactly', 'Exactly the same'],
['Less', 'Less than today']
],
label='
Question 2.
Suppose that in the year 2025, your income has doubled and prices of all '
'goods have doubled too. In 2025, how much will you be able to buy with your income?',
widget=widgets.RadioSelect,
)
guess1_two = models.IntegerField(
label='1st Guess: '
'What percentage of the participants in this experiment do you believe answered Question 2 correctly? ',
min=0,
max=100
)
guess2_two = models.IntegerField(
label='2nd Guess : Consider a randomly chosen participant in this session. '
'What do you think is the 1st guess of this participant? ',
min=0,
max=100
)
q_three = models.StringField(
choices=[['Less', 'Less than 5 years'],
['Between 5-10', 'Between 5 and 10 years'],
['Between 10-15', 'Between 10 and 15 years'],
['Never', 'Never, continue to be in debt']
],
label='
Question 3.
You owe €3,000 on your credit card. You pay a minimum of €30 each '
'month. At an annual interest rate of 12% (or 1% per month), how many years would it take '
'to eliminate your credit card debt if you make no additional new charges?',
widget=widgets.RadioSelect,
)
guess1_three = models.IntegerField(
label='1st Guess: '
'What percentage of the participants in this experiment do you think answered Question 3 correctly? ',
min=0,
max=100
)
guess2_three = models.IntegerField(
label='2nd Guess : Consider a randomly chosen participant in this session. '
'What do you think is the 1st guess of this participant? ',
min=0,
max=100
)
q_four = models.StringField(
choices=[['Even', 'Has broken even in the stock market'],
['Ahead', 'Is ahead of where he began'],
['Lost', 'Has lost money']
],
label='
Question 4.
Simon decided to invest €8,000 in the stock market one day early in 2008. '
'Six months after he invested, on July 17, the stocks he had purchased were down 50%. Fortunately '
'for Simon, from July 17 to October 17, the stocks he had purchased went up 75%. At this point, '
'Simon:',
widget=widgets.RadioSelect,
)
guess1_four = models.IntegerField(
label='1st Guess: What percentage of the participants in this '
'experiment do you think answered Question 4 correctly? ',
min=0,
max=100
)
guess2_four = models.IntegerField(
label='2nd Guess : Consider a randomly chosen participant in this session. '
'What do you think is the 1st guess of this participant? ',
min=0,
max=100
)
q_five = models.IntegerField(
label='
Question 5.
Jerry received both the 15th highest and the 15th lowest '
'mark in the class. How many students are in the class?',
)
guess1_five = models.IntegerField(
label='1st Guess : What percentage of '
'the participants in '
'this experiment do you think answered Question 5 correctly? ',
min=0,
max=100
)
guess2_five = models.IntegerField(
label='2nd Guess : Consider a randomly chosen participant in this session. '
'What do you think is the 1st guess of this participant? ',
min=0,
max=100
)
q_six = models.IntegerField(
label='
Question 6.
A man buys a pig for €60, sells it for €70, buys it back for €80, '
'and sells it finally for €90. How much has he made?',
)
guess1_six = models.IntegerField(
label='1st Guessing Task: What percentage of the participants in this '
'experiment do you think answered Question 6 correctly? ',
min=0,
max=100
)
guess2_six = models.IntegerField(
label='2nd Guess : Consider a randomly chosen participant in this session. '
'What do you think is the 1st guess of this participant? ',
min=0,
max=100
)
bonus_guess = models.IntegerField(
label='How many out of the 6 questions do you think you answered correctly (a number between 0 and 6)?',
min=0,
max=6
)
payoff_q_one = models.FloatField()
payoff_guess1_one = models.FloatField()
payoff_guess2_one = models.FloatField()
correct_one = models.FloatField()
partner_id_one = models.IntegerField()
partner_guess1_one = models.IntegerField()
payoff_q_two = models.FloatField()
payoff_guess1_two = models.FloatField()
payoff_guess2_two = models.FloatField()
correct_two = models.FloatField()
partner_id_two = models.IntegerField()
partner_guess1_two = models.IntegerField()
payoff_q_three = models.FloatField()
payoff_guess1_three = models.FloatField()
payoff_guess2_three = models.FloatField()
correct_three = models.FloatField()
partner_id_three = models.IntegerField()
partner_guess1_three = models.IntegerField()
payoff_q_four = models.FloatField()
payoff_guess1_four = models.FloatField()
payoff_guess2_four = models.FloatField()
correct_four = models.FloatField()
partner_id_four = models.IntegerField()
partner_guess1_four = models.IntegerField()
payoff_q_five = models.FloatField()
payoff_guess1_five = models.FloatField()
payoff_guess2_five = models.FloatField()
correct_five = models.FloatField()
partner_id_five = models.IntegerField()
partner_guess1_five = models.IntegerField()
payoff_q_six = models.FloatField()
payoff_guess1_six = models.FloatField()
payoff_guess2_six = models.FloatField()
correct_six = models.FloatField()
partner_id_six = models.IntegerField()
partner_guess1_six = models.IntegerField()
payoff_bonus_guess = models.FloatField()
num_correct_answers = models.FloatField()
total_payoff_questions = models.FloatField()
total_payoff_survey = models.FloatField()
total_payoff_guesses = models.FloatField()
def compute_payoffs_for_question(self, question_number, correct_answer, guess1, guess2):
#all_players = []
#for subsession in self.session.get_subsessions():
# all_players.extend(subsession.get_players())
all_players = self.subsession.get_players()
other_players = [player for player in all_players if player.participant.id != self.participant.id]
num_correct = sum([getattr(p, f"q_{question_number}") == correct_answer for p in other_players])
percentage_correct = (num_correct / len(other_players))
payoff_guess1 = round((1 - percentage_correct*(1-guess1/100)**2 - (1-percentage_correct)*(guess1/100)**2) * 250)
partner = random.choice(other_players)
partner_id = partner.id_in_group
partner_guess1 = getattr(partner, f"guess1_{question_number}")
payoff_guess2 = round((1 - (partner_guess1/100)*(1-guess2/100)**2 - (1-(partner_guess1/100))*(guess2/100)**2) * 250)
percentage_correct = round(percentage_correct * 100)
if getattr(self, f"q_{question_number}") == correct_answer:
payoff_q = 250
else:
payoff_q = 0
total_payoff_survey = payoff_q + payoff_guess1 + payoff_guess2
# Set the computed payoffs and other variables as attributes
setattr(self, f"payoff_q_{question_number}", payoff_q)
setattr(self, f"payoff_guess1_{question_number}", payoff_guess1)
setattr(self, f"payoff_guess2_{question_number}", payoff_guess2)
setattr(self, f"correct_{question_number}", percentage_correct)
setattr(self, f"partner_id_{question_number}", partner_id)
setattr(self, f"partner_guess1_{question_number}", partner_guess1)
def compute_payoffs(self):
total_payoff_survey = 0
all_questions_payoff = 0
for question_number, correct_answer in C.question_correct_answers.items():
guess1 = getattr(self, f"guess1_{question_number}")
guess2 = getattr(self, f"guess2_{question_number}")
self.compute_payoffs_for_question(question_number, correct_answer, guess1, guess2)
question_total_payoff = getattr(self, f"payoff_q_{question_number}") + \
getattr(self, f"payoff_guess1_{question_number}") + \
getattr(self, f"payoff_guess2_{question_number}")
only_questions_payoff = getattr(self, f"payoff_q_{question_number}")
total_payoff_survey += question_total_payoff
all_questions_payoff += only_questions_payoff
self.total_payoff_questions = all_questions_payoff
self.total_payoff_survey = total_payoff_survey
self.total_payoff_guesses = total_payoff_survey - self.total_payoff_questions
def compute_bonus_payoff(self):
num_correct_answers = sum([getattr(self, f"q_{question_number}") == correct_answer
for question_number, correct_answer in C.question_correct_answers.items()])
self.num_correct_answers = round((num_correct_answers / len(C.question_correct_answers)) * 100)
self.payoff_bonus_guess = round((1 - (self.num_correct_answers / 100) * (1 - self.bonus_guess / 6) ** 2 -
(1 - self.num_correct_answers / 100) * (self.bonus_guess / 6) ** 2) * 300)
self.total_payoff_survey += self.payoff_bonus_guess
self.participant.vars['survey'] = self.total_payoff_survey
class PartOneBegins(Page):
pass
class Quiz(Page):
form_model = 'player'
form_fields = [
'q_one', 'guess1_one', 'guess2_one',
'q_two', 'guess1_two', 'guess2_two',
'q_three', 'guess1_three', 'guess2_three',
'q_four', 'guess1_four', 'guess2_four',
'q_five', 'guess1_five', 'guess2_five',
'q_six', 'guess1_six', 'guess2_six',
]
class SessionResultsWaitPage(WaitPage):
wait_for_all_groups = True
@staticmethod
def after_all_players_arrive(subsession: Subsession):
for player in subsession.get_players():
player.compute_payoffs()
player.compute_bonus_payoff()
class BonusQuestion(Page):
form_model = 'player'
form_fields = ['bonus_guess']
class ShowResults(Page):
def vars_for_template(player: Player):
return {
'payoff_questions': player.total_payoff_questions,
'payoff_guesses': player.total_payoff_guesses,
'payoff_bonus': player.payoff_bonus_guess,
'total_payoff_survey': player.total_payoff_survey
}
class PartOneEnded(Page):
pass
page_sequence = [PartOneBegins,
Quiz,
BonusQuestion,
SessionResultsWaitPage,
PartOneEnded
]