List of functions in MarkovChainHammer
MarkovChainHammer.TransitionMatrix.generator
— Methodgenerator(markov_chain; dt=1)
Description
Calculate the generator matrix from a markov chain.
Arguments
markov_chain::AbstractVector
: A vector of integers representing the state of a markov chain at each time step.dt::Real
: The time step between each state.
Returns
generator_matrix::Matrix
: The generator matrix of the markov chain.
MarkovChainHammer.TransitionMatrix.holding_times
— Methodholding_times(markov_chain, number_of_states; dt=1)
Description
Calculate the holding times of a markov chain.
Arguments
markov_chain::AbstractVector
: A vector of integers representing the state of a markov chain at each time step.number_of_states::Integer
: The number of states in the markov chain.dt::Real
: The time step of the markov chain.
Returns
holding_times::Vector{Vector{Real}}
: A vector of vectors of holding times for each state.
MarkovChainHammer.TransitionMatrix.perron_frobenius
— Methodperron_frobenius(markov_chain; step = 1)
Description
Calculate the perron-frobenius matrix from a markov chain.
Arguments
markov_chain::AbstractVector
: A vector of integers representing the state of a markov chain at each time step.
Keyword Arguments
step::Integer=1
: The step size of the constructed operator.
Returns
perron_frobenius_matrix::Matrix
: The perron-frobenius matrix of the markov chain.
MarkovChainHammer.TransitionMatrix.sparse_generator
— Methodsparsegenerator(markovchain; dt = 1)
Description
Calculate the generator matrix from a markov chain in sparse format
Arguments
markov_chain::AbstractVector
: A vector of integers representing the state of a markov chain at each time step.
Keyword Arguments
dt::Real=1
: The time step of the constructed operator.
Returns
generator_matrix::Matrix
: The generator matrix of the markov chain in sparse format
MarkovChainHammer.TransitionMatrix.sparse_perron_frobenius
— Methodsparseperronfrobenius(markov_chain; step = 1)
Description
Calculate the perron-frobenius matrix from a markov chain in sparse format
Arguments
markov_chain::AbstractVector
: A vector of integers representing the state of a markov chain at each time step.
Keyword Arguments
step::Integer=1
: The step size of the constructed operator.
Returns
perron_frobenius_matrix::Matrix
: The perron-frobenius matrix of the markov chain in sparse format
MarkovChainHammer.TransitionMatrix.symmetric_generator
— Methodsymmetric_generator(markov_chain, symmetries; dt=1)
Description
Calculate the generator matrix from a markov chain with symmetries.
Arguments
markov_chain::AbstractVector
: A vector of integers representing the state of a markov chain at each time step.symmetries::AbstractVector
: A vector of functions that are symmetries of the markov chain.dt::Real
: The time step between each state.
Returns
generator_matrix::Matrix
: The generator matrix of the markov chain.
MarkovChainHammer.TransitionMatrix.symmetric_perron_frobenius
— Methodsymmetric_perron_frobenius(markov_chain, symmetries)
Description
Calculate the perron-frobenius matrix from a markov chain with symmetries.
Arguments
markov_chain::AbstractVector
: A vector of integers representing the state of a markov chain at each time step.symmetries::AbstractVector
: A vector of functions that are symmetries of the markov chain.
Returns
perron_frobenius_matrix::Matrix
: The perron-frobenius matrix of the markov chain.
MarkovChainHammer.BayesianMatrix.BayesianGenerator
— MethodBayesianGenerator(data, prior::GeneratorParameterDistributions; dt=1)
Construct a BayesianGenerator object from data and a prior distribution.
Arguments
data::Vector{Int}
: The data to construct the BayesianGenerator object from.prior::GeneratorParameterDistributions
: The prior distribution for the BayesianGenerator object.
Keyword Arguments
dt::Number=1
: The time step between each data point.
Returns
BayesianGenerator
: A BayesianGenerator object constructed from the data and the prior distribution. Contains the posterior distributions for the rates and exit probabilities, as well as the predictive distributions for the holding times and exit counts.
MarkovChainHammer.BayesianMatrix.BayesianGenerator
— MethodBayesianGenerator(parameters::NamedTuple)
Construct a BayesianGenerator object from a NamedTuple of parameters.
Arguments
parameters::NamedTuple
: A NamedTuple containing the parameters for the BayesianGenerator object. Must contain the fieldsprior
,posterior
andpredictive
, each of which must be a NamedTuple containing the parameters for the respective distributions.
Returns
BayesianGenerator
: A BayesianGenerator object constructed from the parameters. Contains the posterior distributions for the rates and exit probabilities, as well as the predictive distributions for the holding times and exit counts.
MarkovChainHammer.BayesianMatrix.BayesianGenerator
— MethodBayesianGenerator(data::Vector{Vector{Int64}}, prior::GeneratorParameterDistributions; dt=1)
The ensemble version of the BayesianGenerator constructor. Here the data is a vector of vectors of integers, where each vector of integers is a single ensemble member trajectory.
# Arguments
- `data::Vector{Vector{Int64}}`: The data to construct the BayesianGenerator object from.
- `prior::GeneratorParameterDistributions`: The prior distribution for the BayesianGenerator object.
# Keyword Arguments
- `dt::Number=1`: The time step between each data point.
# Returns
- `BayesianGenerator`: A BayesianGenerator object constructed from the data and the prior distribution. Contains the posterior distributions for the rates and exit probabilities, as well as the predictive distributions for the holding times and exit counts.
MarkovChainHammer.BayesianMatrix.GeneratorParameterDistributions
— MethodGeneratorParameterDistributions(number_of_states::Int; α=1, β=1, αs=ones(number_of_states - 1))
Construct a GeneratorParameterDistributions object with Gamma(α, 1/β) prior distributions for the rates and Dirichlet(αs) prior distributions for the exit probabilities. Each state has the same probability distribution This is useful for construction prior distributions quickly. The underlying assumption for default rates is that the units of time are 1, and thus the rates are all by default 1. Futhermore the mean probability of each state is 1/(numberofstates-1)
The reason why the αs is of length(numberofstates - 1) is because they are exit probabilities, and hence the probability of returning to the same state is 0. The index ordering for the exit probability of state i, is [1:i-1..., i+1:numberofstates...]. For example, if numberofstates = 3, then the index ordering for state 1 is indices = [2, 3], i.e., indices[1] = 2 and indices[2] = 3, meaning, the first index corresponds to exiting through state 2 and the second index corresponds to exiting through state 3. The index ordering for state 2 is indices = [1, 3], i.e., indices[1] = 1 and indices[2] = 3, meaning, the first index corresponds to exiting through state 1 and the second index corresponds to exiting through state 3. The index ordering for state 3 is indices = [1, 2], i.e., indices[1] = 1 and indices[2] = 2, meaning, the first index corresponds to exiting through state 1 and the second index corresponds to exiting through state 2.
Arguments
number_of_states::Int
: The number of states in the Markov chain.
Keyword Arguments
α::Number=2
: The shape parameter for the Gamma prior distribution for the rates.β::Number=2
: The rate parameter for the Gamma prior distribution for the rates.αs::Vector{Number}=ones(number_of_states - 1)
: The concentration parameters for the Dirichlet prior distribution for the exit probabilities.
Returns
GeneratorParameterDistributions
: A GeneratorParameterDistributions object with Gamma(α, 1/β) prior distributions for the rates and Dirichlet(αs) prior distributions for the exit probabilities.
Note on the Gamma prior distribution (from https://en.wikipedia.org/wiki/Gamma_distribution)
The Gamma distribution is a conjugate prior distribution for the exponential family distribution of the rates. The Gamma distribution is parameterized by the shape parameter α and the rate parameter β. The mean of the Gamma distribution is α/β and the variance is α/β^2.
Note on the Dirichlet prior distribution (from https://en.wikipedia.org/wiki/Dirichlet_distribution)
The Dirichlet prior distribution is a conjugate prior distribution for the multinomial distribution of the exit probabilities. The Dirichlet prior distribution is parameterized by the concentration parameters αs. The mean of the Dirichlet distribution is E[X⃗] = αs/sum(αs) The covariance is CoVar[X⃗ ⊗ X⃗] = Diagonal(α̃) - α̃ ⊗ α̃ / (α₀ + 1) where α̃ = αs / α₀ and α₀ = sum(αs). The variance Var(Xᵢ) = α̃ᵢ(1-α̃ᵢ) / (α₀ + 1) where α₀ = sum(αs).
MarkovChainHammer.BayesianMatrix.uninformative_prior
— Methoduninformative_prior(number_of_states; scale=eps(1e2))
Construct an uninformative prior distribution for a BayesianGenerator object.
Arguments
number_of_states::Int
: The number of states in the BayesianGenerator object.
Keyword Arguments
scale::Number=eps(1e2)
: The scale parameter for the Gamma and Dirichlet distributions.
Returns
GeneratorParameterDistributions
: An uninformative prior distribution for a BayesianGenerator object.
MarkovChainHammer.BayesianMatrix.unpack
— Methodunpack(Q::BayesianGenerator)
Unpack a BayesianGenerator object into its parameters.
Arguments
Q::BayesianGenerator
: The BayesianGenerator object to unpack.
Returns
Tuple{NamedTuple{(:prior, :posterior, :predictive),Tuple{NamedTuple{(:α, :β, :αs),Tuple{Vector{Float64},Vector{Float64},Vector{Float64}}},NamedTuple{(:α, :β, :αs),Tuple{Vector{Float64},Vector{Float64},Vector{Float64}}},NamedTuple{(:μ, :σ, :ξ, :n, :αs),Tuple{Vector{Float64},Vector{Float64},Vector{Float64},Vector{Int64},Vector{Vector{Float64}}}}}}}
: A tuple containing the parameters for the prior, posterior and predictive distributions.
MarkovChainHammer.Trajectory.ContinuousTimeEmpiricalProcess
— MethodContinuousTimeEmpiricalProcess(markovchain; numberofstates=length(union(markovchain)))
Construct a continuous time empirical process from a discrete time Markov chain. The holding times are taken from the empirical distribution and the transition probabilities from the empirical transition probabilities
Arguments
markov_chain::Vector{Int}
: The discrete time Markov chain to construct the continuous time empirical process from.
Keyword Arguments
number_of_states::Int=length(union(markov_chain))
: The number of states in the Markov chain.
Returns
ContinuousTimeEmpiricalProcess
: A continuous time empirical process constructed from the discrete time Markov chain.
MarkovChainHammer.Trajectory.generate
— Methodgenerate(Q, n; dt=1, initial_condition=rand(1:size(Q)[1]))
Generate a Markov chain of length n
with transition matrix Q
and time step dt
.
Arguments
Q::AbstractMatrix
: The transition matrix or generator of the Markov chain.n::Int
: The length of the Markov chain.dt::Real=1
: The time step of the Markov chain.initial_condition::Int=rand(1:size(Q)[1])
: The initial condition of the Markov chain.
Keyword Arguments
progress::Bool=false
: Whether to show a progress bar.
Returns
Vector{Int}
: A Markov chain of lengthn
generated by transition matrixQ
and time stepdt
.
MarkovChainHammer.Trajectory.generate
— Methodgenerate(process::ContinuousTimeEmpiricalProcess, n; initial_condition=rand(1:size(process.cumulative_distribution)[1]))
Generate a Markov chain of length n
with continuous time empirical process process
.
Arguments
process::ContinuousTimeEmpiricalProcess
: The continuous time empirical process to generate a Markov chain from.n::Int
: Number of jumps to generate. i.e. lower bound to length of markov chaininitial_condition::Int=rand(1:size(process.cumulative_distribution)[1])
: The initial condition of the Markov chain.
Keyword Arguments
progress::Bool=false
: Whether to show a progress bar.
Returns
Vector{Int}
: A Markov chain of generated by a continuous time empiricalprocess
.
MarkovChainHammer.Clustering.leicht_newman
— Methodleicht_newman(A; modularity_type = :giorgini)
Description
Compute the communities of a graph using the Leicht Newman algorithm.
Arguments
A::AbstractArray
: Adjacency matrix of the graph.
Keyword Arguments
modularity_type::Symbol
: Type of modularity matrix to use. Can be:giorgini
,:modularity
,:generator_fine
, or:generator_coarse
. Defaults to:modularity
.
Returns
AbstractArray
: Array of communities.
MarkovChainHammer.Utils.autocovariance
— Methodautocovariance(g⃗, Q::Eigen, timelist; progress=false)
Calculate the autocovariance of observable g⃗ with generator matrix Q and times at timelist.
Arguments
g⃗::AbstractVector
: observableQ::Eigen
: eigenvalue decomposition of generator matrixtimelist::AbstractVector
: times at which to calculate autocovariance
Keyword Arguments
progress::Bool=false
: show a progress bar
Returns
autocov::Vector
: autocovariance of observable g⃗ with generator matrix Q and times at timelist
MarkovChainHammer.Utils.autocovariance
— Methodautocovariance(g⃗, Ps::Vector{Matrix{Float64}}, steps::Int; progress = false)
Calculate the autocovariance of observable g⃗ with transition matrices Ps and number of steps.
Arguments
g⃗::AbstractVector
: observablePs::Vector{Matrix{Float64}}
: transition matricessteps::Int
: number of steps
Keyword Arguments
progress::Bool=false
: show a progress bar
Returns
autocov::Vector
: autocovariance of observable g⃗ with transition matrices Ps and number of steps
MarkovChainHammer.Utils.autocovariance
— Methodautocovariance(x; timesteps=length(x), progress = false)
Calculate the autocovariance of a timeseries x
with timesteps
lags.
Arguments
x::AbstractVector
: timeseries
Keyword Arguments
timesteps::Int
: number of lagsprogress::Bool
: show a progress bar
Returns
autocov::Vector
: autocovariance ofx
withtimesteps
lags
MarkovChainHammer.Utils.decomposition
— Methoddecomposition(Q)
Description
Decompose Generator Matrix Q into a reverible and volume preserving component.
Arguments
Q::Matrix
: The generator matrix or transition matrix.
Returns
- (; exit_probabilities, rates)::NamedTuple: The exit probabilities and rates of the generator matrix.
MarkovChainHammer.Utils.decorrelation_times
— Methoddecorrelation_times(Q)
Description
Calculate the decorrelation times of a generator matrix.
Arguments
Q::Matrix
: The generator matrix or transition matrix.
Returns
decorrelation_times::Vector
: The decorrelation times of the generator matrix.
MarkovChainHammer.Utils.exit_rate
— Methodexit_rate(Q)
Description
Decompose Generator Matrix Q into exit probabilities, E, and rates, R, such that Q = ER
Arguments
Q::Matrix
: The generator matrix or transition matrix.
Returns
- (; exit_probabilities, rates)::NamedTuple: The exit probabilities and rates of the generator matrix.
MarkovChainHammer.Utils.histogram
— Methodhistogram(array; bins=minimum([100, length(array)]), normalization=:uniform, custom_range=false)
Description
Compute the histogram of an array. Useful for barplot in GLMakie.
Arguments
array::AbstractArray
: Array to compute the histogram of.
Keyword Arguments
bins::Integer
: Number of bins to use.normalization::AbstractArray
: Normalization to use. If:uniform
, then the normalization is uniform.custom_range::Tuple
: Custom range to use. Iffalse
, then the range is computed from the data.
Returns
Tuple{AbstractArray, AbstractArray}
: Tuple of the bin centers and the histogram values.
MarkovChainHammer.Utils.koopman_modes
— Methodkoopman_modes(Q)
Description
Calculate the koopman modes of a generator matrix.
Arguments
Q::Matrix
: The generator matrix or transition matrix.
Returns
W::Matrix
: The koopman modes of the generator matrix. Each column is a koopman mode
MarkovChainHammer.Utils.scaled_entropy
— Methodscaled_entropy(p)
Description
Calculate the scaled_entropy of a probability distribution. Normalizethe entropy of the empirical distribution to that of the uniform distribution.
Arguments
p::Vector
: The probability distribution.
Returns
entropy_value::Real
: The entropy of the probability distribution.
MarkovChainHammer.Utils.steady_state
— Methodsteady_state(Q)
Description
Calculate the steady state of a generator matrix.
Arguments
Q::Matrix
: The generator matrix or transition matrix.
Returns
p::Vector
: The steady state of the generator matrix.