Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Intel Community
- Software
- Software Development SDKs and Libraries
- Intel® oneAPI Math Kernel Library
- Computing Variance of Multiple Rows

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Alfredo_L_

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-19-2017
06:51 AM

122 Views

Computing Variance of Multiple Rows

I'm having trouble figuring out how to compute the variance of multiple rows in a Matrix.

Suppose I have an M row by N column matrix X for which I need to compute N different variances, one for each row in a row-major (C-style) matrix.

For a concrete example, suppose my matrix contains time series data containing "periods" observations per day for "days" days. I want to calculate the variance for each day's "periods" observations and have an array "vars" containing "days" variances (one for each day). Below is the code I've attempted to use but it only produces one variance (and the rest, zeroes).

/// \brief computes one variance per day given a matrix of daily time series /// \param obsv The matrix containing the time series /// \param days Number of days in the matrix obsv /// \param periods Number of periods per day /// \param vars Output array for daily variances void daily_variances(double *obsv, int days, int periods, double *vars) { VSLSSTaskPtr task; double *w = nullptr; //weights for VSL routine MKL_INT dim, n, xstorage; dim = days; n = periods; xstorage = VSL_SS_MATRIX_STORAGE_ROWS; //we're using C-style row-major matrices double *means = static_cast<double *>(mkl_malloc((size_t) days, 64)); double *raw2m = static_cast<double *>(mkl_malloc((size_t) days, 64)); std::fill_n(means, days, 0.0); std::fill_n(raw2m, days, 0.0); int status = vsldSSNewTask(&task, &dim, &n, &xstorage, obsv, w, nullptr); if (status != VSL_STATUS_OK) throw std::logic_error("MKL issue"); status = vsldSSEditMoments(task, means, raw2m, nullptr, nullptr, vars, nullptr, nullptr); if (status != VSL_STATUS_OK) throw std::logic_error("MKL issue"); status = vsldSSCompute(task, VSL_SS_2C_MOM, VSL_SS_METHOD_1PASS); if (status != VSL_STATUS_OK) throw std::logic_error("MKL issue"); mkl_free(means); mkl_free(raw2m); vslSSDeleteTask(&task); }

1 Solution

Ilya_B_Intel

Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-19-2017
11:11 PM

122 Views

Ah, yes.

mkl_malloc allocates data with size in bytes, thus, correct code will be:

mkl_malloc(sizeof(double)*days, 64)

Link Copied

7 Replies

Ilya_B_Intel

Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-19-2017
07:30 AM

122 Views

Hello Alfredo,

Clarify question.

Does layout of your data match the following C array?

double obsv[days][periods];

Alfredo_L_

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-19-2017
07:35 AM

122 Views

ILYA B. (Intel) wrote:

Hello Alfredo,

Clarify question.

Does layout of your data match the following C array?

double obsv[days][periods];

Correct. My matrix is in row-major format and not Fortran-style column-major format. So for instance, to access the n-th period of day "d" I would do obsv

Ilya_B_Intel

Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-19-2017
07:51 AM

122 Views

In this case code looks ok to me. Do you see similar problems with means or 2nd order raw moments?

Alfredo_L_

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-19-2017
06:37 PM

122 Views

ILYA B. (Intel) wrote:

In this case code looks ok to me. Do you see similar problems with means or 2nd order raw moments?

So, I was passing the wrong value for "days" as verified by the debugger...

*however*

Now that I fixed that, I get the correct result ONLY if using "new" to allocate the mean and raw 2nd moment arrays. If I use mkl_malloc I get a segmentation fault. Any ideas?

Gennady_F_Intel

Moderator

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-19-2017
08:45 PM

122 Views

Ilya_B_Intel

Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-19-2017
11:11 PM

123 Views

Ah, yes.

mkl_malloc allocates data with size in bytes, thus, correct code will be:

mkl_malloc(sizeof(double)*days, 64)

Alfredo_L_

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-21-2017
10:11 AM

122 Views

ILYA B. (Intel) wrote:

Ah, yes.

mkl_malloc allocates data with size in bytes, thus, correct code will be:

mkl_malloc(sizeof(double)*days, 64)

Whoops... I can't believe I forgot that. That was indeed the issue.

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

For more complete information about compiler optimizations, see our Optimization Notice.