Advertisement
Guest User

Untitled

a guest
Jan 1st, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. t = np.arange(1,10)
  5. y = np.random.uniform(0.0,3.0,size=9)
  6. y = np.sort(y)
  7.  
  8. y0 = 2
  9.  
  10. # Adjusted Log Transformation (for when y is negative)
  11. def transform(y):
  12.     return np.log(1+y-np.min(y))
  13.  
  14. def invtransform(trans_y, orig_y):
  15.     return np.exp(trans_y)-1+min(orig_y)
  16.  
  17. # Just some statistic
  18. def compute_r(y0, ts, ys, n):
  19.     r = 0
  20.     for i in range(0,n):
  21.         r = r + ts[i] * (np.log(ys[i])-np.log(y0)) / (ts[i] ** 2)
  22.     return r
  23.  
  24. rs = []
  25. for n in range(1,len(t)+1):
  26.     rs.append(compute_r(y0, t, y, n))
  27. rs = np.array(rs)
  28.  
  29. xs = np.arange(0, len(rs))
  30. plt.plot(xs, np.array(rs), 'ro', label='orig.')
  31. plt.plot(xs, transform(np.array(rs)), 'o', label='transformed')
  32.  
  33. transy = transform(np.array(rs))
  34.  
  35. coefs = np.polyfit(xs,transy, 1)
  36. fit = coefs[1]+coefs[0]*xs
  37. fitt = invtransform(fit, np.array(rs))
  38.  
  39. plt.plot(xs, fit, 'orange', label='fit', color='blue')
  40. plt.plot(xs, fitt, 'orange', label='fit-backtransformed')
  41. plt.legend(loc='upper right')
  42. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement