Guest User

covar, variance, correl

a guest
Mar 5th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 0.59 KB | None | 0 0
  1. import logging, math
  2.  
  3. proc covar(vx, vy : openArray[float]) : float =
  4.   if len(vx) != len(vy) or len(vx) < 2:
  5.     error("Covar requires equal length arrays, length greater than 2.")
  6.  
  7.   var result : float = 0.0
  8.  
  9.   var mx = mean(vx)
  10.   var my = mean(vy)
  11.  
  12.   for i in 0..len(vx)-1:
  13.     var xd = vx[i] - mx
  14.     var yd = vy[i] - my
  15.     result += (xd * yd - result) / (float(i) + 1.0)
  16.  
  17.   return result
  18.  
  19. proc variance(vx : openArray[float]) : float =
  20.   return covar(vx, vx)
  21.  
  22. proc correl(vx, vy : openArray[float]) : float =
  23.   return covar(vx, vy) / (sqrt(variance(vx)) * sqrt(variance(vy)))
Advertisement
Add Comment
Please, Sign In to add comment