Reputation: 412
I have the following matrix where each element represent a probability of a specific scoreline.
The number of goals of the home team is on the y-axis and the number of goals of the away team is the x-axis. The scoreline 0-0 is, for example, 1.21 and the scoreline of 4-3 is 0.84. I know that the probability of a home win is equal to
np.sum(np.tril(match_score_matrix, -1))
The probability of draw is equal to:
np.sum(np.diag(match_score_matrix))
The probability of a loss is equal to:
np.sum(np.triu(match_score_matrix, 1)),
Now, I want to know the probability of each goal differences. In this matrix, the following goal differences outcomes are possible [-6, -5, ..., 0, ..., 15). How can I write a loop that calculates the probability of each outcome?
def get_probabilities(match_score_matrix, max_goals_home, max_goals_away):
return dict({'max_goals_away': np.something,
'-5', np.something,
'-4', np.something,
...
'0', np.diag(match_score_matrix)),
'1', np.something
...
'max_goals_home', np.something })
How can I write this in an easy-to-use loop? Thank you in advance!
Upvotes: 0
Views: 74
Reputation: 107567
Consider using offset in np.diagonal
. Because the diagonal are when goals are equal to each other between home and away teams, one offset upward are probabilities when away team is one goal higher than home team. Conversely, one offset downward are probabilities when home team is one goal higher than away team. Therefore, sum the two probabilities.
# AWAY ONE GOAL HIGHER
np.sum(np.diagonal(match_score_matrix, offset=1))
# HOME ONE GOAL HIGHER
np.sum(np.diagonal(match_score_matrix, offset=-1))
# AWAY TWO GOALS HIGHER
np.sum(np.diagonal(match_score_matrix, offset=2))
# HOME TWO GOALS HIGHER
np.sum(np.diagonal(match_score_matrix, offset=-2))
...
# AWAY MAX GOALS HIGHER USING array.shape
np.sum(np.diagonal(match_score_matrix, offset=match_score_matrix.shape[0]))
# HOME MAX GOALS HIGHER USING array.shape
np.sum(np.diagonal(match_score_matrix, offset=-match_score_matrix.shape[0]))
And for your needed dictionary, use a dictionary comprehension
def get_probabilities(match_score_matrix, max_goals_home, max_goals_away):
# DICTIONARY COMPREHENSION
return {str(i): np.sum(np.diagonal(match_score_matrix, offset=i)) for i in range(-15,15)}
Upvotes: 1
Reputation: 861
You can use np.diag
to extract the k
-th diagonal and then sum it.
{str(i):np.sum(np.diag(match_score_matrix,k=i)) for i in range(-15,8)}
Upvotes: 0