I would like to automate a simple poisson football scores predictions process.
I started by importing and cleaning home and away England League team tables data to work on it with Poisson distribution:
ClearAll;
Cl = Import["https://www.soccerstats.com/homeaway.asp?league=england",
"Data"];
Chome = Drop[Drop[Cl[[2, 3, 1]]], 1];
Caway = Drop[Drop[Cl[[2, 3, 2]]], 1];
teamsENG = Chome[[All, 2]];
I imported also the next round of the matches to be played respect to the date of the initial data importing:
dataENG =
Import["https://www.soccerstats.com/results.asp?league=england&\
pmtype=bydate", "Data"];
Drop[Drop[Drop[Cases[dataENG, {_, _, _, _}, Infinity], -4], -1, None],
None, -1];
Take[Table[
If[StringContainsQ[%[[i, 2]], ":"] == True, %[[i]], ## &[]], {i, 1,
Length[%]}], Length[teamsENG]/2];
Table[StringSplit[%[[i]], "-"], {i, 1, Length[%]}];
matchesENG =
Transpose[{StringTrim[%[[All, 3, 1]]], StringTrim[%[[All, 3, 2]]]}];
Now, I create two vectors of the poisson parameters for home teams goal score (A
) and away teams goal score (B
) related to next round matches:
A = ConstantArray[0, Length[teamsENG]];
B = ConstantArray[0, Length[teamsENG]];
Do[Do[Table[
If[matchesENG[[i, 1]] == Chome[[j, 2]] &&
matchesENG[[i, 2]] == Caway[[k, 2]],
A[[j]] =
A[[j]] +
N[((Chome[[j, 7]]/Chome[[j, 3]]) + (Caway[[k, 8]]/
Caway[[k, 3]]))/2], ## &[]], {k, 1,
Length[teamsENG]}], {j, 1, Length[teamsENG]}], {i, 1,
Length[matchesENG]}];
Do[Do[Table[
If[matchesENG[[i, 1]] == Chome[[j, 2]] &&
matchesENG[[i, 2]] == Caway[[k, 2]],
B[[k]] =
B[[k]] +
N[((Chome[[j, 8]]/Chome[[j, 3]]) + (Caway[[k, 7]]/
Caway[[k, 3]]))/2], ## &[]], {k, 1,
Length[teamsENG]}], {j, 1, Length[teamsENG]}], {i, 1,
Length[matchesENG]}];
By doing that, I can't transform fractions in a decimal form although I used N operator.
Anyway, the next step is to define the Poisson probability density function p[m_, x_] := PDF[PoissonDistribution[m, x]
for home (m=A
) and away (m=B
)teams and, by taking into account the next matches round thanks to the vector matchesENG
, define the product between p[A, x]*p[B, x]
being able to recognize the actual home team and away team for each match and displaying the scores.
I don't understand how to execute this last part of the code by keeping the right home team and away team for each match.
Taking into account what above mentioned, I would like to automate the code for all european leagues by manipulating lists in a way similar to this I got answered to obtain goalscored for England and German leagues:
dateQ = Composition[Quiet, NumberQ, AbsoluteTime]; (*https://mathematica.stackexchange.com/a/49112/46490*)
countries = {"ENG", "GER"};
Evaluate[countrydata /@ countries] = With[
{data = Import["https://www.soccerstats.com/results.asp?league=" <> # <> "&pmtype=bydate", "Data"]},
Cases[
data,
{_?dateQ, teams_?(Not@*StringContainsQ[DigitCharacter]), score_, _} :> MapAt[ToExpression, StringTrim@StringSplit[#, " - "] & /@ {teams, score}, 2],
∞
]
] & /@ {"england", "germany"};
goalsfor = Join @@ Table[
(#1[[2]] -> #2) & @@@ Total[#, {2}] &@GatherBy[Join @@ (Transpose /@ countrydata[country]), First],
{country, countries}
] // Association
But this is a next step I guess..
N
does not work is that the argument is aString
. e.g.InputForm@A
is{0, 0.5*(" 14 "/" 11 " + ...
. UseToExpression
to convert the strings to integers. $\endgroup$ – Rohit Namjoshi Jan 11 '20 at 23:05