蹒跚学步
First Steps
题目详情
一个婴儿正在客厅沙发的帮助下学习走路。宝宝从沙发上开始,在每个给定的时间宝宝都会做出决定。它要么勇敢地向前迈出一步,站在原地不知道该做什么,要么害怕地向后退一步,走向沙发,概率分别为 、 和 。婴儿永远不会走到沙发后面(因此,当婴儿在沙发上时,向前移动的概率为 ,留在沙发上的概率为 )。
如果你要观察这个婴儿很长一段时间,那么婴儿坐在沙发上的时间比例是多少?
A baby is learning to walk with the assistance of its living room couch. The baby starts at the couch and at each given time the baby will make a decision. It will either take a brave step forward, stand in place not knowing what to do, or fearfully take a step back towards the couch with probabilities , , and respectively. The baby will never go behind the couch (so when at the couch the baby has probability of moving forward and probability of staying at the couch).
If you were to observe this baby for an extremely long amount of time, what proportion of the time would the baby be at the couch?
解析
假设我们创建一个包含状态 的状态图,其中 表示婴儿在距离沙发几步的状态 处花费的时间。使用我们的决策矩阵 ,我们可以绘制婴儿的状态图。在先前状态下位于 并静止不动或从 移回沙发后,婴儿将位于沙发或 处。因此, 同样,我们得到: 通过简化这些方程,我们可以识别出一种模式: 将 代入 方程,我们得到: 显然,所有后续状态方程都可以重写为 这告诉我们,与 相比,婴儿在 上花费的时间与 一样多。我们知道所有这些状态之和必须为一并且具有公共比例。我们可以创建无限几何和: 将总和乘以 我们得到: 用第二个方程减去第一个方程得到: 因此,宝宝在沙发上的时间大约为 。
import random
#logs state baby has traveled to
state_log = [0]
num_transitions = 10_000
for i in range(num_transitions):
#modeling random walk using random variable t
t = random.randint(1,10)
if t <= 2:
state_log.append(state_log[-1] + 1)
elif t <= 7:
state_log.append(state_log[-1])
else:
state_log.append(max(state_log[-1] - 1, 0))
#Expecting value close to 1/3
print((state_log.count(0)-1)/len(state_log))Original Explanation
Suppose we create a state diagram with states where represents the time a baby spends at the state steps from the couch. Using our decision matrix , we can map the state diagram of the baby. The baby will be at the couch, or , after being at the in the previous state and standing still or moving back towards the couch from . Thus,
Similarly, we get:
By simplifying these equations, we can identify a pattern:
Substituting in into our equation we get:
It is evident that all subsequent state equations can be re-written as What that tells us is the baby will spend as much time in compared to . We know that all these states must sum to one and have common ration . We can create the infinite geometric sum:
Upon multiplying the sum by we get:
Subtracting the first equation from the second gets us:
Thus, the baby will be at the couch roughly of the time.
import random
#logs state baby has traveled to
state_log = [0]
num_transitions = 10_000
for i in range(num_transitions):
#modeling random walk using random variable t
t = random.randint(1,10)
if t <= 2:
state_log.append(state_log[-1] + 1)
elif t <= 7:
state_log.append(state_log[-1])
else:
state_log.append(max(state_log[-1] - 1, 0))
#Expecting value close to 1/3
print((state_log.count(0)-1)/len(state_log))