Guest User

correl, variance, covar

a guest
Mar 5th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 0.53 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 of at least 2.")
  6.  
  7.   let mx = mean(vx)
  8.   let my = mean(vy)
  9.  
  10.   for i in 0..high(vx):
  11.     let xd = vx[i] - mx
  12.     let yd = vy[i] - my
  13.     result += (xd * yd - result) / float(i + 1)
  14.  
  15. proc variance(vx: openArray[float]) : float =
  16.   covar(vx, vx)
  17.  
  18. proc correl(vx, vy: openArray[float]) : float =
  19.   covar(vx, vy) / (sqrt(variance(vx)) * sqrt(variance(vy)))
Advertisement
Add Comment
Please, Sign In to add comment