Open AustinTSchaffer opened 4 years ago
I gotta say though, I'm honestly amazed that you managed to write code while in a pub. Bravo.
The 10,000 lilly pad expected # steps is so low because, on average, for each given jump, the frog will jump half way between it's current position and the other bank (because the probabilities are uniformly distributed across all lilypads between the current one and the far bank). As a result, adding significant digits essentially adds more potential space for the frog to land in. EG for n=10 the frogs most likely path is 5, 7.5 (ie 7 or 8), 8.75 (ie 9), 10. For n=10,000 you have just given the frog more "space" in which it can land (ie you've put more space between the 9th lilypad and the far bank than in the n=10 case)
When I saw how low the average was for 200 hops, I thought there was something wrong with my code, but it makes sense that there would be a logarithmic relationship at play here. Still, I can only contribute my monte carlo simulation, not a mathematical treatment.
Feeling kind of dumb right now, is the solution really this simple?
I had some extra time this morning and tried to express each term using only the previous term, and ended up with expectedhops(n) = expectedhops(n-1) + (1/n)
, which is a much simpler recursive definition than what I had before. It's so simple, I wondered if it would be possible to convert it a closed form solution.
eh(n) = eh(n-1) + (1/n)
eh(n) = eh(0) + (sum from 1 to n of (1/n))
eh(0) = 0
eh(n) = (sum from 1 to n) of (1/n)
To express this in Python syntax:
def expected_hops(pads):
if pads <= 0: return 0
return sum((1/n for n in range(1, pads+1)))
No need for output caching, because it's no longer a branching recursive function. Admittedly, this is where my math abilities stop, because I don't know how to go about converting a summation expression into a true closed-form solution, if that's even possible in this case.
Photo evidence of me working out.
For this problem it is that simple. For any given N, the summation you have at the bottom of your picture is the closed form solution. For any given N it is a finite sum of fractions, which is a closed form solution.
You can express it in an alternate way as a Markov Chain with an absorbing state and use matrix algebra to come up with the answer (which has the added benefit of being able to work out the expected number of jumps to any particular state...but that isn't necessary for the Frog Problem as written, we only need the expected number of jumps to the far bank).
Hey Matt,
I've been thinking about this probably too much today and came up with a Python implementation of your recursive solution. I had worked out a slightly different equation on my own, but it turns out that it was exactly the same as yours. I'm really not a trained mathematician in any way, so please don't judge too harshly, I guess.
Anyway, I tossed it around and figured that this can be built up from simple cases, starting with 1 pad ahead of the frog. There's also a case for 0 hops ahead of the frog.
expectedhops(0) = 0
expectedhops(1) = 1
expectedhops(2) = 0.5*1 + 0.5*(1 + expectedhops(1)) = 1.5
expectedhops(3) = 0.3ish*1 + 0.3ish*(1+expectedhops(1)) + 0.3ish*(1+expectedhops(2))
The Python code I have is written to model this is below. I only figured that you'd be interested in it because it's a little bit faster in terms of performance since the results are calculated instead of being randomly generated, which will make it easier to test any explicit solutions. I even added some memoization, for a little bonus kick in performance. Should work on both Python 2.7 and Python 3.X, but really, you need to install Python 3.
Fun fact, it appears that the expected number of hops for 10,000 lily pads is 9.78 hops, which is much lower than what I would have expected.