This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
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 #185 from EcoJulia/master
RDPG
- Loading branch information
Showing
4 changed files
with
93 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" | ||
RDPG(N::BinaryNetwork; rank::Integer=3) | ||
Given a binary network `N`, `RDPG(N)` returns a probabilistic network with the | ||
same number of species, where every interaction happens with a probability equal | ||
to the dot product of species representation in the network `N`'s RDPG space of | ||
rank `rank`. | ||
Because the pairwise dot product obtained by the matrix multiplication of the | ||
two spaces `Left * Right` are not granted to be bounded between 0 and 1 (for | ||
numerical and theoric reasons), we bound the entries to be in the `[0,1]` range. | ||
#### References | ||
Dalla Riva, G.V. and Stouffer, D.B., 2016. Exploring the evolutionary signature | ||
of food webs' backbones using functional traits. Oikos, 125(4), pp.446-456. | ||
https://doi.org/10.1111/oik.02305 | ||
""" | ||
function RDPG(N::T; rank::Integer=3) where {T<:BinaryNetwork} | ||
L, R = svd_truncated(N; rank=rank) | ||
ardpg = L * R | ||
ardp[ardpg .< 0.] .= 0. | ||
ardpg[ardpg .> 1.] .= 1. | ||
ReturnType = typeof(N) <: AbstractBipartiteNetwork ? BipartiteProbabilisticNetwork : UnipartiteProbabilisticNetwork | ||
return ReturnType(ardpg, EcologicalNetworks.species_objects(N)...) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import LinearAlgebra: svd, mul!, diagm | ||
|
||
""" | ||
LinearAlgebra.svd(N::T) where {T <: AbstractEcologicalNetwork} | ||
SVD of a network (i.e. SVD of the adjacency matrix) | ||
""" | ||
LinearAlgebra.svd(N::T) where {T <: AbstractEcologicalNetwork} = svd(adjacency(N)) | ||
|
||
""" | ||
svd_truncated(N::BinaryNetwork, rnk::Integer=3) | ||
Given a binary network `N` which adjacency matrix `A` is of dimension `n × m`, | ||
`svd_truncated(A)` returns two matrices, `Left` and `Right`, with dimensions | ||
respectively `n × rank` and `rank × m`, corresponding to the species | ||
representation in the network `N`'s RDPG space of rank `rank`. | ||
The singular value decomposition is computed using `LinearAlgebra`'s `svd`, | ||
obtaining | ||
`A = U * Diagonal(S) * V = U * Diagonal(√S) * Diagonal(√S) * V`. | ||
The truncation preserves the first `rank` columns of `U * Diagonal(√S)` and the | ||
first `rank` rows `Diagonal(√S) * V`. | ||
We have that, `A ≃ Left * Right` (and the approximation is optimal in a | ||
specified meaning). | ||
#### References | ||
Dalla Riva, G.V. and Stouffer, D.B., 2016. Exploring the evolutionary signature | ||
of food webs' backbones using functional traits. Oikos, 125(4), pp.446-456. | ||
https://doi.org/10.1111/oik.02305 | ||
""" | ||
function rdpg(N::T, rnk::Integer=3) where {T<:BinaryNetwork} | ||
U, singular_values, V = svd(N) | ||
left_space = similar(U) | ||
right_space = similar(V') | ||
mul!(left_space, U, diagm(sqrt.(singular_values))) | ||
mul!(right_space, diagm(sqrt.(singular_values)), V') | ||
left_space = left_space[:,1:rnk] | ||
right_space = right_space[1:rnk,:] | ||
return left_space, right_space | ||
end |
b07ba4d
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()
b07ba4d
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/30441
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: