Esempio n. 1
0
    def generatePoisson(self, team1: str, team2: str, league: str, show: bool = False):
        team1Ratings, team2Ratings, avgGoals, avgGA = self.getRatings(team1, team2, league)

        # Calculate expected goals for each team
        team1xG: float = team1Ratings[0] * team2Ratings[1] * avgGoals
        team2xG: float = team2Ratings[0] * team1Ratings[1] * avgGoals

        # Create 2d array for discrete probabilities, with team1 as columns and team2 as rows
        distribution: np.ndarray((6, 6)) = np.zeros((6, 6))
        for i in range(6):
            for j in range(6):
                team1p = poisson.pmf(j, team1xG)
                team2p = poisson.pmf(i, team2xG)
                if j == 5:
                    team1p = 1 - poisson.cdf(5, team1xG)
                if i == 5:
                    team2p = 1 - poisson.cdf(5, team2xG)

                distribution[i][j] = team1p * team2p

        if show:
            plt.imshow(distribution)
            plt.show()

        return (distribution, team1xG, team2xG)
Esempio n. 2
0
    def generatePoisson(self, show: bool = False) -> np.ndarray:
        try:
            self.team1Ratings
            self.team2Ratings
        except AttributeError:
            self.getRatings()

        # Calculate expected goals for each team
        team1xG: float = self.team1Ratings[0] * \
            self.team2Ratings[1] * self.avgGoals
        team2xG: float = self.team2Ratings[0] * \
            self.team1Ratings[1] * self.avgGoals

        # Create 2d array for discrete probabilities, with team1 as columns and team2 as rows
        distribution: np.ndarray((6, 6)) = np.zeros((6, 6))
        for i in range(6):
            for j in range(6):
                team1p = poisson.pmf(j, team1xG)
                team2p = poisson.pmf(i, team2xG)
                if j == 5:
                    team1p = 1 - poisson.cdf(5, team1xG)
                if i == 5:
                    team2p = 1 - poisson.cdf(5, team2xG)

                distribution[i][j] = team1p * team2p

        if show:
            plt.imshow(distribution)
            plt.show()

        self.dist = distribution
        self.team1xG = team1xG
        self.team2xG = team2xG

        return distribution
Esempio n. 3
0
def independentBivPois2(A, B, alpha=0.01):
    robjects.r["set.seed"](1337)

    m1 = numpy.array(A)
    m2 = numpy.array(B)
    bivpois = importr('bivpois')

    mu1 = mean(m1)
    mu2 = mean(m2)

    numpy2ri.activate()
    # print(bivpois.pbivpois(robjects.r["as.vector"](m1), robjects.r["as.vector"](m2), robjects.r["c"](mu1,mu2,0.00000001)))

    bp = bivpois.simple_bp(robjects.r["as.vector"](m1),
                           robjects.r["as.vector"](m2),
                           maxit=35)
    dpll = max(bp[1])

    idll = numpy.sum(numpy.log(poisson.pmf(m1, mu=mu1))) + numpy.sum(
        numpy.log(poisson.pmf(m2, mu=mu2)))

    lrt = -2.0 * (idll - dpll)

    chisq = chi2.ppf(1.0 - alpha, 1)
    print(bp[0])
    print(dpll)
    print(idll)

    print(lrt)
    print(chisq)
    # is independent
    return lrt <= chisq
Esempio n. 4
0
    def getOneWayPoisson(self, league: str, team1: str, team2: str) -> Union[list, list]:
        dist, team1xG, team2xG = self.generatePoisson(team1, team2, league)

        team1Poisson = []
        team2Poisson = []
        for i in range(5):
            team1Poisson.append(poisson.pmf(i, team1xG))
            team2Poisson.append(poisson.pmf(i, team2xG))

        team1Poisson.append(1-poisson.cdf(4, team1xG))
        team2Poisson.append(1-poisson.cdf(4, team2xG))

        return (team1Poisson, team2Poisson)
Esempio n. 5
0
    def getOneWayPoisson(self) -> Union[list, list]:
        try:
            self.distribution
        except AttributeError:
            self.generatePoisson()
        
        team1Poisson = []
        team2Poisson = []
        for i in range(5):
            team1Poisson.append(poisson.pmf(i, self.team1xG))
            team2Poisson.append(poisson.pmf(i, self.team2xG))

        team1Poisson.append(1-poisson.cdf(4, self.team1xG))
        team2Poisson.append(1-poisson.cdf(4, self.team2xG))

        return [team1Poisson, team2Poisson]
Esempio n. 6
0
    def getLogLikelihood(self, documents, debug=False):
        ll = 0

        if documents.ndim > 1:
            for i in range(0, documents.shape[0]):
                ll += self.getLogLikelihood(documents[i])
        else:
            # it's only one
            document = documents
            lambdas = self.getLambdas(document)
            if debug:
                print(document)
                print(numpy.round(lambdas, 0))
            for j in range(0, self.nF):
                pmfval = poisson.pmf(document[j], lambdas[j])
                if pmfval == 0.0:
                    pmfval = numpy.finfo(float).eps
                ll += math.log(pmfval)
        return ll