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

Add cumulative moving average function #4555

Merged
merged 1 commit into from Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/shogun/mathematics/linalg/LinalgNamespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,36 @@ namespace shogun
return infer_backend(a)->mean(a);
}

/** Method that updates moving mean vector with new datum point.
*
* @param cma the previous moving mean
* @param datum new datum point
* @param n number of previous data points including the new datum point
*/
template <typename T>
void update_mean(SGVector<T>& cma, const SGVector<T>& datum, int32_t n)
{
REQUIRE(n > 0, "Number of data points (%d) must be at least 1", n);
T alpha = (T)(1.0) / n;
T beta = 1 - alpha;
add(datum, cma, cma, alpha, beta);
}

/** Method that updates moving mean scalar with new datum point.
*
* @param cma the previous moving mean
* @param datum new datum point
* @param n number of previous data points including the new datum point
*/
template <typename T>
void update_mean(T& cma, const T datum, int32_t n)
{
REQUIRE(n > 0, "Number of data points (%d) must be at least 1", n);
T alpha = (T)(1.0) / n;
T beta = 1 - alpha;
cma = alpha * datum + beta * cma;
}

/**
* Method that computes the standard deviation of vectors or matrices
* composed of real numbers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,63 @@ TYPED_TEST(LinalgBackendEigenAllTypesTest, SGMatrix_mean)
EXPECT_NEAR(result, 4, get_epsilon<TypeParam>());
}

TYPED_TEST(LinalgBackendEigenNonIntegerTypesTest, Scalar_update_mean)
{
const index_t n = 1e5;
const TypeParam scale = 2;

TypeParam mean_increasing = 0;
TypeParam mean_decreasing = 0;
for (index_t i = 0; i < n; i++)
{
TypeParam datum_inc(scale * i);
TypeParam datum_dec(scale * (n - 1 - i));
update_mean(mean_increasing, datum_inc, i + 1);
update_mean(mean_decreasing, datum_dec, i + 1);
}
TypeParam mean_true((scale * (n - 1)) / 2.0);

auto epsilon = n * get_epsilon<TypeParam>();
EXPECT_NEAR(mean_increasing, mean_true, epsilon);
EXPECT_NEAR(mean_decreasing, mean_true, epsilon);
EXPECT_THROW(
update_mean(mean_increasing, mean_increasing, 0), ShogunException);
}

TYPED_TEST(LinalgBackendEigenNonIntegerTypesTest, SGVector_update_mean)
{
const index_t n = 1e5;
const index_t length = 3;
const TypeParam scale = 2;

SGVector<TypeParam> mean_increasing(length);
SGVector<TypeParam> mean_decreasing(length);
SGVector<TypeParam>::fill_vector(mean_increasing, length, 0);
SGVector<TypeParam>::fill_vector(mean_decreasing, length, 0);

SGVector<TypeParam> datum(length);
for (int i = 0; i < n; i++)
{
datum.range_fill(i);
datum.scale(scale);
update_mean(mean_increasing, datum, i + 1);
datum.range_fill(n - 1 - i);
datum.scale(scale);
update_mean(mean_decreasing, datum, i + 1);
}

auto epsilon = n * get_epsilon<TypeParam>();
for (int j = 0; j < length; j++)
{
EXPECT_NEAR(
mean_increasing[j], ((2 * j + n - 1) * scale) / 2.0, epsilon);
EXPECT_NEAR(
mean_decreasing[j], ((2 * j + n - 1) * scale) / 2.0, epsilon);
}
EXPECT_THROW(
update_mean(mean_increasing, mean_increasing, 0), ShogunException);
}

TYPED_TEST(LinalgBackendEigenAllTypesTest, SGMatrix_std_deviation_colwise)
{
const index_t nrows = 3, ncols = 3;
Expand Down