SHARE
TWEET

Untitled

a guest Nov 12th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def simulate(initial_sigma, motion_sigma, gps_sigma, n_steps):
  2.     """Simulate a sequence of locations and noisy GPS measurements
  3.  
  4.     Args:
  5.         initial_sigma, motion_sigma, gps_sigma: parameters of the simulation
  6.         n_steps: number of timesteps
  7.  
  8.     Returns:
  9.         a DataFrame with columns 'x' and 'gps' giving the true location and
  10.         gps readouts.
  11.     """
  12.    
  13.     # Sample an initial location from the distribution ovetr the initial loc
  14.     x = 0 # sstats.norm.rvs(scale=np.sqrt(initial_sigma))
  15.     loc_hist = []
  16.     for s in range(n_steps):
  17.         # TODO: sample a new x and gps readout
  18.         x = sstats.norm.rvs(loc=x, scale=np.sqrt(motion_sigma))
  19.         gps_readout = sstats.norm.rvs(loc=x, scale=np.sqrt(gps_sigma))
  20.         loc_hist.append((x, gps_readout))
  21.     loc_df = pd.DataFrame(loc_hist, columns=['x', 'gps'])
  22.     return loc_df
  23.  
  24.  
  25. def kalman_predict(loc_df, initial_sigma, motion_sigma, gps_sigma):
  26.     # Set our initial belief about our location
  27.     prior_mu = 0
  28.     prior_sigma = initial_sigma
  29.     predictions = []
  30.     for gps_readout in loc_df.gps:
  31.         # expand the prior by the movement
  32.         prior_sigma = prior_sigma + motion_sigma
  33.         # now do the bayes update
  34.         k = prior_sigma / (prior_sigma + gps_sigma)
  35.         posterior_mu = prior_mu + k * (gps_readout - prior_mu)
  36.         posterior_sigma = prior_sigma - k * prior_sigma
  37.         prior_mu = posterior_mu
  38.         prior_sigma = posterior_sigma
  39.         predictions.append((posterior_mu, posterior_sigma))
  40.  
  41.     predictions_df = pd.DataFrame(predictions, columns=['mu', 'sigma'])
  42.     return predictions_df
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top