Skip to content

Commit

Permalink
Merge pull request #223 from BaselDataScience/master
Browse files Browse the repository at this point in the history
markovchain accepts sparse transition matrix
  • Loading branch information
spedygiorgio authored Oct 7, 2024
2 parents 331e8c3 + 697f1dc commit 191d093
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 13 deletions.
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: markovchain
Type: Package
Title: Easy Handling Discrete Time Markov Chains
Version: 0.10.0
Version: 0.10.0.9100
Authors@R: c(
person("Giorgio Alfredo", "Spedicato", role = c("aut", "cre"),
email = "[email protected]",comment = c(ORCID = "0000-0002-0315-8888")),
Expand All @@ -14,22 +14,22 @@ Authors@R: c(
person("Toni", "Giorgino", role="ctb", comment = c(ORCID = "0000-0001-6449-0596")),
person("Richèl J.C.", "Bilderbeek", role = "ctb", comment = c(ORCID = "0000-0003-1107-7049")),
person("Daniel", "Ebbert", email = "[email protected]", role = "ctb", comment = c(ORCID = "0000-0003-3666-7205")),
person("Shreyash", "Maheshwari", email = "[email protected]", role = "ctb")
person("Shreyash", "Maheshwari", email = "[email protected]", role = "ctb"),
person("Reinhold", "Koch", role = "ctb")
)
Maintainer: Giorgio Alfredo Spedicato <[email protected]>
Description: Functions and S4 methods to create and manage discrete time Markov
chains more easily. In addition functions to perform statistical (fitting
and drawing random variates) and probabilistic (analysis of their structural
proprieties) analysis are provided. See Spedicato (2017) <doi:10.32614/RJ-2017-036>.
Some functions for continuous times Markov chains depends on the suggested ctmcd package, that,
as May 2023, can be retrieved from <https://cran.r-project.org/src/contrib/Archive/ctmcd/ctmcd_1.4.2.tar.gz>.
Some functions for continuous times Markov chains depend on the suggested ctmcd package.
License: MIT + file LICENSE
Depends:
R (>= 4.2.0),
Matrix (>= 1.5-0),
methods
Imports:
igraph,
Matrix (>= 1.5-0),
expm,
stats4,
parallel,
Expand Down
14 changes: 9 additions & 5 deletions R/classesAndMethods.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#' coerce,markovchain,data.frame-method coerce,table,markovchain-method
#' coerce,markovchain,igraph-method coerce,markovchain,matrix-method
#' coerce,markovchain,sparseMatrix-method coerce,sparseMatrix,markovchain-method
#' coerce,matrix,markovchain-method coerce,msm,markovchain-method
#' coerce,matrix,markovchain-method coerce,Matrix,markovchain-method
#' coerce,msm,markovchain-method
#' coerce,msm.est,markovchain-method coerce,etm,markovchain-method
#' dim,markovchain-method initialize,markovchain-method
#' names<-,markovchain-method plot,markovchain,missing-method
Expand Down Expand Up @@ -93,7 +94,7 @@
#' \item \code{markovchain} object are backed by S4 Classes.
#' \item Validation method is used to assess whether either columns or rows totals to one.
#' Rounding is used up to \code{.Machine$double.eps * 100}. If state names are not properly
#' defined for a probability \code{matrix}, coercing to \code{markovhcain} object leads
#' defined for a probability \code{matrix}, coercing to \code{markovchain} object leads
#' to overriding states name with artificial "s1", "s2", ... sequence. In addition, operator
#' overloading has been applied for \eqn{+,*,^,==,!=} operators.
#' }
Expand Down Expand Up @@ -204,7 +205,7 @@ setMethod(
.Object,
states = states,
byrow = byrow,
transitionMatrix = transitionMatrix,
transitionMatrix = as.matrix(transitionMatrix),
name = name,
...
)
Expand Down Expand Up @@ -687,7 +688,7 @@ setMethod("plot", signature(x = "markovchain", y = "missing"),

# firstly, check size
if (ncol(matr) != nrow(matr)) {
if(verbose) stop("Error! Not a rectangular matrix")
if(verbose) stop("Error! Not a quadratic matrix")
return(FALSE)
}

Expand All @@ -707,7 +708,9 @@ setMethod("plot", signature(x = "markovchain", y = "missing"),

# Internal function to return a markovchain object given a matrix
.matrix2Mc <- function(from) {

# enforce class matrix
from <- as.matrix(from)

# whether given matrix is a transition matrix or not
# if it is then how probabilities are stored
# row-wise or columnwise
Expand Down Expand Up @@ -751,6 +754,7 @@ NULL
# coerce matrix to markovchain object using internal method
# example: as("some matrix", "markovchain")
setAs(from = "matrix", to = "markovchain", def = .matrix2Mc)
setAs(from = "Matrix", to = "markovchain", def = .matrix2Mc)

# Function to transform a markovchain into a data.frame
# Args:
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
8 changes: 7 additions & 1 deletion data-raw/db4Tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ M[4,3] <- M[4,5] <- 1/2
M[5,5] <- 1
mcDrunkard <- new("markovchain", transitionMatrix = M)

# sparse matrix example based on Fisher's iris data
data(iris)
sparsematrix <- Matrix::sparseMatrix(i=rep(seq.int(nrow(iris)),5),
j=FNN::get.knn(iris[,-5], k=5)$nn.index,
x=.2)

#SAVING

usethis::use_data(simpleMcCiaoFit, checksAlofiRawTransitions, checkmarkovchainFitList, knownSteadyStatesMCs,
mcHitting, mcDrunkard, internal = TRUE, overwrite = TRUE)
mcHitting, mcDrunkard, sparsematrix, internal = TRUE, overwrite = TRUE)
3 changes: 2 additions & 1 deletion man/markovchain-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/testthat/test-sparse.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test_that("sparse transition matrix is accepted", {
expect_is(as(sparsematrix, "markovchain"), "markovchain")
})
2 changes: 1 addition & 1 deletion vignettes/an_introduction_to_markovchain_package.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ defaultMc <- new("markovchain")

The quicker way to create `markovchain` objects is made possible thanks to the implemented `initialize` S4 method that checks that:

* the `transitionMatrix` to be a transition matrix, i.e., all entries to be probabilities and either all rows or all columns to sum up to one.
* the `transitionMatrix`, either of class matrix or Matrix, to be a transition matrix, i.e., all entries to be probabilities and either all rows or all columns to sum up to one.
* the columns and rows names of `transitionMatrix` to be defined and to coincide with `states` vector slot.

The `markovchain` objects can be collected in a list within `markovchainList` S4 objects as following example shows.
Expand Down

0 comments on commit 191d093

Please sign in to comment.