oTree Forum >

Average of players' actions from the previous round

#1 by macrotl (edited )

I have a game with several rounds where participants have to predict the value of a variable. In the first round they read a constant and submit their guess. In the following round, I want to show them the average of all the guesses from all players in the first round, before they make their guess for round 2. I managed to calculate the average as a group variable, but cannot pass that information on a page where the form_filed is player. I also managed to pass the guess variable between rounds if stored in a participant variable but do not know how to calculate the average if stored in this way. Ideally, I would need to do this over several rounds.

Thanks a lot for your help!

class C(BaseConstants):
    NAME_IN_URL = 'take_one'
    NUM_ROUNDS = 2
    START_VALUE = float(2.0)

class Subsession(BaseSubsession):

class Group(BaseGroup):
    average = models.FloatField()

class Player(BasePlayer):
    your_guess = models.FloatField(
        label="What is your prediction for tomorrow?"
    earning = models.FloatField()
    average = models.FloatField()

def set_payoffs(group: Group):
    players = group.get_players()
    group.average = sum([p.your_guess for p in players]) / 2
    for p in players:
        p.earning = round(100 / (1 + abs(p.your_guess - group.average)), 2)
        p.payoff = p.earning * 0.005

class Prediction_start(Page):
    form_model = 'player'
    form_fields = ['your_guess']

    def is_displayed(player: Player):
        return player.round_number == 1

    def before_next_page(player: Player, timeout_happened):
        player.participant.vars['your_guess'] = player.your_guess

class Prediction_next(Page):
    form_model = 'player'
    form_fields = ['your_guess']

    def is_displayed(player: Player):
        return player.round_number > 1

    def vars_for_template(player: Player):
        player.your_guess = player.participant.vars['your_guess']

class Wait(WaitPage):
    after_all_players_arrive = set_payoffs

class Results(Page):
page_sequence = [Prediction_start, Prediction_next, Wait, Results]

#2 by Newbie

Hello, I will start working on a similar problem shortly and wanted to ask if you managed to complete your task or if you are still facing any problems.

Write a reply

Set forum username