4 ms·
I think that specific example is very easy walks = np.random.choice([-1,1], (100, 1000)) running_sum = np.cumsum(walks, axis=0) plt.plot(running_su
by mgraczyk 4y ago
I think that specific example is very easy
walks = np.random.choice([-1,1], (100, 1000))
running_sum = np.cumsum(walks, axis=0)
plt.plot(running_sum)
Intuitively it seems like the more complicated the problem, the bigger the advantage for a language like numpy where functions are used for most higher level abstractions. For example what if you want to change the logic to do a stopped game, where you now keep each walk at zero once it becomes zero, and count the portion of walks that stop after some fixed duration? In numpy I can always fall back to list comprehensions or for loops for something like that. Is it easy to express logic like this in J?
- rak1507 4y agoYes, that example is easy, although you got it wrong, I said 100 random walks of length 1000, which should be (1000, 100) not (100, 1000). However, there are still more 'moving parts' in numpy than in APL/J, you have to modify the tuple, and the axis, and other things. It all adds up when making lots of small iterations. The modification you mentioned is straightforward in any array language. Here is what you mentioned in APL (I know APL more than J but I'm sure the J would be basically the same): ⊃⍤⍸⍤1⊢0=+\¯1*?100 1000⍴2 This is the number of steps to return to 0 for each of 100 random walks of length 1000. (It is then easy to analyse the frequency/average/etc with only a few more characters)
- mgraczyk 4y agoInteresting, but I guess I'd have to learn APL or J to compare. Without understanding what those symbols mean, it seems much less like "math" than the numpy version. 1 + np.min(np.argwhere(running_sum[1:] == 0), axis=0)