# Hidden markov model to detect Stock outs in Hourly sales Time series data

I'm currently dealing with time series data about sales of beverages in a supermarket. I have data for each minute, but I'm aggregating by hour since the dataset is already huge with a daily granularity.

I would like to detect stock outs occurring to a product, where a stock out happens when all the items of a given product where sold and the product is not available until refilled. I was suggested to use a Hidden Markov Model, but having little knowledge about it I don't know how to set up the problem, so I ask for help with references and suggestions of the statistical and mathematical assumptions to use. My idea would be something like:

$Y_i$ = hourly time series of sales, $i=1,...,n$,

$Y_i \in Z^+$

$Z_i$ = hourly time series of latent states, $i=1,...,n$

$Z_i \in {0,1}$, with $0$ corresponding to no items available/stock out

Basically I would like to infer the latent state given the observable sequence of sales.

I'm currently trying to find it using depmixS4 library in R:

dep2 <- depmix(resp ~ xreg,nstates=2,family=poisson(),ntimes=length(resp))
hmm2 <- fit(dep2)
ba2 <- BIC(hmm2)
summary(hmm2)


• Have you tried the approach I suggested as I believe it answers your question as nobody on the list has taken up the challenge for a bake-off . – IrishStat Dec 6 '16 at 16:23
• @IrishStat Actually I'm implementing the hidden markov model, I'll give some more time to others before accepting your answer :) – Tommaso Guerrini Dec 6 '16 at 16:43
• I would like to see your results in terms of model adequacy i.e. tests of significance of included structure ; validation of sufficiency i.e. critical examination of model residuals suggesting that they are free of remaining structure . You might also share the data (it can be coded to mask the actual ) so as your results AND mine can be compared/criticized and improved.by other readers/practitioners. In this way we all learn even the teachers ! – IrishStat Dec 6 '16 at 16:56
• @TommasoGuerrini: I am working on something very similar. If its not an issue could you share a snapshot of your data and code. I am also new to HMM and am not sure how to preprocess the data or implement HMM. Thanks. – Raj Mar 2 '17 at 0:48
• Hi Yuvaraj, look at the code here (stats.stackexchange.com/questions/255697/…) By the way, can I ask you which kind of problem? I am writing my final dissertation and whichever example could help – Tommaso Guerrini Mar 2 '17 at 9:50