Community
cancel
Showing results for 
Search instead for 
Did you mean: 
101 Views

Linear Regression in IPP

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

0 Kudos
4 Replies
matthieu_darbois
New Contributor III
101 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]

matthieu_darbois
New Contributor III
101 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

matthieu_darbois
New Contributor III
101 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]

101 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.

Reply