# drEffects from __future__ import division import otree.models from otree.db import models from otree import widgets from otree.common import Currency as c, currency_range, safe_json from otree.constants import BaseConstants from otree.models import BaseSubsession, BaseGroup, BasePlayer from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random import json from random import randrange from django import forms from decimal import Decimal author = 'Dominik Duell, MPL taken from Felix Holzmeister' doc = """Demographics and risk preference elicitation via Holt/Laury (2002)-list """ class Constants(BaseConstants): name_in_url = 'demographics' players_per_group = None num_rounds = 1 lottery_a_hi = 80 lottery_a_lo = 60 lottery_b_hi = 150 lottery_b_lo = 4 num_choices = 10 certain_choice = True one_choice_per_page = False random_order = False enforce_consistency = False percentage = False small_pies = True large_pies = True progress_bar = True instructions = True results = True if one_choice_per_page: if certain_choice: num_rounds = num_choices else: num_rounds = num_choices - 1 else: num_rounds = 1 class Subsession(BaseSubsession): def creating_session(self): n = Constants.num_choices for p in self.get_players(): indices = [j for j in range(1, n)] indices.append(n) if Constants.certain_choice else None if Constants.percentage: probabilities = [ "{0:.2f}".format(k / n * 100) + "%" for k in indices ] else: probabilities = [ str(k) + "/" + str(n) for k in indices ] form_fields = ['choice_' + str(k) for k in indices] p.participant.vars['mpl_choices'] = list( zip(indices, form_fields, probabilities) ) p.participant.vars['mpl_index_to_pay'] = random.choice(indices) p.participant.vars['mpl_choice_to_pay'] = 'choice_' + str(p.participant.vars['mpl_index_to_pay']) if Constants.random_order: random.shuffle(p.participant.vars['mpl_choices']) p.participant.vars['mpl_choices_made'] = [None for j in range(1, n + 1)] for participant in self.session.get_participants(): participant.vars['mpl_switching_point'] = random.randint(1, n) class Group(BaseGroup): pass class Player(BasePlayer): conversion_rate = models.FloatField() participation_fee = models.FloatField() # Demographics age = models.PositiveIntegerField(min=18, max=99, label='What is your age?') gender = models.PositiveIntegerField( choices=[(1, 'Female'), (0, 'Male'), (2, 'Other'), (99, 'Prefer not to say')], widget=widgets.RadioSelect(), label='Which gender do you identify with?' ) ethnicity = models.PositiveIntegerField( choices=[(1, 'White British'), (2, 'Any other white background'), (3, 'White and Black Caribbean'), (4, 'White and Black African'), (5, 'White and Asian'), (6, 'Any other mixed background'), (7, 'Indian'), (8, 'Pakistani'), (9, 'Bangladeshi'), (10, 'Any other Asian background'), (11, 'Black Caribbean'), (12, 'Black African'), (13, 'Any other black background'), (14, 'Chinese'), (15, 'Other ethnic group'), (16, 'Prefer not to say')], widget=widgets.RadioSelect(), label='To which of these groups do you consider you belong?' ) residence = models.CharField( label="Where did you spend most of your life? Please specify town and, if not in the UK, country." ) income = models.PositiveIntegerField( choices=[ (1, 'under 5,000 per year'), (2, '5,000 to 9,999 per year'), (3, '10,000 to 14,999 per year'), (4, '15,000 to 19,999 per year'), (5, '20,000 to 24,999 per year'), (6, '25,000 to 29,999 per year'), (7, '30,000 to 34,999 per year'), (8, '35,000 to 39,999 per year'), (9, '40,000 to 44,999 per year'), (10, '45,000 to 49,999 per year'), (11, '50,000 to 59,999 per year'), (12, '60,000 to 69,999 per year'), (13, '70,000 to 99,999 per year'), (14, '100,000 to 149,999 per year'), (15, '150,000 and over'), (16, 'Don`t know'), (17, 'Prefer not to say')], widget=widgets.RadioSelect(), label='What is your gross household income (in GBP)?' ) degreeLevel = models.PositiveIntegerField( choices=[ (1, 'undergraduate student'), (2, 'postgraduate student'), (3, 'no student'), (99, 'Prefer not to say')], widget=widgets.RadioSelect(), label='Are you a ... ?' ) degree = models.CharField( label="If you are a student, what is your degree program? If you are not a student, just type 'no student'." ) religious = models.PositiveIntegerField( choices=[ (1, 'Yes'), (2, 'Maybe'), (3, 'No'), (99, 'Prefer not to say')], widget=widgets.RadioSelect(), label='Do you consider religion to be an important part of your life?' ) vote = models.PositiveIntegerField( choices=[ (1, 'Yes'), (2, 'No'), (99, 'Prefer not to say')], widget=widgets.RadioSelect(), label='Did you ever vote in a national election (either in the UK or elsewhere)?' ) vote_election = models.CharField( label="If you voted, which election was that? If you did not vote, just type 'did not vote'." ) vote_party = models.CharField( label="If you voted, which party did you vote for in that election? If you did not vote, just type 'did not vote'." ) left_right = models.PositiveIntegerField( choices=[(1, '0 = Left'), (2, '1'), (3, '2'), (4, '3'), (5, '4'), (6, '5'), (7, '6'), (8, '7'), (9, '8'), (10, '9'), (10, '10 = Right'), ], widget=widgets.RadioSelectHorizontal(), label='In politics people sometimes talk of left and right. Where would you place yourself on the following scale where left is 0 and right is 10?' ) efficacy_1 = models.PositiveIntegerField( choices=[ (1, 'Agree'), (2, 'Neither agree nor disagree'), (3, 'Disagree'), (99, 'Prefer not to say')], widget=widgets.RadioSelect(), label="Here is a statement about politics, please indicate whether you agree or disagree: 'people like me don't have any say about what the government does.'" ) efficacy_2 = models.PositiveIntegerField( choices=[ (1, 'Agree'), (2, 'Neither agree nor disagree'), (3, 'Disagree'), (99, 'Prefer not to say')], widget=widgets.RadioSelect(), label="Here is a statement about politics, please indicate whether you agree or disagree: 'public officials don't care much what people think.'" ) political_knowledge = models.PositiveIntegerField( choices=[ (1, 'Extremely well'), (2, 'Very well'), (3, 'moderately well'), (4, 'Slightly well'), (4, 'Not well at all'), (99, 'Prefer not to say')], widget=widgets.RadioSelect(), label="How well do you understand the important political issues of the day?" ) if Constants.certain_choice: for j in range(1, Constants.num_choices + 1): locals()['choice_' + str(j)] = models.StringField() del j else: for j in range(1, Constants.num_choices): locals()['choice_' + str(j)] = models.StringField() del j random_draw = models.IntegerField() choice_to_pay = models.StringField() option_to_pay = models.StringField() inconsistent = models.IntegerField() switching_row = models.IntegerField() def set_payoffs(self): self.random_draw = randrange(1, len(self.participant.vars['mpl_choices'])) self.choice_to_pay = self.participant.vars['mpl_choice_to_pay'] self.option_to_pay = getattr(self, self.choice_to_pay) if self.option_to_pay == 'A': if self.random_draw <= self.participant.vars['mpl_index_to_pay']: self.payoff = Constants.lottery_a_hi self.participant.vars['task_C_score'] = Constants.lottery_a_hi else: self.payoff = Constants.lottery_a_lo self.participant.vars['task_C_score'] = Constants.lottery_a_lo else: if self.random_draw <= self.participant.vars['mpl_index_to_pay']: self.payoff = Constants.lottery_b_hi self.participant.vars['task_C_score'] = Constants.lottery_b_hi else: self.payoff = Constants.lottery_b_lo self.participant.vars['task_C_score'] = Constants.lottery_b_lo def set_consistency(self): n = Constants.num_choices self.participant.vars['mpl_choices_made'] = [ 1 if j == 'A' else 0 for j in self.participant.vars['mpl_choices_made'] ] for j in range(1, n): choices = self.participant.vars['mpl_choices_made'] self.inconsistent = 1 if choices[j] > choices[j - 1] else 0 if self.inconsistent == 1: break def set_switching_row(self): if self.inconsistent == 0: self.switching_row = sum(self.participant.vars['mpl_choices_made']) + 1