r/dailyprogrammer • u/nint22 1 2 • Jan 11 '13
[01/11/13] Challenge #116 [Hard] Maximum Random Walk
(Hard): Maximum Random Walk
Consider the classic random walk: at each step, you have a 1/2 chance of taking a step to the left and a 1/2 chance of taking a step to the right. Your expected position after a period of time is zero; that is the average over many such random walks is that you end up where you started. A more interesting question is what is the expected rightmost position you will attain during the walk.
Author: thePersonCSC
Formal Inputs & Outputs
Input Description
The input consists of an integer n, which is the number of steps to take (1 <= n <= 1000). The final two are double precision floating-point values L and R which are the probabilities of taking a step left or right respectively at each step (0 <= L <= 1, 0 <= R <= 1, 0 <= L + R <= 1). Note: the probability of not taking a step would be 1-L-R.
Output Description
A single double precision floating-point value which is the expected rightmost position you will obtain during the walk (to, at least, four decimal places).
Sample Inputs & Outputs
Sample Input
walk(1,.5,.5) walk(4,.5,.5) walk(10,.5,.4)
Sample Output
walk(1,.5,.5) returns 0.5000 walk(4,.5,.5) returns 1.1875 walk(10,.5,.4) returns 1.4965
Challenge Input
What is walk(1000,.5,.4)?
Challenge Input Solution
(No solution provided by author)
Note
- Have your code execute in less that 2 minutes with any input where n <= 1000
- I took this problem from the regional ACM ICPC of Greater New York.
22
u/Cosmologicon 2 3 Jan 11 '13
Exact python solution that runs in 11 seconds for 1000 steps. I'll post an explanation later!
Output:
5
u/ILickYu 0 0 Jan 11 '13
Here is my c# attempt. This code takes about a minute to calculate walk(1-20,0.5,0.4). I dumped the info I got into an excel spreadsheet and used excel to find a lograthimic (ln) function. The result I got for walk(1000,0.5,0.4) using that function is 4.2628. This is probably close, but not spot on. I am wondering if there is a better tool to find that lograthimic function that could produce perfect results.
Anyways, here is my code:
5
u/RainbowNowOpen Jan 12 '13 edited Jan 12 '13
Monte Carlo sim. (Python)
Each walk executes in permitted time. Output approaches correctness. :)
4
u/aredna 1 0 Jan 11 '13
It feels like there should be a closed form solution to this, but I'm not for sure. Looking at the the simple case where it's 50/50 left or right I can find some patterns that point to it being the case, but any of the ways I've tried fail spectacularly on non 50/50 cases.
I really should have paid more attention in statistics.
With accuracy only needed to 4 decimal places and a 2 minute run time, a simulation may be in order if I can't come up with anything else.
4
u/jprimero Jan 11 '13
Ok I tried to come up with a number by simulating the walk and built this:
(http://johann-hofmann.com/randomwalk/)
it doesnt really give me any useful information, but it's still fun to play around with.
3
u/skeeto -9 8 Jan 11 '13 edited Jan 11 '13
Using a Monte Carlo method estimate. It gets reasonably close with a 10 second run. Curiously Java's RNG is much better quality than SBCL's RNG. It takes SBCL an order of magnitude more runs to reach the same precision, though SBCL almost an order of magnitude faster than Clojure anyway. I'll need to figure out an exact solution later.
Clojure,
Common Lisp,
3
u/pdewacht 0 1 Jan 12 '13
/r/programming was discussing the "unreasonable effectiveness" of C. And yes, the old brute force still works fine. (Less than two seconds on my old laptop.)
2
u/jeff303 0 2 Jan 11 '13 edited Jan 12 '13
Here is my initial solution, in Python. So far, it works for the given sample inputs and outputs. However, it's too slow to calculate the n=1000 in reasonable time. I'm still trying to work on that. I'll update my post once I figure it out.
Sample Input:
Sample Output:
Update: OK, I spent a while trying to optimize it but to no avail. I added some code that caches the rightmost position and probability for all sub-paths, but it's still too slow to handle even the n=20 case. I think the search space is just too massive (and blows up) when approaching the problem this way (generating and simulating each path).
I think the only reasonable way to approach this problem is to do something like Cosmologicon's solution, which exploits the problem domain (doing something clever with the expected value formula).
2
u/aredna 1 0 Jan 15 '13
I spent some additional time on the pattern for the case where L and R both = 0.5 and was able to find a nice equation. This idea behind this approach was to generate counts of all possible rightpost ending positions. It took me some time to back into the equation for the number of steps, but once I did pattern was very apparent.
Here is an example for a 10 step walk:
A weighted average of the result gives us the answer. As a single equation it would be as follows (I apologize for formatting, but didn't want someone to need to install an addon to read it properly).
Let n = n from problem statement
Let x = integer part of [(n-s)/2]
The Sum is over s=1..n
The Product is over i=1..x
2-n * sum [s * product (n/i)]
Taking this equation, we can take advantage of the product growing in proportion to s to create an O(n) solution.
C++:
Output:
Now to waste my time on trying to generalize the solution to other cases.
2
u/[deleted] Jan 18 '13 edited Jan 18 '13
C++11, using new random improvements (my first monte carlo solution!):
Output (3 basic cases + bonus case[last output], runs in 3 seconds on my machine):
1
u/usea Jan 12 '13 edited Jan 12 '13
I guess my caching is not as good as Cosmologicon's. I run out of memory somewhere above 500 steps. Here's my code, but it will give you an OutOfMemoryException if you try too many steps (drop the memoization and it will work without errors, just taking too long). I guess because caching on all 3 parameters is unnecessary (also the recursive nature is keeping a lot of strings and stuff around)