from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'savings_survey' players_per_group = None num_rounds = 1 # For risk elicitation task safe_choice_constant = 0.75 safe_choice_list = [0.83, 0.91, 0.99, 1.07, 1.15, 1.23, 1.31, 1.39, 1.47, 1.55] risky_choice_constant = 0.15 risky_choice_list = [1.16, 1.32, 1.47, 1.63, 1.80, 2.01, 2.29, 2.69, 3.65, 3.85] def make_risk_choice(step): safe_choice = Constants.safe_choice_list[step] risky_choice = Constants.risky_choice_list[step] return models.IntegerField( choices=[[1, "A"], [ 2, "B"]], label="Lottery A: 50% chance of $" + str(Constants.safe_choice_constant) + \ ", 50% chance of $" + str(safe_choice) + \ "; Lottery B: 50% chance of $" + str(Constants.risky_choice_constant) + \ ", 50% chance of $" + str(risky_choice), widget=widgets.RadioSelectHorizontal()) class Subsession(BaseSubsession): payingGamble = models.IntegerField() def paying_Gamble(self): self.payingGamble = random.randint(1,10) class Group(BaseGroup): pass class Player(BasePlayer): # Answers for Cognitive Reflection Test answer_1 = models.IntegerField() answer_2 = models.IntegerField() answer_3 = models.IntegerField() answer_4 = models.StringField( label="", choices=['lost money in the stock market', 'broken even in the stock market', 'made money in the stock market'], widget=widgets.RadioSelect(), ) MC_answer_1 = models.StringField( label="", choices=['More than $102', 'Exactly $102', 'Less than $102', 'Do not know', 'Refuse to answer'], ) MC_answer_2 = models.StringField( label="", choices=['More than today', 'Exactly the same', 'Less than today', 'Do not know', 'Refuse to answer'], ) MC_answer_3 = models.StringField( label="", choices=['True', 'False', 'Do not know', 'Refuse to answer'], ) # Choice 1 is risk-averse option, Choice 2 is risk-seeking option risk_answer_01 = make_risk_choice(0) risk_answer_02 = make_risk_choice(1) risk_answer_03 = make_risk_choice(2) risk_answer_04 = make_risk_choice(3) risk_answer_05 = make_risk_choice(4) risk_answer_06 = make_risk_choice(5) risk_answer_07 = make_risk_choice(6) risk_answer_08 = make_risk_choice(7) risk_answer_09 = make_risk_choice(8) risk_answer_10 = make_risk_choice(9) risk_payoff = models.FloatField() # Demographic Questionnaire risk = models.IntegerField( label='1. In general, how willing are you to take risks?', choices=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], widget=widgets.RadioSelectHorizontal, blank=True, ) discounting = models.IntegerField( label='2. How willing are you to give up something that is beneficial for you today in order to benefit more from that in the future?', choices=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], widget=widgets.RadioSelectHorizontal, blank=True, ) die = models.IntegerField(label = '1. At what age do you expect to die?') retire = models.IntegerField(label = '2. At what age do you plan to retire?') claim = models.IntegerField(label = '3. At what age do you plan to claim Social Security Benefits? (In answering this question, you may consult the table below which shows the benefit formula)') confidenceInSSB = models.IntegerField( label = '4. How confident are you that you will receive the Social Security Benefits that you are entitled to?', choices = [[1, 'Not at all confident'], [2, 'Not very confident'], [3, 'Somewhat confident'], [4, 'Very confident'], [5, 'Completely confident']], widget=widgets.RadioSelect ) age = models.IntegerField( label="What is your age?", blank=True, ) birthyear = models.IntegerField( label="What year were you born?", choices=[tuple([x,x]) for x in range(1902, 2003)], blank=True, ) gender = models.StringField( label="What is your gender?", choices=['Male', 'Female', 'Nonbinary'], blank=True, ) race = models.StringField( label="What is your race?", choices=['Asian', 'African American or Black', 'Hispanic or Latinx', 'Mixed Race', 'Native American', 'Pacific Islander', 'White'], blank=True, ) income = models.StringField( label="What is your personal annual income?", choices=['$0 to $25,000', '$25,001 to $50,000', '$50,001 to $75,000', '$75,001 to $100,000', '$100,001 or more'], blank=True, ) education = models.StringField( label="What is your highest level of education completed?", choices=['Less than high school', 'High school', 'Some college', 'College degree', 'Graduate or professional degree'], blank=True, ) ZIP = models.StringField( label="What is your ZIP or postal code?" ) comments = models.LongStringField( label="Please enter any comments about the experiment, or suggestions for improvement.", blank=True, ) def set_payoffs(self): self.payoff = self.risk_payoff @property def payoff(self): return self._payoff @payoff.setter def payoff(self, value): if value is None: value = 0 delta = value - self._payoff self._payoff += delta self.participant.payoff += delta # should save it because it may not be obvious that modifying # player.payoff also changes a field on a different model self.participant.save() if delta == 0: return print( f'Setting payoff to: {value}. player:\t{self._payoff}, participant:\t{self.participant.payoff}, participant.code:{self.participant.code} ({Constants.name_in_url}, round {self.round_number})')