!pip install yfinance import yfinance as yf import datetime as dt end = dt.datetime.today() start = dt.datetime(end.year-10, end.month, end.day) df = yf.download('005930.KS', start, end) df import numpy as np import matplotlib.pyplot as plt plt.figure(figsize=(14,9), dpi=100) plt.plot(df['Adj Close'], label='Samsung Electronics') plt.xlabel('date') plt.ylabel('stock price') plt.grid() plt.legend() plt.show() import numpy as np import matplotlib.pyplot as plt y = df['Adj Close'].values y -= np.mean(y) y /= np.std(y) n = len(y) plt.figure(figsize=(14,9), dpi=100) plt.plot(y, label='Samsung Electronics') plt.xlabel('days') plt.ylabel('normalized price') plt.grid() plt.legend() plt.show() lam = 1e5 A = np.eye(n) D = np.zeros((n-2,n)) for i in range(D.shape[0]): D[i,i:i+3] = [1, -2, 1] A_tilde = np.vstack((A,np.sqrt(lam)*D)) y_tilde = np.hstack((y,np.zeros(n-2))) xhat = np.linalg.lstsq(A_tilde,y_tilde,rcond=None)[0] plt.figure(figsize=(14,9), dpi=100) plt.plot(y, alpha=0.4, label='Samsung Electronics') plt.plot(xhat,linewidth=3, label=r'$\ell_2$ trend filter') plt.xlabel('days') plt.ylabel('normalized price') plt.grid() plt.legend() plt.show() import scipy.sparse as ssp import scipy.sparse.linalg as sla lam = 1e5 As = ssp.eye(n) Ds = ssp.spdiags([np.ones(n), -2*np.ones(n), np.ones(n)], [0, 1, 2], n-2, n) As_tilde = ssp.vstack((As, np.sqrt(lam)*Ds)) y_tilde = np.hstack((y, np.zeros(n-2))) xhats = sla.lsqr(As_tilde,y_tilde)[0] plt.figure(figsize=(14,9), dpi=100) plt.plot(y, alpha=0.4, label='Samsung Electronics') plt.plot(xhats,linewidth=3, label=r'$\ell_2$ trend filter (sparse)') plt.xlabel('days') plt.ylabel('normalized price') plt.grid() plt.legend() plt.show() import cvxpy as cp x = cp.Variable(n) obj = cp.Minimize( cp.norm(y-As@x,1) + 100*cp.norm(Ds@x,1) ) prob = cp.Problem(obj) prob.solve(solver=cp.ECOS, verbose=False, max_iters=100) plt.figure(figsize=(14,9), dpi=100) plt.plot(y, alpha=0.4, label='Samsung Electronics') plt.plot(xhats,linewidth=4, alpha=0.6, label=r'$\ell_2$ trend filter') plt.plot(x.value,linewidth=3, label=r'$\ell_1$ trend filter') plt.xlabel('days') plt.ylabel('normalized price') plt.grid() plt.legend() plt.show() z = 10*np.random.randn(n) for i in range(n): if np.random.rand() > 0.01: z[i] = 0 y_cor = y + z plt.figure(figsize=(14,9), dpi=100) plt.plot(y_cor, alpha=0.4, label='Samsung Electronics (corrupted)') plt.xlabel('days') plt.ylabel('normalized price') plt.ylim(-1.5,3.5) plt.grid() plt.legend() plt.show() b_tilde_cor = np.hstack((y_cor, np.zeros(n-2))) xhats_cor = sla.lsqr(As_tilde,b_tilde_cor)[0] plt.figure(figsize=(14,9), dpi=100) plt.plot(y_cor, alpha=0.4, label='Samsung Electronics (corrupted)') plt.plot(xhats,linewidth=4, alpha=0.6, label=r'$\ell_2$ trend filter (on clean data)') plt.plot(xhats_cor,linewidth=4, alpha=0.6, label=r'$\ell_2$ trend filter (on corrupted data)') plt.xlabel('days') plt.ylabel('normalized price') plt.ylim(-1.5,3.5) plt.grid() plt.legend() plt.show() x_cor = cp.Variable(n) obj = cp.Minimize( cp.norm(y_cor-As@x_cor,1) + 100*cp.norm(Ds@x_cor,1) ) prob = cp.Problem(obj) prob.solve(solver=cp.ECOS, verbose=False, max_iters=100) plt.figure(figsize=(14,9), dpi=100) plt.plot(y_cor, alpha=0.4, label='Samsung Electronics') plt.plot(x.value, linewidth=4, label=r'$\ell_1$ trend filter (on clean data)') plt.plot(x_cor.value, linewidth=3, label=r'$\ell_1$ trend filter (on corrupted data)') plt.xlabel('days') plt.ylabel('normalized price') plt.ylim(-1.5,3.5) plt.grid() plt.legend() plt.show()