-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from sandreza/sparse_operations
Sparse operations
- Loading branch information
Showing
7 changed files
with
128 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "MarkovChainHammer" | ||
uuid = "38c40fd0-bccb-4723-b30d-b2caea0ad8d9" | ||
authors = ["Andre Souza <[email protected]>"] | ||
version = "0.0.11" | ||
version = "0.0.12" | ||
|
||
[deps] | ||
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" | ||
|
@@ -13,15 +13,16 @@ ProgressBars = "49802e3a-d2f1-5c88-81d8-b72133a6f568" | |
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
Reexport = "189a3867-3050-52da-a836-e630ba90ab69" | ||
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" | ||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[compat] | ||
julia = "1.8, 1.9, 1.10" | ||
Distributions = "0.25" | ||
Documenter = "0.27" | ||
DocumenterTools = "0.1" | ||
PrecompileTools = "1.2" | ||
ProgressBars = "1.4" | ||
Revise = "3.4, 3.5" | ||
PrecompileTools = "1.2" | ||
Reexport = "1.2" | ||
Revise = "3.4, 3.5" | ||
julia = "1.8, 1.9, 1.10" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
@reexport module TransitionMatrix | ||
|
||
include("construct_from_data.jl") | ||
include("sparse_operations.jl") | ||
|
||
end # module TransitionMatrix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using SparseArrays, ProgressBars | ||
export sparse_perron_frobenius, sparse_generator | ||
|
||
function sparse_count_operator(markov_chain::Vector{S}, number_of_states::S, step::Int) where S | ||
count_matrix = spzeros(typeof(markov_chain[1]), number_of_states, number_of_states) | ||
for i in ProgressBar(0:step-1) | ||
reduced_markov_chain = markov_chain[1+i:step:end] | ||
for j in 1:length(reduced_markov_chain)-1 | ||
count_matrix[reduced_markov_chain[j+1], reduced_markov_chain[j]] += 1 | ||
end | ||
end | ||
return count_matrix | ||
end | ||
|
||
function sparse_count_operator(markov_chain::Vector{S}, number_of_states::S) where S | ||
@info "computing sparse count matrix" | ||
count_matrix = spzeros(S, number_of_states, number_of_states) | ||
for i in ProgressBar(1:length(markov_chain)-1) | ||
count_matrix[markov_chain[i+1], markov_chain[i]] += 1 | ||
end | ||
return count_matrix | ||
end | ||
|
||
sparse_count_operator(markov_chain) = sparse_count_operator(markov_chain, maximum(markov_chain)) | ||
|
||
""" | ||
sparse_perron_frobenius(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 | ||
""" | ||
function sparse_perron_frobenius(partitions::Vector{S}; step = 1) where S | ||
number_of_states = maximum(partitions) | ||
count_matrix = sparse_count_operator(partitions, number_of_states, step) | ||
number_of_states = maximum(partitions) | ||
perron_frobenius_matrix = Float64.(count_matrix) | ||
normalization = sum(count_matrix, dims=1) | ||
for i in ProgressBar(eachindex(normalization)) | ||
for j in perron_frobenius_matrix[:, i].nzind | ||
perron_frobenius_matrix[j, i] /= normalization[i] | ||
end | ||
end | ||
return perron_frobenius_matrix | ||
end | ||
|
||
""" | ||
sparse_generator(markov_chain; 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 | ||
""" | ||
function sparse_generator(partitions::Vector{S}; dt = 1) where S | ||
number_of_states = maximum(partitions) | ||
count_matrix = sparse_count_operator(partitions, number_of_states) | ||
generator_matrix = Float64.(count_matrix) | ||
for i in 1:number_of_states | ||
count_matrix[i, i] = 0.0 | ||
end | ||
normalization = sum(count_matrix, dims=1) | ||
holding_scale = 1 ./ mean.(holding_times(partitions, number_of_states; dt=dt)) | ||
# calculate generator and handle edge case where no transitions occur | ||
for i in ProgressBar(eachindex(normalization)) | ||
for j in generator_matrix[:, i].nzind | ||
generator_matrix[j, i] /= normalization[i] | ||
end | ||
generator_matrix[i, i] = -1.0 | ||
for j in generator_matrix[:, i].nzind | ||
generator_matrix[j, i] *= holding_scale[i] | ||
end | ||
if normalization[i] == 0.0 | ||
for j in generator_matrix[:, i].nzind | ||
generator_matrix[j, i] *= false | ||
end | ||
end | ||
end | ||
return generator_matrix | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44de626
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
44de626
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/96704
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: