Linear Regression - Structure Acceleration in Swift

My first question is here at Stackoverflow ... I hope my question is quite specific.

I have an array in Swift with dimensions at specific dates. How:

var myArray:[(day: Int, mW: Double)] = [] myArray.append(day:0, mW: 31.98) myArray.append(day:1, mW: 31.89) myArray.append(day:2, mW: 31.77) myArray.append(day:4, mW: 31.58) myArray.append(day:6, mW: 31.46) 

A few days are not enough, I just did not take the measurement ... All measurements should be on the line, more or less. So I thought of linear regression. I found the Accelerate structure, but the documentation is missing and I cannot find examples.

For missing dimensions, I would like to have a function to enter the missing day and as a conclusion to the best guess based on other dimensions.

 func bG(day: Int) -> Double { return // return best guess for measurement } 

Thanks for helping. Yang

+5
source share
2 answers

My answer does not specifically mention the Accelerate Framework, however, I thought this question was interesting, and I thought I would give it a hit. From what I'm going to, you basically want to create a line of best fit and interpolate or extrapolate more mW values ​​to it. To do this, I used the least square method described in detail here: http://hotmath.com/hotmath_help/topics/line-of-best-fit.html and implemented this on playgrounds using Swift:

 // The typealias allows us to use '$X.day' and '$X.mW', // instead of '$X.0' and '$X.1' in the following closures. typealias PointTuple = (day: Double, mW: Double) // The days are the values on the x-axis. // mW is the value on the y-axis. let points: [PointTuple] = [(0.0, 31.98), (1.0, 31.89), (2.0, 31.77), (4.0, 31.58), (6.0, 31.46)] // When using reduce, $0 is the current total. let meanDays = points.reduce(0) { $0 + $1.day } / Double(points.count) let meanMW = points.reduce(0) { $0 + $1.mW } / Double(points.count) let a = points.reduce(0) { $0 + ($1.day - meanDays) * ($1.mW - meanMW) } let b = points.reduce(0) { $0 + pow($1.day - meanDays, 2) } // The equation of a straight line is: y = mx + c // Where m is the gradient and c is the y intercept. let m = a / b let c = meanMW - m * meanDays 

In the above code a and b on the website, refer to the following formula:

a : enter image description here

b enter image description here

Now you can create a function that uses the line of best fit to interpolate / extrapolate mW :

 func bG(day: Double) -> Double { return m * day + c } 

And use it like this:

 bG(3) // 31.70 bG(5) // 31.52 bG(7) // 31.35 
+10
source

If you want to perform fast linear regressions in Swift, I suggest using Upsurge . It provides a number of simple functions that wrap the acceleration library, and so you get the benefits of SIMD on iOS or OSX without having to worry about the complexity of vDSP calls.

For linear regression with basic Upsurge functions, simply:

  let meanx = mean (x)
 let meany = mean (y)
 let meanxy = mean (x * y)
 let meanx_sqr = measq (x)

 let slope = (meanx * meany - meanxy) / (meanx * meanx - meanx_sqr)
 let intercept = meany - slope * meanx

This is essentially what is implemented in the linregress function.

You can use it with the [Double] array, other classes such as RealArray (supplied with Upsurge), or your own objects if they can expose continuous memory.

So, a script to meet your needs would look like this:

  #! / usr / bin / env cato

 import upsurge

 typealias PointTuple = (day: Double, mW: Double)

 var myArray: [PointTuple] = []

 myArray.append ((0, 31.98))
 myArray.append ((1, 31.89))
 myArray.append ((2, 31.77))
 myArray.append ((4, 31.58))
 myArray.append ((6, 31.46))

 let x = myArray.map {$ 0.day}
 let y = myArray.map {$ 0.mW}

 let (slope, intercept) = Upsurge.linregress (x, y)

 func bG (day: Double) -> Double {
     return slope * day + intercept
 }

(I left the attachments, and did not use literals, since you probably programmatically add your array if it is of considerable length)

and full disclaimer: I entered the linregress code. Hopefully at some point in the future we will also add a definition factor.

+3
source

All Articles