I found an awesome github repo that presents a fully functioning poisson model for estimating the outcome of football matches and other parameters of interest.

I want to know how to follow the modelled `home_goals[g]`

and `away_goals[g]`

so that I can build a grid of probabilities for different scores. These are fed in as ‘data’ but at the end of the model they are also estimated as `home_goals[g] ~ poisson(home_expected_goals[g])`

.

How do i follow these parameters from my code?

The relevant lines of python (model fitting are here - src/soccerstan.py lines 79-104).

```
# dict of the model data
model_data = {
'n_teams': len(team_map),
'n_games': len(data),
'home_team': data['home_team_id'],
'away_team': data['away_team_id'],
'home_goals': data['home_goals'],
'away_goals': data['away_goals']
}
# sample the model
fit = stan_model.sampling(data=model_data, **kwargs)
output = fit.extract()
# odict_keys(['home_advantage', 'offense_raw', 'defense_raw',
# 'offense', 'defense', 'lp__'])
# Tidy the output a little...
reverse_map = {v: k for k, v in team_map.items()}
for param in model.team_parameters:
df = pd.DataFrame(output[param])
# rename the columns with the team names
df.columns = [reverse_map[id_ + 1] for id_ in df.columns]
# output (OrderedDict):
# keys: parameters from the model
# values: estimated values for iterations of sampling (ND-array)
output[param] = df
```

```
# stan/maher.stan
data {
int<lower=1> n_teams;
int<lower=1> n_games;
int<lower=1, upper=n_teams> home_team[n_games];
int<lower=1, upper=n_teams> away_team[n_games];
int<lower=0> home_goals[n_games];
int<lower=0> away_goals[n_games];
}
parameters {
real home_advantage;
real offense_raw[n_teams - 1];
real defense_raw[n_teams - 1];
}
transformed parameters {
// Enforce sum-to-zero constraint
real offense[n_teams];
real defense[n_teams];
for (t in 1:(n_teams-1)) {
offense[t] = offense_raw[t];
defense[t] = defense_raw[t];
}
offense[n_teams] = -sum(offense_raw);
defense[n_teams] = -sum(defense_raw);
}
model {
vector[n_games] home_expected_goals;
vector[n_games] away_expected_goals;
// Priors (uninformative)
offense ~ normal(0, 10);
defense ~ normal(0, 10);
home_advantage ~ normal(0, 100);
for (g in 1:n_games) {
home_expected_goals[g] = exp(offense[home_team[g]] + defense[away_team[g]] + home_advantage);
away_expected_goals[g] = exp(offense[away_team[g]] + defense[home_team[g]]);
home_goals[g] ~ poisson(home_expected_goals[g]);
away_goals[g] ~ poisson(away_expected_goals[g]);
}
}
```

Data is here:

example.csv (97.6 KB)

I want to follow `home_goals`

and `away_goals`

for each match so that I have a trace of possible match outcomes. Is this as simple as following these parameters or does the model need more development?