from otree.api import * import random import os import csv import numpy as np from otree.forms import widgets from otree.api import models doc = """ does communicating uncertainty improve trust """ class C(BaseConstants): PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 NAME_IN_URL = 'turst_uncertainty' JACKPOT = cu(100) GUESS_MAX = 100 class Subsession(BaseSubsession): pass class Group(BaseGroup): two_thirds_avg = models.FloatField() class Con(BaseConstants): form_model = 'player' form_fields = ['my_field'] #price1 cost 30*4 #price range cost 10*4-40*4 data = [ {'day': 'Mon', 'price1': 156, 'price_range1': [143, 174],'probability1': 73, 'prob_range1':[90, 30], 'price2': 186, 'price_range2': [178, 194],'probability2': 85, 'prob_range2':[92, 71],'price3': 137, 'probability3': 43}, {'day': 'Sun', 'price1': 169, 'price_range1': [159, 182],'probability1': 80, 'prob_range1':[91, 54], 'price2': 191, 'price_range2': [183, 198],'probability2': 85, 'prob_range2':[93, 74],'price3': 145, 'probability3': 60}, ] class Player(BasePlayer): # guess = models.IntegerField( # min=0, max=C.GUESS_MAX # ) instruction_version = models.StringField() player_version = models.StringField() cost_version_1 = models.IntegerField() cost_version_2 = models.IntegerField() cost_version_3 = models.IntegerField() cost_version_4 = models.IntegerField() #pic_version = models.IntegerField() #random_sequence = models.StringField() data_test1 = models.IntegerField(label="data_test1") data_1 = models.IntegerField(label="data_1") data_2 = models.IntegerField(label="data_2") data_3 = models.IntegerField(label="data_3") data_4 = models.IntegerField(label="data_4") pay_off = models.IntegerField(label="pay_off") booky = models.IntegerField(label="booky") round1 = models.StringField data_Mon_2 = models.IntegerField(label="Mon_2") experience_short_term_rental = models.StringField( label="Please select your experience with Short-Term Rentals (e.g., Airbnb):", choices=['Hosting', 'Renting', 'Both', 'None'], widget=widgets.RadioSelect ) education = models.StringField( label="What is your highest level of education completed?", choices=['Elementary School','High School', 'University Degree'], widget=widgets.RadioSelect ) experience_probability_statistics = models.StringField( label="Please indicate your experience with Probability and Statistics:", choices=['Completed a high school course', 'Completed a university course', 'None'], widget=widgets.RadioSelect ) numerical_skills = models.StringField( label="How would you rate your numerical skills?", choices=['Low', 'Medium', 'High'], widget=widgets.RadioSelect ) age_group = models.StringField( label="Please select your age group:", choices=['Under 18', '18-24', '25-34', '35-44', '45-54', '55 and over'], widget=widgets.RadioSelect ) ai = models.StringField( label="Did AI suggestion help you:", choices=['For all of the questions', 'For some of the questions', 'For none of the questions'], widget=widgets.RadioSelect ) howprice = models.StringField( label='Which of the followings did you use to select prices:', choices=['AI price suggestion only', 'Neighbors prices only', 'Your cleaning cost', 'AI suggestion and Neighbors prices', 'All of the above'], widget=widgets.RadioSelect ) # FUNCTIONS def set_payoffs(group: Group): players = group.get_players() guesses = [p.guess for p in players] two_thirds_avg = (2 / 3) * sum(guesses) / len(players) group.two_thirds_avg = round(two_thirds_avg, 2) group.best_guess = min(guesses, key=lambda guess: abs(guess - group.two_thirds_avg)) winners = [p for p in players if p.guess == group.best_guess] group.num_winners = len(winners) for p in winners: p.is_winner = True p.pay_off = C.JACKPOT / group.num_winners # PAGES class Welcome(Page): @staticmethod def is_displayed(player: Player): player.instruction_version = random.choice(['a', 'b', 'c','d','e']) #player.player_version = random.choice(['4bed1', '4bed2', '3bed1','3bed2', '2bed1', '2bed2', '1bed1', '1bed2']) player.player_version = random.choice(['4bed1']) #bed*30 player.cost_version_1 = random.randint(100, 140) player.cost_version_2 = random.randint(100, 140) player.cost_version_3 = random.randint(100, 140) player.cost_version_4 = random.randint(100, 140) return player.round_number == 1 class pricingdetails(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class demographic(Page): form_model = 'player' form_fields = [ 'experience_short_term_rental', 'education', 'experience_probability_statistics', 'numerical_skills', 'ai', 'howprice' ] def vars_for_template(self): return { # Define any variables to pass to the template here } class letspredict1(Page): form_model = 'player' form_fields = ['data_1'] def vars_for_template(player: Player): return {'data': Con.data} class letspredict2(Page): form_model = 'player' form_fields = ['data_2'] def vars_for_template(player: Player): return {'data': Con.data} class letspredict3(Page): form_model = 'player' form_fields = ['data_3'] def vars_for_template(player: Player): return {'data': Con.data} class letspredict4(Page): form_model = 'player' form_fields = ['data_4'] def vars_for_template(player: Player): return {'data': Con.data} class read(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class intro0(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class intr(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class nowreal(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class neighberhoud(Page): form_model = 'player' #form_fields = ['guess'] @staticmethod def vars_for_template(player: Player): imgg_pp=f'http://localhost:8000/static/{player.id_in_group}_plot.png' #plt.clf() #print(img_path) return { 'image_path': imgg_pp} class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs class test_pre(Page): form_model = 'player' form_fields = ['data_test1'] def is_displayed(player: Player): return player.round_number == 1 class test(Page): @staticmethod def vars_for_template(player: Player): form_model = 'player' mo = 2 price_all = [player.data_test1] #cost_all = [player.cost_version_1, player.cost_version_2, player.cost_version_3, player.cost_version_4] # Combine the lists into pairs # Choose a random pair price_selected = random.choice(price_all) cost_selected = player.cost_version_1 a = [-0.65,-0.45,-0.2,-0.25,-0.3,-0.4,-0.7,-0.9,-0.8,-0.45,-0.5,-0.7] b = [-3.7, -2.5] c = [-3.7, -2.5] month_fix = [-10,-10,60,-5,-5,-10,-10,-10,-5,20,-5,-10] housing_options = [ {"p": 180, "bath": 1, "review": 190, "entire": 1, "cleaningFee": 0, "maxGuest": 1, "minStay": 1, "ratings": 4.8}, {"p": 170, "bath": 1, "review": 190, "entire": 1, "cleaningFee": 0, "maxGuest": 2, "minStay": 1, "ratings": 4.4}, {"p": 250, "bath": 1, "review": 215, "entire": 1, "cleaningFee": 0, "maxGuest": 5, "minStay": 1, "ratings": 4.2}, {"p": 120, "bath": 1, "review": 250, "entire": 1, "cleaningFee": 0, "maxGuest": 3, "minStay": 1, "ratings": 4.7}, {"p": 90, "bath": 1, "review": 70, "entire": 0, "cleaningFee": 35, "maxGuest": 2, "minStay": 1, "ratings": 4.0}, {"p": 200, "bath": 3, "review": 70, "entire": 1, "cleaningFee": 50, "maxGuest": 1, "minStay": 1, "ratings": 4.1}, {"p": price_selected, "bath": 1, "review": 10, "entire": 1, "cleaningFee": 0, "maxGuest": 1, "minStay": 1, "ratings": 4.0} ] housing_count = len(housing_options) individual = 100 # Modify this as needed u_max = np.zeros(individual, dtype=int) # Lists to store month and event variables for each time period month_list = [] weekend_list = [] event_list = [] price_list=[] alpha = [] beta1 = [] gamma = [] gamma1 = [] zeta = [] bedno = [] zeta2 = [] house_n=housing_count sum_all_u= np.zeros( individual, dtype=int) # Reset available housing indices at the beginning of each time frame available_housing_indices = list(range(housing_count)) alpha.append([-0.1]*house_n) beta1.append([np.random.normal(0.185, 0.444, size=1)[0]]*house_n) gamma.append([np.random.normal(-0.093, 0.042, size=1)[0]]*house_n) gamma1.append([np.random.normal(+3.0, 0.051, size=1)[0]]*house_n) zeta.append([np.random.normal(0.101, 0.030, size=1)[0]]*house_n) bedno.append([np.random.normal(1.0, 0.071, size=1)[0]]*house_n) zeta2.append([np.random.normal(-0.019, 0.054, size=1)[0]]*house_n) # Randomly select one and only one element to be 1 in the dummy variables month = [0] * 12 weekend = [0, 0] event = [0, 0] month[mo] = 1 month_list.append([6]*house_n) weekend_list.append([np.argmax(weekend)]*house_n) event_list.append([np.argmax(event)]*house_n) for h1 in available_housing_indices: housing_options[h1]["p"] = 60*housing_options[h1]["maxGuest"] + 20*housing_options[h1]["ratings"] - 100 + month_fix[mo] housing_options[-1]["p"]=price_selected for indi in range(individual): u = [] for h in available_housing_indices: error = np.random.gumbel(0, 1, 1) housing = housing_options[h] ui = ( alpha[-1][0] * housing["p"] + gamma1[-1][0] * housing["ratings"] + bedno[-1][0] * housing["maxGuest"] ) + np.dot(a, month) + np.dot(b, weekend) + np.dot(c, event) + error u.append(ui) sum_all_u[indi] = (ui) u = np.array(u) if np.any(u > 0): max_index = np.argmax(u) selected_housing_index = available_housing_indices[max_index] u_max[indi] = selected_housing_index # Remove the selected housing from the available options available_housing_indices.pop(max_index) else: special_character = 100 # Replace with your desired special character u_max[indi] = special_character booking_info = np.zeros(housing_count, dtype=int) for indi in range(individual): booked_housing_index = u_max[indi] if booked_housing_index != 100: # Check if the housing was booked booking_info[booked_housing_index] = 1 # Set 1 for booked print(booking_info) if booking_info[-1] == 1: player.booky = 1 else: player.booky = 0 return {'round_n' : 1} class payment(Page): @staticmethod def vars_for_template(player: Player): form_model = 'player' mo = 0 price_all = [player.data_1, player.data_2, player.data_3, player.data_4] #cost_all = [player.cost_version_1, player.cost_version_2, player.cost_version_3, player.cost_version_4] # Combine the lists into pairs # Choose a random pair price_selected = random.choice(price_all) cost_selected = player.cost_version_1 a = [-0.65,-0.45,-0.2,-0.25,-0.3,-0.4,-0.7,-0.9,-0.8,-0.45,-0.5,-0.7] b = [-3.7, -2.5] c = [-3.7, -2.5] month_fix = [-10,-10,60,-5,-5,-10,-10,-10,-5,20,-5,-10] housing_options = [ {"p": 180, "bath": 1, "review": 190, "entire": 1, "cleaningFee": 0, "maxGuest": 4, "minStay": 1, "ratings": 4.0}, {"p": 170, "bath": 1, "review": 190, "entire": 1, "cleaningFee": 0, "maxGuest": 4, "minStay": 1, "ratings": 4.4}, {"p": 250, "bath": 1, "review": 215, "entire": 1, "cleaningFee": 0, "maxGuest": 3, "minStay": 1, "ratings": 4.2}, {"p": 120, "bath": 1, "review": 250, "entire": 1, "cleaningFee": 0, "maxGuest": 2, "minStay": 1, "ratings": 4.7}, {"p": 90, "bath": 1, "review": 170, "entire": 0, "cleaningFee": 35, "maxGuest": 5, "minStay": 1, "ratings": 4.0}, {"p": 200, "bath": 3, "review": 170, "entire": 1, "cleaningFee": 50, "maxGuest": 4, "minStay": 1, "ratings": 4.1}, {"p": 150, "bath": 2, "review": 170, "entire": 1, "cleaningFee": 10, "maxGuest": 6, "minStay": 1, "ratings": 4.2}, {"p": 100, "bath": 2, "review": 200, "entire": 1, "cleaningFee": 0, "maxGuest": 6, "minStay": 1, "ratings": 4.9}, {"p": price_selected, "bath": 1, "review": 100, "entire": 1, "cleaningFee": 0, "maxGuest": 4, "minStay": 1, "ratings": 4.2} ] housing_count = len(housing_options) individual = 100 # Modify this as needed u_max = np.zeros(individual, dtype=int) # Lists to store month and event variables for each time period month_list = [] weekend_list = [] event_list = [] price_list=[] alpha = [] beta1 = [] gamma = [] gamma1 = [] zeta = [] bedno = [] zeta2 = [] house_n=housing_count sum_all_u= np.zeros( individual, dtype=int) # Reset available housing indices at the beginning of each time frame available_housing_indices = list(range(housing_count)) alpha.append([-0.1]*house_n) beta1.append([np.random.normal(0.185, 0.444, size=1)[0]]*house_n) gamma.append([np.random.normal(-0.093, 0.042, size=1)[0]]*house_n) gamma1.append([np.random.normal(+1.5, 0.051, size=1)[0]]*house_n) zeta.append([np.random.normal(0.101, 0.030, size=1)[0]]*house_n) bedno.append([np.random.normal(2.6, 0.071, size=1)[0]]*house_n) zeta2.append([np.random.normal(-0.019, 0.054, size=1)[0]]*house_n) # Randomly select one and only one element to be 1 in the dummy variables month = [0] * 12 weekend = [0, 0] event = [0, 0] month[mo] = 1 month_list.append([6]*house_n) weekend_list.append([np.argmax(weekend)]*house_n) event_list.append([np.argmax(event)]*house_n) for h1 in available_housing_indices: housing_options[h1]["p"] = 60*housing_options[h1]["maxGuest"] + 20*housing_options[h1]["ratings"] - 100 + month_fix[mo] housing_options[-1]["p"]=price_selected for indi in range(individual): u = [] for h in available_housing_indices: error = np.random.gumbel(0, 1, 1) housing = housing_options[h] ui = ( alpha[-1][0] * housing["p"] + gamma1[-1][0] * housing["ratings"] + bedno[-1][0] * housing["maxGuest"] ) + np.dot(a, month) + np.dot(b, weekend) + np.dot(c, event) + error u.append(ui) sum_all_u[indi] = (ui) u = np.array(u) if np.any(u > 0): max_index = np.argmax(u) selected_housing_index = available_housing_indices[max_index] u_max[indi] = selected_housing_index # Remove the selected housing from the available options available_housing_indices.pop(max_index) else: special_character = 100 # Replace with your desired special character u_max[indi] = special_character #price_values = [housing_options[0]["p"]] price_values = [housing_options[0]["p"], housing_options[1]["p"], housing_options[2]["p"], housing_options[3]["p"], housing_options[4]["p"], housing_options[5]["p"], price_selected] price_list.append(price_values) booking_info = np.zeros(housing_count, dtype=int) for indi in range(individual): booked_housing_index = u_max[indi] if booked_housing_index != 100: # Check if the housing was booked booking_info[booked_housing_index] = 1 # Set 1 for booked if booking_info[-1] == 1 and price_selected >= cost_selected : print(price_selected) if (price_selected-cost_selected)/10 < 20: player.pay_off = 6 + round((price_selected-cost_selected)/10) else: player.pay_off = 6 + round((price_selected-cost_selected)/100) else: player.pay_off = 6 return {'round_n' : 1} page_sequence = [Welcome, read, intro0, intr, pricingdetails, test_pre, test, nowreal, letspredict1, letspredict2, letspredict3, letspredict4, demographic, payment]