Last updated: 2026-01-15
Checks: 7 0
Knit directory: fiveMinuteStats/analysis/
This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(12345) was run prior to running the
code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 82d368b. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish or
wflow_git_commit). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Unstaged changes:
Modified: analysis/index.Rmd
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown
(analysis/markov_chains_discrete_intro.Rmd) and HTML
(docs/markov_chains_discrete_intro.html) files. If you’ve
configured a remote Git repository (see ?wflow_git_remote),
click on the hyperlinks in the table below to view the files as they
were in that past version.
| File | Version | Author | Date | Message |
|---|---|---|---|---|
| Rmd | 82d368b | Peter Carbonetto | 2026-01-15 | Generated pdf version of markov_chains_discrete_intro vignette. |
| html | 82d368b | Peter Carbonetto | 2026-01-15 | Generated pdf version of markov_chains_discrete_intro vignette. |
| Rmd | c345a1a | Nan Xiao | 2024-03-03 | Fix broken and moved URL |
| Rmd | 6780695 | GitHub | 2022-01-25 | Update markov_chains_discrete_intro.Rmd |
| Rmd | 78ce414 | GitHub | 2021-02-28 | Fix typo |
| html | 5f62ee6 | Matthew Stephens | 2019-03-31 | Build site. |
| Rmd | 0cd28bd | Matthew Stephens | 2019-03-31 | workflowr::wflow_publish(all = TRUE) |
| html | 34bcc51 | John Blischak | 2017-03-06 | Build site. |
| Rmd | 5fbc8b5 | John Blischak | 2017-03-06 | Update workflowr project with wflow_update (version 0.4.0). |
| Rmd | 391ba3c | John Blischak | 2017-03-06 | Remove front and end matter of non-standard templates. |
| html | 8e61683 | Marcus Davy | 2017-03-03 | rendered html using wflow_build(all=TRUE) |
| html | 5d0fa13 | Marcus Davy | 2017-03-02 | wflow_build() rendered html files |
| Rmd | d674141 | Marcus Davy | 2017-02-26 | typos, refs |
| html | c3b365a | John Blischak | 2017-01-02 | Build site. |
| Rmd | 67a8575 | John Blischak | 2017-01-02 | Use external chunk to set knitr chunk options. |
| Rmd | 5ec12c7 | John Blischak | 2017-01-02 | Use session-info chunk. |
| Rmd | bb814ef | jnovembre | 2016-01-31 | Initial commit |
See here for a PDF version of this vignette.
An understanding of matrix multiplication and matrix powers.
Here we provide a quick introduction to discrete Markov Chains.
A Markov Chain is a discrete stochastic process with the Markov property : \(P(X_t|X_{t-1},\ldots,X_1)= P(X_t|X_{t-1})\). It is fully determined by a probability transition matrix \(P\) which defines the transition probabilities (\(P_{ij}=P(X_t=j|X_{t-1}=i)\) and an initial probability distribution specified by the vector \(x\) where \(x_i=P(X_0=i)\). The time-dependent random variable \(X_t\) is describing the state of our probabilistic system at time-step \(t\).
In Sheldon Ross’s Introduction to Probability Models, he has an example (4.3) of a Markov Chain for modeling Gary’s mood. Gary alternates between 3 state: Cheery (\(X=1\)), So-So (\(X=2\)), or Glum (\(X=3\)). Here we input the \(P\) matrix given by Ross and we input an arbitrary initial probability matrix.
# Define prob transition matrix
# (note matrix() takes vectors in column form so there is a transpose here to switch col's to row's)
P=t(matrix(c(c(0.5,0.4,0.1),c(0.3,0.4,0.3),c(0.2,0.3,0.5)),nrow=3))
# Check sum across = 1
apply(P,1,sum)
# [1] 1 1 1
# Definte initial probability vector
x0=c(0.1,0.2,0.7)
# Check sums to 1
sum(x0)
# [1] 1
If initial prob distribution \(x_0\)
is \(3 \times 1\) column vector, then
\(x_0^T P= x_1^T\). In R, the
%*% operator automatically promotes a vector to the
appropriate matrix to make the arguments conformable. In the case of
multiplying a length 3 vector by a \(3 \times
3\) matrix, it takes the vector to be a row-vector. This means
our math can be written simply as:
# After one step
x0%*%P
# [,1] [,2] [,3]
# [1,] 0.25 0.33 0.42
And after two time-steps:
## The two-step prob trans matrix
P%*%P
# [,1] [,2] [,3]
# [1,] 0.39 0.39 0.22
# [2,] 0.33 0.37 0.30
# [3,] 0.29 0.35 0.36
## Multiplied by the initial state probability
x0%*%P%*%P
# [,1] [,2] [,3]
# [1,] 0.308 0.358 0.334
To generalize to an arbitrary number of time steps into the future, we can compute a the matrix power. In R, this can be done easily with the package expm. Let’s load the library and verify the second power is the same as we saw for P%*%P above.
# Load library
library(expm)
# Loading required package: Matrix
#
# Attaching package: 'expm'
# The following object is masked from 'package:Matrix':
#
# expm
# Verify the second power is P%*%P
P%^%2
# [,1] [,2] [,3]
# [1,] 0.39 0.39 0.22
# [2,] 0.33 0.37 0.30
# [3,] 0.29 0.35 0.36
And now let’s push this by looking at the state of the chain after many steps, say 100. First let’s look at the probability transition matrix…
P%^%100
# [,1] [,2] [,3]
# [1,] 0.3387097 0.3709677 0.2903226
# [2,] 0.3387097 0.3709677 0.2903226
# [3,] 0.3387097 0.3709677 0.2903226
What do you notice about the rows? And let’s see what this does for various different starting distributions:
c(1,0,0) %*%(P%^%100)
# [,1] [,2] [,3]
# [1,] 0.3387097 0.3709677 0.2903226
c(0.2,0.5,0.3) %*%(P%^%100)
# [,1] [,2] [,3]
# [1,] 0.3387097 0.3709677 0.2903226
Note that after a large number of steps the initial state does not matter any more, the probability of the chain being in any state \(j\) is independent of where we started. This is our first view of the equilibrium distribuion of a Markov Chain. These are also known as the limiting probabilities of a Markov chain or stationary distribution.
sessionInfo()
# R version 4.3.3 (2024-02-29)
# Platform: aarch64-apple-darwin20 (64-bit)
# Running under: macOS 15.7.1
#
# Matrix products: default
# BLAS: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib
# LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
#
# locale:
# [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#
# time zone: America/Chicago
# tzcode source: internal
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] expm_1.0-0 Matrix_1.6-5
#
# loaded via a namespace (and not attached):
# [1] vctrs_0.6.5 cli_3.6.5 knitr_1.50 rlang_1.1.6
# [5] xfun_0.52 stringi_1.8.7 promises_1.3.3 jsonlite_2.0.0
# [9] workflowr_1.7.1 glue_1.8.0 rprojroot_2.0.4 git2r_0.33.0
# [13] htmltools_0.5.8.1 httpuv_1.6.14 sass_0.4.10 rmarkdown_2.29
# [17] grid_4.3.3 evaluate_1.0.4 jquerylib_0.1.4 tibble_3.3.0
# [21] fastmap_1.2.0 yaml_2.3.10 lifecycle_1.0.4 whisker_0.4.1
# [25] stringr_1.5.1 compiler_4.3.3 fs_1.6.6 Rcpp_1.1.0
# [29] pkgconfig_2.0.3 later_1.4.2 lattice_0.22-5 digest_0.6.37
# [33] R6_2.6.1 pillar_1.11.0 magrittr_2.0.3 bslib_0.9.0
# [37] tools_4.3.3 cachem_1.1.0