Record Statistics for Discrete Random Walks












5












$begingroup$


I code a random walk of length 100 drawn from a LaplaceDistribution:



Accumulate[RandomVariate[LaplaceDistribution[0, 1], 10000]]


I am having trouble counting the number of record events in a discrete random walk. A record occurs at time t if the value of the random walk at time t is greater than all values of the walk for all times less than t. I want a function that will let count the number of records that occur in a walk of length n.



Thanks.










share|improve this question











$endgroup$








  • 1




    $begingroup$
    Why do you first generate 10000 samples and then only keep 100 of them randomly?
    $endgroup$
    – Thies Heidecke
    Dec 3 '18 at 22:28












  • $begingroup$
    I've realised it is completely pointless to do that lol.
    $endgroup$
    – Will
    Dec 3 '18 at 22:51










  • $begingroup$
    No problem :) Just wanted to clarify if there's some deeper meaning behind it that i missed.
    $endgroup$
    – Thies Heidecke
    Dec 3 '18 at 22:53












  • $begingroup$
    Is the mean number of records in such a walk the desired end result? If so, then n Binomial[2 n, n]/2^(2 n - 1) with n the step number suffices...
    $endgroup$
    – ciao
    Dec 4 '18 at 0:48
















5












$begingroup$


I code a random walk of length 100 drawn from a LaplaceDistribution:



Accumulate[RandomVariate[LaplaceDistribution[0, 1], 10000]]


I am having trouble counting the number of record events in a discrete random walk. A record occurs at time t if the value of the random walk at time t is greater than all values of the walk for all times less than t. I want a function that will let count the number of records that occur in a walk of length n.



Thanks.










share|improve this question











$endgroup$








  • 1




    $begingroup$
    Why do you first generate 10000 samples and then only keep 100 of them randomly?
    $endgroup$
    – Thies Heidecke
    Dec 3 '18 at 22:28












  • $begingroup$
    I've realised it is completely pointless to do that lol.
    $endgroup$
    – Will
    Dec 3 '18 at 22:51










  • $begingroup$
    No problem :) Just wanted to clarify if there's some deeper meaning behind it that i missed.
    $endgroup$
    – Thies Heidecke
    Dec 3 '18 at 22:53












  • $begingroup$
    Is the mean number of records in such a walk the desired end result? If so, then n Binomial[2 n, n]/2^(2 n - 1) with n the step number suffices...
    $endgroup$
    – ciao
    Dec 4 '18 at 0:48














5












5








5


3



$begingroup$


I code a random walk of length 100 drawn from a LaplaceDistribution:



Accumulate[RandomVariate[LaplaceDistribution[0, 1], 10000]]


I am having trouble counting the number of record events in a discrete random walk. A record occurs at time t if the value of the random walk at time t is greater than all values of the walk for all times less than t. I want a function that will let count the number of records that occur in a walk of length n.



Thanks.










share|improve this question











$endgroup$




I code a random walk of length 100 drawn from a LaplaceDistribution:



Accumulate[RandomVariate[LaplaceDistribution[0, 1], 10000]]


I am having trouble counting the number of record events in a discrete random walk. A record occurs at time t if the value of the random walk at time t is greater than all values of the walk for all times less than t. I want a function that will let count the number of records that occur in a walk of length n.



Thanks.







probability-or-statistics random discrete random-process






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 '18 at 0:51







Will

















asked Dec 3 '18 at 22:14









WillWill

3026




3026








  • 1




    $begingroup$
    Why do you first generate 10000 samples and then only keep 100 of them randomly?
    $endgroup$
    – Thies Heidecke
    Dec 3 '18 at 22:28












  • $begingroup$
    I've realised it is completely pointless to do that lol.
    $endgroup$
    – Will
    Dec 3 '18 at 22:51










  • $begingroup$
    No problem :) Just wanted to clarify if there's some deeper meaning behind it that i missed.
    $endgroup$
    – Thies Heidecke
    Dec 3 '18 at 22:53












  • $begingroup$
    Is the mean number of records in such a walk the desired end result? If so, then n Binomial[2 n, n]/2^(2 n - 1) with n the step number suffices...
    $endgroup$
    – ciao
    Dec 4 '18 at 0:48














  • 1




    $begingroup$
    Why do you first generate 10000 samples and then only keep 100 of them randomly?
    $endgroup$
    – Thies Heidecke
    Dec 3 '18 at 22:28












  • $begingroup$
    I've realised it is completely pointless to do that lol.
    $endgroup$
    – Will
    Dec 3 '18 at 22:51










  • $begingroup$
    No problem :) Just wanted to clarify if there's some deeper meaning behind it that i missed.
    $endgroup$
    – Thies Heidecke
    Dec 3 '18 at 22:53












  • $begingroup$
    Is the mean number of records in such a walk the desired end result? If so, then n Binomial[2 n, n]/2^(2 n - 1) with n the step number suffices...
    $endgroup$
    – ciao
    Dec 4 '18 at 0:48








1




1




$begingroup$
Why do you first generate 10000 samples and then only keep 100 of them randomly?
$endgroup$
– Thies Heidecke
Dec 3 '18 at 22:28






$begingroup$
Why do you first generate 10000 samples and then only keep 100 of them randomly?
$endgroup$
– Thies Heidecke
Dec 3 '18 at 22:28














$begingroup$
I've realised it is completely pointless to do that lol.
$endgroup$
– Will
Dec 3 '18 at 22:51




$begingroup$
I've realised it is completely pointless to do that lol.
$endgroup$
– Will
Dec 3 '18 at 22:51












$begingroup$
No problem :) Just wanted to clarify if there's some deeper meaning behind it that i missed.
$endgroup$
– Thies Heidecke
Dec 3 '18 at 22:53






$begingroup$
No problem :) Just wanted to clarify if there's some deeper meaning behind it that i missed.
$endgroup$
– Thies Heidecke
Dec 3 '18 at 22:53














$begingroup$
Is the mean number of records in such a walk the desired end result? If so, then n Binomial[2 n, n]/2^(2 n - 1) with n the step number suffices...
$endgroup$
– ciao
Dec 4 '18 at 0:48




$begingroup$
Is the mean number of records in such a walk the desired end result? If so, then n Binomial[2 n, n]/2^(2 n - 1) with n the step number suffices...
$endgroup$
– ciao
Dec 4 '18 at 0:48










2 Answers
2






active

oldest

votes


















6












$begingroup$

Here's one take:



path = Accumulate[RandomVariate[LaplaceDistribution[0, 1], 1000]];
records = FoldList[Max, path];

ListPlot[{
path,
records
}]


Mathematica graphics



records has lots of duplicates in it. It's a list where in each position we have the largest value up to that point. If we take the union of the values (or use DeleteDuplicates), we get the unique largest-so-far values, and if we count those we get the desired answer:



Length@Union[records]



52







share|improve this answer









$endgroup$













  • $begingroup$
    Thank you. This is perfect.
    $endgroup$
    – Will
    Dec 3 '18 at 22:51



















5












$begingroup$

Let's generate data from a random walk first



SeedRandom[42]
walkdata = Accumulate[RandomVariate[LaplaceDistribution[0, 1], 100]]


random walk data plot



, 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.






share|improve this answer











$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "387"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f187255%2frecord-statistics-for-discrete-random-walks%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6












    $begingroup$

    Here's one take:



    path = Accumulate[RandomVariate[LaplaceDistribution[0, 1], 1000]];
    records = FoldList[Max, path];

    ListPlot[{
    path,
    records
    }]


    Mathematica graphics



    records has lots of duplicates in it. It's a list where in each position we have the largest value up to that point. If we take the union of the values (or use DeleteDuplicates), we get the unique largest-so-far values, and if we count those we get the desired answer:



    Length@Union[records]



    52







    share|improve this answer









    $endgroup$













    • $begingroup$
      Thank you. This is perfect.
      $endgroup$
      – Will
      Dec 3 '18 at 22:51
















    6












    $begingroup$

    Here's one take:



    path = Accumulate[RandomVariate[LaplaceDistribution[0, 1], 1000]];
    records = FoldList[Max, path];

    ListPlot[{
    path,
    records
    }]


    Mathematica graphics



    records has lots of duplicates in it. It's a list where in each position we have the largest value up to that point. If we take the union of the values (or use DeleteDuplicates), we get the unique largest-so-far values, and if we count those we get the desired answer:



    Length@Union[records]



    52







    share|improve this answer









    $endgroup$













    • $begingroup$
      Thank you. This is perfect.
      $endgroup$
      – Will
      Dec 3 '18 at 22:51














    6












    6








    6





    $begingroup$

    Here's one take:



    path = Accumulate[RandomVariate[LaplaceDistribution[0, 1], 1000]];
    records = FoldList[Max, path];

    ListPlot[{
    path,
    records
    }]


    Mathematica graphics



    records has lots of duplicates in it. It's a list where in each position we have the largest value up to that point. If we take the union of the values (or use DeleteDuplicates), we get the unique largest-so-far values, and if we count those we get the desired answer:



    Length@Union[records]



    52







    share|improve this answer









    $endgroup$



    Here's one take:



    path = Accumulate[RandomVariate[LaplaceDistribution[0, 1], 1000]];
    records = FoldList[Max, path];

    ListPlot[{
    path,
    records
    }]


    Mathematica graphics



    records has lots of duplicates in it. It's a list where in each position we have the largest value up to that point. If we take the union of the values (or use DeleteDuplicates), we get the unique largest-so-far values, and if we count those we get the desired answer:



    Length@Union[records]



    52








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 3 '18 at 22:35









    C. E.C. E.

    50.3k397202




    50.3k397202












    • $begingroup$
      Thank you. This is perfect.
      $endgroup$
      – Will
      Dec 3 '18 at 22:51


















    • $begingroup$
      Thank you. This is perfect.
      $endgroup$
      – Will
      Dec 3 '18 at 22:51
















    $begingroup$
    Thank you. This is perfect.
    $endgroup$
    – Will
    Dec 3 '18 at 22:51




    $begingroup$
    Thank you. This is perfect.
    $endgroup$
    – Will
    Dec 3 '18 at 22:51











    5












    $begingroup$

    Let's generate data from a random walk first



    SeedRandom[42]
    walkdata = Accumulate[RandomVariate[LaplaceDistribution[0, 1], 100]]


    random walk data plot



    , 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.






    share|improve this answer











    $endgroup$


















      5












      $begingroup$

      Let's generate data from a random walk first



      SeedRandom[42]
      walkdata = Accumulate[RandomVariate[LaplaceDistribution[0, 1], 100]]


      random walk data plot



      , 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.






      share|improve this answer











      $endgroup$
















        5












        5








        5





        $begingroup$

        Let's generate data from a random walk first



        SeedRandom[42]
        walkdata = Accumulate[RandomVariate[LaplaceDistribution[0, 1], 100]]


        random walk data plot



        , 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.






        share|improve this answer











        $endgroup$



        Let's generate data from a random walk first



        SeedRandom[42]
        walkdata = Accumulate[RandomVariate[LaplaceDistribution[0, 1], 100]]


        random walk data plot



        , 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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 3 '18 at 23:03

























        answered Dec 3 '18 at 22:37









        Thies HeideckeThies Heidecke

        6,9362638




        6,9362638






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Mathematica Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f187255%2frecord-statistics-for-discrete-random-walks%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bundesstraße 106

            Verónica Boquete

            Ida-Boy-Ed-Garten