from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random import numpy as np import itertools author = 'Zheng Li' doc = """ Subjective uncertainty in risk """ class Constants(BaseConstants): name_in_url = 'risk_list' players_per_group = None probabilities = [50] amounts = [30, 40, 50] steps = 2 num_rounds = 3 # generate the certainty-uncertainty spectrum bar list1 = np.arange(1 * 20, - 1, -1) list2 = [] list2.append(0) for x in range(20): list2.append((x + 1) * 5) example_set = [(50, 50, 20, 0)] class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: for p in self.get_players(): lottery_list = list() for amount in Constants.amounts: probability = random.choice(Constants.probabilities) lottery = (probability, amount) lottery_list.append(lottery) p.participant.vars['lotteries'] = random.sample(lottery_list, Constants.num_rounds) for p in self.get_players(): p.set_current_task_number() p.participant.vars['tests_passed'] = True class Group(BaseGroup): pass class Player(BasePlayer): probability_1 = models.IntegerField() probability_2 = models.IntegerField() amount_1 = models.IntegerField() amount_2 = models.IntegerField() # Field which is 0 if always Option B, 1 if switching point, 2 if never option B indicator_never_always_switcher = models.IntegerField() switching_point = models.FloatField() confidence = models.FloatField() def set_current_task_number(self): setattr(self, 'probability_1', self.participant.vars['lotteries'][self.round_number - 1][0]) setattr(self, 'probability_2', 100 - self.participant.vars['lotteries'][self.round_number - 1][0]) setattr(self, 'amount_1', self.participant.vars['lotteries'][self.round_number - 1][1]) setattr(self, 'amount_2', 0) # set player.current_choice def current_choice(self): iet_choice = [(self.probability_1, self.probability_2, self.amount_1, self.amount_2)] return iet_choice # determine number of table rows in price list page def table_length1(self): return abs(self.amount_1)/Constants.steps + 1 # generate the list of earlier payments for option B in price list page def frange(self, start, stop, step): i = start + step while i < stop: if isinstance(i, int): yield i else: yield round(i, 2) i += step # display the list of earlier payments for option B in price list page def right_side_amounts1(self): lst = self.frange(self.amount_2, self.amount_1 + 1, Constants.steps) return list(enumerate(lst, 1)) def range_bounds(self): bounds = [self.amount_2, self.amount_1] return bounds def set_switching_point_and_indicator(self): if self.switching_point == 9999: # the template sets it to be 9999 when the player always chooses option A self.switching_point = self.amount_1 + Constants.steps self.indicator_never_always_switcher = 2 elif self.switching_point == Constants.steps: # the template sets it to be 0 when the player always chooses option B self.indicator_never_always_switcher = 0 else: # when a middle point is chosen (what is desired) self.indicator_never_always_switcher = 1