from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random import math author = 'Benjamin Pichl' doc = """ Insurance Choice Experiment for SFB Credence Goods at the UIBK. ENDO treatment. """ class Constants(BaseConstants): name_in_url = 'insurance_choice_endo' players_per_group = 2 num_rounds = 10 initial_endowment = c(80) payoff_major_th = c(130) payoff_major_tl = c(110) payoff_minor = c(80) cost_hqt = c(60) cost_lqt = c(20) outside_option = c(30) q1 = [14.45, 55] q2 = [17.8, 55] q3 = [6.8, 30] q4 = [28.75, 70] class Subsession(BaseSubsession): def creating_session(self): if len(self.get_players()) == 24: grouping_matrix_rnd1_g1 = [[1, 2], [3, 6], [5, 4], [7, 8]] grouping_matrix_rnd1_g2 = [[x + 8 for x in grouping_matrix_rnd1_g1[0]], [x + 8 for x in grouping_matrix_rnd1_g1[1]], [x + 8 for x in grouping_matrix_rnd1_g1[2]], [x + 8 for x in grouping_matrix_rnd1_g1[3]]] grouping_matrix_rnd1_g3 = [[x + 16 for x in grouping_matrix_rnd1_g1[0]], [x + 16 for x in grouping_matrix_rnd1_g1[1]], [x + 16 for x in grouping_matrix_rnd1_g1[2]], [x + 16 for x in grouping_matrix_rnd1_g1[3]]] grouping_matrix_rnd2_g1 = [[1, 4], [3, 8], [5, 2], [7, 6]] grouping_matrix_rnd2_g2 = [[x + 8 for x in grouping_matrix_rnd2_g1[0]], [x + 8 for x in grouping_matrix_rnd2_g1[1]], [x + 8 for x in grouping_matrix_rnd2_g1[2]], [x + 8 for x in grouping_matrix_rnd2_g1[3]]] grouping_matrix_rnd2_g3 = [[x + 16 for x in grouping_matrix_rnd2_g1[0]], [x + 16 for x in grouping_matrix_rnd2_g1[1]], [x + 16 for x in grouping_matrix_rnd2_g1[2]], [x + 16 for x in grouping_matrix_rnd2_g1[3]]] grouping_matrix_rnd3_g1 = [[1, 6], [3, 2], [5, 8], [7, 4]] grouping_matrix_rnd3_g2 = [[x + 8 for x in grouping_matrix_rnd3_g1[0]], [x + 8 for x in grouping_matrix_rnd3_g1[1]], [x + 8 for x in grouping_matrix_rnd3_g1[2]], [x + 8 for x in grouping_matrix_rnd3_g1[3]]] grouping_matrix_rnd3_g3 = [[x + 16 for x in grouping_matrix_rnd3_g1[0]], [x + 16 for x in grouping_matrix_rnd3_g1[1]], [x + 16 for x in grouping_matrix_rnd3_g1[2]], [x + 16 for x in grouping_matrix_rnd3_g1[3]]] grouping_matrix_rnd4_g1 = [[1, 8], [3, 4], [5, 6], [7, 2]] grouping_matrix_rnd4_g2 = [[x + 8 for x in grouping_matrix_rnd4_g1[0]], [x + 8 for x in grouping_matrix_rnd4_g1[1]], [x + 8 for x in grouping_matrix_rnd4_g1[2]], [x + 8 for x in grouping_matrix_rnd4_g1[3]]] grouping_matrix_rnd4_g3 = [[x + 16 for x in grouping_matrix_rnd4_g1[0]], [x + 16 for x in grouping_matrix_rnd4_g1[1]], [x + 16 for x in grouping_matrix_rnd4_g1[2]], [x + 16 for x in grouping_matrix_rnd4_g1[3]]] grouping_matrix_rnd5_g1 = [[1, 2], [3, 4], [5, 6], [7, 8]] grouping_matrix_rnd5_g2 = [[x + 8 for x in grouping_matrix_rnd5_g1[0]], [x + 8 for x in grouping_matrix_rnd5_g1[1]], [x + 8 for x in grouping_matrix_rnd5_g1[2]], [x + 8 for x in grouping_matrix_rnd5_g1[3]]] grouping_matrix_rnd5_g3 = [[x + 16 for x in grouping_matrix_rnd5_g1[0]], [x + 16 for x in grouping_matrix_rnd5_g1[1]], [x + 16 for x in grouping_matrix_rnd5_g1[2]], [x + 16 for x in grouping_matrix_rnd5_g1[3]]] if self.round_number in (1, 6): self.set_group_matrix(grouping_matrix_rnd1_g1 + grouping_matrix_rnd1_g2 + grouping_matrix_rnd1_g3) if self.round_number in (2, 7): self.set_group_matrix(grouping_matrix_rnd2_g1 + grouping_matrix_rnd2_g2 + grouping_matrix_rnd2_g3) if self.round_number in (3, 8): self.set_group_matrix(grouping_matrix_rnd3_g1 + grouping_matrix_rnd3_g2 + grouping_matrix_rnd3_g3) if self.round_number in (4, 9): self.set_group_matrix(grouping_matrix_rnd4_g1 + grouping_matrix_rnd4_g2 + grouping_matrix_rnd4_g3) if self.round_number in (5, 10): self.set_group_matrix(grouping_matrix_rnd5_g1 + grouping_matrix_rnd5_g2 + grouping_matrix_rnd5_g3) elif len(self.get_players()) == 16: grouping_matrix_rnd1_g1 = [[1, 2], [3, 6], [5, 4], [7, 8]] grouping_matrix_rnd1_g2 = [[x + 8 for x in grouping_matrix_rnd1_g1[0]], [x + 8 for x in grouping_matrix_rnd1_g1[1]], [x + 8 for x in grouping_matrix_rnd1_g1[2]], [x + 8 for x in grouping_matrix_rnd1_g1[3]]] grouping_matrix_rnd2_g1 = [[1, 4], [3, 8], [5, 2], [7, 6]] grouping_matrix_rnd2_g2 = [[x + 8 for x in grouping_matrix_rnd2_g1[0]], [x + 8 for x in grouping_matrix_rnd2_g1[1]], [x + 8 for x in grouping_matrix_rnd2_g1[2]], [x + 8 for x in grouping_matrix_rnd2_g1[3]]] grouping_matrix_rnd3_g1 = [[1, 6], [3, 2], [5, 8], [7, 4]] grouping_matrix_rnd3_g2 = [[x + 8 for x in grouping_matrix_rnd3_g1[0]], [x + 8 for x in grouping_matrix_rnd3_g1[1]], [x + 8 for x in grouping_matrix_rnd3_g1[2]], [x + 8 for x in grouping_matrix_rnd3_g1[3]]] grouping_matrix_rnd4_g1 = [[1, 8], [3, 4], [5, 6], [7, 2]] grouping_matrix_rnd4_g2 = [[x + 8 for x in grouping_matrix_rnd4_g1[0]], [x + 8 for x in grouping_matrix_rnd4_g1[1]], [x + 8 for x in grouping_matrix_rnd4_g1[2]], [x + 8 for x in grouping_matrix_rnd4_g1[3]]] grouping_matrix_rnd5_g1 = [[1, 2], [3, 4], [5, 6], [7, 8]] grouping_matrix_rnd5_g2 = [[x + 8 for x in grouping_matrix_rnd5_g1[0]], [x + 8 for x in grouping_matrix_rnd5_g1[1]], [x + 8 for x in grouping_matrix_rnd5_g1[2]], [x + 8 for x in grouping_matrix_rnd5_g1[3]]] if self.round_number in (1, 6): self.set_group_matrix(grouping_matrix_rnd1_g1 + grouping_matrix_rnd1_g2) if self.round_number in (2, 7): self.set_group_matrix(grouping_matrix_rnd2_g1 + grouping_matrix_rnd2_g2) if self.round_number in (3, 8): self.set_group_matrix(grouping_matrix_rnd3_g1 + grouping_matrix_rnd3_g2) if self.round_number in (4, 9): self.set_group_matrix(grouping_matrix_rnd4_g1 + grouping_matrix_rnd4_g2) if self.round_number in (5, 10): self.set_group_matrix(grouping_matrix_rnd5_g1 + grouping_matrix_rnd5_g2) elif len(self.get_players()) == 8: grouping_matrix_rnd1_g1 = [[1, 2], [3, 6], [5, 4], [7, 8]] grouping_matrix_rnd2_g1 = [[1, 4], [3, 8], [5, 2], [7, 6]] grouping_matrix_rnd3_g1 = [[1, 6], [3, 2], [5, 8], [7, 4]] grouping_matrix_rnd4_g1 = [[1, 8], [3, 4], [5, 6], [7, 2]] grouping_matrix_rnd5_g1 = [[1, 2], [3, 4], [5, 6], [7, 8]] if self.round_number in (1, 6): self.set_group_matrix(grouping_matrix_rnd1_g1) if self.round_number in (2, 7): self.set_group_matrix(grouping_matrix_rnd2_g1) if self.round_number in (3, 8): self.set_group_matrix(grouping_matrix_rnd3_g1) if self.round_number in (4, 9): self.set_group_matrix(grouping_matrix_rnd4_g1) if self.round_number in (5, 10): self.set_group_matrix(grouping_matrix_rnd5_g1) class Group(BaseGroup): price_hqt = models.CurrencyField(min=Constants.cost_hqt, max=130) price_lqt = models.CurrencyField(min=Constants.cost_lqt) cir = models.IntegerField(min=0, max=100, widget=widgets.Slider(attrs={'step': '10'})) expert_proposed_solution = models.StringField( choices=[["HQT", "Behandlung mit hoher Qualität"], ["LQT", "Behandlung mit niedriger Qualität"]], widget=widgets.RadioSelect ) enter_market = models.StringField( choices=[["Yes", "Ja"], ["No", "Nein"]], label="Wollen Sie in den Markt eintreten?", widget=widgets.RadioSelect ) def set_payoffs(self): expert = self.get_player_by_id(2) customer = self.get_player_by_id(1) if self.enter_market == 'Yes': if customer.customer_type == 'high': if customer.problem_type == 'Major': if self.expert_proposed_solution == "HQT": expert.payoff = self.price_hqt - Constants.cost_hqt customer.payoff = \ Constants.payoff_major_th - \ (145 * self.cir/100 * self.cir/100) - \ (1 - self.cir/100) * self.price_hqt elif self.expert_proposed_solution == "LQT": expert.payoff = self.price_lqt - Constants.cost_lqt customer.payoff = \ - \ (145 * self.cir / 100 * self.cir / 100) - \ (1 - self.cir / 100) * self.price_lqt elif customer.problem_type == 'Minor': if self.expert_proposed_solution == "HQT": expert.payoff = self.price_hqt - Constants.cost_hqt customer.payoff = \ Constants.payoff_minor - \ (145 * self.cir/100 * self.cir/100) - \ (1 - self.cir/100) * self.price_hqt elif self.expert_proposed_solution == "LQT": expert.payoff = self.price_lqt - Constants.cost_lqt customer.payoff = \ Constants.payoff_minor - \ (145 * self.cir/100 * self.cir/100) - \ (1 - self.cir/100) * self.price_lqt elif customer.customer_type == 'low': if customer.problem_type == 'Major': if self.expert_proposed_solution == "HQT": expert.payoff = self.price_hqt - Constants.cost_hqt customer.payoff = \ Constants.payoff_major_tl - \ (145 * self.cir / 100 * self.cir / 100) - \ (1 - self.cir / 100) * self.price_hqt elif self.expert_proposed_solution == "LQT": expert.payoff = self.price_lqt - Constants.cost_lqt customer.payoff = \ - \ (145 * self.cir / 100 * self.cir / 100) - \ (1 - self.cir / 100) * self.price_lqt elif customer.problem_type == 'Minor': if self.expert_proposed_solution == "HQT": expert.payoff = self.price_hqt - Constants.cost_hqt customer.payoff = \ Constants.payoff_minor - \ (145 * self.cir / 100 * self.cir / 100) - \ (1 - self.cir / 100) * self.price_hqt elif self.expert_proposed_solution == "LQT": expert.payoff = self.price_lqt - Constants.cost_lqt customer.payoff = \ Constants.payoff_minor - \ (145 * self.cir / 100 * self.cir / 100) - \ (1 - self.cir / 100) * self.price_lqt else: expert.payoff = Constants.outside_option customer.payoff = Constants.outside_option - (145 * self.cir / 100 * self.cir / 100) class Player(BasePlayer): def role(self): if self.participant.id_in_session in (1, 9, 17): return 'S1' if self.participant.id_in_session in (3, 11, 19): return 'S2' if self.participant.id_in_session in (5, 13, 21): return 'S3' if self.participant.id_in_session in (7, 15, 23): return 'S4' if self.participant.id_in_session in (2, 10, 18): return 'A' if self.participant.id_in_session in (4, 12, 20): return 'B' if self.participant.id_in_session in (6, 14, 22): return 'C' if self.participant.id_in_session in (8, 16, 24): return 'D' problem_type = models.StringField() # def get_setting(self): # if self.role() in ('S1', 'S3'): # for t in (1, 3): # self.in_round(t).problem_type = "Minor" # self.in_round(t).participant.vars['problem_type'] = 'Minor' # # self.in_round(2).problem_type = "Major" # self.in_round(2).participant.vars['problem_type'] = 'Major' # # elif self.role() in ('S2', 'S4'): # self.in_round(2).problem_type = "Minor" # self.in_round(2).participant.vars['problem_type'] = 'Minor' # for t in (1, 3): # self.in_round(t).problem_type = "Major" # self.in_round(t).participant.vars['problem_type'] = 'Major' # # if self.role() in ('A', 'B'): # self.in_round(1).problem_type = "Minor" # self.in_round(1).participant.vars['problem_type'] = 'Minor' # for t in (2, 3): # self.in_round(t).problem_type = "Major" # self.in_round(t).participant.vars['problem_type'] = 'Major' # # if self.role() in ('C', 'D'): # for t in (2, 3): # self.in_round(t).problem_type = "Minor" # self.in_round(t).participant.vars['problem_type'] = 'Minor' # # self.in_round(1).problem_type = "Major" # self.in_round(1).participant.vars['problem_type'] = 'Major' def get_setting(self): if self.role() in ('S1', 'S3'): for t in (1, 3, 5, 6, 8, 10): self.in_round(t).problem_type = "Minor" self.in_round(t).participant.vars['problem_type'] = 'Minor' for t in (2, 4, 7, 9): self.in_round(t).problem_type = "Major" self.in_round(t).participant.vars['problem_type'] = 'Major' elif self.role() in ('S2', 'S4'): for t in (2, 4, 5, 7, 9, 10): self.in_round(t).problem_type = "Minor" self.in_round(t).participant.vars['problem_type'] = 'Minor' for t in (1, 3, 6, 8): self.in_round(t).problem_type = "Major" self.in_round(t).participant.vars['problem_type'] = 'Major' if self.role() in ('A', 'B'): for t in (1, 4, 5, 6, 9, 10): self.in_round(t).problem_type = "Minor" self.in_round(t).participant.vars['problem_type'] = 'Minor' for t in (2, 3, 7, 8): self.in_round(t).problem_type = "Major" self.in_round(t).participant.vars['problem_type'] = 'Major' if self.role() in ('C', 'D'): for t in (2, 3, 5, 7, 8, 10): self.in_round(t).problem_type = "Minor" self.in_round(t).participant.vars['problem_type'] = 'Minor' for t in (1, 4, 6, 9): self.in_round(t).problem_type = "Major" self.in_round(t).participant.vars['problem_type'] = 'Major' customer_type = models.StringField() # def get_type(self): # if self.role() in ('S1', 'S2'): # for t in (1, 2, 3): # self.in_round(t).customer_type = 'high' # elif self.role() in ('S3', 'S4'): # for t in (1, 2, 3): # self.in_round(t).customer_type = 'low' def get_type(self): if self.role() in ('S1', 'S2'): for t in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10): self.in_round(t).customer_type = 'high' elif self.role() in ('S3', 'S4'): for t in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10): self.in_round(t).customer_type = 'low' risk_invest = models.CurrencyField(min=0, max=50) lottery_win_risk = models.BooleanField() risk_payoff = models.CurrencyField() def set_risk_payoffs(self): lottery_win_risk = random.choice([True, False]) self.lottery_win_risk = lottery_win_risk self.risk_payoff = (c(50) - self.risk_invest) if lottery_win_risk: self.risk_payoff += (self.risk_invest * 3) self.participant.payoff += self.risk_payoff survey_age = models.IntegerField( verbose_name='Alter:', min=17, max=99) survey_gender = models.StringField( choices=['Männlich', 'Weiblich', 'Anderes'], verbose_name='Geschlecht:', widget=widgets.RadioSelect) survey_faculty = models.StringField( choices=['Wirtschaft', 'Soziolgie', 'Politik', 'Psychologie', 'Medizin', 'Sonstiges'], verbose_name='Geben Sie bitte Ihre Studienrichtung an.', widget=widgets.RadioSelect) survey_knowledge = models.StringField( choices=['Abitur/Matura', 'Bachelor', 'Magister/Master', 'PhD/Doktorat', 'Sonstiges'], verbose_name='Geben Sie bitte Ihren bisher höchsten Bildungsabschluss an.', widget=widgets.RadioSelect) survey_charity = models.IntegerField( min=0, max=1000, verbose_name='Stellen Sie sich folgende Situation vor: Heute erhalten Sie unerwarteter ' 'Weise 1.000 Euro. Wie viel von diesem Betrag wäre Sie bereit, einer ' 'wohltätigen Organisation zu spenden?') survey_insurance_accident = models.BooleanField(blank=True, widget=widgets.CheckboxInput, label="Unfallversicherung") survey_insurance_additional_health = models.BooleanField(blank=True, widget=widgets.CheckboxInput, label="Zusätzliche Krankenversicherung") survey_insurance_car = models.BooleanField(blank=True, widget=widgets.CheckboxInput, label="Kfz (Voll)kasko") survey_insurance_electronics_big = models.BooleanField(blank=True, widget=widgets.CheckboxInput, label="Haushaltsgeräte (Fernseher, Waschmaschine, ...)") survey_insurance_electronics_small = models.BooleanField(blank=True, widget=widgets.CheckboxInput, label="Elektronische Geräte (Handy, Laptop, ...") survey_insurance_travel = models.BooleanField(blank=True, widget=widgets.CheckboxInput, label="Reiseversicherung") survey_insurance_life = models.BooleanField(blank=True, widget=widgets.CheckboxInput, label="Lebensversicherung") survey_insurance_legal = models.BooleanField(blank=True, widget=widgets.CheckboxInput, label="Rechtsschutzversicherung") survey_insurance_household = models.BooleanField(blank=True, widget=widgets.CheckboxInput, label="Haushaltsversicherung") survey_insurance_other = models.BooleanField(blank=True, widget=widgets.CheckboxInput, label="sonstige Versicherungen") q1_counter = models.IntegerField(initial=0) q2_counter = models.IntegerField(initial=0) q3_counter = models.IntegerField(initial=0) q4_counter = models.IntegerField(initial=0) q1_expert = models.IntegerField() q1_customer = models.FloatField() q2_expert = models.IntegerField() q2_customer = models.FloatField() q3_expert = models.IntegerField() q3_customer = models.FloatField() q4_expert = models.IntegerField() q4_customer = models.FloatField()