- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I would like to calculate simple linear regression coefficients with IPP:
Y=a+bX
Where Y is the dependent variable, a is the y intercept, b is the gradient or slope of the line,
X is independent variable
Are there simple example how to do that with IPP?
Thanks in advance,
Constantine
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
you could do that with a few ipps functions. Here is some code which I haven't use for sometime so feel free to check for the exactness of the formula used.
[cpp]void LinearRegression_64f(const Ipp64f* pSrcX, const Ipp64f* pSrcY, int length, Ipp64f *a, Ipp64f *b) { Ipp64f cov_xy; Ipp64f mean_x; Ipp64f mean_y; Ipp64f var_x; ippsMean_64f(pSrcX, length, &mean_x); ippsMean_64f(pSrcY, length, &mean_y); ippsDotProd_64f(pSrcX, pSrcY, length, &cov_xy); cov_xy = (cov_xy / (double)length) - mean_x * mean_y; ippsStdDev_64f(pSrcX, length, &var_x); var_x = var_x * var_x; *a = cov_xy / var_x; *b = mean_y - *a * mean_x; }[/cpp]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi again,
A little mistake in the code I gave you. It's for Y = aX+b and not Y = a + bX as you asked.
Regards,
Matthieu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Maybe this implementation would gain in speed over the other one. I'll let you try it.
[cpp]void LinearRegression_64f(const Ipp64f* pSrcX, const Ipp64f* pSrcY, int length, Ipp64f *a, Ipp64f *b) { Ipp64f cov_xy; Ipp64f mean_x; Ipp64f mean_y; Ipp64f var_x; ippsMean_64f(pSrcX, length, &mean_x); ippsMean_64f(pSrcY, length, &mean_y); ippsDotProd_64f(pSrcX, pSrcY, length, &cov_xy); cov_xy = (cov_xy / (double)length) - mean_x * mean_y; ippsDotProd_64f(pSrcX, pSrcX, length, &var_x); var_x = (var_x / (double)length) - mean_x * mean_x; *a = cov_xy / var_x; *b = mean_y - *a * mean_x; }[/cpp]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Maybe this implementation would gain in speed over the other one. I'll let you try it.
void LinearRegression_64f(const Ipp64f* pSrcX, const Ipp64f* pSrcY, int length, Ipp64f *a, Ipp64f *b) { Ipp64f cov_xy; Ipp64f mean_x; Ipp64f mean_y; Ipp64f var_x; ippsMean_64f(pSrcX, length, &mean_x); ippsMean_64f(pSrcY, length, &mean_y); ippsDotProd_64f(pSrcX, pSrcY, length, &cov_xy); cov_xy = (cov_xy / (double)length) - mean_x * mean_y; ippsDotProd_64f(pSrcX, pSrcX, length, &var_x); var_x = (var_x / (double)length) - mean_x * mean_x; *a = cov_xy / var_x; *b = mean_y - *a * mean_x; }
Thanks a lot - I will try it now.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page