1
$\begingroup$

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..

$\endgroup$
  • 2
    $\begingroup$ The reason N does not work is that the argument is a String. e.g. InputForm@A is {0, 0.5*(" 14 "/" 11 " + .... Use ToExpression to convert the strings to integers. $\endgroup$ – Rohit Namjoshi Jan 11 at 23:05
  • $\begingroup$ Thank you Rohit! $\endgroup$ – Mick Jan 13 at 20:44
0
$\begingroup$

I modified code as follows to obtain decimal form:

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]];
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]]]}];
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[((ToExpression[Chome[[j, 7]]]/
             ToExpression[Chome[[j, 3]]]) + (ToExpression[
              Caway[[k, 8]]]/ToExpression[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[((ToExpression[Chome[[j, 8]]]/
             ToExpression[Chome[[j, 3]]]) + (ToExpression[
              Caway[[k, 7]]]/ToExpression[Caway[[k, 3]]]))/
         2], ## &[]], {k, 1, Length[teamsENG]}], {j, 1, 
    Length[teamsENG]}], {i, 1, Length[matchesENG]}];

I still can't obtain poisson value and ordinate them respect to next matches. I tried something like this but it doesn't work.

p1[m1_, x_] := 
 PDF[PoissonDistribution[Round[Do[A[[i]], {i, 1, Length[A]}]]], x]
p2[m2_, x_] := 
 PDF[PoissonDistribution[Round[Do[B[[i]], {i, 1, Length[B]}]]], x]
hprob = Table[p[A, x], {x, 0, 10}]
aprob = Table[p[B, x], {x, 0, 10}]

Any help?

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.