# Higher Order Markov Chains

Continuous time Markov chains are discussed in the CTMC vignette which is a part of the package.

An experimental `fitHigherOrder` function has been written in order to fit a higher order Markov chain (Ching et al. (2008)). `fitHigherOrder` takes two inputs

1. sequence: a categorical data sequence.
2. order: order of Markov chain to fit with default value 2.

The output will be a `list` which consists of

1. lambda: model parameter(s).
2. Q: a list of transition matrices. Qi is the i t h step transition matrix stored column-wise.
3. X: frequency probability vector of the given sequence.

Its quadratic programming problem is solved using `solnp` function of the Rsolnp package (Ghalanos and Theussl 2014).

    if (requireNamespace("Rsolnp", quietly = TRUE)) {
      data(rain)
      rain_small <- rain$rain[1:150]
      fitHigherOrder(rain_small, 2)
    }
    #> $lambda
    #> [1] 0.5 0.5
    #>
    #> $Q
    #> $Q[[1]]
    #>             0       1-5      6+
    #> 0   0.5538462 0.4230769 0.21875
    #> 1-5 0.2923077 0.4038462 0.37500
    #> 6+  0.1538462 0.1730769 0.40625
    #>
    #> $Q[[2]]
    #>             0       1-5      6+
    #> 0   0.5384615 0.3725490 0.34375
    #> 1-5 0.2615385 0.3921569 0.43750
    #> 6+  0.2000000 0.2352941 0.21875
    #>
    #>
    #> $X
    #>         0       1-5        6+
    #> 0.4333333 0.3466667 0.2200000

# Higher Order Multivariate Markov Chains

## Introduction

HOMMC model is used for modeling behaviour of multiple categorical sequences generated by similar sources. The main reference is (Ching et al. 2008). Assume that there are s categorical sequences and each has possible states in M. In nth order MMC the state probability distribution of the jth sequence at time t=r+ 1 depend on the state probability distribution of all the sequences (including itself) at times t=r,r− 1, ...,r−n+ 1.

[ x_{r+1}^{(j)} = {k=1}{s}_{h=1}{n}{jk}{(h)}P_{h}{(jk)}x_{r-h+1}^{(k)}, j = 1, 2, …, s, r = n-1, n, … ]

with initial distribution x0(k),x1(k), ...,xn− 1(k)(k= 1, 2, ...,s). Here

[ {jk}^{(h)} , 1j, ks, 1hn and {k=1}{s}_{h=1}{n} _{jk}^{(h)} = 1, j = 1, 2, 3, … , s. ]

Now we will see the simpler representation of the model which will help us understand the result of `fitHighOrderMultivarMC` method.

Let Xr(j)= ((xr(j))T, (xr− 1(j))T, ..., (xr−n+ 1(j))T)Tf o r j= 1, 2, 3, ...,s. Then

[ =

]

[B^{ii} =

_{mn*mn} ]

[ B^{ij} =

_{mn*mn} ij. ]

## Representation of parameters in the code

Ph(i j) is represented as P h(i,j) and λi j(h) as Lambdah(i,j). For example: P2(13) as P2(1, 3) and λ45(3) as Lambda3(4,5).

## Definition of HOMMC class

    showClass("hommc")
    #> Class "hommc" [package "markovchain"]
    #>
    #> Slots:
    #>
    #> Name:      order    states         P    Lambda     byrow      name
    #> Class:   numeric character     array   numeric   logical character

Any element of `hommc` class is comprised by following slots:

1. states: a character vector, listing the states for which transition probabilities are defined.
2. byrow: a logical element, indicating whether transition probabilities are shown by row or by column.
3. order: order of Multivariate Markov chain.
4. P: an array of all transition matrices.
5. Lambda: a vector to store the weightage of each transition matrix.
6. name: optional character element to name the HOMMC

## How to create an object of class HOMMC

    states <- c('a', 'b')
    P <- array(dim = c(2, 2, 4), dimnames = list(states, states))
    P[ , , 1] <- matrix(c(1/3, 2/3, 1, 0), byrow = FALSE, nrow = 2, ncol = 2)

    P[ , , 2] <- matrix(c(0, 1, 1, 0), byrow = FALSE, nrow = 2, ncol = 2)

    P[ , , 3] <- matrix(c(2/3, 1/3, 0, 1), byrow = FALSE, nrow = 2, ncol = 2)

    P[ , , 4] <- matrix(c(1/2, 1/2, 1/2, 1/2), byrow = FALSE, nrow = 2, ncol = 2)

    Lambda <- c(.8, .2, .3, .7)

    hob <- new("hommc", order = 1, Lambda = Lambda, P = P, states = states,
               byrow = FALSE, name = "FOMMC")
    hob
    #> Order of multivariate markov chain = 1
    #> states = a b
    #>
    #> List of Lambda's and the corresponding transition matrix (by cols) :
    #> Lambda1(1,1) : 0.8
    #> P1(1,1) :
    #>           a b
    #> a 0.3333333 1
    #> b 0.6666667 0
    #>
    #> Lambda1(1,2) : 0.2
    #> P1(1,2) :
    #>   a b
    #> a 0 1
    #> b 1 0
    #>
    #> Lambda1(2,1) : 0.3
    #> P1(2,1) :
    #>           a b
    #> a 0.6666667 0
    #> b 0.3333333 1
    #>
    #> Lambda1(2,2) : 0.7
    #> P1(2,2) :
    #>     a   b
    #> a 0.5 0.5
    #> b 0.5 0.5

## Fit HOMMC

`fitHighOrderMultivarMC` method is available to fit HOMMC. Below are the 3 parameters of this method.

1. seqMat: a character matrix or a data frame, each column represents a categorical sequence.
2. order: order of Multivariate Markov chain. Default is 2.
3. Norm: Norm to be used. Default is 2.

# A Marketing Example

We tried to replicate the example found in (Ching et al. 2008) for an application of HOMMC. A soft-drink company in Hong Kong is facing an in-house problem of production planning and inventory control. A pressing issue is the storage space of its central warehouse, which often finds itself in the state of overflow or near capacity. The company is thus in urgent needs to study the interplay between the storage space requirement and the overall growing sales demand. The product can be classified into six possible states (1, 2, 3, 4, 5, 6) according to their sales volumes. All products are labeled as 1 = no sales volume, 2 = very slow-moving (very low sales volume), 3 = slow-moving, 4 = standard, 5 = fast-moving or 6 = very fast-moving (very high sales volume). Such labels are useful from both marketing and production planning points of view. The data is cointaind in `sales` object.

    data(sales)
    head(sales)
    #>      A   B   C   D   E
    #> [1,] "6" "1" "6" "6" "6"
    #> [2,] "6" "6" "6" "2" "2"
    #> [3,] "6" "6" "6" "2" "2"
    #> [4,] "6" "1" "6" "2" "2"
    #> [5,] "2" "6" "6" "2" "2"
    #> [6,] "6" "1" "6" "3" "3"

The company would also like to predict sales demand for an important customer in order to minimize its inventory build-up. More importantly, the company can understand the sales pattern of this customer and then develop a marketing strategy to deal with this customer. Customer’s sales demand sequences of five important products of the company for a year. We expect sales demand sequences generated by the same customer to be correlated to each other. Therefore by exploring these relationships, one can obtain a better higher-order multivariate Markov model for such demand sequences, hence obtain better prediction rules.

In (Ching et al. 2008) application, they choose the order arbitrarily to be eight, i.e., n = 8. We first estimate all the transition probability matrices Phi j and we also have the estimates of the stationary probability distributions of the five products:.

$\widehat{\boldsymbol{x}}^{(1)} = \begin{pmatrix} 0.0818& 0.4052& 0.0483& 0.0335& 0.0037& 0.4275 \end{pmatrix}^{\boldsymbol{T}}$

$\widehat{\boldsymbol{x}}^{(2)} = \begin{pmatrix} 0.3680& 0.1970& 0.0335& 0.0000& 0.0037& 0.3978 \end{pmatrix}^{\boldsymbol{T}}$

$\widehat{\boldsymbol{x}}^{(3)} = \begin{pmatrix} 0.1450& 0.2045& 0.0186& 0.0000& 0.0037& 0.6283 \end{pmatrix}^{\boldsymbol{T}}$

$\widehat{\boldsymbol{x}}^{(4)} = \begin{pmatrix} 0.0000& 0.3569& 0.1338& 0.1896& 0.0632& 0.2565 \end{pmatrix}^{\boldsymbol{T}}$

$\widehat{\boldsymbol{x}}^{(5)} = \begin{pmatrix} 0.0000& 0.3569& 0.1227& 0.2268& 0.0520& 0.2416 \end{pmatrix}^{\boldsymbol{T}}$

By solving the corresponding linear programming problems, we obtain the following higher-order multivariate Markov chain model:

xr+ 1(1)=P1(12)xr(2)

xr+ 1(2)= 0.6364P1(22)xr(2)+ 0.3636P3(22)xr(2)

xr+ 1(3)=P1(35)xr(5)

xr+ 1(4)= 0.2994P8(42)xr(2)+ 0.4324P1(45)xr(5)+ 0.2681P2(45)xr(5)

xr+ 1(5)= 0.2718P8(52)xr(2)+ 0.6738P1(54)xr(4)+ 0.0544P2(55)xr(5)

According to the constructed 8th order multivariate Markov model, Products A and B are closely related. In particular, the sales demand of Product A depends strongly on Product B. The main reason is that the chemical nature of Products A and B is the same, but they have different packaging for marketing purposes. Moreover, Products B, C, D and E are closely related. Similarly, products C and E have the same product flavor, but different packaging. In this model, it is interesting to note that both Product D and E quite depend on Product B at order of 8, this relationship is hardly to be obtained in conventional Markov model owing to huge amount of parameters. The results show that higher-order multivariate Markov model is quite significant to analyze the relationship of sales demand.

    # fit 8th order multivariate markov chain
    if (requireNamespace("Rsolnp", quietly = TRUE)) {
    object <- fitHighOrderMultivarMC(sales, order = 8, Norm = 2)
    }

We choose to show only results shown in the paper. We see that λ values are quite close, but not equal, to those shown in the original paper.

## References

Ching, Wai-Ki, Ximin Huang, Michael K Ng, and Tak-Kuen Siu. 2013. “Higher-Order Markov Chains.” In Markov Chains. Springer.
Ching, Wai-Ki, Michael K Ng, and Eric S Fung. 2008. “Higher-Order Multivariate Markov Chains and Their Applications.” Linear Algebra and Its Applications 428 (2): 492–507.
Ghalanos, Alexios, and Stefan Theussl. 2014. Rsolnp: General Non-Linear Optimization Using Augmented Lagrange Multiplier Method.