D = np.array([[0, 0]]) Q = np.array([[0.1, 0], [0, 0.1]]) R = np.array([[0.1]]) params = (A, B, C, D, Q, R) # Initial Values init_mu = np.array([[1], [2]]) init_V = np.array([[INIT_VAR, 0],[0, INIT_VAR]]) init_state = (init_mu, init_V) # Data Generator space_gen = KalmanSpaceGenerator(params, init_mu, init_V, 100) y_0tT, u_0tT = space_gen.generate_data() # Kalman Filter kf = KalmanFilter(params, init_mu, init_V) # Assign data (ys, us) = line_gen(100) kf.ys = y_0tT kf.us = u_0tT y_pred, ll, s = kf.smooth_filter() l1, = plt.plot(y_0tT.flatten(), label='Kalman Space', color='blue') l2, = plt.plot(y_pred.flatten(), label='Online Predictions', color='green') l3, = plt.plot(y_0tT.flatten() - y_pred.flatten(), label='Error', color='red') plt.legend(handles=[l1, l2]) plt.show()
[0, 0, 0, 0, 0], [0, 0, 0, 0, 1]]) R = np.array([[1, 0], [0, 1]]) init_mu = np.array([[0], [5], [0], [0], [0]]) init_V = np.array( [[100000000000, 100000000000, 100000000000, 100000000000, 100000000000], [100000000000, 100000000000, 100000000000, 100000000000, 100000000000], [100000000000, 100000000000, 100000000000, 100000000000, 100000000000], [100000000000, 100000000000, 100000000000, 100000000000, 100000000000], [100000000000, 100000000000, 100000000000, 100000000000, 100000000000]]) kf = KalmanFilter((A, B, C, D, Q, R), init_mu, init_V) kf.ys = obs kf.us = conds (y_pred, ll, _) = kf.smooth_filter() (x, y, t) = y_pred.shape pred = pd.DataFrame(y_pred.reshape((x, t)).T, columns=['Predicted Liquidity', 'Predicted Prices']) # Projected prices # (projection, V_projections) = kf.project(30) # plt.plot(projection[1, :, :].flatten(), c='r') # plt.show() # Ordinary Least Squares Calculation ols = sum((rmd.data.values[:, 2] - pred.values[:, 1]) * (rmd.data.values[:, 2] - pred.values[:, 1])) / 2 print(ols) # Plot results
B = np.array([[0, 0], [0, 0]]) C = np.array([[1, 0]]) D = np.array([[0, 0]]) Q = np.array([[0.1, 0], [0, 0.1]]) R = np.array([[0.1]]) params = (A, B, C, D, Q, R) # Initial Values init_mu = np.array([[1], [2]]) init_V = np.array([[INIT_VAR, 0], [0, INIT_VAR]]) init_state = (init_mu, init_V) # Data Generator space_gen = KalmanSpaceGenerator(params, init_mu, init_V, 100) y_0tT, u_0tT = space_gen.generate_data() # Kalman Filter kf = KalmanFilter(params, init_mu, init_V) # Assign data (ys, us) = line_gen(100) kf.ys = y_0tT kf.us = u_0tT y_pred, ll, s = kf.smooth_filter() l1, = plt.plot(y_0tT.flatten(), label='Kalman Space', color='blue') l2, = plt.plot(y_pred.flatten(), label='Online Predictions', color='green') l3, = plt.plot(y_0tT.flatten() - y_pred.flatten(), label='Error', color='red') plt.legend(handles=[l1, l2]) plt.show()
init_mu = np.array([[0], [5], [0], [0], [0]]) init_V = np.array([[100000000000, 100000000000, 100000000000, 100000000000, 100000000000], [100000000000, 100000000000, 100000000000, 100000000000, 100000000000], [100000000000, 100000000000, 100000000000, 100000000000, 100000000000], [100000000000, 100000000000, 100000000000, 100000000000, 100000000000], [100000000000, 100000000000, 100000000000, 100000000000, 100000000000]]) kf = KalmanFilter((A, B, C, D, Q, R), init_mu, init_V) kf.ys = obs kf.us = conds (y_pred, ll, _) = kf.smooth_filter() (x, y, t) = y_pred.shape pred = pd.DataFrame(y_pred.reshape((x, t)).T, columns=['Predicted Liquidity', 'Predicted Prices']) # Projected prices # (projection, V_projections) = kf.project(30) # plt.plot(projection[1, :, :].flatten(), c='r') # plt.show() # Ordinary Least Squares Calculation ols = sum((rmd.data.values[:, 2] - pred.values[:, 1]) * (rmd.data.values[:, 2] - pred.values[:, 1])) / 2 print(ols) # Plot results #area_ols if pred < low -> +, low <= pred <= high -> 0. high < pred -> + ax = pred.plot(x=rmd.data[ccs.date.value], y=['Predicted Prices'])