File: SPRT.h

package info (click to toggle)
leela-zero 0.17-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,792 kB
  • sloc: cpp: 20,958; python: 4,894; makefile: 66
file content (115 lines) | stat: -rw-r--r-- 3,388 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
    This file is part of Leela Zero.
    Copyright (C) 2017-2018 Marco Calignano
    originally taken from Cute Chess (http://github.com/cutechess)
    Copyright (C) 2016 Ilari Pihlajisto

    Leela Zero is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Leela Zero is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Leela Zero.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef SPRT_H
#define SPRT_H
/*!
 * \brief A Sequential Probability Ratio Test
 *
 * The Sprt class implements a Sequential Probability Ratio Test (SPRT) that
 * can be used as a termination criterion for stopping a match between two
 * players when the Elo difference is known to be outside of the specified
 * interval.
 *
 * \sa http://en.wikipedia.org/wiki/Sequential_probability_ratio_test
 */

#include <QMutex>
#include <QTextStream>
#include <tuple>

class Sprt
{
public:
        /*! The result of the test. */
    enum Result
    {
        Continue,    //!< Continue monitoring
        AcceptH0,    //!< Accept null hypothesis H0
        AcceptH1    //!< Accept alternative hypothesis H1
    };

    /*! The result of a chess game. */
    enum GameResult
    {
        NoResult = 0,    //!< Game ended with no result
        Win,        //!< First player won
        Loss,        //!< First player lost
        Draw,        //!< Game was drawn
        NotEnded    //!< Game was interrupted
    };

    /*! The status of the test. */
    struct Status
    {
        Result result;    //!< Test result
        double llr;    //!< Log-likelihood ratio
        double lBound;    //!< Lower bound
        double uBound;    //!< Upper bound
    };

    /*! Creates a new uninitialized Sprt object. */
    Sprt();

    /*!
     * Returns true if the SPRT is uninitialized; otherwise
     * returns false.
     */
    bool isNull() const;

    /*!
     * Initializes the SPRT.
     *
     * \a elo0 is the Elo difference between player A and
     * player B for H0 and \a elo1 for H1.
     *
     * \a alpha is the maximum probability for a type I error and
     * \a beta for a type II error outside interval [elo0, elo1].
     */
    void initialize(double elo0, double elo1,
                    double alpha, double beta);

    /*! Returns the current status of the test. */
    Status status() const;

    /*! Returns current win/draw/loss score. */
    std::tuple<int, int, int> getWDL() const;

    /*!
     * Updates the test with \a result.
     *
     * After calling this function, status() should be called to
     * check if H0 or H1 can be accepted.
     */
    void addGameResult(GameResult result);
    friend QTextStream& operator<<(QTextStream& stream, const Sprt& sprt);
    friend QTextStream& operator>>(QTextStream& stream, Sprt& sprt);
private:
    double m_elo0;
    double m_elo1;
    double m_alpha;
    double m_beta;
    int m_wins;
    int m_losses;
    int m_draws;
    mutable QMutex m_mutex;
};

#endif // SPRT_H