| 1 |
// -*- C++ -*- |
| 2 |
|
| 3 |
// Copyright (C) 2005-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the terms |
| 7 |
// of the GNU General Public License as published by the Free Software |
| 8 |
// Foundation; either version 3, or (at your option) any later |
| 9 |
// version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, but |
| 12 |
// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
// General Public License for more details. |
| 15 |
|
| 16 |
// You should have received a copy of the GNU General Public License |
| 17 |
// along with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
|
| 21 |
// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. |
| 22 |
|
| 23 |
// Permission to use, copy, modify, sell, and distribute this software |
| 24 |
// is hereby granted without fee, provided that the above copyright |
| 25 |
// notice appears in all copies, and that both that copyright notice |
| 26 |
// and this permission notice appear in supporting documentation. None |
| 27 |
// of the above authors, nor IBM Haifa Research Laboratories, make any |
| 28 |
// representation about the suitability of this software for any |
| 29 |
// purpose. It is provided "as is" without express or implied |
| 30 |
// warranty. |
| 31 |
|
| 32 |
/** |
| 33 |
* @file result_recorder.hpp |
| 34 |
* Contains a class for recording results |
| 35 |
*/ |
| 36 |
|
| 37 |
#ifndef PB_DS_RES_RECORDER_HPP |
| 38 |
#define PB_DS_RES_RECORDER_HPP |
| 39 |
|
| 40 |
#include <statistic/sample_mean.hpp> |
| 41 |
#include <statistic/sample_variance.hpp> |
| 42 |
#include <statistic/sample_mean_confidence_checker.hpp> |
| 43 |
|
| 44 |
namespace __gnu_pbds |
| 45 |
{ |
| 46 |
namespace test |
| 47 |
{ |
| 48 |
namespace detail |
| 49 |
{ |
| 50 |
/* |
| 51 |
* Records results until the probability that the sample mean is 10% away |
| 52 |
* from the true mean is ~ 0.05. |
| 53 |
*/ |
| 54 |
template<typename Value_Type> |
| 55 |
class result_recorder |
| 56 |
{ |
| 57 |
public: |
| 58 |
typedef Value_Type value_type; |
| 59 |
|
| 60 |
result_recorder() |
| 61 |
: m_sample_mean(value_type()), m_sample_var(value_type()) |
| 62 |
{ } |
| 63 |
|
| 64 |
bool |
| 65 |
add_result(value_type res); |
| 66 |
|
| 67 |
inline value_type |
| 68 |
get_sample_mean() const |
| 69 |
{ return m_sample_mean; } |
| 70 |
|
| 71 |
private: |
| 72 |
typedef std::list<value_type> list_type; |
| 73 |
|
| 74 |
list_type m_l; |
| 75 |
value_type m_sample_mean; |
| 76 |
value_type m_sample_var; |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
template<typename Value_Type> |
| 81 |
bool |
| 82 |
result_recorder<Value_Type>:: |
| 83 |
add_result(value_type res) |
| 84 |
{ |
| 85 |
m_l.push_back(res); |
| 86 |
m_sample_mean = sample_mean(m_l.begin(), m_l.end()); |
| 87 |
m_sample_var = sample_variance(m_l.begin(), m_l.end(), m_sample_mean); |
| 88 |
|
| 89 |
size_t dist = std::distance(m_l.begin(), m_l.end()); |
| 90 |
return sample_mean_confidence_checker(m_sample_mean, m_sample_var, |
| 91 |
dist, 0.1); |
| 92 |
} |
| 93 |
} // namespace detail |
| 94 |
} // namespace test |
| 95 |
} // namespace __gnu_pbds |
| 96 |
|
| 97 |
#endif |
| 98 |
|