from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random author = 'Thomas Graeber' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'survey_beliefs' players_per_group = None conditions = { # 'income': [ # (0, 1), # (9000, 5), # (14000, 10), # (30000, 25), # (62000, 50), # (111000, 75), # (179000, 90), # (236000, 95), # (434000, 99), # (100000000, 100) # ], # 'inflation': [ # (-0.4, 1), # (0.1, 5), # (1.5, 10), # (2.1, 25), # (2.85, 50), # (3.5, 75), # (4.8, 90), # (6.1, 95), # (10.3, 99) # ], 'stocks': [ (-38, 1), (-23, 5), (-10, 10), (0, 25), (12, 50), (23, 75), (27, 90), (31, 95), (34, 99) ], } num_rounds = len(conditions) class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: for p in self.get_players(): if 'failed_comprehension' not in p.participant.vars: p.participant.vars['failed_comprehension'] = False p.participant.vars['page_sequence'] = random.sample(Constants.conditions.keys(), Constants.num_rounds) p.participant.vars['paying_round'] = random.randint(1, Constants.num_rounds) for p in self.get_players(): p.set_task() if p.round_number == p.participant.vars['paying_round']: p.on_paying_round = True else: p.on_paying_round = False class Group(BaseGroup): pass class Player(BasePlayer): belief = models.FloatField() certainty = models.FloatField() task_type = models.StringField() task_specification = models.FloatField() paying_round = models.IntegerField() on_paying_round = models.BooleanField() looked_up_answer = models.BooleanField( label="", blank=False ) used_calc = models.BooleanField( label="", blank=False ) true_percentile = models.IntegerField() actual_draw_below_threshold = models.BooleanField() random_payoff = models.CurrencyField() expected_payoff = models.CurrencyField() squared_deviation = models.IntegerField() def set_task(self): setattr(self, 'task_type', self.participant.vars['page_sequence'][self.round_number-1]) setattr(self, 'task_specification', random.choice(Constants.conditions[self.task_type])[0]) setattr(self, 'true_percentile', [x[1] for x in Constants.conditions[self.task_type] if float(x[0]) == self.task_specification][0]) setattr(self, 'actual_draw_below_threshold', random.random() < self.true_percentile/100) # def set_payoff(self): # prize_lottery_q = random.randint(0, 2500) # squared_distance = (100 - self.belief) ** 2 if not self.actual_draw_below_threshold else self.belief ** 2 # self.squared_deviation = int(squared_distance) # self.random_payoff = squared_distance < prize_lottery_q # self.expected_payoff = max((2500-squared_distance)/2500, 0) # if self.on_paying_round: # possible_payoff = 0 # if squared_distance < prize_lottery_q: # possible_payoff += self.session.config['survey_prize'] # if 'possible_payoffs' in self.participant.vars: # self.participant.vars['possible_payoffs'].append(round(possible_payoff, 2)) # else: # self.participant.vars['possible_payoffs'] = [round(possible_payoff, 2)]