首页 > 解决方案 > Ant Path Integration in Python and calculating probabilities

问题描述

UPDATED NEW CODE

So, I have a problem for homework. Before, i get to that problem, here is the problem and answer before that one.

Problem 1a

Suppose that an ant wandered randomly by taking steps (x,y), one per second, where at each ant step, x and y come from a normal distribution with a mean of 0 and a standard deviation of 1.0mm (assume this for all questions below). Plot a trace of the ant’s path over the course of an hour.

def path():

  s = 0
  steps_x = [s]

  for i in range (3600):
    mu, sigma = 0, 1
    s += np.random.normal(mu, sigma)
    steps_x.append(s)

  r = 0
  steps_y = [r]

  for i in range (3600):
    mu, sigma = 0, 1
    r += np.random.normal(mu, sigma)
    steps_y.append(r)

  return (steps_x, steps_y)

plt.plot(*path())

Problem 1B

Let’s think about why ants need to perform path integration. Suppose that instead of path integration, when an ant found food, it just continued to wander with random steps until it got back to the nest. Using a simulation, find the probability that an ant who finds food after 1 hour will make its way back to within 10mm of the nest over the course of the next hour (note that if it comes within 10mm of a nest, it stops). Is this a good strategy? Why or why not?

my attempt at programming this:

def path1B():

  s = 0
  steps_x = [s]
  counter = 0

  for i in range (3600):
    mu, sigma = 0, 1
    s += np.random.normal(mu, sigma)
    steps_x.append(s)

  r = 0
  steps_y = [r]

  for i in range (3600):
    mu, sigma = 0, 1
    r += np.random.normal(mu, sigma)
    steps_y.append(r)

  steps_x

  if scipy.spatial.distance.euclidean(steps_x, steps_y) > 10:

    for i in range (3600):
      mu, sigma = 0, 1
      s += np.random.normal(mu, sigma)
      steps_x.append(s)

    for i in range (3600):
      mu, sigma = 0, 1
      r += np.random.normal(mu, sigma)
      steps_y.append(r) 

    counter += 1

  return counter


total = 0

for i in range (0, 1000):
  total += path1B()

print(total/1000)  

I basically tried to do the same as I did in problem 1a, but I took an extra 3600 steps (the hour after) and if it the distance reached less than 10mm I would just break the loop. I am not trying to cheat on my homework, I have genuinely given it my best, but am really stuck

标签: pythonprobability

解决方案


推荐阅读