Constructing transition probability matrix

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
1












I have the following list:



x="A", "A", "A", "E", "D", "D", "D", "C", "B", "E", "E", "E", "D", 
"B", "A", "D", "B", "E", "C", "A", "D", "A", "A", "A", "A", "C", "C",
"C", "D", "D", "E"


I want to make the Markov transition probability matrix of first order. To do so I have started by writing:



Partition[x, 2, 1] // Sort // Counts


This will give:



<|"A", "A" -> 5, "A", "C" -> 1, "A", "D" -> 2, "A", "E" -> 
1, "B", "A" -> 1, "B", "E" -> 2, "C", "A" -> 1, "C", "B" ->
1, "C", "C" -> 2, "C", "D" -> 1, "D", "A" -> 1, "D", "B" ->
2, "D", "C" -> 1, "D", "D" -> 3, "D", "E" -> 1, "E", "C" ->
1, "E", "D" -> 2, "E", "E" -> 2|>


above shows the frequencies of state transition A to A, A to B, A to C, A to D and A to E and so on for other letters, I wonder how can I show this result as a matrix?







share|improve this question























    up vote
    4
    down vote

    favorite
    1












    I have the following list:



    x="A", "A", "A", "E", "D", "D", "D", "C", "B", "E", "E", "E", "D", 
    "B", "A", "D", "B", "E", "C", "A", "D", "A", "A", "A", "A", "C", "C",
    "C", "D", "D", "E"


    I want to make the Markov transition probability matrix of first order. To do so I have started by writing:



    Partition[x, 2, 1] // Sort // Counts


    This will give:



    <|"A", "A" -> 5, "A", "C" -> 1, "A", "D" -> 2, "A", "E" -> 
    1, "B", "A" -> 1, "B", "E" -> 2, "C", "A" -> 1, "C", "B" ->
    1, "C", "C" -> 2, "C", "D" -> 1, "D", "A" -> 1, "D", "B" ->
    2, "D", "C" -> 1, "D", "D" -> 3, "D", "E" -> 1, "E", "C" ->
    1, "E", "D" -> 2, "E", "E" -> 2|>


    above shows the frequencies of state transition A to A, A to B, A to C, A to D and A to E and so on for other letters, I wonder how can I show this result as a matrix?







    share|improve this question





















      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      I have the following list:



      x="A", "A", "A", "E", "D", "D", "D", "C", "B", "E", "E", "E", "D", 
      "B", "A", "D", "B", "E", "C", "A", "D", "A", "A", "A", "A", "C", "C",
      "C", "D", "D", "E"


      I want to make the Markov transition probability matrix of first order. To do so I have started by writing:



      Partition[x, 2, 1] // Sort // Counts


      This will give:



      <|"A", "A" -> 5, "A", "C" -> 1, "A", "D" -> 2, "A", "E" -> 
      1, "B", "A" -> 1, "B", "E" -> 2, "C", "A" -> 1, "C", "B" ->
      1, "C", "C" -> 2, "C", "D" -> 1, "D", "A" -> 1, "D", "B" ->
      2, "D", "C" -> 1, "D", "D" -> 3, "D", "E" -> 1, "E", "C" ->
      1, "E", "D" -> 2, "E", "E" -> 2|>


      above shows the frequencies of state transition A to A, A to B, A to C, A to D and A to E and so on for other letters, I wonder how can I show this result as a matrix?







      share|improve this question











      I have the following list:



      x="A", "A", "A", "E", "D", "D", "D", "C", "B", "E", "E", "E", "D", 
      "B", "A", "D", "B", "E", "C", "A", "D", "A", "A", "A", "A", "C", "C",
      "C", "D", "D", "E"


      I want to make the Markov transition probability matrix of first order. To do so I have started by writing:



      Partition[x, 2, 1] // Sort // Counts


      This will give:



      <|"A", "A" -> 5, "A", "C" -> 1, "A", "D" -> 2, "A", "E" -> 
      1, "B", "A" -> 1, "B", "E" -> 2, "C", "A" -> 1, "C", "B" ->
      1, "C", "C" -> 2, "C", "D" -> 1, "D", "A" -> 1, "D", "B" ->
      2, "D", "C" -> 1, "D", "D" -> 3, "D", "E" -> 1, "E", "C" ->
      1, "E", "D" -> 2, "E", "E" -> 2|>


      above shows the frequencies of state transition A to A, A to B, A to C, A to D and A to E and so on for other letters, I wonder how can I show this result as a matrix?









      share|improve this question










      share|improve this question




      share|improve this question









      asked Aug 7 at 12:19









      William

      35517




      35517




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You can use SparseArray with additive assembly as follows:



          x = RandomChoice[Alphabet["English", "IndexCharacters"], 1000000];
          data = Flatten[ToCharacterCode[x]] - (ToCharacterCode["A"][[1]] - 1); // AbsoluteTiming // First
          A = With[
          spopt = SystemOptions["SparseArrayOptions"],
          Internal`WithLocalSettings[
          (*switch to additive assembly*)
          SetSystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries" -> Total],

          (*assemble matrix*)
          SparseArray[
          Partition[data, 2, 1] -> 1,
          Max[data] 1, 1
          ]

          ,
          (*reset "SparseArrayOptions" to previous value*)
          SetSystemOptions[spopt]]
          ]; // AbsoluteTiming // First



          0.739454



          0.114682




          Remarks



          • As the timings suggest, it is worthwhile to avoid strings in the first place.


          • Formerly, I used LetterNumber, but ToCharacterCode is much, much faster.


          • It is "TreatRepeatedEntries" -> Total which enables summing of entries. Count is not needed anymore. Developer`ToPackedArray might speed up things a bit if x is very long. The other hokus-pokus is for making things bulletproof against aborts (i.e., options are reset even if computations are interrupted). See also (37566) and (136017).






          share|improve this answer























          • That "TreatRepeatedEntries" -> Total is a nice trick!
            – Chris K
            Aug 7 at 13:41










          • Thank you, @ChrisK. You know, I could not live without the "TreatRepeatedEntries" option.
            – Henrik Schumacher
            Aug 7 at 13:46










          • @HenrikSchumacher Thank you for this, I turned this into matrix form using //MatrixForm and I got the answer I was after, I wonder if it is possible to use the same for different size of partition and offsets? Such as SparseArray[ Partition[Developer`ToPackedArray[LetterNumber[x]], 3, 1] -> 1] in that case I get wrong matrix positions
            – William
            Aug 7 at 14:56










          • You may try this, instead: A = With[ data = Developer`ToPackedArray[LetterNumber[x]], spopt = SystemOptions["SparseArrayOptions"] , Internal`WithLocalSettings[ SetSystemOptions[ "SparseArrayOptions" -> "TreatRepeatedEntries" -> Total], SparseArray[Partition[data, 3, 1] -> 1, Max[data] 1, 1, 1], SetSystemOptions[spopt]]]. It prescribes the tensor dimensions explicitly.
            – Henrik Schumacher
            Aug 7 at 14:59


















          up vote
          4
          down vote













          You can use the package CrossTabulate.m. (More detailed references are given in this MSE answer.)



          Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/CrossTabulate.m"]

          cmat = CrossTabulate[Partition[x, 2, 1]];

          MatrixForm[cmat]


          enter image description here



          cmat["SparseMatrix"] = cmat["SparseMatrix"]/Total[cmat["SparseMatrix"], 2];
          MatrixForm[cmat]


          enter image description here



          ArrayRules[cmat["SparseMatrix"]]

          (* 1, 1 -> 5/9, 1, 5 -> 1/9, 1, 4 -> 2/9, 1, 3 -> 1/
          9, 2, 5 -> 2/3, 2, 1 -> 1/3, 3, 2 -> 1/5, 3, 1 -> 1/
          5, 3, 3 -> 2/5, 3, 4 -> 1/5, 4, 4 -> 3/8, 4, 3 -> 1/
          8, 4, 2 -> 1/4, 4, 1 -> 1/8, 4, 5 -> 1/8, 5, 4 -> 2/
          5, 5, 5 -> 2/5, 5, 3 -> 1/5, _, _ -> 0 *)





          share|improve this answer






























            up vote
            1
            down vote













            You can also use EstimatedProcess and MarkovProcessProperties as follows:



            states = DeleteDuplicates[x];
            ordering = Ordering[states];
            data = ArrayComponents @ x ;
            estproc = EstimatedProcess[data, DiscreteMarkovProcess[Length@states]];
            tm = MarkovProcessProperties[estproc, "TransitionMatrix"][[ordering, ordering]]

            TeXForm[TableForm[tm, TableHeadings -> states[[ordering]], states[[ordering]]]]



            $beginarraycccccc
            & textA & textB & textC & textD & textE \
            textA & frac59 & 0 & frac19 & frac29 & frac19 \
            textB & frac13 & 0 & 0 & 0 & frac23 \
            textC & frac15 & frac15 & frac25 & frac15 & 0 \
            textD & frac18 & frac14 & frac18 & frac38 & frac18 \
            textE & 0 & 0 & frac15 & frac25 & frac25 \
            endarray$







            share|improve this answer





















            • This is very neat thank you what if you want to find the same transition probability matrix for higher orders? For example in the second order case one would have the same columns namely, A, B, C, D and E and 5^2 rows AA,AB,AC,AD,AE,BA,...,EA,EB,EC,ED,EE.
              – William
              Aug 8 at 9:12










            • @William, that's a good question. Can't think of a way off the top of my head.
              – kglr
              Aug 8 at 9:19










            • @William if you have a follow up question it's best to ask a new question that links to this one.
              – rhermans
              Aug 8 at 9:20










            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: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f179613%2fconstructing-transition-probability-matrix%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            You can use SparseArray with additive assembly as follows:



            x = RandomChoice[Alphabet["English", "IndexCharacters"], 1000000];
            data = Flatten[ToCharacterCode[x]] - (ToCharacterCode["A"][[1]] - 1); // AbsoluteTiming // First
            A = With[
            spopt = SystemOptions["SparseArrayOptions"],
            Internal`WithLocalSettings[
            (*switch to additive assembly*)
            SetSystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries" -> Total],

            (*assemble matrix*)
            SparseArray[
            Partition[data, 2, 1] -> 1,
            Max[data] 1, 1
            ]

            ,
            (*reset "SparseArrayOptions" to previous value*)
            SetSystemOptions[spopt]]
            ]; // AbsoluteTiming // First



            0.739454



            0.114682




            Remarks



            • As the timings suggest, it is worthwhile to avoid strings in the first place.


            • Formerly, I used LetterNumber, but ToCharacterCode is much, much faster.


            • It is "TreatRepeatedEntries" -> Total which enables summing of entries. Count is not needed anymore. Developer`ToPackedArray might speed up things a bit if x is very long. The other hokus-pokus is for making things bulletproof against aborts (i.e., options are reset even if computations are interrupted). See also (37566) and (136017).






            share|improve this answer























            • That "TreatRepeatedEntries" -> Total is a nice trick!
              – Chris K
              Aug 7 at 13:41










            • Thank you, @ChrisK. You know, I could not live without the "TreatRepeatedEntries" option.
              – Henrik Schumacher
              Aug 7 at 13:46










            • @HenrikSchumacher Thank you for this, I turned this into matrix form using //MatrixForm and I got the answer I was after, I wonder if it is possible to use the same for different size of partition and offsets? Such as SparseArray[ Partition[Developer`ToPackedArray[LetterNumber[x]], 3, 1] -> 1] in that case I get wrong matrix positions
              – William
              Aug 7 at 14:56










            • You may try this, instead: A = With[ data = Developer`ToPackedArray[LetterNumber[x]], spopt = SystemOptions["SparseArrayOptions"] , Internal`WithLocalSettings[ SetSystemOptions[ "SparseArrayOptions" -> "TreatRepeatedEntries" -> Total], SparseArray[Partition[data, 3, 1] -> 1, Max[data] 1, 1, 1], SetSystemOptions[spopt]]]. It prescribes the tensor dimensions explicitly.
              – Henrik Schumacher
              Aug 7 at 14:59















            up vote
            3
            down vote



            accepted










            You can use SparseArray with additive assembly as follows:



            x = RandomChoice[Alphabet["English", "IndexCharacters"], 1000000];
            data = Flatten[ToCharacterCode[x]] - (ToCharacterCode["A"][[1]] - 1); // AbsoluteTiming // First
            A = With[
            spopt = SystemOptions["SparseArrayOptions"],
            Internal`WithLocalSettings[
            (*switch to additive assembly*)
            SetSystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries" -> Total],

            (*assemble matrix*)
            SparseArray[
            Partition[data, 2, 1] -> 1,
            Max[data] 1, 1
            ]

            ,
            (*reset "SparseArrayOptions" to previous value*)
            SetSystemOptions[spopt]]
            ]; // AbsoluteTiming // First



            0.739454



            0.114682




            Remarks



            • As the timings suggest, it is worthwhile to avoid strings in the first place.


            • Formerly, I used LetterNumber, but ToCharacterCode is much, much faster.


            • It is "TreatRepeatedEntries" -> Total which enables summing of entries. Count is not needed anymore. Developer`ToPackedArray might speed up things a bit if x is very long. The other hokus-pokus is for making things bulletproof against aborts (i.e., options are reset even if computations are interrupted). See also (37566) and (136017).






            share|improve this answer























            • That "TreatRepeatedEntries" -> Total is a nice trick!
              – Chris K
              Aug 7 at 13:41










            • Thank you, @ChrisK. You know, I could not live without the "TreatRepeatedEntries" option.
              – Henrik Schumacher
              Aug 7 at 13:46










            • @HenrikSchumacher Thank you for this, I turned this into matrix form using //MatrixForm and I got the answer I was after, I wonder if it is possible to use the same for different size of partition and offsets? Such as SparseArray[ Partition[Developer`ToPackedArray[LetterNumber[x]], 3, 1] -> 1] in that case I get wrong matrix positions
              – William
              Aug 7 at 14:56










            • You may try this, instead: A = With[ data = Developer`ToPackedArray[LetterNumber[x]], spopt = SystemOptions["SparseArrayOptions"] , Internal`WithLocalSettings[ SetSystemOptions[ "SparseArrayOptions" -> "TreatRepeatedEntries" -> Total], SparseArray[Partition[data, 3, 1] -> 1, Max[data] 1, 1, 1], SetSystemOptions[spopt]]]. It prescribes the tensor dimensions explicitly.
              – Henrik Schumacher
              Aug 7 at 14:59













            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            You can use SparseArray with additive assembly as follows:



            x = RandomChoice[Alphabet["English", "IndexCharacters"], 1000000];
            data = Flatten[ToCharacterCode[x]] - (ToCharacterCode["A"][[1]] - 1); // AbsoluteTiming // First
            A = With[
            spopt = SystemOptions["SparseArrayOptions"],
            Internal`WithLocalSettings[
            (*switch to additive assembly*)
            SetSystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries" -> Total],

            (*assemble matrix*)
            SparseArray[
            Partition[data, 2, 1] -> 1,
            Max[data] 1, 1
            ]

            ,
            (*reset "SparseArrayOptions" to previous value*)
            SetSystemOptions[spopt]]
            ]; // AbsoluteTiming // First



            0.739454



            0.114682




            Remarks



            • As the timings suggest, it is worthwhile to avoid strings in the first place.


            • Formerly, I used LetterNumber, but ToCharacterCode is much, much faster.


            • It is "TreatRepeatedEntries" -> Total which enables summing of entries. Count is not needed anymore. Developer`ToPackedArray might speed up things a bit if x is very long. The other hokus-pokus is for making things bulletproof against aborts (i.e., options are reset even if computations are interrupted). See also (37566) and (136017).






            share|improve this answer















            You can use SparseArray with additive assembly as follows:



            x = RandomChoice[Alphabet["English", "IndexCharacters"], 1000000];
            data = Flatten[ToCharacterCode[x]] - (ToCharacterCode["A"][[1]] - 1); // AbsoluteTiming // First
            A = With[
            spopt = SystemOptions["SparseArrayOptions"],
            Internal`WithLocalSettings[
            (*switch to additive assembly*)
            SetSystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries" -> Total],

            (*assemble matrix*)
            SparseArray[
            Partition[data, 2, 1] -> 1,
            Max[data] 1, 1
            ]

            ,
            (*reset "SparseArrayOptions" to previous value*)
            SetSystemOptions[spopt]]
            ]; // AbsoluteTiming // First



            0.739454



            0.114682




            Remarks



            • As the timings suggest, it is worthwhile to avoid strings in the first place.


            • Formerly, I used LetterNumber, but ToCharacterCode is much, much faster.


            • It is "TreatRepeatedEntries" -> Total which enables summing of entries. Count is not needed anymore. Developer`ToPackedArray might speed up things a bit if x is very long. The other hokus-pokus is for making things bulletproof against aborts (i.e., options are reset even if computations are interrupted). See also (37566) and (136017).







            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Aug 7 at 15:13


























            answered Aug 7 at 13:09









            Henrik Schumacher

            33.5k247100




            33.5k247100











            • That "TreatRepeatedEntries" -> Total is a nice trick!
              – Chris K
              Aug 7 at 13:41










            • Thank you, @ChrisK. You know, I could not live without the "TreatRepeatedEntries" option.
              – Henrik Schumacher
              Aug 7 at 13:46










            • @HenrikSchumacher Thank you for this, I turned this into matrix form using //MatrixForm and I got the answer I was after, I wonder if it is possible to use the same for different size of partition and offsets? Such as SparseArray[ Partition[Developer`ToPackedArray[LetterNumber[x]], 3, 1] -> 1] in that case I get wrong matrix positions
              – William
              Aug 7 at 14:56










            • You may try this, instead: A = With[ data = Developer`ToPackedArray[LetterNumber[x]], spopt = SystemOptions["SparseArrayOptions"] , Internal`WithLocalSettings[ SetSystemOptions[ "SparseArrayOptions" -> "TreatRepeatedEntries" -> Total], SparseArray[Partition[data, 3, 1] -> 1, Max[data] 1, 1, 1], SetSystemOptions[spopt]]]. It prescribes the tensor dimensions explicitly.
              – Henrik Schumacher
              Aug 7 at 14:59

















            • That "TreatRepeatedEntries" -> Total is a nice trick!
              – Chris K
              Aug 7 at 13:41










            • Thank you, @ChrisK. You know, I could not live without the "TreatRepeatedEntries" option.
              – Henrik Schumacher
              Aug 7 at 13:46










            • @HenrikSchumacher Thank you for this, I turned this into matrix form using //MatrixForm and I got the answer I was after, I wonder if it is possible to use the same for different size of partition and offsets? Such as SparseArray[ Partition[Developer`ToPackedArray[LetterNumber[x]], 3, 1] -> 1] in that case I get wrong matrix positions
              – William
              Aug 7 at 14:56










            • You may try this, instead: A = With[ data = Developer`ToPackedArray[LetterNumber[x]], spopt = SystemOptions["SparseArrayOptions"] , Internal`WithLocalSettings[ SetSystemOptions[ "SparseArrayOptions" -> "TreatRepeatedEntries" -> Total], SparseArray[Partition[data, 3, 1] -> 1, Max[data] 1, 1, 1], SetSystemOptions[spopt]]]. It prescribes the tensor dimensions explicitly.
              – Henrik Schumacher
              Aug 7 at 14:59
















            That "TreatRepeatedEntries" -> Total is a nice trick!
            – Chris K
            Aug 7 at 13:41




            That "TreatRepeatedEntries" -> Total is a nice trick!
            – Chris K
            Aug 7 at 13:41












            Thank you, @ChrisK. You know, I could not live without the "TreatRepeatedEntries" option.
            – Henrik Schumacher
            Aug 7 at 13:46




            Thank you, @ChrisK. You know, I could not live without the "TreatRepeatedEntries" option.
            – Henrik Schumacher
            Aug 7 at 13:46












            @HenrikSchumacher Thank you for this, I turned this into matrix form using //MatrixForm and I got the answer I was after, I wonder if it is possible to use the same for different size of partition and offsets? Such as SparseArray[ Partition[Developer`ToPackedArray[LetterNumber[x]], 3, 1] -> 1] in that case I get wrong matrix positions
            – William
            Aug 7 at 14:56




            @HenrikSchumacher Thank you for this, I turned this into matrix form using //MatrixForm and I got the answer I was after, I wonder if it is possible to use the same for different size of partition and offsets? Such as SparseArray[ Partition[Developer`ToPackedArray[LetterNumber[x]], 3, 1] -> 1] in that case I get wrong matrix positions
            – William
            Aug 7 at 14:56












            You may try this, instead: A = With[ data = Developer`ToPackedArray[LetterNumber[x]], spopt = SystemOptions["SparseArrayOptions"] , Internal`WithLocalSettings[ SetSystemOptions[ "SparseArrayOptions" -> "TreatRepeatedEntries" -> Total], SparseArray[Partition[data, 3, 1] -> 1, Max[data] 1, 1, 1], SetSystemOptions[spopt]]]. It prescribes the tensor dimensions explicitly.
            – Henrik Schumacher
            Aug 7 at 14:59





            You may try this, instead: A = With[ data = Developer`ToPackedArray[LetterNumber[x]], spopt = SystemOptions["SparseArrayOptions"] , Internal`WithLocalSettings[ SetSystemOptions[ "SparseArrayOptions" -> "TreatRepeatedEntries" -> Total], SparseArray[Partition[data, 3, 1] -> 1, Max[data] 1, 1, 1], SetSystemOptions[spopt]]]. It prescribes the tensor dimensions explicitly.
            – Henrik Schumacher
            Aug 7 at 14:59











            up vote
            4
            down vote













            You can use the package CrossTabulate.m. (More detailed references are given in this MSE answer.)



            Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/CrossTabulate.m"]

            cmat = CrossTabulate[Partition[x, 2, 1]];

            MatrixForm[cmat]


            enter image description here



            cmat["SparseMatrix"] = cmat["SparseMatrix"]/Total[cmat["SparseMatrix"], 2];
            MatrixForm[cmat]


            enter image description here



            ArrayRules[cmat["SparseMatrix"]]

            (* 1, 1 -> 5/9, 1, 5 -> 1/9, 1, 4 -> 2/9, 1, 3 -> 1/
            9, 2, 5 -> 2/3, 2, 1 -> 1/3, 3, 2 -> 1/5, 3, 1 -> 1/
            5, 3, 3 -> 2/5, 3, 4 -> 1/5, 4, 4 -> 3/8, 4, 3 -> 1/
            8, 4, 2 -> 1/4, 4, 1 -> 1/8, 4, 5 -> 1/8, 5, 4 -> 2/
            5, 5, 5 -> 2/5, 5, 3 -> 1/5, _, _ -> 0 *)





            share|improve this answer



























              up vote
              4
              down vote













              You can use the package CrossTabulate.m. (More detailed references are given in this MSE answer.)



              Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/CrossTabulate.m"]

              cmat = CrossTabulate[Partition[x, 2, 1]];

              MatrixForm[cmat]


              enter image description here



              cmat["SparseMatrix"] = cmat["SparseMatrix"]/Total[cmat["SparseMatrix"], 2];
              MatrixForm[cmat]


              enter image description here



              ArrayRules[cmat["SparseMatrix"]]

              (* 1, 1 -> 5/9, 1, 5 -> 1/9, 1, 4 -> 2/9, 1, 3 -> 1/
              9, 2, 5 -> 2/3, 2, 1 -> 1/3, 3, 2 -> 1/5, 3, 1 -> 1/
              5, 3, 3 -> 2/5, 3, 4 -> 1/5, 4, 4 -> 3/8, 4, 3 -> 1/
              8, 4, 2 -> 1/4, 4, 1 -> 1/8, 4, 5 -> 1/8, 5, 4 -> 2/
              5, 5, 5 -> 2/5, 5, 3 -> 1/5, _, _ -> 0 *)





              share|improve this answer

























                up vote
                4
                down vote










                up vote
                4
                down vote









                You can use the package CrossTabulate.m. (More detailed references are given in this MSE answer.)



                Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/CrossTabulate.m"]

                cmat = CrossTabulate[Partition[x, 2, 1]];

                MatrixForm[cmat]


                enter image description here



                cmat["SparseMatrix"] = cmat["SparseMatrix"]/Total[cmat["SparseMatrix"], 2];
                MatrixForm[cmat]


                enter image description here



                ArrayRules[cmat["SparseMatrix"]]

                (* 1, 1 -> 5/9, 1, 5 -> 1/9, 1, 4 -> 2/9, 1, 3 -> 1/
                9, 2, 5 -> 2/3, 2, 1 -> 1/3, 3, 2 -> 1/5, 3, 1 -> 1/
                5, 3, 3 -> 2/5, 3, 4 -> 1/5, 4, 4 -> 3/8, 4, 3 -> 1/
                8, 4, 2 -> 1/4, 4, 1 -> 1/8, 4, 5 -> 1/8, 5, 4 -> 2/
                5, 5, 5 -> 2/5, 5, 3 -> 1/5, _, _ -> 0 *)





                share|improve this answer















                You can use the package CrossTabulate.m. (More detailed references are given in this MSE answer.)



                Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/CrossTabulate.m"]

                cmat = CrossTabulate[Partition[x, 2, 1]];

                MatrixForm[cmat]


                enter image description here



                cmat["SparseMatrix"] = cmat["SparseMatrix"]/Total[cmat["SparseMatrix"], 2];
                MatrixForm[cmat]


                enter image description here



                ArrayRules[cmat["SparseMatrix"]]

                (* 1, 1 -> 5/9, 1, 5 -> 1/9, 1, 4 -> 2/9, 1, 3 -> 1/
                9, 2, 5 -> 2/3, 2, 1 -> 1/3, 3, 2 -> 1/5, 3, 1 -> 1/
                5, 3, 3 -> 2/5, 3, 4 -> 1/5, 4, 4 -> 3/8, 4, 3 -> 1/
                8, 4, 2 -> 1/4, 4, 1 -> 1/8, 4, 5 -> 1/8, 5, 4 -> 2/
                5, 5, 5 -> 2/5, 5, 3 -> 1/5, _, _ -> 0 *)






                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Aug 8 at 20:22


























                answered Aug 7 at 14:32









                Anton Antonov

                21.2k161107




                21.2k161107




















                    up vote
                    1
                    down vote













                    You can also use EstimatedProcess and MarkovProcessProperties as follows:



                    states = DeleteDuplicates[x];
                    ordering = Ordering[states];
                    data = ArrayComponents @ x ;
                    estproc = EstimatedProcess[data, DiscreteMarkovProcess[Length@states]];
                    tm = MarkovProcessProperties[estproc, "TransitionMatrix"][[ordering, ordering]]

                    TeXForm[TableForm[tm, TableHeadings -> states[[ordering]], states[[ordering]]]]



                    $beginarraycccccc
                    & textA & textB & textC & textD & textE \
                    textA & frac59 & 0 & frac19 & frac29 & frac19 \
                    textB & frac13 & 0 & 0 & 0 & frac23 \
                    textC & frac15 & frac15 & frac25 & frac15 & 0 \
                    textD & frac18 & frac14 & frac18 & frac38 & frac18 \
                    textE & 0 & 0 & frac15 & frac25 & frac25 \
                    endarray$







                    share|improve this answer





















                    • This is very neat thank you what if you want to find the same transition probability matrix for higher orders? For example in the second order case one would have the same columns namely, A, B, C, D and E and 5^2 rows AA,AB,AC,AD,AE,BA,...,EA,EB,EC,ED,EE.
                      – William
                      Aug 8 at 9:12










                    • @William, that's a good question. Can't think of a way off the top of my head.
                      – kglr
                      Aug 8 at 9:19










                    • @William if you have a follow up question it's best to ask a new question that links to this one.
                      – rhermans
                      Aug 8 at 9:20














                    up vote
                    1
                    down vote













                    You can also use EstimatedProcess and MarkovProcessProperties as follows:



                    states = DeleteDuplicates[x];
                    ordering = Ordering[states];
                    data = ArrayComponents @ x ;
                    estproc = EstimatedProcess[data, DiscreteMarkovProcess[Length@states]];
                    tm = MarkovProcessProperties[estproc, "TransitionMatrix"][[ordering, ordering]]

                    TeXForm[TableForm[tm, TableHeadings -> states[[ordering]], states[[ordering]]]]



                    $beginarraycccccc
                    & textA & textB & textC & textD & textE \
                    textA & frac59 & 0 & frac19 & frac29 & frac19 \
                    textB & frac13 & 0 & 0 & 0 & frac23 \
                    textC & frac15 & frac15 & frac25 & frac15 & 0 \
                    textD & frac18 & frac14 & frac18 & frac38 & frac18 \
                    textE & 0 & 0 & frac15 & frac25 & frac25 \
                    endarray$







                    share|improve this answer





















                    • This is very neat thank you what if you want to find the same transition probability matrix for higher orders? For example in the second order case one would have the same columns namely, A, B, C, D and E and 5^2 rows AA,AB,AC,AD,AE,BA,...,EA,EB,EC,ED,EE.
                      – William
                      Aug 8 at 9:12










                    • @William, that's a good question. Can't think of a way off the top of my head.
                      – kglr
                      Aug 8 at 9:19










                    • @William if you have a follow up question it's best to ask a new question that links to this one.
                      – rhermans
                      Aug 8 at 9:20












                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    You can also use EstimatedProcess and MarkovProcessProperties as follows:



                    states = DeleteDuplicates[x];
                    ordering = Ordering[states];
                    data = ArrayComponents @ x ;
                    estproc = EstimatedProcess[data, DiscreteMarkovProcess[Length@states]];
                    tm = MarkovProcessProperties[estproc, "TransitionMatrix"][[ordering, ordering]]

                    TeXForm[TableForm[tm, TableHeadings -> states[[ordering]], states[[ordering]]]]



                    $beginarraycccccc
                    & textA & textB & textC & textD & textE \
                    textA & frac59 & 0 & frac19 & frac29 & frac19 \
                    textB & frac13 & 0 & 0 & 0 & frac23 \
                    textC & frac15 & frac15 & frac25 & frac15 & 0 \
                    textD & frac18 & frac14 & frac18 & frac38 & frac18 \
                    textE & 0 & 0 & frac15 & frac25 & frac25 \
                    endarray$







                    share|improve this answer













                    You can also use EstimatedProcess and MarkovProcessProperties as follows:



                    states = DeleteDuplicates[x];
                    ordering = Ordering[states];
                    data = ArrayComponents @ x ;
                    estproc = EstimatedProcess[data, DiscreteMarkovProcess[Length@states]];
                    tm = MarkovProcessProperties[estproc, "TransitionMatrix"][[ordering, ordering]]

                    TeXForm[TableForm[tm, TableHeadings -> states[[ordering]], states[[ordering]]]]



                    $beginarraycccccc
                    & textA & textB & textC & textD & textE \
                    textA & frac59 & 0 & frac19 & frac29 & frac19 \
                    textB & frac13 & 0 & 0 & 0 & frac23 \
                    textC & frac15 & frac15 & frac25 & frac15 & 0 \
                    textD & frac18 & frac14 & frac18 & frac38 & frac18 \
                    textE & 0 & 0 & frac15 & frac25 & frac25 \
                    endarray$








                    share|improve this answer













                    share|improve this answer



                    share|improve this answer











                    answered Aug 8 at 7:36









                    kglr

                    155k8180376




                    155k8180376











                    • This is very neat thank you what if you want to find the same transition probability matrix for higher orders? For example in the second order case one would have the same columns namely, A, B, C, D and E and 5^2 rows AA,AB,AC,AD,AE,BA,...,EA,EB,EC,ED,EE.
                      – William
                      Aug 8 at 9:12










                    • @William, that's a good question. Can't think of a way off the top of my head.
                      – kglr
                      Aug 8 at 9:19










                    • @William if you have a follow up question it's best to ask a new question that links to this one.
                      – rhermans
                      Aug 8 at 9:20
















                    • This is very neat thank you what if you want to find the same transition probability matrix for higher orders? For example in the second order case one would have the same columns namely, A, B, C, D and E and 5^2 rows AA,AB,AC,AD,AE,BA,...,EA,EB,EC,ED,EE.
                      – William
                      Aug 8 at 9:12










                    • @William, that's a good question. Can't think of a way off the top of my head.
                      – kglr
                      Aug 8 at 9:19










                    • @William if you have a follow up question it's best to ask a new question that links to this one.
                      – rhermans
                      Aug 8 at 9:20















                    This is very neat thank you what if you want to find the same transition probability matrix for higher orders? For example in the second order case one would have the same columns namely, A, B, C, D and E and 5^2 rows AA,AB,AC,AD,AE,BA,...,EA,EB,EC,ED,EE.
                    – William
                    Aug 8 at 9:12




                    This is very neat thank you what if you want to find the same transition probability matrix for higher orders? For example in the second order case one would have the same columns namely, A, B, C, D and E and 5^2 rows AA,AB,AC,AD,AE,BA,...,EA,EB,EC,ED,EE.
                    – William
                    Aug 8 at 9:12












                    @William, that's a good question. Can't think of a way off the top of my head.
                    – kglr
                    Aug 8 at 9:19




                    @William, that's a good question. Can't think of a way off the top of my head.
                    – kglr
                    Aug 8 at 9:19












                    @William if you have a follow up question it's best to ask a new question that links to this one.
                    – rhermans
                    Aug 8 at 9:20




                    @William if you have a follow up question it's best to ask a new question that links to this one.
                    – rhermans
                    Aug 8 at 9:20












                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f179613%2fconstructing-transition-probability-matrix%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    這個網誌中的熱門文章

                    tkz-euclide: tkzDrawCircle[R] not working

                    How to combine Bézier curves to a surface?

                    1st Magritte Awards