@H_
403_0@
有人会有一个很好的算法来测量使用Swift(v3)增长时间序列数据的峰值吗?因此,在数据流入时检测峰值.
例如.一个smooth z-wave algorithm的Swift版本.该算法似乎是合适的.
我需要检测峰值,如下所示.数据包含正数和负数.输出应该是峰值的计数器,和/或该特定样本的真/假.
样本数据集(最后一个系列的摘要):
let samples = [0.01,-0.02,0.01,-0.01,0.00,0.10,0.31,-0.10,-0.73,-0.68,0.21,1.22,0.67,-0.59,-1.04,0.06,0.42,0.07,0.03,-0.18,0.11,-0.06,0.16,-0.89,0.18,1.31,0.66,-1.62,-0.16,0.19,-0.42,0.23,-0.05,0.27,0.15,-0.50,-1.18,1.30,0.93,-1.32,0.55,-0.03,-0.23,-0.04,0.12,0.35,-0.38,-1.11,1.46,0.61,-1.16,0.29,0.54,0.02,-0.75,-0.95,1.51,0.70,-0.30,-1.48,0.13,0.50,0.01]
更新:感谢Jean-Paul为initial Swift port.但不确定z-wave算法是否适用于此数据集. lag = 10,threshold = 3,influence = 0.2适用于最后一系列数据集,但我无法找到完整数据集的组合.
问题:如果没有包含大量滞后的第一个数据样本,我需要每个峰值一个信号,算法需要进一步的工作才能提高效率.
例如.完整数据集的结果,使用Python code,并且(例如)滞后= 5,阈值= 2.5,影响= 0.7缺少系列1和2的峰值,并且在静默期显示太多误报:
完整数据集(应该产生25个峰值):
let samples = [-1.38,-0.97,-1.20,-2.06,-2.26,-0.99,-0.47,-2.61,-0.88,-0.74,-1.12,-1.19,-0.72,-1.21,-1.41,-0.27,-0.43,-1.77,-2.75,-0.61,-1.53,-1.02,-1.14,-1.06,-0.78,-2.41,-1.55,-0.44,-2.02,-1.66,-0.93,-1.51,-0.86,-1.10,-0.84,-1.26,-2.59,-0.92,-1.31,-2.40,-0.56,-1.09,-0.90,-1.34,-0.08,-0.36,-1.89,-1.60,-0.55,-1.46,-0.96,-0.98,-1.07,-1.79,-1.78,-1.54,-1.25,-1.00,-0.46,-0.20,-0.15,-0.13,-0.11,-0.09,0.20,-0.31,-1.35,1.34,0.52,0.80,-0.91,0.53,0.60,-0.83,-1.87,-0.21,1.26,0.44,0.86,0.73,-2.05,1.04,0.72,0.63,-2.14,-0.48,0.77,0.58,-1.01,-1.28,0.09,-0.07,0.05,0.25,-0.69,-1.05,-0.54,0.46,1.12,1.05,0.68,0.39,-1.61,-0.14,0.22,0.14,0.04,-0.41,-0.94,-1.03,1.10,1.03,0.79,0.69,-0.34,-1.17,-0.22,0.37,0.47,-0.12,0.43,0.95,0.64,-0.85,0.38,0.32,0.97,0.45,-0.52,0.33,0.34,-0.24,-0.45,-1.13,-0.28,1.35,0.56,0.17,-1.38,-0.76,-0.62,0.78,1.36,1.07,0.59,0.75,-1.65,-3.16,0.24,1.44,1.50,0.84,0.40,-1.50,-2.71,-1.22,1.20,1.55,0.92,-2.34,-2.28,0.36,1.41,1.56,0.89,1.16,1.65,-1.52,-1.68,-0.39,0.49,0.08,0.28,-0.26,-1.08,1.18,0.71,0.65,-0.80,-1.30,-0.64,-0.58,-1.27,0.62,-0.35,-0.33,0.26,-0.32,0.30,0.88,1.40,1.14,0.48,0.51,1.37,-0.17,-0.87,1.42,0.98,-1.59,0.00]
因此,我不确定z波算法是否适用于此类数据集.
将流畅的z-
score算法翻译成Swift
好吧,快点帮助你:这是Algo到Swift:Demo in Swift Sandbox的翻译
警告:我绝不是一个快速的程序员,因此可能会出现错误!
还要注意我已经关闭了负信号,因为OP的目的我们只想要正信号.
SWIFT代码:
import Glibc // or Darwin/ Foundation/ Cocoa/ UIKit (depending on OS)
// Function to calculate the arithmetic mean
func arithmeticMean(array: [Double]) -> Double {
var total: Double = 0
for number in array {
total += number
}
return total / Double(array.count)
}
// Function to calculate the standard deviation
func standardDeviation(array: [Double]) -> Double
{
let length = Double(array.count)
let avg = array.reduce(0,{$0 + $1}) / length
let sumOfSquaredAvgDiff = array.map { pow($0 - avg,2.0)}.reduce(0,{$0 + $1})
return sqrt(sumOfSquaredAvgDiff / length)
}
// Function to extract some range from an array
func subArray<T>(array: [T],s: Int,e: Int) -> [T] {
if e > array.count {
return []
}
return Array(array[s..<min(e,array.count)])
}
// Smooth z-score thresholding filter
func ThresholdingAlgo(y: [Double],lag: Int,threshold: Double,influence: Double) -> ([Int],[Double],[Double]) {
// Create arrays
var signals = Array(repeating: 0,count: y.count)
var filteredY = Array(repeating: 0.0,count: y.count)
var avgFilter = Array(repeating: 0.0,count: y.count)
var stdFilter = Array(repeating: 0.0,count: y.count)
// Initialise variables
for i in 0...lag-1 {
signals[i] = 0
filteredY[i] = y[i]
}
// Start filter
avgFilter[lag-1] = arithmeticMean(array: subArray(array: y,s: 0,e: lag-1))
stdFilter[lag-1] = standardDeviation(array: subArray(array: y,e: lag-1))
for i in lag...y.count-1 {
if abs(y[i] - avgFilter[i-1]) > threshold*stdFilter[i-1] {
if y[i] > avgFilter[i-1] {
signals[i] = 1 // Positive signal
} else {
// Negative signals are turned off for this application
//signals[i] = -1 // Negative signal
}
filteredY[i] = influence*y[i] + (1-influence)*filteredY[i-1]
} else {
signals[i] = 0 // No signal
filteredY[i] = y[i]
}
// Adjust the filters
avgFilter[i] = arithmeticMean(array: subArray(array: filteredY,s: i-lag,e: i))
stdFilter[i] = standardDeviation(array: subArray(array: filteredY,e: i))
}
return (signals,avgFilter,stdFilter)
}
// Demo
let samples = [0.01,0.01]
// Run filter
let (signals,stdFilter) = ThresholdingAlgo(y: samples,lag: 10,threshold: 3,influence: 0.2)
// Print output to console
print("\nOutput: \n ")
for i in 0...signals.count - 1 {
print("Data point \(i)\t\t sample: \(samples[i]) \t signal: \(signals[i])\n")
}
// Raw data for creating a plot in Excel
print("\n \n Raw data for creating a plot in Excel: \n ")
for i in 0...signals.count - 1 {
print("\(i+1)\t\(samples[i])\t\(signals[i])\t\(avgFilter[i])\t\(stdFilter[i])\n")
}
对于样本数据的结果(对于滞后= 10,阈值= 3,影响= 0.2):
更新
您可以通过对平均值和标准差的滞后使用不同的值来提高算法的性能.例如.:
// Smooth z-score thresholding filter
func ThresholdingAlgo(y: [Double],lagMean: Int,lagStd: Int,influenceMean: Double,influenceStd: Double) -> ([Int],count: y.count)
var filteredYmean = Array(repeating: 0.0,count: y.count)
var filteredYstd = Array(repeating: 0.0,count: y.count)
// Initialise variables
for i in 0...lagMean-1 {
signals[i] = 0
filteredYmean[i] = y[i]
filteredYstd[i] = y[i]
}
// Start filter
avgFilter[lagMean-1] = arithmeticMean(array: subArray(array: y,e: lagMean-1))
stdFilter[lagStd-1] = standardDeviation(array: subArray(array: y,e: lagStd-1))
for i in max(lagMean,lagStd)...y.count-1 {
if abs(y[i] - avgFilter[i-1]) > threshold*stdFilter[i-1] {
if y[i] > avgFilter[i-1] {
signals[i] = 1 // Positive signal
} else {
signals[i] = -1 // Negative signal
}
filteredYmean[i] = influenceMean*y[i] + (1-influenceMean)*filteredYmean[i-1]
filteredYstd[i] = influenceStd*y[i] + (1-influenceStd)*filteredYstd[i-1]
} else {
signals[i] = 0 // No signal
filteredYmean[i] = y[i]
filteredYstd[i] = y[i]
}
// Adjust the filters
avgFilter[i] = arithmeticMean(array: subArray(array: filteredYmean,s: i-lagMean,e: i))
stdFilter[i] = standardDeviation(array: subArray(array: filteredYstd,s: i-lagStd,stdFilter)
}
然后使用例如let(signals,stdFilter)= ThresholdingAlgo(y:samples,lagMean:10,lagStd:100,threshold:2,influenceMean:0.5,influenceStd:0.1)可以给出更好的结果:
DEMO