iphonelinear-regressionaccelerate-frameworkvdsp

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


I am looking for a fastest / easiest solution to calculate regression for bunch of double points stored in array.
I have tried to find proper function in Accelerate framework or tutorial but with no luck.

Has anyone done it?


Solution

  • Assuming you already knew the parameters of your model, you can do it with matrix-vector multiplication. Linear regression is the inner product of matrix X of samples and vector Theta of the 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]
    

    For more details read intro to linear regression on wikipedia