# Are there any ways to model markov chains from time series data?

I want to make a thing that produces a stochastic process from time series data. The time series data is recorded every hour over the year, which means 24-hour of patterns exist for 365 days.

What I want to do is something like below:

1. Fit a probability distribution using data for each hour so that I can sample the most probable value for this hour.

2. Repeat it for 24 hours to generate a pattern of a day.

BUT! I want the sampling to be done considering previous values rather than being done in an independent manner.

For example, I want to sample from $p_{h1}(x_t|x_{t-1}, x_{t-2}, ... , x_{t-k})$ or just $p_{h1}(x_t|x_{t-1})$ rather than $p_{h1}(x_t)$ when $h1$ refers to a specific hour.

What I came up with was the Markov chain, but I couldn't find any reference or materials on how to model it from real data.

Could anyone give me a comment for this issue?