Record Statistics for Discrete Random Walks











up vote
3
down vote

favorite
1












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









New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Why do you first generate 10000 samples and then only keep 100 of them randomly?
    – Thies Heidecke
    7 hours ago












  • I've realised it is completely pointless to do that lol.
    – Will
    7 hours ago










  • No problem :) Just wanted to clarify if there's some deeper meaning behind it that i missed.
    – Thies Heidecke
    7 hours ago












  • 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...
    – ciao
    5 hours ago















up vote
3
down vote

favorite
1












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









New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Why do you first generate 10000 samples and then only keep 100 of them randomly?
    – Thies Heidecke
    7 hours ago












  • I've realised it is completely pointless to do that lol.
    – Will
    7 hours ago










  • No problem :) Just wanted to clarify if there's some deeper meaning behind it that i missed.
    – Thies Heidecke
    7 hours ago












  • 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...
    – ciao
    5 hours ago













up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





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









New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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









New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 5 hours ago





















New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 7 hours ago









Will

163




163




New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    Why do you first generate 10000 samples and then only keep 100 of them randomly?
    – Thies Heidecke
    7 hours ago












  • I've realised it is completely pointless to do that lol.
    – Will
    7 hours ago










  • No problem :) Just wanted to clarify if there's some deeper meaning behind it that i missed.
    – Thies Heidecke
    7 hours ago












  • 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...
    – ciao
    5 hours ago














  • 1




    Why do you first generate 10000 samples and then only keep 100 of them randomly?
    – Thies Heidecke
    7 hours ago












  • I've realised it is completely pointless to do that lol.
    – Will
    7 hours ago










  • No problem :) Just wanted to clarify if there's some deeper meaning behind it that i missed.
    – Thies Heidecke
    7 hours ago












  • 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...
    – ciao
    5 hours ago








1




1




Why do you first generate 10000 samples and then only keep 100 of them randomly?
– Thies Heidecke
7 hours ago






Why do you first generate 10000 samples and then only keep 100 of them randomly?
– Thies Heidecke
7 hours ago














I've realised it is completely pointless to do that lol.
– Will
7 hours ago




I've realised it is completely pointless to do that lol.
– Will
7 hours ago












No problem :) Just wanted to clarify if there's some deeper meaning behind it that i missed.
– Thies Heidecke
7 hours ago






No problem :) Just wanted to clarify if there's some deeper meaning behind it that i missed.
– Thies Heidecke
7 hours ago














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...
– ciao
5 hours ago




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...
– ciao
5 hours ago










2 Answers
2






active

oldest

votes

















up vote
4
down vote













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





















  • Thank you. This is perfect.
    – Will
    7 hours ago


















up vote
3
down vote













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























    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',
    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
    });


    }
    });






    Will is a new contributor. Be nice, and check out our Code of Conduct.










    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








    up vote
    4
    down vote













    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





















    • Thank you. This is perfect.
      – Will
      7 hours ago















    up vote
    4
    down vote













    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





















    • Thank you. This is perfect.
      – Will
      7 hours ago













    up vote
    4
    down vote










    up vote
    4
    down vote









    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












    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 7 hours ago









    C. E.

    49.1k395197




    49.1k395197












    • Thank you. This is perfect.
      – Will
      7 hours ago


















    • Thank you. This is perfect.
      – Will
      7 hours ago
















    Thank you. This is perfect.
    – Will
    7 hours ago




    Thank you. This is perfect.
    – Will
    7 hours ago










    up vote
    3
    down vote













    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



























      up vote
      3
      down vote













      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

























        up vote
        3
        down vote










        up vote
        3
        down vote









        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














        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 6 hours ago

























        answered 7 hours ago









        Thies Heidecke

        6,7562438




        6,7562438






















            Will is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Will is a new contributor. Be nice, and check out our Code of Conduct.













            Will is a new contributor. Be nice, and check out our Code of Conduct.












            Will is a new contributor. Be nice, and check out our Code of Conduct.
















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


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


            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

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)