Let's generate data from a random walk first
SeedRandom[42]
walkdata = Accumulate[RandomVariate[LaplaceDistribution[0, 1], 100]]

, then one way to get what you want is with a Fold
:
Last@Fold[
Function[{state,newvalue},
With[{currentrecord=state[[1]],recordcounter=state[[2]]},
If[newvalue > currentrecord,
{newvalue,recordcounter+1},
state
]
]
],
{0,0},
walkdata
]
33
During the fold we keep track of the currentrecord
and the number of records (starting with {0,0}
) and update it when we find a higher value, otherwise we keep the old. The endresult is the last record and the number of records we encountered from which we just keep the number of record updates (with Last
).
Comparing it with C.E.s solution this mainly trades some code clarity (if that's most important definitely go with C.E.s version) for some potential speed up by saving the overhead of the Union
function call. If you are dealing with long random walks or doing a lot of them this might become relevant. There is also the additional option to Compile
if you need better performance.
Binomial[2 n, n]/2^(2 n - 1)
withn
the step number suffices... $\endgroup$