Advertisement
zvoulgaris

Weird means drill

Apr 13th, 2021
1,049
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Statistics
  2.  
  3. const tol = 1e-9 # tolerance threshold
  4. const expo = 1/3 # cube root exponent
  5.  
  6. function agm(x::Real, y::Real)
  7.     while abs(x - y) > tol
  8.         a = (x + y)/2
  9.         b = sqrt(x*y)
  10.         x, y = a, b
  11.     end
  12.  
  13.     return x
  14. end
  15.  
  16. function gmdm(x::Array{<:Real})
  17.     a = mean(x)
  18.     g = (prod(x))^(1/length(x))
  19.     m = median(x)
  20.     x = [a, g, m]
  21.     smallest, largest = extrema(x)
  22.  
  23.     while largest - smallest > tol
  24.         a = mean(x)
  25.         g = (prod(x))^expo
  26.         m = median(x)
  27.         x = [a, g, m]
  28.         smallest, largest = extrema(x)
  29.     end
  30.  
  31.     return a
  32. end
Advertisement
RAW Paste Data Copied
Advertisement