Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def lowess(x, y, f=2. / 3., iter=3):
- '''....the number of robustifying iterations is given by iter. The
- function will run faster with a smaller number of iterations'''
- n = len(x)
- r = int(ceil(f * n))
- h = [np.sort(np.abs(x - x[i]))[r] for i in range(n)]
- w = np.clip(np.abs((x[:, None] - x[None, :]) / h), 0.0, 1.0)
- w = (1 - w ** 3) ** 3
- yest = np.zeros(n)
- delta = np.ones(n)
- for iteration in range(iter):
- for i in range(n):
- weights = delta * w[:, i]
- b = np.array([np.sum(weights * y), np.sum(weights * y * x)])
- A = np.array([[np.sum(weights), np.sum(weights * x)],
- [np.sum(weights * x), np.sum(weights * x * x)]])
- beta = linalg.solve(A, b)
- yest[i] = beta[0] + beta[1] * x[i]
- residuals = y - yest
- s = np.median(np.abs(residuals))
- delta = np.clip(residuals / (6.0 * s), -1, 1)
- delta = (1 - delta ** 2) ** 2
- return yest
Add Comment
Please, Sign In to add comment