from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import itertools author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'correlation' players_per_group = None DKK = dict() char = dict() DKK['confound'] = dict() char['confound'] = dict() char['clean'] = dict() # option that is more skewed always comes second DKK['confound']['A'] = [[[90, 120, 96], [10, 0, 216]], [[10, 120, 216], [10, 0, 96], [80, 120, 96]]] DKK['confound']['B'] = [[[64, 135, 81], [36, 60, 156]], [[36, 135, 156], [36, 60, 81], [28, 135, 81]]] DKK['confound']['C'] = [[[90, 40, 32], [10, 0, 72]], [[80, 40, 32], [10, 40, 72], [10, 0, 32]]] DKK['confound']['D'] = [[[64, 45, 27], [36, 20, 52]], [[36, 45, 52], [28, 45, 27], [36, 20, 27]]] DKK['confound']['E'] = [[[90, 80, 64], [10, 0, 144]], [[80, 80, 64], [10, 80, 144], [10, 0, 64]]] DKK['confound']['F'] = [[[64, 90, 54], [36, 40, 104]], [[28, 90, 54], [36, 90, 104], [36, 40, 54]]] # # 5 ABC lotteries (2nd once is more relative skewed) + 3 state-wise dominant lotteries (second one is dominant) # DKK['confound']['G'] = [[[33, 20, 73], [33, 73, 64], [33, 64, 20]], [[33, 0, 120], [33, 33, 0], [33, 120, 33]]] # DKK['confound']['H'] = [[[33, 0, 101], [33, 101, 53], [33, 53, 0]], [[25, 0, 149], [25, 16, 0], [25, 50, 16], [25, 149, 50]]] # DKK['confound']['I'] = [[[25, 0, 120], [25, 20, 0], [25, 60, 20], [25, 120, 60]]] # # DKK['confound']['J'] = [[[30, 35, 40], [20, 30, 110], [50, 20, 45]], [[34, 31, 33], [66, 34, 87]]] # Allais common consequence (second one is risky (also more skewed) z_1_1 = 0 z_1_2 = 120 DKK['confound']['K'] = [[[1, 120, 0], [33, 120, 125], [66, z_1_1, z_1_1]], [[23, 120, 0], [11, 120, 125], [22, 0, 125], [44, 0, 0]]] DKK['confound']['L'] = [[[1, 120, 0], [33, 120, 125], [66, z_1_2, z_1_2]]] z_3_1 = 5 z_3_2 = 76 DKK['confound']['M'] = [[[5, 76, 5], [25, 76, 95], [70, z_3_1, z_3_1]], [[23, 76, 5], [7, 76, 95], [18, 5, 95], [52, 5, 5]]] DKK['confound']['N'] = [[[5, 76, 5], [25, 76, 95], [70, z_3_2, z_3_2]]] z_2_1 = 22 z_2_2 = 104 DKK['confound']['O'] = [[[10, 104, 22], [30, 104, 145], [60, z_2_1, z_2_1]], [[10, 104, 22], [30, 104, 145], [60, z_2_2, z_2_2]]] DKK['confound']['P'] = [[[28, 104, 22], [12, 104, 145], [18, 22, 145], [42, 22, 22]]] # Doninant and dominated lotteries. 1st one is state-wise, second option is dominant. second correlation is # more skewed. DKK['confound']['Q'] = [[[33, 71, 76], [33, 48, 53], [33, 12, 17]], [[33, 12, 76], [33, 71, 53], [33, 48, 17]]] DKK['confound']['R'] = [[[25, 104, 107], [25, 61, 64], [25, 18, 21], [25, 5, 8]], [[25, 5, 107], [25, 104, 64], [25, 61, 21], [25, 18, 8]]] DKK['clean'] = dict() DKK['clean']['A'] = [[[80, 120, 96], [10, 120, 96], [10, 0, 216]], [[10, 120, 216], [10, 0, 96], [80, 120, 96]]] DKK['clean']['B'] = [[[36, 135, 81], [28, 135, 81], [36, 60, 156]], [[36, 135, 156], [36, 60, 81], [28, 135, 81]]] DKK['clean']['C'] = [[[80, 40, 32], [10, 40, 32], [10, 0, 72]], [[80, 40, 32], [10, 40, 72], [10, 0, 32]]] DKK['clean']['D'] = [[[36, 45, 27], [28, 45, 27], [36, 20, 52]], [[36, 45, 52], [28, 45, 27], [36, 20, 27]]] DKK['clean']['E'] = [[[80, 80, 64], [10, 80, 64], [10, 0, 144]], [[80, 80, 64], [10, 80, 144], [10, 0, 64]]] DKK['clean']['F'] = [[[36, 90, 54], [28, 90, 54], [36, 40, 104]], [[28, 90, 54], [36, 90, 104], [36, 40, 54]]] # 5 ABC lotteries (2nd once is more relative skewed) + 3 state-wise dominant lotteries (second one is dominant) # DKK['clean']['G'] = [[[33, 9, 110], [33, 33, 9], [33, 110, 33]], [[33, 15, 101], [33, 41, 15], [33, 101, 41]]] # DKK['clean']['H'] = [[[33, 3, 86], [33, 50, 3], [33, 86, 50]], [[25, 7, 143], [25, 26, 7], [25, 32, 26], [25, 143, 32]]] # DKK['clean']['I'] = [[[25, 13, 94], [25, 37, 13], [25, 81, 37], [25, 94, 81]]] # # DKK['clean']['J'] = [[[30, 35, 40], [20, 30, 110], [50, 20, 45]], [[34, 31, 33], [66, 34, 87]]] # Allais common consequence z_1_1 = 0 z_1_2 = 120 DKK['clean']['K'] = [[[22, z_1_1, z_1_1], [22, 120, 125], [1, 120, 0], [11, 120, 125], [44, z_1_1, z_1_1]], [[22, z_1_1, 125], [22, 120, z_1_1], [1, 120, 0], [11, 120, 125], [44, z_1_1, z_1_1]]] DKK['clean']['L'] = [[[22, z_1_2, z_1_2], [22, 120, 125], [1, 120, 0], [11, 120, 125], [44, z_1_2, z_1_2]]] z_3_1 = 5 z_3_2 = 76 DKK['clean']['M'] = [[[5, 76, 5], [18, z_3_1, z_3_1], [52, z_3_1, z_3_1], [18, 76, 95], [7, 76, 95]], [[5, 76, 5], [18, z_3_1, 95], [52, z_3_1, z_3_1], [18, 76, z_3_1], [7, 76, 95]]] DKK['clean']['N'] = [[[5, 76, 5], [18, z_3_2, z_3_2], [52, z_3_2, z_3_2], [18, 76, 95], [7, 76, 95]]] z_2_1 = 22 z_2_2 = 104 DKK['clean']['O'] = [[[12, 104, 145], [18, 104, 145], [42, z_2_1, z_2_1], [18, z_2_1, z_2_1], [10, 104, 22]], [[12, 104, 145], [18, z_2_1, 145], [42, z_2_1, z_2_1], [18, 104, z_2_1], [10, 104, 22]]] DKK['clean']['P'] = [[[12, 104, 145], [18, 104, 145], [42, z_2_2, z_2_2], [18, z_2_2, z_2_2], [10, 104, 22]]] # Doninant and dominated lotteries. 1st one is state-wise, second option is dominant. second correlation is # more skewed. DKK['clean']['Q'] = [[[33, 71, 76], [33, 48, 53], [33, 12, 17]], [[33, 12, 76], [33, 71, 53], [33, 48, 17]]] DKK['clean']['R'] = [[[25, 104, 107], [25, 61, 64], [25, 18, 21], [25, 5, 8]], [[25, 5, 107], [25, 104, 64], [25, 61, 21], [25, 18, 8]]] # ABC lotteries with feedback. second one has higher relative skewness ABC = dict() ABC['B1'] = dict() ABC['B2'] = dict() # ABC['B1']['AF'] = [[33, 20, 73], [33, 73, 64], [33, 64, 20]] # ABC['B1']['BF'] = [[33, 0, 120], [33, 33, 0], [33, 120, 33]] # ABC['B1']['CF'] = [[33, 0, 101], [33, 101, 53], [33, 53, 0]] # ABC['B1']['DF'] = [[25, 0, 149], [25, 16, 0], [25, 50, 16], [25, 149, 50]] # ABC['B1']['EF'] = [[25, 0, 120], [25, 20, 0], [25, 60, 20], [25, 120, 60]] # # ABC['B2']['AF'] = [[33, 9, 110], [33, 33, 9], [33, 110, 33]] # ABC['B2']['BF'] = [[33, 15, 101], [33, 41, 15], [33, 101, 41]] # ABC['B2']['CF'] = [[33, 3, 86], [33, 50, 3], [33, 86, 50]] # ABC['B2']['DF'] = [[25, 7, 143], [25, 26, 7], [25, 32, 26], [25, 143, 32]] # ABC['B2']['EF'] = [[25, 13, 94], [25, 37, 13], [25, 81, 37], [25, 94, 81]] ABC['B1']['AA'] = [[[33, 20, 73], [33, 73, 64], [33, 64, 20]], [[33, 0, 120], [33, 33, 0], [33, 120, 33]]] ABC['B1']['BB'] = [[[33, 0, 101], [33, 101, 53], [33, 53, 0]], [[25, 0, 149], [25, 16, 0], [25, 50, 16], [25, 149, 50]]] ABC['B1']['CC'] = [[[25, 0, 120], [25, 20, 0], [25, 60, 20], [25, 120, 60]]] ABC['B2']['AA'] = [[[33, 9, 110], [33, 33, 9], [33, 110, 33]], [[33, 15, 101], [33, 41, 15], [33, 101, 41]]] ABC['B2']['BB'] = [[[33, 3, 86], [33, 50, 3], [33, 86, 50]], [[25, 7, 143], [25, 26, 7], [25, 32, 26], [25, 143, 32]]] ABC['B2']['CC'] = [[[25, 13, 94], [25, 37, 13], [25, 81, 37], [25, 94, 81]]] ABC_char = dict() for t in ('B1', 'B2'): ABC_char[t] = dict() j = 1 for i in ABC[t]: ABC_char[t][i] = list() ABC_char[t][i].append(('Same Marginal', 'Same Marginal')) ABC_char[t][i].append((j, j+1)) ABC_char[t][i].append((t, t)) ABC_char[t][i].append(('na', 'na')) j = j + 2 for t in ('clean', 'confound'): j = 1 for i in ('A', 'B', 'C', 'D', 'E', 'F'): char[t][i] = list() char[t][i].append(('DKK', 'DKK')) char[t][i].append((j, j)) char[t][i].append(('negative', 'positive')) char[t][i].append(('na', 'na')) j = j + 1 del j for i in ('K', 'L', 'M', 'N', 'O', 'P'): if i == 'K' or i == 'L': j = 1 elif i == 'M' or i == 'N': j = 2 else: j = 3 char[t][i] = list() char[t][i].append(('Allais', 'Allais')) char[t][i].append((j, j)) if i == 'K' or i == 'M' or i == 'O': char[t][i].append(('positive', 'negative')) char[t][i].append(('z=a_low', 'z=a_low')) else: char[t][i].append(('NA', 'NA')) char[t][i].append(('z=b', 'z=b')) j = j + 1 j = 1 for i in ('R', 'Q'): char[t][i] = list() char[t][i].append(('Dominant', 'Dominant')) char[t][i].append((j, j)) char[t][i].append(('positive', 'negative')) char[t][i].append(('state-wise', 'FOSD')) j = j+1 del j num_rounds_nf = 0 for i in DKK["clean"]: num_rounds_nf = num_rounds_nf+len(DKK["clean"][i]) num_rounds_f = 0 for i in ABC["B1"]: num_rounds_f = num_rounds_f + len(ABC["B1"][i]) num_rounds_f = num_rounds_f num_rounds_nf = num_rounds_nf + num_rounds_f num_rounds = num_rounds_f + num_rounds_nf def fields(pp): fields_to, fields_from = list(), list() a, b = 1, 0 for i in pp: fields_from.append(a) a = a + i b = b + i fields_to.append(b) return [fields_from, fields_to] def give_dict(a: list): b = "dict(" for i in range(len(a)): b = b + a[i] + '=' + a[i] + ',' b = b + ')' return b class Subsession(BaseSubsession): def creating_session(subsession): if subsession.round_number == 1: tt = ['confound', 'clean'] random.shuffle(tt) treatments = itertools.cycle(tt) for p in subsession.get_players(): treatment = next(treatments) block = ['B1', 'B2'] random.shuffle(block) import copy DKK = copy.deepcopy(Constants.DKK[treatment]) char = copy.deepcopy(Constants.char[treatment]) same = copy.deepcopy(Constants.ABC[block[0]]) same_char = copy.deepcopy(Constants.ABC_char[block[0]]) for i in same: DKK[i] = same[i] for i in same_char: char[i] = same_char[i] h1 = list() for i in range(1, len(DKK) + 1): h1.append(i) a1 = list() for d in DKK: a1.append(d) random.shuffle(a1) games = dict() features = dict() order = dict() count = 0 for h, d in zip(h1, a1): task_order = [0, 1] #random.shuffle(DKK[d]) random.shuffle(task_order) if len(DKK[d]) == 1: count = count + 1 task_order = [0, 1] a = h + len(h1) - count games[h] = dict() games[h] = DKK[d][task_order[0]] features[h] = list() order[d] = h features[h].append(char[d][0][task_order[0]]) features[h].append(char[d][1][task_order[0]]) features[h].append(char[d][2][task_order[0]]) features[h].append(char[d][3][task_order[0]]) if len(DKK[d]) == 2: features[a] = list() order[a] = d games[a] = DKK[d][task_order[1]] features[a].append(char[d][0][task_order[1]]) features[a].append(char[d][1][task_order[1]]) features[a].append(char[d][2][task_order[1]]) features[a].append(char[d][3][task_order[1]]) #add the feedback tasks in the end. ABC_1 = copy.deepcopy(Constants.ABC[block[1]]) ABC = dict() for i in ABC_1: s = ABC_1[i] c = 1 for j in s: z = i + str(c) c = c + 1 ABC[z] = j ABC_char = dict() j = 1 for i in ABC: ABC_char[i] = list() ABC_char[i].append('Feedback') ABC_char[i].append(j) ABC_char[i].append(block[1]) ABC_char[i].append('na') j = j + 1 h2 = list() for i in range(len(games) + 1, len(games) + len(ABC) + 1): h2.append(i) a2 = list() for d in ABC: a2.append(d) random.shuffle(a2) for h, d in zip(h2, a2): games[h] = dict() games[h] = ABC[d] features[h] = list() features[h] = ABC_char[d] # this randomizes the order of the states for i in games.keys(): random.shuffle(games[i]) # this randomizes whether the first or the second option appear first. #pos = [1, 2] #for k in games.keys(): #game = games[k] #copy = list() #for i in range(len(game)): #a = game[i][:] #copy.append(a) #random.shuffle(pos) #for i in range(len(game)): #game[i][pos[0]] = copy[i][1] #game[i][pos[1]] = copy[i][2] p.participant.vars['games'] = games p.participant.vars['features'] = features p.participant.vars['treatment'] = treatment p.participant.vars['ABC'] = ABC p.participant.vars['h2'] = h2 p.participant.vars['a2'] = a2 p.participant.vars['feedback_block'] = block[1] #create matrix with all game outcomes possible_payoffs_dict = dict() possible_fields_dict = dict() realized_field_dict = dict() import numpy for a in games: x_game = games[a] weights = list() population = list() for i in range(len(x_game)): weights.append(x_game[i][0]) population.append(i) h = random.choices( population=population, weights=weights )[0] possible_payoffs_dict[a] = x_game[h][1:] possible_fields_dict[a] = [Constants.fields(weights)[0][h], Constants.fields(weights)[1][h]] realized_field_dict[a] = numpy.random.randint(possible_fields_dict[a][0], possible_fields_dict[a][1] + 1) p.participant.vars['possible_payoffs_dict'] = possible_payoffs_dict p.participant.vars['realized_field_dict'] = realized_field_dict relevant = random.choice(list(games.keys())) #also make this into matrix possible_payoffs = possible_payoffs_dict[relevant] possible_fields = realized_field_dict[relevant] realized_field = realized_field_dict[relevant] p.participant.vars['payoff_relevant'] = relevant p.participant.vars['possible_payoff'] = possible_payoffs p.participant.vars['realized_field'] = realized_field p.participant.vars['choice_dict'] = dict() p.participant.vars['test_tries'] = 0 p.participant.vars['test_1'] = 0 p.participant.vars['test_2'] = 0 p.participant.vars['test_3'] = 0 p.treatment = treatment p.block_feedback = block[1] class Group(BaseGroup): pass class Player(BasePlayer): skewed = models.IntegerField( choices=[ [0, ''], [1, ''], ], widget=widgets.RadioSelectHorizontal, ) A_skewed = models.IntegerField() test_1 = models.IntegerField() test_2 = models.IntegerField() test_3 = models.IntegerField() test_tries = models.IntegerField() choice_dict = models.LongStringField() # features of choice part = models.StringField() lottery_nr = models.IntegerField() correlation = models.StringField() z = models.StringField() state_order = models.StringField() treatment = models.StringField() block_feedback = models.StringField() # questionnaire about decision making Columns = models.IntegerField( label=''' 1. 当在两个选项中做决策时, 我逐列比较了两个选项能够带来的收益 (1表示“非常不符合”, 而9表示“非常符合”)''', choices=[1, 2, 3, 4, 5, 6, 7, 8, 9], widget=widgets.RadioSelectHorizontal ) Rows = models.IntegerField( label=''' 2. 当在两个选项中做决策时, 我逐行单独看待每个选项 (1表示“非常不符合”, 而9表示“非常符合”)''', choices=[1, 2, 3, 4, 5, 6, 7, 8, 9], widget=widgets.RadioSelectHorizontal ) Probas = models.IntegerField( label=''' 3. 当在两个选项中做决策时, 我考虑了不同报酬发生的可能性 (1表示“非常不符合”, 而9表示“非常符合”)''', choices=[1, 2, 3, 4, 5, 6, 7, 8, 9], widget=widgets.RadioSelectHorizontal ) EV = models.IntegerField( label=''' 4. 当在两个选项中做决策时, 我计算了每个选项能够产生的期望报酬 (1表示“非常不符合”, 而9表示“非常符合”)。''', choices=[1, 2, 3, 4, 5, 6, 7, 8, 9], widget=widgets.RadioSelectHorizontal ) open_end = models.LongStringField(label=''' 5. 还有其他方面的因素影响您的决策吗?如果有,请在下方填写''', blank=True )