Is there a function to calculate linear regression for an array of points in the Accelerate Framework for iPhone?

I am looking for the fastest / easiest solution to calculate regression for a bunch of double points stored in an array.
I tried to find the right function in Accelerate view or tutorial, but no luck.

Has anyone done this?

+3
source share
1 answer

Assuming that you already know the parameters of your model, you can do this by multiplying matrix vectors. Linear regression is an internal product of the matrix of X samples and a vector theta of model parameters.

// Simple scenario for linear regression: Y = theta0 + theta1*X
int rows = 4;
int cols = 2;
double theta[] = {5.0, 2.0};
double x[] = {1.0, 10.0, 
              1.0, 20.0, 
              1.0, 30.0, 
              1.0, 40.0};
double y[rows];

// This is matrix-matrix multiplication function, 
// but vector is just a matrix with one row/column. 
vDSP_mmulD(x, 1, theta, 1, y, 1, rows, 1, cols);

NSLog(@"[%f, %f, %f, %f]", y[0], y[1], y[2], y[3]);

[25.000000, 45.000000, 65.000000, 85.000000]

+1

All Articles