Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- t = np.arange(1,10)
- y = np.random.uniform(0.0,3.0,size=9)
- y = np.sort(y)
- y0 = 2
- # Adjusted Log Transformation (for when y is negative)
- def transform(y):
- return np.log(1+y-np.min(y))
- def invtransform(trans_y, orig_y):
- return np.exp(trans_y)-1+min(orig_y)
- # Just some statistic
- def compute_r(y0, ts, ys, n):
- r = 0
- for i in range(0,n):
- r = r + ts[i] * (np.log(ys[i])-np.log(y0)) / (ts[i] ** 2)
- return r
- rs = []
- for n in range(1,len(t)+1):
- rs.append(compute_r(y0, t, y, n))
- rs = np.array(rs)
- xs = np.arange(0, len(rs))
- plt.plot(xs, np.array(rs), 'ro', label='orig.')
- plt.plot(xs, transform(np.array(rs)), 'o', label='transformed')
- transy = transform(np.array(rs))
- coefs = np.polyfit(xs,transy, 1)
- fit = coefs[1]+coefs[0]*xs
- fitt = invtransform(fit, np.array(rs))
- plt.plot(xs, fit, 'orange', label='fit', color='blue')
- plt.plot(xs, fitt, 'orange', label='fit-backtransformed')
- plt.legend(loc='upper right')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement