python - 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
解决方案
推荐阅读
- jenkins - 在 Jenkins 下运行时,maven-gpg-plugin 失败并显示“设备的 ioctl 不合适”
- qt - [-Wstrict-overflow] 在这里是什么意思?
- google-apps-script - 在 CalendarEvent 上调用 addGuest 时如何发送标准邀请电子邮件?
- ruby-on-rails - 在嵌套表单的表单中显示错误消息?
- java - Eclipse - allowDeadlock 运行时选项
- android-studio - 无需按手机返回键即可返回
- cmake - 在 CMake 中对版本字符串进行排序
- vb.net - 用 Alea GPU 编程编译 VB.net
- flutter - 如何使 Flutter Flipkart 像主页中的搜索视图?
- php - 如何从另一台计算机或设备访问 XAMPP 虚拟主机?