Skip to content

Commit

Permalink
rolling min and max
Browse files Browse the repository at this point in the history
  • Loading branch information
qchateau committed Dec 21, 2018
1 parent b827ac8 commit 8037520
Show file tree
Hide file tree
Showing 6 changed files with 372 additions and 0 deletions.
93 changes: 93 additions & 0 deletions doc/accumulators.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -2077,6 +2077,99 @@ The rolling count is the current number of elements in the rolling window.

[endsect]

[section:rolling_max rolling_max]

The rolling sum is the sum of the last /N/ samples.

[variablelist
[[Result Type] [``_sample_type_``]]
[[Depends On] [`sorted_rolling_window`]]
[[Variants] [['none]]]
[[Initialization Parameters] [`tag::rolling_window::window_size`]]
[[Accumulator Parameters] [['none]]]
[[Extractor Parameters] [['none]]]
[[Accumulator Complexity] [O(log N), where N is the window size]]
[[Extractor Complexity] [O(1)]]
]

[*Header]
[def _ROLLING_MAX_HPP_ [headerref boost/accumulators/statistics/rolling_max.hpp]]

#include <_ROLLING_MAX_HPP_>

[*Example]

accumulator_set<int, stats<tag::rolling_max> > acc(tag::rolling_window::window_size = 3);

acc(1);
BOOST_CHECK_EQUAL(1, rolling_max(acc));

acc(2);
BOOST_CHECK_EQUAL(2, rolling_max(acc));

acc(3);
BOOST_CHECK_EQUAL(3, rolling_max(acc));

acc(1);
BOOST_CHECK_EQUAL(3, rolling_max(acc));

acc(-1);
BOOST_CHECK_EQUAL(1, rolling_max(acc));

acc(0);
BOOST_CHECK_EQUAL(1, rolling_max(acc));

[*See also]

* [classref boost::accumulators::impl::rolling_max_impl [^rolling_max_impl]]

[endsect]

[section:rolling_min rolling_min]

The rolling sum is the sum of the last /N/ samples.

[variablelist
[[Result Type] [``_sample_type_``]]
[[Depends On] [`sorted_rolling_window`]]
[[Variants] [['none]]]
[[Initialization Parameters] [`tag::rolling_window::window_size`]]
[[Accumulator Parameters] [['none]]]
[[Extractor Parameters] [['none]]]
[[Accumulator Complexity] [O(log N), where N is the window size]]
[[Extractor Complexity] [O(1)]]
]

[*Header]
[def _ROLLING_MIN_HPP_ [headerref boost/accumulators/statistics/rolling_min.hpp]]

#include <_ROLLING_MIN_HPP_>

[*Example]

accumulator_set<int, stats<tag::rolling_min> > acc(tag::rolling_window::window_size = 3);

acc(1);
BOOST_CHECK_EQUAL(1, rolling_min(acc));

acc(2);
BOOST_CHECK_EQUAL(1, rolling_min(acc));

acc(3);
BOOST_CHECK_EQUAL(1, rolling_min(acc));

acc(4);
BOOST_CHECK_EQUAL(2, rolling_min(acc));

acc(-1);
BOOST_CHECK_EQUAL(-1, rolling_min(acc));

[*See also]

* [classref boost::accumulators::impl::rolling_min_impl [^rolling_min_impl]]

[endsect]

[section:rolling_sum rolling_sum]

The rolling sum is the sum of the last /N/ samples.
Expand Down
77 changes: 77 additions & 0 deletions include/boost/accumulators/statistics/rolling_max.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
///////////////////////////////////////////////////////////////////////////////
// rolling_max.hpp
//
// Copyright 2018 Quentin Chateau. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_MAX_HPP_QC_20_12_2018
#define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MAX_HPP_QC_20_12_2018

#include <boost/accumulators/framework/accumulator_base.hpp>
#include <boost/accumulators/framework/extractor.hpp>
#include <boost/accumulators/framework/parameters/sample.hpp>
#include <boost/accumulators/framework/depends_on.hpp>
#include <boost/accumulators/statistics_fwd.hpp>
#include <boost/accumulators/statistics/sorted_rolling_window.hpp>

namespace boost { namespace accumulators
{

namespace impl
{
///////////////////////////////////////////////////////////////////////////////
// rolling_max_impl
template<typename Sample>
struct rolling_max_impl
: accumulator_base
{
// for boost::result_of
typedef Sample result_type;

rolling_max_impl(dont_care)
{
}

template<typename Args>
result_type result(Args const &args) const
{
if (sorted_rolling_window(args).empty())
{
return numeric::as_min(Sample());
}
return sorted_rolling_window(args).back();
}
};

} // namespace impl

///////////////////////////////////////////////////////////////////////////////
// tag::rolling_max
//
namespace tag
{
struct rolling_max
: depends_on< sorted_rolling_window >
{
/// INTERNAL ONLY
///
typedef accumulators::impl::rolling_max_impl<mpl::_1> impl;
};
}

///////////////////////////////////////////////////////////////////////////////
// extract::rolling_max
//
namespace extract
{
extractor<tag::rolling_max> const rolling_max = {};

BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_max)
}

using extract::rolling_max;

}} // namespace boost::accumulators

#endif
77 changes: 77 additions & 0 deletions include/boost/accumulators/statistics/rolling_min.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
///////////////////////////////////////////////////////////////////////////////
// rolling_min.hpp
//
// Copyright 2018 Quentin Chateau. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_MIN_HPP_QC_20_12_2018
#define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MIN_HPP_QC_20_12_2018

#include <boost/accumulators/framework/accumulator_base.hpp>
#include <boost/accumulators/framework/extractor.hpp>
#include <boost/accumulators/framework/parameters/sample.hpp>
#include <boost/accumulators/framework/depends_on.hpp>
#include <boost/accumulators/statistics_fwd.hpp>
#include <boost/accumulators/statistics/sorted_rolling_window.hpp>

namespace boost { namespace accumulators
{

namespace impl
{
///////////////////////////////////////////////////////////////////////////////
// rolling_min_impl
template<typename Sample>
struct rolling_min_impl
: accumulator_base
{
// for boost::result_of
typedef Sample result_type;

rolling_min_impl(dont_care)
{
}

template<typename Args>
result_type result(Args const &args) const
{
if (sorted_rolling_window(args).empty())
{
return numeric::as_max(Sample());
}
return sorted_rolling_window(args).front();
}
};

} // namespace impl

///////////////////////////////////////////////////////////////////////////////
// tag::rolling_min
//
namespace tag
{
struct rolling_min
: depends_on< sorted_rolling_window >
{
/// INTERNAL ONLY
///
typedef accumulators::impl::rolling_min_impl<mpl::_1> impl;
};
}

///////////////////////////////////////////////////////////////////////////////
// extract::rolling_min
//
namespace extract
{
extractor<tag::rolling_min> const rolling_min = {};

BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_min)
}

using extract::rolling_min;

}} // namespace boost::accumulators

#endif
2 changes: 2 additions & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ test-suite "accumulators"
[ run reference.cpp ]
[ run rolling_count.cpp ]
[ run rolling_sum.cpp ]
[ run rolling_max.cpp ]
[ run rolling_mean.cpp ]
[ run rolling_min.cpp ]
[ run skewness.cpp ]
[ run sorted_rolling_window.cpp ]
[ run sum.cpp ]
Expand Down
63 changes: 63 additions & 0 deletions test/rolling_max.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// (C) Copyright Eric Niebler 2008.
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/test/unit_test.hpp>
#include <boost/container/set.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_max.hpp>

using namespace boost;
using namespace unit_test;
using namespace accumulators;
using namespace container;

typedef accumulator_set<int, stats<tag::rolling_max> > RollingmaxAccumulator;

///////////////////////////////////////////////////////////////////////////////
// test_stat
//
void test_stat()
{
accumulator_set<int, stats<tag::rolling_max> > acc(tag::rolling_window::window_size = 3);

BOOST_CHECK_EQUAL(numeric::as_min(int()), rolling_max(acc));

acc(1);
BOOST_CHECK_EQUAL(1, rolling_max(acc));

acc(3);
BOOST_CHECK_EQUAL(3, rolling_max(acc));

acc(5);
BOOST_CHECK_EQUAL(5, rolling_max(acc));

acc(7);
BOOST_CHECK_EQUAL(7, rolling_max(acc));

acc(6);
BOOST_CHECK_EQUAL(7, rolling_max(acc));

acc(1);
BOOST_CHECK_EQUAL(7, rolling_max(acc));

acc(3);
BOOST_CHECK_EQUAL(6, rolling_max(acc));

acc(2);
BOOST_CHECK_EQUAL(3, rolling_max(acc));
}

///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite *test = BOOST_TEST_SUITE("rolling max test");

test->add(BOOST_TEST_CASE(&test_stat));

return test;
}
60 changes: 60 additions & 0 deletions test/rolling_min.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// (C) Copyright Eric Niebler 2008.
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/test/unit_test.hpp>
#include <boost/container/set.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_min.hpp>

using namespace boost;
using namespace unit_test;
using namespace accumulators;
using namespace container;

typedef accumulator_set<int, stats<tag::rolling_min> > RollingMinAccumulator;

///////////////////////////////////////////////////////////////////////////////
// test_stat
//
void test_stat()
{
accumulator_set<int, stats<tag::rolling_min> > acc(tag::rolling_window::window_size = 3);

BOOST_CHECK_EQUAL(numeric::as_max(int()), rolling_min(acc));

acc(1);
BOOST_CHECK_EQUAL(1, rolling_min(acc));

acc(3);
BOOST_CHECK_EQUAL(1, rolling_min(acc));

acc(5);
BOOST_CHECK_EQUAL(1, rolling_min(acc));

acc(7);
BOOST_CHECK_EQUAL(3, rolling_min(acc));

acc(6);
BOOST_CHECK_EQUAL(5, rolling_min(acc));

acc(7);
BOOST_CHECK_EQUAL(6, rolling_min(acc));

acc(1);
BOOST_CHECK_EQUAL(1, rolling_min(acc));\
}

///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite *test = BOOST_TEST_SUITE("rolling min test");

test->add(BOOST_TEST_CASE(&test_stat));

return test;
}

0 comments on commit 8037520

Please sign in to comment.