ara*_*ker 7 accelerate-framework swift
我在Stackoverflow的第一个问题...希望我的问题足够具体.
我在Swift中有一个数组,在某些日期有测量值.喜欢:
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)
Run Code Online (Sandbox Code Playgroud)
有些日子不见了,我只是没有进行测量......所有测量都应该在一条线上,或多或少.所以我想到了线性回归.我找到了Accelerate框架,但缺少文档,我找不到示例.
对于缺失的测量,我希望有一个功能,输入缺失的一天,输出最佳猜测,基于其他测量.
func bG(day: Int) -> Double {
return // return best guess for measurement
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.一月
ABa*_*ith 13
我的回答没有具体谈论加速框架,但我认为这个问题很有意思,并且认为我会给它一个刺.从我收集的内容来看,你基本上都希望创建一个最佳拟合线,并插入或推断更多的值mW.为此,我使用了最小二乘法,详细信息请参见http://hotmath.com/hotmath_help/topics/line-of-best-fit.html并使用Swift在Playgrounds中实现:
// 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
Run Code Online (Sandbox Code Playgroud)
在上面的代码中a,b请参考网站上的以下公式:
a:

b:
现在您可以创建使用最佳拟合线来插值/外推的函数mW:
func bG(day: Double) -> Double {
return m * day + c
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
bG(3) // 31.70
bG(5) // 31.52
bG(7) // 31.35
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2370 次 |
| 最近记录: |