barakuda_stat.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. # BaraKuda statistics misc. functions...
  2. import sys
  3. import numpy as nmp
  4. import random
  5. def least_sqr_line(ZX,ZY):
  6. Nx = len(ZX) ; Ny = len(ZY)
  7. if Nx != Ny: print 'barakuda_stat.least_sqr_line: ERROR in size!'; sys.exit(0)
  8. Sx = nmp.sum(ZX[:]) ; Sy = nmp.sum(ZY[:])
  9. Sxx = nmp.sum(ZX[:]*ZX[:]) ; Sxy = nmp.sum(ZX[:]*ZY[:])
  10. rA = ( Sx*Sy - Nx*Sxy ) / ( Sx*Sx - Nx*Sxx )
  11. rB = ( Sy - rA*Sx ) / Nx
  12. return ( rA, rB )
  13. def stat_serie(ZX,ZY):
  14. nt = len(ZX)
  15. # Least-square curve:
  16. [ A, B ] = least_sqr_line(ZX,ZY)
  17. ZYn = nmp.zeros(nt)
  18. # Removing the trend:
  19. ZYn = ZY - A*ZX[:] + B
  20. # Mean
  21. moy_y = nmp.mean(ZYn[:])
  22. # Ecart-type des Y:
  23. sigma = nmp.sqrt(nmp.sum((ZYn[:] - moy_y)*(ZYn[:] - moy_y))/nt)
  24. print 'A, B, moy_y, sigma_y =', A, B, moy_y, sigma
  25. ZYn = (ZYn - moy_y)/sigma
  26. return ZYn
  27. def std_dev(VX):
  28. Np = len(VX)
  29. mn = nmp.mean(VX)
  30. sgm = nmp.sqrt( nmp.sum((VX[:] - mn)*(VX[:] - mn)) / Np )
  31. return sgm
  32. def correl(VX,VY, Np_force=0):
  33. # Compute the Pearson correlation coefficient
  34. Nx = len(VX) ; Ny = len(VY)
  35. if Nx != Ny: print 'barakuda_stat.correl: ERROR in size! => ', Nx, Ny; sys.exit(0)
  36. Np = Nx
  37. if Np_force > 1: Np = Np_force
  38. mx = nmp.mean(VX[:]) ; my = nmp.mean(VY[:])
  39. sx = std_dev(VX) ; sy = std_dev(VY)
  40. Rxy = nmp.sum((VX[:] - mx)*(VY[:] - my)) / ( Np*sx*sy )
  41. return Rxy
  42. def cross_correl(VX, VY, klag):
  43. # Cross-correlation for a lag of klag points...
  44. Np = len(VX)
  45. if Np != len(VY): print 'barakuda_stat.cross_correl: ERROR in size! => ', Np, len(VY); sys.exit(0)
  46. if klag >= 0:
  47. Rxyl = correl(VX[:Np-klag], VY[klag:])
  48. else:
  49. Rxyl = correl(VX[-klag:] , VY[:Np+klag])
  50. return Rxyl
  51. def auto_correl(VX, klag):
  52. # Auto-correlation for a lag of klag points...
  53. Rxx = cross_correl(VX, VX, klag)
  54. return Rxx
  55. def running_mean_3(VV, l_fill_bounds=False):
  56. nv = len(VV) ; Vrm = nmp.zeros(nv)
  57. for jv in nmp.arange(1,nv-1):
  58. Vrm[jv] = (VV[jv-1] + VV[jv] + VV[jv+1]) / 3.
  59. Vrm[0] = nmp.nan ; Vrm[nv-1:nv] = nmp.nan
  60. if l_fill_bounds:
  61. jv = 0
  62. Vrm[jv] = ( VV[jv] + VV[jv+1] + VV[jv+2] ) / 3. # Asymetric !!!
  63. jv = nv-1
  64. Vrm[jv] = ( VV[jv] + VV[jv-1] + VV[jv-2] ) / 3. # Asymetric !!!
  65. return Vrm
  66. def running_mean_5(VV, l_fill_bounds=False):
  67. nv = len(VV) ; Vrm = nmp.zeros(nv)
  68. for jv in nmp.arange(2,nv-2):
  69. Vrm[jv] = (VV[jv-2] + VV[jv-1] + VV[jv] + VV[jv+1] + VV[jv+2]) / 5.
  70. Vrm[0:2] = nmp.nan ; Vrm[nv-2:nv] = nmp.nan
  71. if l_fill_bounds:
  72. jv = 1
  73. Vrm[jv] = ( VV[jv-1] + VV[jv] + VV[jv+1] ) / 3. # Asymetric !!!
  74. jv = nv-jv-1
  75. Vrm[jv] = ( VV[jv-1] + VV[jv] + VV[jv+1] ) / 3. # Asymetric !!!
  76. jv = 0
  77. Vrm[jv] = ( VV[jv] + VV[jv+1] + VV[jv+2] ) / 3. # Asymetric !!!
  78. jv = nv-1
  79. Vrm[jv] = ( VV[jv] + VV[jv-1] + VV[jv-2] ) / 3. # Asymetric !!!
  80. return Vrm
  81. def running_mean_11(VV, l_fill_bounds=False):
  82. # Input:
  83. # VV: inut vector
  84. # l_fill_bounds: fill or leave as 'nan' the 4 first and 4 last value of the vector
  85. #
  86. # Output:
  87. # Vrm : shame size as VV
  88. nv = len(VV)
  89. Vrm = nmp.zeros(nv)
  90. #
  91. for jv in nmp.arange(5,nv-5):
  92. Vrm[jv] = ( VV[jv-5] + VV[jv-4] + VV[jv-3] + VV[jv-2] + VV[jv-1] + VV[jv]
  93. + VV[jv+1] + VV[jv+2] + VV[jv+3] + VV[jv+4] + VV[jv+5] ) / 11.
  94. # About begining and end:
  95. Vrm[0:5] = nmp.nan ; Vrm[nv-5:nv] = nmp.nan
  96. if l_fill_bounds:
  97. jv = 4
  98. Vrm[jv] = ( VV[jv-4]+VV[jv-3]+VV[jv-2]+VV[jv-1]+VV[jv]+VV[jv+1]+VV[jv+2]+VV[jv+3]+VV[jv+4] ) / 9.
  99. jv = nv-jv-1
  100. Vrm[jv] = ( VV[jv-4]+VV[jv-3]+VV[jv-2]+VV[jv-1]+VV[jv]+VV[jv+1]+VV[jv+2]+VV[jv+3]+VV[jv+4] ) / 9.
  101. jv = 3
  102. Vrm[jv] = ( VV[jv-3] + VV[jv-2] + VV[jv-1] + VV[jv] + VV[jv+1] + VV[jv+2] + VV[jv+3] ) / 7.
  103. jv = nv-jv-1
  104. Vrm[jv] = ( VV[jv-3] + VV[jv-2] + VV[jv-1] + VV[jv] + VV[jv+1] + VV[jv+2] + VV[jv+3] ) / 7.
  105. jv = 2
  106. Vrm[jv] = ( VV[jv-2] + VV[jv-1] + VV[jv] + VV[jv+1] + VV[jv+2] ) / 5.
  107. jv = nv-jv-1
  108. Vrm[jv] = ( VV[jv-2] + VV[jv-1] + VV[jv] + VV[jv+1] + VV[jv+2] ) / 5.
  109. jv = 1
  110. Vrm[jv] = ( VV[jv-1] + VV[jv] + VV[jv+1] + VV[jv+2]+VV[jv+3] ) / 5. # Asymetric !!!
  111. jv = nv-jv-1
  112. Vrm[jv] = ( VV[jv-1] + VV[jv-2]+VV[jv-3] + VV[jv] + VV[jv+1] ) / 5. # Asymetric !!!
  113. jv = 0
  114. Vrm[jv] = ( VV[jv] + VV[jv+1] + VV[jv+2] + VV[jv+3] + VV[jv+4]) / 5. # Asymetric !!!
  115. jv = nv-1
  116. Vrm[jv] = ( VV[jv] + VV[jv-1] + VV[jv-2] + VV[jv-3] + VV[jv-4]) / 5. # Asymetric !!!
  117. return Vrm[:]
  118. def running_mean_21(VV):
  119. # Input:
  120. # VV: inut vector
  121. #
  122. # Output:
  123. # Vrm : shame size as VV
  124. nv = len(VV)
  125. Vrm = nmp.zeros(nv)
  126. for jv in nmp.arange(10,nv-10):
  127. Vrm[jv] = ( VV[jv-10]+ VV[jv-9] + VV[jv-8] + VV[jv-7] + VV[jv-6] \
  128. + VV[jv-5] + VV[jv-4] + VV[jv-3] + VV[jv-2] + VV[jv-1] \
  129. + VV[jv] \
  130. + VV[jv+1] + VV[jv+2] + VV[jv+3] + VV[jv+4] + VV[jv+5] \
  131. + VV[jv+6] + VV[jv+7] + VV[jv+8] + VV[jv+9] + VV[jv+10] ) / 21.
  132. # About begining and end:
  133. Vrm[0:10] = nmp.nan ; Vrm[nv-10:nv] = nmp.nan
  134. return Vrm[:]
  135. def running_mean_31(VV):
  136. # Input:
  137. # VV: inut vector
  138. #
  139. # Output:
  140. # Vrm : shame size as VV
  141. nv = len(VV)
  142. Vrm = nmp.zeros(nv)
  143. for jv in nmp.arange(15,nv-15):
  144. Vrm[jv] = ( VV[jv-15]+ VV[jv-14] +VV[jv-13] +VV[jv-12] +VV[jv-11] \
  145. + VV[jv-10]+ VV[jv-9] + VV[jv-8] + VV[jv-7] + VV[jv-6] \
  146. + VV[jv-5] + VV[jv-4] + VV[jv-3] + VV[jv-2] + VV[jv-1] \
  147. + VV[jv] \
  148. + VV[jv+1] + VV[jv+2] + VV[jv+3] + VV[jv+4] + VV[jv+5] \
  149. + VV[jv+6] + VV[jv+7] + VV[jv+8] + VV[jv+9] + VV[jv+10] \
  150. + VV[jv+11]+ VV[jv+12]+ VV[jv+13]+ VV[jv+14]+ VV[jv+15] ) / 31.
  151. return Vrm[:]
  152. def shuffle(x):
  153. lx = list(x)
  154. random.shuffle(lx)
  155. return nmp.asarray(lx)