Gilles Lesire
Gilles Lesire

Reputation: 1237

Objective-C Quadratic/Polynomial regression (Linest function in excel)

The objective-c math library seems pretty basic.

I'm looking for some statistics analysis functions like the Excel function "linest" to retrieve the quadratic or polynomial regressions of a data set with a given order.

Is there any function similar to the "linest" function for objective-c? Or a known statistics library/framework?

I have a hard time to believe I'm the first person to stumble upon this problem in iOS.

Upvotes: 1

Views: 747

Answers (2)

Lutz Lehmann
Lutz Lehmann

Reputation: 25992

The standard math library in general only gives you an interface to the elementary mathematical operations that are implemented in the FPU part of a CPU.

For linear regression you need either your own algorithm, it is not that complicated to implement in a handful of loops, or a dedicated (most likely) statistics library.


Writing your own algorithm for higher order or general regression is simple if a QR decomposition algorithm is available, for instance via bindings for LAPACK or similar. Then to solve

minimize sum (b[0]*f[0](x[k])+...+b[n]*f[n](x[k])-y[k])^2

one has just to construct the matrix [X|Y] where X[k,j]=f[j](x[k]) is the matrix of the values of the ansatz functions and Y[k]=y[k] is the column vector of the values to approximate. Apply the QR algorithm to [X|Y], identify or extract the R factor from its result and solve for b in

R*[b|1]'=0

via back-substitution.

Upvotes: 2

Gilles Lesire
Gilles Lesire

Reputation: 1237

I spend several days getting through the math and getting it in code because I couldn't find a math library for iOS with the function I needed. I wouldn't recommend anyone do to that again, it wasn't a walk in the park, so I published my solution on my github. You can find it here: https://github.com/KingIsulgard/iOS-Polynomial-Regression

It's easy to use, just give the x values and y values of the data and the order of polynomial you want to get and voila, you got it.

Hope this might help some people. Feel free to improve if you can. I'm just happy it finally worked.

Upvotes: 2

Related Questions