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?

Henrik Schumacher 08/07/2018.

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

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

Anton Antonov 08/08/2018.

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} *)
```

kglr 08/08/2018.

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]]}]]
```

$\begin{array}{cccccc} & \text{A} & \text{B} & \text{C} & \text{D} & \text{E} \\ \text{A} & \frac{5}{9} & 0 & \frac{1}{9} & \frac{2}{9} & \frac{1}{9} \\ \text{B} & \frac{1}{3} & 0 & 0 & 0 & \frac{2}{3} \\ \text{C} & \frac{1}{5} & \frac{1}{5} & \frac{2}{5} & \frac{1}{5} & 0 \\ \text{D} & \frac{1}{8} & \frac{1}{4} & \frac{1}{8} & \frac{3}{8} & \frac{1}{8} \\ \text{E} & 0 & 0 & \frac{1}{5} & \frac{2}{5} & \frac{2}{5} \\ \end{array}$

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

- Total Variation Distance of probability matrix
- Nicely illustrating the evolution and end-state of a discrete-time Markov chain
- How to obtain the number of Markov Chain transitions in a simulation?
- State “i” goes to state “j”: list accessible states in a Markov-chain
- Why am I getting wildly incorrect results from FirstPassageTimeDistribution with inexact transition matrix?
- Nonlinear Markov chain (numerical simulation)
- Generalization of DiscreteMarkovProcess?
- Estimate process parameters of geometric Brownian motion with a two-state Markov chain
- (Inefficient) Lumpability Check of Markov Chain
- Constructing higher order transition probability matrix
- SetOptions locally?
- Constructing higher order transition probability matrix
- Creating a contingency table

- My boss wants me to attend a meeting every day before work hours
- Is there any combination of two airports that are connected via taxiways?
- How should I fry onions without burning them?
- Trim that distracting background off!
- Can any body be uniform in the universe?
- What does it take to get a 0 credit score
- What is this fighter-like plane with huge wings?
- How can I fix my relationship with my 7 month old cat after I've constantly abused her the past month?
- Extract the symmetric matrix built-in another matrix
- My SanDisk USB flash drive shows that 43GB is used when I just copied a 10GB file after formatting
- "In" vs. "Since" in "She has graduated in/since 1990"
- How do I turn off some formats in GeoServer's WMS?
- Why does sort say that ɛ = e?
- Why a simulation of a probability experiment is off by a factor of 10?
- Stove top ground wire connected to neutral wire
- Possible issues of a player switching Bard archetypes randomly with long rests?
- Would a society with two different species that can interbreed be viable?
- A man on a work project in West Africa asks me for 2000 - is this a scam?
- Transformer Inefficiency and Heat
- If being "unseated" by a bludger is a common occurrence, why did everyone freak out about Harry falling off of his broom?
- I think my DM is consistently faking dice rolls for saves against a specific spell; how do I call my DM out?
- How do you deal with "bugs" that can never be reproduced
- Does Xavier's School for Gifted Youngsters teach normal school subjects?
- How did Groot manage to raise the axe of Thor when even Hulk failed?