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

Add cumulative moving average function #4555

merged 1 commit into from Mar 6, 2019

Conversation

ghost
Copy link

@ghost ghost commented Mar 3, 2019

Moving average is implemented in the form average_n = (1-alpha)average_{n-1} + alphax. This has an additional multiplication compared to average_n = average_{n-1} + alpha*(x-average_n), but it should has less rounding error when alpha is small (not sure).

T alpha = (T)(1.0) / n;
T beta = 1 - alpha;
linalg::add<T>(datum, cma, result, alpha, beta);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should do a round of clang-format at some point to fix the indents!

template<class T>
static void cumulative_moving_average(
const SGVector<T>& cma, const SGVector<T>& datum,
int32_t n, SGVector<T>& result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to return the result rather than modify the reference?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving averages imply that something is modified so I think is good to just modify the given vector wih the new datum. Not even sure we need the result parameter. Would just call the method “update_mean” and then have only 3 parameters: the mean, the new vector, and n

@ghost
Copy link
Author

ghost commented Mar 4, 2019

It seems that C's "complex.h" in both clang and gcc defined macros that interfered with the codebase and caused build failures. Including the linalg namespace in "Statistics.h" revealed this. Luckily C++'s "complex" doesn't have this problem. Lapack is the only library that uses "complex.h" but it has the option to use anything else (in lapacke.h)

/* Complex type (single precision) */
#ifndef lapack_complex_float
#include <complex.h>
#define lapack_complex_float    float _Complex
#endif
/* Complex type (double precision) */
#ifndef lapack_complex_double
#include <complex.h>
#define lapack_complex_double   double _Complex
#endif

So

#include <complex>
#define lapack_complex_float std::complex<float>
#define lapack_complex_double std::complex<double>

before including "Eigen/Eigen" (which includes lapacke) seems to fix the problem for clang and gcc.

@karlnapf
Copy link
Member

karlnapf commented Mar 4, 2019

Solution: move this to the linalg code. Sorry I didn’t realise earlier that you defined this in the statistics module. Check out how things like the mean are defined in there

@ghost
Copy link
Author

ghost commented Mar 4, 2019

I think it would be better if the main problem is solved, since just including linalg in Statistics cause these problems . In clang for example, <complex.h> defines 'I' for complex numbers and google tests uses I as a template parameter name. Check this issue Reference-LAPACK/lapack#320.
Also check the build report for clang here https://dev.azure.com/shogunml/shogun/_build/results?buildId=769

@karlnapf
Copy link
Member

karlnapf commented Mar 4, 2019

Yeah you are right of course. My term solution wasn’t appropriate ;)
I’ll check into this later (on mobile atm), maybe @gf712 also has an idea.
I think we should solve this in yet another pr to keep things separate.

And Nevertheless, the averaging functions should live in the linalg module. Which also makes this pr independent of the complex problem. Thanks for spotting that btw :)

@ghost ghost changed the title Add cumulative and exponential moving averages functions Add cumulative moving average function Mar 5, 2019
@ghost ghost marked this pull request as ready for review March 5, 2019 19:40
@gf712 gf712 mentioned this pull request Mar 6, 2019
Copy link
Member

@karlnapf karlnapf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, clean addition. Fine to merge from my side. Thanks!

@karlnapf karlnapf merged commit 901c04b into shogun-toolbox:develop Mar 6, 2019
vigsterkr pushed a commit to vigsterkr/shogun that referenced this pull request Mar 9, 2019
ktiefe pushed a commit to ktiefe/shogun that referenced this pull request Jul 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants