def __init__(self, dt, conv_speed, x0=np.zeros((1, 4)), covar0=5 * np.eye(4).reshape((1, 4, 4))): self.dt = dt self.conv_speed = conv_speed self.state_dim = 4 # --- parameters --- self.A = np.array([[1, 0, dt, 0], [0, 1, 0, dt], [0, 0, 0, 0], [0, 0, 0, 0]]) self.B = np.array([[0, 0], [0, 0], [1, 0], [0, 1]]) self.u = np.array([ conv_speed[1] * np.cos(conv_speed[0]), conv_speed[1] * np.sin(conv_speed[0]) ]) #conveyer speed factored into x and y self.C = np.eye(4) self.d = np.zeros(4, ) self.R = 0.7 * np.eye(4) # state (process) noise covariance self.Q = 1.0 * np.eye(4) # observation noise covariance self.x0 = x0 self.covar0 = covar0 transition_matrix = self.A transition_offset = np.matmul(self.B, self.u) # B*u = b*v_conv observation_matrix = self.C observation_offset = self.d transition_covariance = self.R observation_covariance = self.Q initial_state_mean = self.x0 initial_state_covariance = self.covar0 self.kf = KalmanFilter(transition_matrix, observation_matrix, transition_covariance, observation_covariance, transition_offset, observation_offset, initial_state_mean, initial_state_covariance) self.states_means = self.x0 # list of predictions of states means from KF self.states_covars = self.covar0 # list of predictions of states covariances from KF self.observations = self.x0 # list of the observations
def _process_update(self, source: str, timestamp: int, data: Dict[str, float]): result = {} if input.size() >= self.length: independent_var = self.first_input.get_by_idx_range(key=None, start_idx=0, end_idx=-1) symbol_set = set(self.first_input.keys) depend_symbol = symbol_set.difference(self.first_input.default_key) depend_var = self.first_input.get_by_idx_range(key=depend_symbol, start_idx=0, end_idx=-1) obs_mat = np.vstack([ independent_var.values, np.ones(independent_var.values.shape) ]).T[:, np.newaxis] model = KalmanFilter(n_dim_obs=1, n_dim_state=2, initial_state_mean=np.zeros(2), initial_state_covariance=np.ones((2, 2)), transition_matrices=np.eye(2), observation_matrices=obs_mat, observation_covariance=1.0, transition_covariance=self.trans_cov) state_means, state_covs = model.filter(depend_var.values) slope = state_means[:, 0][-1] result[Indicator.VALUE] = slope result[KalmanFilteringPairRegression.SLOPE] = slope result[KalmanFilteringPairRegression.SLOPE] = state_means[:, 1][-1] self.add(timestamp=timestamp, data=result) else: result[Indicator.VALUE] = np.nan result[KalmanFilteringPairRegression.SLOPE] = np.nan result[KalmanFilteringPairRegression.SLOPE] = np.nan self.add(timestamp=timestamp, data=result)
def KalmanSequence(size, a, rand): # X_{t+1} = a*X_t + noise ########## # specify parameters random_state = np.random.RandomState(rand) transition_matrix = [[a]] transition_offset = [0] observation_matrix = np.eye(1) #+ random_state.randn(2, 2) * 0.1 observation_offset = [0] transition_covariance = np.eye(1) observation_covariance = np.eye(1) #+ random_state.randn(2, 2) * 0.1 initial_state_mean = [0] initial_state_covariance = [[1]] # for 2-dim # random_state = np.random.RandomState(0) # transition_matrix = [[a, 0], [0, a]] # transition_offset = [0, 0] # observation_matrix = np.eye(2) #+ random_state.randn(2, 2) * 0.1 # observation_offset = [0, 0] # transition_covariance = np.eye(2) # observation_covariance = np.eye(2) #+ random_state.randn(2, 2) * 0.1 # initial_state_mean = [5, -5] # initial_state_covariance = [[1, 0], [0, 1]] # sample from model kf = KalmanFilter( transition_matrix, observation_matrix, transition_covariance, observation_covariance, transition_offset, observation_offset, initial_state_mean, initial_state_covariance, random_state=random_state ) states, observations = kf.sample(n_timesteps=size, initial_state=initial_state_mean) # estimate state with filtering and smoothing filtered_state_estimates = kf.filter(states)[0] # filtered_state_estimates = kf.filter(observations)[0] # smoothed_state_estimates = kf.smooth(observations)[0] return states, observations, filtered_state_estimates
def __init__(self, init_state, transition_matrix, observation_matrix, n_iter=5, train_size=15): """ Create the Kalman filter. :param init_state: Initial state of the Kalman filter. Should be equal to first element in first_train_batch. :param transition_matrix: adjacency matrix representing state transition from t to t+1 for any time t Example: http://campar.in.tum.de/Chair/KalmanFilter Most likely, this will be an NxN eye matrix the where N is the number of variables being estimated :param observation_matrix: translation matrix from measurement coordinate system to desired coordinate system See: https://dsp.stackexchange.com/a/27488 Most likely, this will be an NxN eye matrix the where N is the number of variables being estimated :param n_iter: Number of times to repeat the parameter estimation function (estimates noise) """ init_state = np.array(init_state) transition_matrix = np.array(transition_matrix) observation_matrix = np.array(observation_matrix) self._expected_shape = init_state.shape self._filter = KalmanFilter( transition_matrices=transition_matrix, observation_matrices=observation_matrix, initial_state_mean=init_state, ) self._calibration_countdown = 0 self._calibration_observations = [] self._em_iter = n_iter self.calibrate(train_size, n_iter) self._x_now = np.zeros(3) self._p_now = np.zeros(3)
def __init__(self): self.state = [0, 0] self.covariance = np.eye(2) # self.transition_covariance = np.array([ # [0.0000025, 0.000005], # [0.0000005, 0.0000001], # ]) self.observation_covariance = np.array([[0.01]]) self.transition_covariance = np.array([ [0.003, 0.0002], [0.0002, 0.0002], ]) #self.transition_covariance = np.array([ # [0.00003, 0.000002], # [0.000002, 0.000002], #]) self.kf = KalmanFilter( transition_covariance=self.transition_covariance, # H observation_covariance=self.observation_covariance, # Q ) self.previous_update = None
def init_kalman_filter(self): transition_matrix = [[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]] observation_matrix = [[1, 0, 0, 0], [0, 1, 0, 0]] initstate = [0, 0, 0, 0] initial_state_covariance = [[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 0.5, 0], [0, 0, 0, 0.5]] ## value of Q transition_covariance = [[5, 0, 0, 0], [0, 5, 0, 0], [0, 0, 2, 0], [0, 0, 0, 0.5]] ##Value of R #observation_covariance = [[5,0,0,0],[0,5,0,0],[0,0,2,0],[0,0,2,0]] observation_covariance = [[5, 0], [0, 5]] self.kf = KalmanFilter( transition_matrices=transition_matrix, observation_matrices=observation_matrix, initial_state_mean=initstate, initial_state_covariance=initial_state_covariance, transition_covariance=transition_covariance, observation_covariance=observation_covariance #em_vars=['transition_covariance', 'initial_state_covariance'] )
def get_interpolated_speed_list(path, kalman=False, smoothFactor=0.01): gpx_file = open(path, 'r') gpx = gpxpy.parse(gpx_file) interpolated_list = [] previous_speed = 0 for point in range(len(gpx.tracks[0].segments[0].points) - 1): p1 = gpx.tracks[0].segments[0].points[point] p1_time = time.mktime(p1.time.timetuple()) p2 = gpx.tracks[0].segments[0].points[point + 1] p2_time = time.mktime(p2.time.timetuple()) ## average speed between data points speed = p1.speed_between(p2) if speed is None or speed == 0: if previous_speed > 7: if speed == 0: speed = previous_speed - 1 else: speed = previous_speed else: speed = 0 previous_speed = speed interpolated_list.append(speed) ## if time difference is greater than one second, the missing seconds are filled with average speed if p2_time - p1_time > 1: seconds = int(p2_time - p1_time - 1) for second in range(seconds): interpolated_list.append(speed) if kalman is True: kf = KalmanFilter(transition_matrices=np.array([[1, 1], [0, 1]]), transition_covariance=smoothFactor * np.eye(2)) states_pred = kf.em(interpolated_list).smooth(interpolated_list)[0] return states_pred[:, 0] else: return interpolated_list
def next(obj, field): objvals = self._message_df[self._message_df['object_id'] == obj][field] if len(self._message_df[self._message_df['object_id'] == obj]) < 2: self._result = message.get(field, None) else: self.initial_state_mean = objvals.iat[0] self.kf = KalmanFilter( transition_matrices=self.F, transition_covariance=self.Q, observation_matrices=self.H, observation_covariance=self.R, initial_state_mean=self.initial_state_mean, initial_state_covariance=self.P) state_means, _ = self.kf.filter(objvals.values) state_means = pd.Series(state_means.flatten()) self._result = state_means.iloc[-1] if len(objvals) < 10: pass else: self._message_df[self._message_df['object_id'] == obj].drop( self._message_df[self._message_df['object_id'] == obj].head(1).index) return self._result
def initKalman(initstate, initcovariance): deltaT_default = 1.0 / 60 #60Hz frames Transition_Matrix, b = transitionMatrices(deltaT_default, initstate) Observation_Matrix = [[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0]] posCov = 5e-4 velCov = 1e-4 transistionCov = np.diag( [posCov, posCov, posCov, velCov, velCov, velCov] ) #how confident are we of our model..how about impact cases. more certain about pos than speed? observationCov = 0.5e-2 * np.eye( 3) #measure from data, may change according to blob size kf = KalmanFilter(transition_matrices=Transition_Matrix, observation_matrices=Observation_Matrix, initial_state_mean=initstate, initial_state_covariance=initcovariance, transition_covariance=transistionCov, observation_covariance=observationCov) return kf
def kalman_filter_regression(x, y, EM_on=False, EM_n_iter=5): """" Kalman Filter with choice to run Expectation-Maximization """ # Transition Covariance delta = 1e-6 # trans_cov = delta * np.eye(2) trans_cov = np.array([[1e-4, 0.], [0., 1e-6]]) # Observation matrix obs_mat = np.vstack([x, np.ones(x.shape)]).T[:, np.newaxis] kf = KalmanFilter( n_dim_obs=1, # one observed value n_dim_state=2, # two states: slope and intercept initial_state_mean=[0, 0], # initiate means initial_state_covariance=np.ones((2, 2)), # initiate state covariances transition_matrices=np.eye(2), # identitiy matrix observation_matrices=obs_mat, observation_covariance=2, # variance of y transition_covariance=trans_cov) # variance of coefficients if EM_on is True: kf = kf.em(y.values, n_iter=EM_n_iter) state_means, state_covs = kf.filter(y.values) return state_means
def checkMLDS(T, A, C, Q, S): """ Quick sanity check to test multivariate LDS Generate Dx latent dynamics and fit Dy observation """ Dx = A.shape[0] Dy = C.shape[0] obs = np.zeros([T, Dy]) hid = np.zeros([T, Dx]) for i in range(1, T): hid[i] = np.dot(A, hid[i - 1]) + np.dot(Q, np.random.randn(Dx)) obs[i] = np.dot(C, hid[i]) + np.dot(S, np.random.randn(Dy)) plt.figure() plt.title("Simulated Data") plt.plot(hid, c='blue') plt.plot(obs, c='red') plt.legend(['hidden', 'observed']) plt.show() mykf = KalmanFilter(initial_state_mean=np.zeros(Dx), n_dim_state=Dx, n_dim_obs=Dy, em_vars=[ 'transition_matrices', 'observation_matrices', 'transition_covariance', 'observation_covariance' ]) mykf.em(obs, n_iter=100) plt.figure() myZ, mySig = mykf.smooth(obs) plt.title("Estimated States vs Ground Truth") plt.plot(myZ, c='red') plt.plot(hid, c='blue') plt.legend(['smoothed', 'true']) plt.show() return mykf.transition_matrices, mykf.observation_matrices, mykf.transition_covariance, mykf.observation_covariance
def __init__(self, initial_y, initial_x, delta=1e-5, maxlen=3000): self._x = initial_x.name self._y = initial_y.name self.maxlen = maxlen trans_cov = delta / (1 - delta) * np.eye(2) obs_mat = np.expand_dims(np.vstack([[initial_x], [np.ones(initial_x.shape[0])]]).T, axis=1) self.kf = KalmanFilter(n_dim_obs=1, n_dim_state=2, initial_state_mean=np.zeros(2), initial_state_covariance=np.ones((2, 2)), transition_matrices=np.eye(2), observation_matrices=obs_mat, observation_covariance=1.0, transition_covariance=trans_cov) state_means, state_covs = self.kf.filter(initial_y.values) self.means = pd.DataFrame(state_means, index=initial_y.index, columns=['beta', 'alpha']) self.state_cov = state_covs[-1]
def calculate_velocities(times, obd_velocities): """ Filters the obd velocities [km/h]. :param times: Time vector [s]. :type times: numpy.array :param obd_velocities: OBD velocity vector [km/h]. :type obd_velocities: numpy.array :return: Velocity vector [km/h]. :rtype: numpy.array """ dt = float(np.median(np.diff(times))) t = np.arange(times[0], times[-1] + dt, dt) v = np.interp(t, times, obd_velocities) return np.interp(times, t, KalmanFilter().em(v).smooth(v)[0].T[0])
def kalman(df, N_ITER): measurements = np.asarray(list(zip(df['cx'], df['cy']))) initial_state_mean = [measurements[0, 0], 0, measurements[0, 1], 0] transition_matrix = [[1, 1, 0, 0], [0, 1, 0, 0], [0, 0, 1, 1], [0, 0, 0, 1]] observation_matrix = [[1, 0, 0, 0], [0, 0, 1, 0]] kf1 = KalmanFilter(transition_matrices = transition_matrix, observation_matrices = observation_matrix, initial_state_mean = initial_state_mean) kf1 = kf1.em(measurements, n_iter=N_ITER) (smoothed_state_means, smoothed_state_covariances) = kf1.smooth(measurements) return smoothed_state_means[:,0], smoothed_state_means[:,2]
def fit_KF(self, X_train_data, Y_train_data): print("X_train_data " + str(type(X_train_data))) # get mean and variance of X_train_data (mean1,var1) and mean and # variance of Y_train_data (mean2,var2) mean1 = X_train_data.mean() var1 = X_train_data.var() mean2 = Y_train_data.mean() var2 = Y_train_data.var() #self.MY_train_data = Y_train_data kf = KalmanFilter(transition_matrices=[[1, 1], [0, 1]], observation_matrices=[[0.1, 0.5], [-0.3, 0.0]]) measurements = np.asarray([[1, 0], [0, 0], [0, 1]]) # 3 observations kf = kf.em(measurements, n_iter=5) (filtered_state_means, filtered_state_covariances) = kf.filter(measurements) (smoothed_state_means, smoothed_state_covariances) = kf.smooth(measurements) return self
def calc_slope_intercept_kalman(etfs, prices): """ Utilise the Kalman Filter from the pyKalman package to calculate the slope and intercept of the regressed ETF prices. """ delta = 1e-5 trans_cov = delta / (1 - delta) * np.eye(2) obs_mat = np.vstack([prices[etfs[0]], np.ones(prices[etfs[0]].shape)]).T[:, np.newaxis] kf = KalmanFilter(n_dim_obs=1, n_dim_state=2, initial_state_mean=np.zeros(2), initial_state_covariance=np.ones((2, 2)), transition_matrices=np.eye(2), observation_matrices=obs_mat, observation_covariance=1.0, transition_covariance=trans_cov) state_means, state_covs = kf.filter(prices[etfs[1]].values) return state_means, state_covs
def checkLDS(T, A, C, Q, S): """ Quick sanity check to make sure code works... generate and fit 1d -> 1d system """ obs = np.zeros(T) hid = np.zeros(T) hid[0] = 4 for i in range(1, T): hid[i] = A * hid[i - 1] + Q * np.random.randn() obs[i] = C * hid[i] + S * np.random.randn() plt.figure() plt.title("Simulated Data") plt.plot(hid, c='blue') plt.plot(obs, c='red') plt.legend(['hidden', 'observed']) plt.show() mykf = KalmanFilter(initial_state_mean=4, n_dim_state=1, n_dim_obs=1, em_vars=[ 'transition_matrices', 'observation_matrices', 'transition_covariance', 'observation_covariance' ]) mykf.em(obs, n_iter=200) plt.figure() myZ, mySig = mykf.smooth(obs) plt.title("Estimated States vs Ground Truth") plt.plot(myZ, c='red') plt.plot(hid, c='blue') plt.legend(['smoothed', 'true']) plt.show() return mykf.transition_matrices, mykf.observation_matrices, mykf.transition_covariance, mykf.observation_covariance
def missingDataKalman(X, t): X = ma.where(X == -1, ma.masked, X) dt = t[2] - t[1] F = [[1, dt, 0.5 * dt * dt], [0, 1, dt], [0, 0, 1]] H = [1, 0, 0] Q = [[1, 0, 0], [0, 1e-4, 0], [0, 0, 1e-6]] R = [0.01] if (X[0] == ma.masked): X0 = [0, 0, 0] else: X0 = [X[0], 0, 0] P0 = [[10, 0, 0], [0, 1, 0], [0, 0, 1]] n_tsteps = len(t) n_dim_state = 3 filtered_state_means = np.zeros((n_tsteps, n_dim_state)) filtered_state_covariances = np.zeros((n_tsteps, n_dim_state, n_dim_state)) kf = KalmanFilter(transition_matrices=F, observation_matrices=H, transition_covariance=Q, observation_covariance=R, initial_state_mean=X0, initial_state_covariance=P0) for t in range(n_tsteps): if t == 0: filtered_state_means[t] = X0 filtered_state_covariances[t] = P0 else: filtered_state_means[t], filtered_state_covariances[t] = ( kf.filter_update(filtered_state_means[t - 1], filtered_state_covariances[t - 1], observation=X[t])) return filtered_state_means[:, 0]
def __init__(self, **kwargs): super(PairSpreadStrategy_0, self).__init__(**kwargs) assert len(self.p.asset_names) == 1, 'Only one derivative spread asset is supported' if isinstance(self.p.asset_names, str): self.p.asset_names = [self.p.asset_names] self.action_key = list(self.p.asset_names)[0] self.last_action = None assert len(self.getdatanames()) == 2, \ 'Expected exactly two input datalines but {} where given'.format(self.getdatanames()) # Keeps track of virtual spread position # long_ spread: >0, short_spread: <0, no positions: 0 self.spread_position_size = 0 # Reserve 5% of initial cash when checking if it is possible to add up virtual spread: self.margin_reserve = self.env.broker.get_cash() * .05 self.kf = KalmanFilter( initial_state_mean=0, transition_covariance=.01, observation_covariance=1, n_dim_obs=1 ) self.kf_state = [0, 0] self.reward_debug = dict( n=0, f1=0, f1_mean=0, f1_sum=0, fp=0, fp_mean=0, r_mean=0, r_sum=0, )
def kalman(): filter = KalmanFilter(dim_x=2, dim_z=1) x = array([0., 1.]) P = eye(2) * 100. enf = EnsembleKalmanFilter(x=x, P=P, dim_z=1, dt=1., N=20, hx=hx, fx=fx) measurements = [] results = [] ps = [] kf_results = [] for i in range(len(data)): z = data.iloc[i].as_array() enf.predict() enf.update(asarray([z])) filter.predict() filter.update(asarray([[z]])) results.append (enf.x[0]) kf_results.append (kf.x[0,0]) measurements.append(z) ps.append(3*(enf.P[0,0]**.5)) results = asarray(results) ps = asarray(ps)
def __init__(self, motion_mat=None, observation_mat=None, transition_covariance=None, observation_covariance=None): ndim, dt = 4, 1. if motion_mat is None: self.motion_mat = np.eye(2 * ndim, 2 * ndim) for i in range(ndim): self.motion_mat[i, ndim + i] = dt else: self.motion_mat = motion_mat if observation_mat is None: self.observation_mat = np.eye(ndim, 2 * ndim) else: self.observation_mat = observation_mat print(self.motion_mat) print(self.observation_mat) self.kf = KalmanFilter(transition_matrices=self.motion_mat, observation_matrices=self.observation_mat, transition_covariance=transition_covariance, observation_covariance=observation_covariance)
def smooth(df): points = df.copy(deep=True) # the first data point is a good guess of where the walk started initial_state = points.iloc[0] # One degree of latitude is about 10^5 meters - close enough for calculating error # observation_covariance: phone is accurate to 15 to 20 meters observation_covariance = np.diag([20 / 10000, 20 / 10000])**2 # transition_matrices: current position will be the same as previous position transition_matrix = np.diag([1, 1]) # transition_covariance: ggbaker walks at 1m/s, and the data contains an observation ~every 10 s transition_covariance = np.diag([10 / 10000, 10 / 10000])**2 kf = KalmanFilter(transition_matrices=transition_matrix, transition_covariance=transition_covariance, observation_covariance=observation_covariance, initial_state_mean=initial_state) kf_smoothed, _ = kf.smooth(points) kf_df = pd.DataFrame(kf_smoothed, columns=['lat', 'lon']) return kf_df
def __init__(self, bars, events, start_date): """ Initialises the bollinger band johansen strategy. :param bars: The DataHandler object that provides bar information :type bars: DataHandler :param events: The Event Queue object. :type events: Queue """ self.bars = bars self.events = events self.current_date = start_date self.kf = KalmanFilter(transition_matrices=[1], observation_matrices=[1], initial_state_mean=0, initial_state_covariance=1, observation_covariance=1, transition_covariance=0.01) self.hedge_ratio = self._find_stationary_portfolio() self.long = False self.short = False self.enter = 2.0
def kalman_filter(df): # KF parameters t = 2 # seconds # state is [lon, lat, v_lon, v_lat] transition_matrix = [[1, 0, t, 0], [0, 1, 0, t], [0, 0, 1, 0], [0, 0, 0, 1]] transition_offset = [0, 0, 0, 0] observation_matrix = np.eye(4) observation_offset = [0, 0, 0, 0] transition_covariance = np.eye(4) * 0.00001 observation_covariance = np.eye(4) * 0.001 initial_state_mean = df[['lon', 'lat', 'v_lon', 'v_lat']].iloc[0] initial_state_covariance = np.eye(4) # KF init kf = KalmanFilter(transition_matrix, observation_matrix, transition_covariance, observation_covariance, transition_offset, observation_offset, initial_state_mean, initial_state_covariance) observations = df[['lon', 'lat', 'v_lon', 'v_lat']].values # filter state_mean, _ = kf.filter(observations) df = pd.DataFrame(state_mean, columns=['lon', 'lat', 'v_lon', 'v_lat']) return df
def online_kf(x, cov, tm=1, om=1, burnout=40): """ simple 1D online Kalman filter with one state :param x: signal :param cov: transition covariance :param tm: transition coef (the float of the 1x1 matrix) :param om: observation coef (the float of the 1x1 matrix) :param burnout: number of previous samples at each sample :return: the filtered signal (with length = len(x)-burnout) """ kf = KalmanFilter(transition_matrices=[[tm]], transition_covariance=[[cov]], observation_matrices=[[om]]) f = [] measurements = [] for i, xi in enumerate(x): if i > burnout: m, c = kf.filter(measurements[-burnout:]) f.append(m) m, c = kf.filter_update(m[-1], c[-1], xi) measurements.append(xi) return np.r_[f][:, -1, 0]
def calc_slope_intercept_kalman(etfs, prices, initial_state_mean, initial_state_covariance, transition_matrices, observation_covariance, trans_cov): """ Utilise the Kalman Filter from the pyKalman package to calculate the slope and intercept of the regressed ETF prices. """ obs_mat = numpy.vstack( [prices[etfs[1:]].values.T, numpy.ones(prices[etfs[1]].shape)]).T[:, numpy.newaxis] kf = KalmanFilter(n_dim_obs=1, n_dim_state=len(etfs), initial_state_mean=initial_state_mean, initial_state_covariance=initial_state_covariance, transition_matrices=transition_matrices, observation_matrices=obs_mat, observation_covariance=observation_covariance, transition_covariance=trans_cov) state_means, state_covs = kf.filter(prices[etfs[0]].values) return state_means, state_covs
def __init__(self, init_state=tuple(0 for _ in range(18)), n_iter=5, train_size=15): """ Create the Kalman filter. :param init_state: Initial state of the Kalman filter. Should be equal to first element in first_train_batch. :param n_iter: Number of times to repeat the parameter estimation function (estimates noise) """ init_state = np.array(init_state) if init_state.size != 18: raise ValueError( "EulerKalman expects init: [x,y,z,vx,vy,vz,ax,ay,az,r,p,w,vr,vp,vw,ar,ap,aw]" ) self.t_prev = time.time() transition_matrix = self._get_transition_matrix() observation_matrix = np.eye(18) self._expected_shape = init_state.shape self._filter = KalmanFilter( transition_matrices=transition_matrix, observation_matrices=observation_matrix, initial_state_mean=init_state, ) self._calibration_countdown = 0 self._calibration_observations = [] self._em_iter = n_iter self.calibrate(train_size, n_iter) self._x_now = np.zeros(3) self._p_now = np.zeros(3)
def get_state(self, data): #using Kalman filter to get the true value/price in a minute observations, open,high,low,close prices d = np.log( np.array([ data.get_t_data(-i)[2:6] for i in range(self.config.observation_window) ])) mean = d.mean() kf = KalmanFilter(initial_state_mean=mean, n_dim_obs=4) v = kf.em(d) h = self.norm_it(v.smooth(d)[0]) s = h.std() h = h / s o = '' for i in range(len(h)): if h[i] > 0.43: o = o + 'a ' elif h[i] < -.43: o = o + 'c ' else: o = o + 'b ' return o.split()
def smooth(df): """ This function apply Kalman smoothing on the given data. :param df: DataFrame :return: DataFrame """ # No prior knowledge of where the walk started. Taking first data point as the starting point. initial_state = df.iloc[0] # GPS is accurate to about 5 metres, however, in reality it can to be several times that: 15 or 20 metres. # Hence taking 10 meters as the mean standard deviation. (Note: 1 degree of latitude or longitude is about 10^5 meters) observation_covariance = np.diag([0.00010, 0.00010]) ** 2 # No prior knowledge to predict the next coordinates hence using the default transition matrix. transition_matrix = np.diag([1,1]) transition_covariance = np.diag([0.00010, 0.00010]) ** 2 kf = KalmanFilter( initial_state_mean=initial_state, observation_covariance=observation_covariance, transition_matrices=transition_matrix, transition_covariance=transition_covariance ) kalman_smoothed, _ = kf.smooth(df) return pd.DataFrame(data={'lat': kalman_smoothed[:, 0], 'lon': kalman_smoothed[:, 1]}, dtype=float)
def __init__(self): self._observations = pickle.load( open('dumps/marker_observations.dump')) transition_covariance = np.array([ [0.025, 0.005], [0.0005, 0.01], ]) self.kf = KalmanFilter( transition_covariance=transition_covariance, # H observation_covariance=np.eye(1) * 1, # Q ) self.altitude = [] self.co = [] self.state = [0, 0] self.covariance = np.eye(2) test = 'test_9' self._baro = pickle.load(open('data/duedalen/%s/barometer.dump' % test)) self._throttle = pickle.load( open('data/duedalen/%s/throttle.dump' % test)) self._sonar = pickle.load(open('data/12.11.13/%s/sonar.dump' % test)) self self.acceleration = pickle.load( open('data/12.11.13/%s/acceleration.dump' % test)) self.z_velocity = [a.z_velocity for a in self.acceleration] self.baro = [i[1] for i in self._baro] self.throttle = [(i[1] - 1000.0) / (1000.0) for i in self._throttle] print self.throttle self.sonar = [i[1] for i in self._sonar] self.cam_alt = [ marker.z if marker else np.ma.masked for marker in self._observations ] for i in xrange(len(self._baro) - 1): dt = self._baro[i + 1][0] - self._baro[i][0] print dt