Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rolling min max #22

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
qchateau marked this conversation as resolved.
Show resolved Hide resolved

[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.
qchateau marked this conversation as resolved.
Show resolved Hide resolved

[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
99 changes: 99 additions & 0 deletions include/boost/accumulators/statistics/sorted_rolling_window.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
///////////////////////////////////////////////////////////////////////////////
// sorted_rolling_window.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_SORTED_ROLLING_WINDOW_HPP_QC_20_12_2018
#define BOOST_ACCUMULATORS_STATISTICS_SORTED_ROLLING_WINDOW_HPP_QC_20_12_2018

#include <boost/container/set.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/accumulators/accumulators_fwd.hpp>
#include <boost/accumulators/framework/extractor.hpp>
#include <boost/accumulators/framework/depends_on.hpp>
#include <boost/accumulators/framework/accumulator_base.hpp>
#include <boost/accumulators/framework/parameters/sample.hpp>
#include <boost/accumulators/framework/parameters/accumulator.hpp>
#include <boost/accumulators/statistics_fwd.hpp>
#include <boost/accumulators/statistics/rolling_window.hpp>

namespace boost { namespace accumulators
{

namespace impl
{
///////////////////////////////////////////////////////////////////////////////
// sorted_rolling_window
// stores the latest N samples, where N is specified at construction time
// with the rolling_window_size named parameter. samples are sorted
// on insersion
template<typename Sample>
struct sorted_rolling_window_impl
: accumulator_base
{
typedef typename container::multiset<Sample>::const_iterator const_iterator;
typedef iterator_range<const_iterator> result_type;

template<typename Args>
sorted_rolling_window_impl(Args const &)
{}

template<typename Args>
void operator ()(Args const &args)
{
if(is_rolling_window_plus1_full(args))
{
const_iterator it = this->sorted_buffer_.find(rolling_window_plus1(args).front());
this->sorted_buffer_.erase(it);
}
this->sorted_buffer_.insert(args[sample]);
}

result_type result(dont_care) const
{
return result_type(this->sorted_buffer_.begin(), this->sorted_buffer_.end());
}

private:
container::multiset<Sample> sorted_buffer_;
};

} // namespace impl

///////////////////////////////////////////////////////////////////////////////
// tag::sorted_rolling_window
//
namespace tag
{
struct sorted_rolling_window
: depends_on< rolling_window_plus1 >
{
/// INTERNAL ONLY
///
typedef accumulators::impl::sorted_rolling_window_impl< mpl::_1 > impl;

#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
/// tag::sorted_rolling_window::size named parameter
static boost::parameter::keyword<tag::rolling_window_size> const window_size;
#endif
};

} // namespace tag

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

BOOST_ACCUMULATORS_IGNORE_GLOBAL(sorted_rolling_window)
}

using extract::sorted_rolling_window;

}} // namespace boost::accumulators

#endif
3 changes: 3 additions & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ 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 ]
[ run sum_kahan.cpp ]
[ run tail.cpp ]
Expand Down
Loading