Constructing transition probability matrix

Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
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?
list-manipulation matrix markov-chains markov-process
add a comment |Â
up vote
4
down vote
favorite
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?
list-manipulation matrix markov-chains markov-process
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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?
list-manipulation matrix markov-chains markov-process
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?
list-manipulation matrix markov-chains markov-process
asked Aug 7 at 12:19
William
35517
35517
add a comment |Â
add a comment |Â
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, butToCharacterCodeis much, much faster.It is
"TreatRepeatedEntries" -> Totalwhich enables summing of entries.Countis not needed anymore.Developer`ToPackedArraymight speed up things a bit ifxis 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).
That"TreatRepeatedEntries" -> Totalis 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
add a comment |Â
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]

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

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 *)
add a comment |Â
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$
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
add a comment |Â
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, butToCharacterCodeis much, much faster.It is
"TreatRepeatedEntries" -> Totalwhich enables summing of entries.Countis not needed anymore.Developer`ToPackedArraymight speed up things a bit ifxis 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).
That"TreatRepeatedEntries" -> Totalis 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
add a comment |Â
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, butToCharacterCodeis much, much faster.It is
"TreatRepeatedEntries" -> Totalwhich enables summing of entries.Countis not needed anymore.Developer`ToPackedArraymight speed up things a bit ifxis 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).
That"TreatRepeatedEntries" -> Totalis 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
add a comment |Â
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, butToCharacterCodeis much, much faster.It is
"TreatRepeatedEntries" -> Totalwhich enables summing of entries.Countis not needed anymore.Developer`ToPackedArraymight speed up things a bit ifxis 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).
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, butToCharacterCodeis much, much faster.It is
"TreatRepeatedEntries" -> Totalwhich enables summing of entries.Countis not needed anymore.Developer`ToPackedArraymight speed up things a bit ifxis 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).
edited Aug 7 at 15:13
answered Aug 7 at 13:09
Henrik Schumacher
33.5k247100
33.5k247100
That"TreatRepeatedEntries" -> Totalis 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
add a comment |Â
That"TreatRepeatedEntries" -> Totalis 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
add a comment |Â
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]

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

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 *)
add a comment |Â
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]

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

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 *)
add a comment |Â
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]

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

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 *)
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]

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

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 *)
edited Aug 8 at 20:22
answered Aug 7 at 14:32
Anton Antonov
21.2k161107
21.2k161107
add a comment |Â
add a comment |Â
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$
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
add a comment |Â
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$
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
add a comment |Â
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$
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$
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password