mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Common: Add mean() and meanSquaredError()
This commit is contained in:
parent
94538efc0e
commit
957ca00588
@ -27,3 +27,54 @@
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
||||
namespace solidity::phaser::test
|
||||
{
|
||||
|
||||
// STATISTICAL UTILITIES
|
||||
|
||||
/// Calculates the mean value of a series of samples given in a vector.
|
||||
///
|
||||
/// Supports any integer and real type as a convenience but the precision of the result is limited
|
||||
/// to the precision of type @a double as all the values are internally converted to it.
|
||||
///
|
||||
/// This is a very simple, naive implementation that's more than enough for tests where we usually
|
||||
/// deal with relatively short sequences of small, positive integers. It's not numerically stable
|
||||
/// in more complicated cases. Don't use in production.
|
||||
template <typename T>
|
||||
double mean(std::vector<T> const& _samples)
|
||||
{
|
||||
assert(_samples.size() > 0);
|
||||
|
||||
double sum = 0;
|
||||
for (T const& sample: _samples)
|
||||
sum += static_cast<double>(sample);
|
||||
|
||||
return sum / _samples.size();
|
||||
}
|
||||
|
||||
/// Calculates the sum of squared differences between @a _expectedValue and the values of a series
|
||||
/// of samples given in a vector.
|
||||
///
|
||||
/// Supports any integer and real type as a convenience but the precision of the result is limited
|
||||
/// to the precision of type @a double as all the values are internally converted to it.
|
||||
///
|
||||
/// This is a very simple, naive implementation that's more than enough for tests where we usually
|
||||
/// deal with relatively short sequences of small, positive integers. It's not numerically stable
|
||||
/// in more complicated cases. Don't use in production.
|
||||
template <typename T>
|
||||
double meanSquaredError(std::vector<T> const& _samples, double _expectedValue)
|
||||
{
|
||||
assert(_samples.size() > 0);
|
||||
|
||||
double sumOfSquaredDifferences = 0;
|
||||
for (T const& sample: _samples)
|
||||
sumOfSquaredDifferences += (sample - _expectedValue) * (sample - _expectedValue);
|
||||
|
||||
return sumOfSquaredDifferences / _samples.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,14 +15,43 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace boost::test_tools;
|
||||
|
||||
namespace solidity::phaser::test
|
||||
{
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(CommonTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(mean_should_calculate_statistical_mean)
|
||||
{
|
||||
BOOST_TEST(mean<int>({0}) == 0.0);
|
||||
BOOST_TEST(mean<int>({0, 0, 0, 0}) == 0.0);
|
||||
BOOST_TEST(mean<int>({5, 5, 5, 5}) == 5.0);
|
||||
BOOST_TEST(mean<int>({0, 1, 2, 3}) == 1.5);
|
||||
BOOST_TEST(mean<int>({-4, -3, -2, -1, 0, 1, 2, 3}) == -0.5);
|
||||
|
||||
BOOST_TEST(mean<double>({1.3, 1.1, 0.0, 1.5, 1.1, 2.0, 1.5, 1.5}) == 1.25);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(meanSquaredError_should_calculate_average_squared_difference_between_samples_and_expected_value)
|
||||
{
|
||||
BOOST_TEST(meanSquaredError<int>({0}, 0.0) == 0.0);
|
||||
BOOST_TEST(meanSquaredError<int>({0}, 1.0) == 1.0);
|
||||
BOOST_TEST(meanSquaredError<int>({0, 0, 0, 0}, 0.0) == 0.0);
|
||||
BOOST_TEST(meanSquaredError<int>({0, 0, 0, 0}, 1.0) == 1.0);
|
||||
BOOST_TEST(meanSquaredError<int>({0, 0, 0, 0}, 2.0) == 4.0);
|
||||
BOOST_TEST(meanSquaredError<int>({5, 5, 5, 5}, 1.0) == 16.0);
|
||||
BOOST_TEST(meanSquaredError<int>({0, 1, 2, 3}, 2.0) == 1.5);
|
||||
BOOST_TEST(meanSquaredError<int>({-4, -3, -2, -1, 0, 1, 2, 3}, -4.0) == 17.5);
|
||||
|
||||
BOOST_TEST(meanSquaredError<double>({1.3, 1.1, 0.0, 1.5, 1.1, 2.0, 1.5, 1.5}, 1.0) == 0.3575, tolerance(0.0001));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user