Intel® Integrated Performance Primitives
Deliberate problems developing high-performance vision, signal, security, and storage applications.
6739 Discussions

## Linear Regression in IPP

Beginner
761 Views

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

4 Replies
New Contributor III
761 Views

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]```

New Contributor III
761 Views

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

New Contributor III
761 Views

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]```

Beginner
761 Views

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.