gpt4 book ai didi

arrays - Swift Accelerate 计算均值和标准差

转载 作者:搜寻专家 更新时间:2023-10-31 22:12:16 25 4
gpt4 key购买 nike

我正在查看 Accelerate 以计算 Swift 中数组的均值和标准差。

我能做到。我如何计算标准偏差?

let rr: [Double] = [ 18.0, 21.0, 41.0, 42.0, 48.0, 50.0, 55.0, 90.0 ]

var mn: Double = 0.0

vDSP_meanvD(rr, 1, &mn, vDSP_Length(rr.count))

print(mn) // prints correct mean as 45.6250

// Standard Deviation should be 22.3155

最佳答案

您可以计算平均值的标准偏差和均方值(比较 https://en.wikipedia.org/wiki/Standard_deviation#Identities_and_mathematical_propertieshttps://en.wikipedia.org/wiki/Algebraic_formula_for_the_variance ):

import Accelerate

let rr: [Double] = [ 18.0, 21.0, 41.0, 42.0, 48.0, 50.0, 55.0, 90.0 ]

var mn: Double = 0.0 // mean value
vDSP_meanvD(rr, 1, &mn, vDSP_Length(rr.count))

var ms: Double = 0.0 // mean square value
vDSP_measqvD(rr, 1, &ms, vDSP_Length(rr.count))

let sddev = sqrt(ms - mn * mn) * sqrt(Double(rr.count)/Double(rr.count - 1))

print(mn, sddev)
// 45.625 22.315513501982

或者(对于 iOS 9.0 及更高版本或 macOS 10.11 及更高版本),使用 vDSP_normalizeD :

var mn = 0.0
var sddev = 0.0
vDSP_normalizeD(rr, 1, nil, 1, &mn, &sddev, vDSP_Length(rr.count))
sddev *= sqrt(Double(rr.count)/Double(rr.count - 1))

print(mn, sddev)
// 45.625 22.315513501982

关于arrays - Swift Accelerate 计算均值和标准差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42133107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com