Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def simulate(initial_sigma, motion_sigma, gps_sigma, n_steps):
- """Simulate a sequence of locations and noisy GPS measurements
- Args:
- initial_sigma, motion_sigma, gps_sigma: parameters of the simulation
- n_steps: number of timesteps
- Returns:
- a DataFrame with columns 'x' and 'gps' giving the true location and
- gps readouts.
- """
- # Sample an initial location from the distribution ovetr the initial loc
- x = 0 # sstats.norm.rvs(scale=np.sqrt(initial_sigma))
- loc_hist = []
- for s in range(n_steps):
- # TODO: sample a new x and gps readout
- x = sstats.norm.rvs(loc=x, scale=np.sqrt(motion_sigma))
- gps_readout = sstats.norm.rvs(loc=x, scale=np.sqrt(gps_sigma))
- loc_hist.append((x, gps_readout))
- loc_df = pd.DataFrame(loc_hist, columns=['x', 'gps'])
- return loc_df
- def kalman_predict(loc_df, initial_sigma, motion_sigma, gps_sigma):
- # Set our initial belief about our location
- prior_mu = 0
- prior_sigma = initial_sigma
- predictions = []
- for gps_readout in loc_df.gps:
- # expand the prior by the movement
- prior_sigma = prior_sigma + motion_sigma
- # now do the bayes update
- k = prior_sigma / (prior_sigma + gps_sigma)
- posterior_mu = prior_mu + k * (gps_readout - prior_mu)
- posterior_sigma = prior_sigma - k * prior_sigma
- prior_mu = posterior_mu
- prior_sigma = posterior_sigma
- predictions.append((posterior_mu, posterior_sigma))
- predictions_df = pd.DataFrame(predictions, columns=['mu', 'sigma'])
- return predictions_df
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement