def measure_likelihood(self, y: np.array, u: np.array) -> float: # Time before update. (mus indexed at +1 for initial mu) tm1 = self.mus.shape[Axis.time] - 2 (A, B, C, D, Q, R) = self.parameters(tm1) (mu, V) = self.state(tm1) (y_pred, mu_pred) = self.predict(A, B, C, D, mu, u) V_pred = self.predict_covariance(A, V, Q) # Update observation self.observe(tm1 + 1, y, u) # Update State return KalmanFilter.update(self, tm1 + 1, mu_pred, V_pred, y, y_pred, compute_likelihood=True)
[0]]) init_V = np.eye(A.shape[0], A.shape[1]) return (A, B, C, D, Q, R), init_mu, init_V # Split the data into first half and second half (r, c, ts) = obs.shape training_start = 0 test_start = 252 n_data_points = 252 test_end = test_start + n_data_points training_ys = obs[:, :, training_start:test_start] training_us = conds[:, :, training_start:test_start] test_ys = obs[:, :, test_start:test_end] test_us = conds[:, :, test_start:test_end] (kf, ll_hists) = KalmanFilter.fit(training_ys, training_us, gen_params(1, 4), iters=4) # Show Kalman Filter hist # for l in ll_hists: # print(l, sum(l)) # Get test values kf.ys = obs[:, :, :test_end] kf.us = conds[:, :, :test_end] (y_pred, ll, _) = kf.filter() y_pred = y_pred[:, :, test_start:test_end] (x, y, pred_ts) = y_pred.shape pred = pd.DataFrame(y_pred.reshape((x, pred_ts)).T, columns=['Predicted Prices'])
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()
return (A, B, C, D, Q, R), init_mu, init_V # Split the data into first half and second half (r, c, ts) = obs.shape training_start = 0 test_start = 252 n_data_points = 252 test_end = test_start + n_data_points training_ys = obs[:, :, training_start:test_start] training_us = conds[:, :, training_start:test_start] test_ys = obs[:, :, test_start:test_end] test_us = conds[:, :, test_start:test_end] (kf, ll_hists) = KalmanFilter.fit(training_ys, training_us, gen_params(1, 4), iters=4) # Show Kalman Filter hist # for l in ll_hists: # print(l, sum(l)) # Get test values kf.ys = obs[:, :, :test_end] kf.us = conds[:, :, :test_end] (y_pred, ll, _) = kf.filter() y_pred = y_pred[:, :, test_start:test_end] (x, y, pred_ts) = y_pred.shape pred = pd.DataFrame(y_pred.reshape((x, pred_ts)).T,
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()
def observe(self, t, y_t, u_t): KalmanFilter.observe(self, t, y_t, u_t)
def project(self, n): return KalmanFilter.project(self, n)
def __init__(self, init_params, init_mu, init_V): Particle.__init__(self) KalmanFilter.__init__(self, init_params, init_mu, init_V)
[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]) C = np.array([[1, 0, 0, 0, 0], [0, 1, 0, 0, 0]]) D = np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]) Q = np.array([[0.8817, 0, 0, 0, 0], [0, 17.81, 0, 0, 0], [0, 0, 1, 0, 0], [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]) *
[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)
# Check if we have already trained a model for this. # TODO Move to a caching module, to hide the cache and restore kalman filters. # TODO Simplify Caching code possible_files = cache_utils.get_matching_ids("kalman", stock, training_start_dates[period], test_start_dates[period], True, cache_dir=CACHE_DIR) identified_cached_kalman = [kalman_identified(f, data_manager.features(), True, cache_dir=CACHE_DIR) for f in possible_files] if any(identified_cached_kalman): # Debug purposes if len(identified_cached_kalman) > 1: raise Exception("There is a problem with the cache!") # Get the identified: kalman_files = [possible_files[i] for i, is_match in enumerate(identified_cached_kalman) if is_match] cached_kalman = kalman_files.pop(0) kf = KalmanFilter.restore(cached_kalman, cache_dir=CACHE_DIR) print("RESTORED {cached}.".format(cached=cached_kalman)) else: # Train a new kalman filter and cache it kf = None for n_its in range(8, 0, -1): run_title = "{stock} from period {start} - {finish} with {n_its} EM iterations"\ .format(stock=stock, start=utils.pretty_date_str(training_start_dates[period]), finish=utils.pretty_date_str(test_start_dates[period]), n_its=n_its) kf = kalman_gym.select_best_model(training_ys, training_us, iters=n_its, min_fit=n_obs - 10, n_models=25, run_title=run_title) if kf is not None: break if kf is None: