Simple function to calculate standard deviation

Post your working scripts, libraries and tools.
Banaanae
Posts: 34
Joined: 19 Feb 2024, 04:08
Contact:

Simple function to calculate standard deviation

19 Feb 2024, 04:21

Couldn't find any functions to calculate standard deviation so I made my own.

Code: Select all

StandardDeviation(arr) {
  mean := sd2 := 0

  Loop(arr.Length) {
    mean += arr[A_Index]
  }

  mean /= arr.Length

  Loop(arr.Length) {
    sd2 += (arr[A_Index] - mean) ** 2
  }

  return sd := sqrt(sd2 /= arr.Length)
}
Works well enough, if anyone else has something faster do tell
iPhilip
Posts: 853
Joined: 02 Oct 2013, 12:21

Re: Simple function to calculate standard deviation

24 Feb 2024, 14:14

@Banaanae, The version below is 10-20% faster. It uses the for-loop instead of the normal loop.

Code: Select all

StandardDeviation(arr) {
   mean := sd2 := 0
   for val in arr
      mean += val
   mean /= arr.Length
   for val in arr
      sd2 += (val - mean) ** 2
   return sqrt(sd2 / arr.Length)
}
If you were interested in using properties, the following might be more convenient:

Code: Select all

DefineStatistics()
DefineStatistics() {
   Array.Prototype.DefineProp('mean', {get: mean})
   Array.Prototype.DefineProp('sd', {get: sd})
   mean(arr) {
      local mean := 0
      for val in arr
         mean += val
      return mean / arr.length
   }
   sd(arr) {
      local sd := 0, mean := arr.mean
      for val in arr
         sd += (val - mean) ** 2
      return sqrt(sd / arr.length)
   }
}
arr := [1,2,3,4]
MsgBox arr.mean '`n' arr.sd
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
Banaanae
Posts: 34
Joined: 19 Feb 2024, 04:08
Contact:

Re: Simple function to calculate standard deviation

25 Feb 2024, 02:39

I got results ~55% faster, thank you!
image.png
image.png (5.91 KiB) Viewed 389 times

Code: Select all

arr := PopulateArray(100000)

DllCall("QueryPerformanceFrequency", "Int64*", &freq := 0)
DllCall("QueryPerformanceCounter", "Int64*", &CounterBefore := 0)
LoopStandardDeviation(arr)
DllCall("QueryPerformanceCounter", "Int64*", &CounterAfter := 0)
time1 := (CounterAfter - CounterBefore) / freq * 1000

DllCall("QueryPerformanceFrequency", "Int64*", &freq := 0)
DllCall("QueryPerformanceCounter", "Int64*", &CounterBefore := 0)
ForStandardDeviation(arr)
DllCall("QueryPerformanceCounter", "Int64*", &CounterAfter := 0)
time2 := (CounterAfter - CounterBefore) / freq * 1000


MsgBox("Standard Deviation Calculator`n`n  Loop: " time1 "`n  For-Loop: " time2)

PopulateArray(amount) {
    arr := []
    Loop(amount) {
        arr.push(Random(0,100))
    }
    return arr
}

LoopStandardDeviation(arr) {
    mean := sd2 := 0
  
    Loop(arr.Length) {
      mean += arr[A_Index]
    }
  
    mean /= arr.Length
  
    Loop(arr.Length) {
      sd2 += (arr[A_Index] - mean) ** 2
    }
  
    return sd := sqrt(sd2 /= arr.Length)
  }
  
ForStandardDeviation(arr) {
    mean := sd2 := 0
    for val in arr
       mean += val
    mean /= arr.Length
    for val in arr
       sd2 += (val - mean) ** 2
    return sqrt(sd2 / arr.Length)
}
iPhilip
Posts: 853
Joined: 02 Oct 2013, 12:21

Re: Simple function to calculate standard deviation

25 Feb 2024, 11:45

You are welcome! Great comparison work! :)
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: gdqb521 and 14 guests