Reference
qmm.core.structure
Define model structure in graph, matrix or equation forms.
import_digraph
Import a JSON model and convert to a NetworkX DiGraph with sign attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Union[str, dict]
|
Path to JSON file or dictionary containing model structure |
required |
file_path
|
bool
|
If True, data is a file path. If False, data is a dictionary |
True
|
Returns:
| Type | Description |
|---|---|
DiGraph
|
nx.DiGraph: Signed directed graph (signed digraph) |
Examples:
from qmm import import_digraph
model_dict = {
"nodes": [{"id": "A"}, {"id": "B"}],
"edges": [{"from": "A", "to": "B", "arrows": {"to": {"type": "triangle"}}}]
}
list(import_digraph(model_dict, file_path=False).nodes())
# ['A', 'B']
G = import_digraph(model_dict, file_path=False)
list(G.edges(data='sign'))
# [('A', 'B', 1)]
Source code in qmm/core/structure.py
create_matrix
Create an interaction matrix from a signed digraph in symbolic, signed, or binary form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing a signed digraph model |
required |
form
|
Literal['symbolic', 'signed', 'binary']
|
Type of matrix elements ('symbolic', 'signed', or 'binary') |
'symbolic'
|
matrix_type
|
Literal['A', 'B', 'C', 'D']
|
Type of matrix to create ('A', 'B', 'C', or 'D') |
'A'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Interaction matrix |
References
- Levins, R. (1968). Evolution in changing environments; some theoretical explorations.
- Dambacher, J.M., Li, H.W., Rossignol, P.A. (2002). Relevance of Community Structure in Assessing Indeterminacy of Ecological Predictions. Ecology 83, 1372–1385.
Examples:
from qmm import load_digraph, create_matrix
create_matrix(load_digraph("snowshoe_rp"), form='symbolic')
# Matrix([
# [-a_R,R, -a_R,C, 0],
# [ a_C,R, 0, -a_C,P],
# [ a_P,R, a_P,C, -a_P,P]])
create_matrix(load_digraph("snowshoe_rp"), form='signed')
# Matrix([
# [-1, -1, 0],
# [ 1, 0, -1],
# [ 1, 1, -1]])
create_matrix(load_digraph("snowshoe_rp"), form='binary')
# Matrix([
# [1, 1, 0],
# [1, 0, 1],
# [1, 1, 1]])
Source code in qmm/core/structure.py
create_equations
Create linear system of differential equations from a signed digraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing a signed digraph model |
required |
form
|
Literal['state', 'output']
|
Type of equations to create ('state' or 'output') |
'state'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Linear system of differential equations |
References
Levins, R. (1974). The qualitative analysis of partially specified systems. Annals of the New York Academy of Sciences 231, 123–138.
Examples:
from qmm import load_digraph, create_equations
create_equations(load_digraph("snowshoe_rp"), form='state')
# Matrix([
# [ -a_R,C*x_C - a_R,R*x_R],
# [ -a_C,P*x_P + a_C,R*x_R],
# [a_P,C*x_C - a_P,P*x_P + a_P,R*x_R]])
Source code in qmm/core/structure.py
nodes_table
Create a table of node metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Node, label, category, and description |
Examples:
from qmm import load_digraph, nodes_table
nodes_table(load_digraph("snowshoe_rp"))
# Node Label Category Description
# 0 $x_{R}$ R State None
# 1 $x_{C}$ C State None
# 2 $x_{P}$ P State None
Source code in qmm/core/structure.py
edges_table
Create a table of edge metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Edge, from, sign, to, dashes, and description |
Examples:
from qmm import load_digraph, edges_table
edges_table(load_digraph("snowshoe_rp"))
# Edge From Sign To Dashes Description
# 0 $a_{R,R}$ R - R False None
# 1 $a_{C,R}$ R + C False None
# 2 $a_{P,R}$ R + P False None
# 3 $a_{R,C}$ C - R False None
# 4 $a_{P,C}$ C + P False None
# 5 $a_{C,P}$ P - C False None
# 6 $a_{P,P}$ P - P False None
Source code in qmm/core/structure.py
qmm.core.stability
Analyse the stability properties of a system based on its structure.
sign_stability
Evaluate necessary and sufficient conditions for sign stability including color test.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Test results for sign stability conditions |
References
- May, R.M. (1973). Qualitative Stability in Model Ecosystems. Ecology 54, 638–641.
- Jeffries, C. (1974). Qualitative Stability and Digraphs in Model Ecosystems. Ecology 55, 1415–1419.
Examples:
from qmm import load_digraph, sign_stability
sign_stability(load_digraph("snowshoe_rp"))
# Test Definition Result
# 0 Condition i No positive self-effects True
# 1 Condition ii At least one node is self-regulating True
# 2 Condition iii The product of any pairwise interaction is non-positive True
# 3 Condition iv No cycles greater than length two False
# 4 Condition v Non-zero determinant (all nodes have at least one incoming and outgoing link) True
# 5 Colour test Fails Jeffries' colour test True
# 6 Sign stable Satisfies necessary and sufficient conditions for sign stability False
Source code in qmm/core/stability.py
system_feedback
cached
Calculate the product of conjunct and disjunct feedback cycles for any level of the system (coefficients of the characteristic polynomial).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
level
|
Optional[int]
|
Level of feedback to compute (None for all levels) |
None
|
form
|
Literal['symbolic', 'signed']
|
Type of feedback ('symbolic', 'signed') |
'symbolic'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Feedback cycle products at specified levels |
References
- Lyapunov, A.M. (1892). The general problem of the stability of motion. International Journal of Control 55, 531–534.
- Levins, R. (1974). The qualitative analysis of partially specified systems. Annals of the New York Academy of Sciences 231, 123–138.
- Puccia, C.J., Levins, R. (1985). Qualitative Modeling of Complex Systems: An Introduction to Loop Analysis and Time Averaging. Harvard University Press.
Examples:
from qmm import load_digraph, system_feedback
system_feedback(load_digraph("snowshoe_rp"), level=2, form='symbolic')
# Matrix([[-a_C,P*a_P,C - a_C,R*a_R,C - a_P,P*a_R,R]])
system_feedback(load_digraph("snowshoe_rp"), level=3, form='symbolic')
# Matrix([[-a_C,P*a_P,C*a_R,R + a_C,P*a_P,R*a_R,C - a_C,R*a_P,P*a_R,C]])
system_feedback(load_digraph("snowshoe_rp"), form='symbolic')
# Matrix([
# [ -1],
# [ -a_P,P - a_R,R],
# [ -a_C,P*a_P,C - a_C,R*a_R,C - a_P,P*a_R,R],
# [-a_C,P*a_P,C*a_R,R + a_C,P*a_P,R*a_R,C - a_C,R*a_P,P*a_R,C]])
Source code in qmm/core/stability.py
net_feedback
cached
Calculate net feedback at a specified level of the system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
level
|
Optional[int]
|
Level of feedback to compute (None for all levels) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Net feedback at specified levels |
References
- Dambacher, J.M., Luh, H.-K., Li, H.W., Rossignol, P.A. (2003). Qualitative stability and ambiguity in model ecosystems. The American Naturalist 161, 876–888.
Examples:
from qmm import load_digraph, net_feedback
net_feedback(load_digraph("snowshoe_rp"), level=2)
# Matrix([[-3]])
net_feedback(load_digraph("snowshoe_rp"))
# Matrix([
# [-1],
# [-2],
# [-3],
# [-1]])
Source code in qmm/core/stability.py
absolute_feedback
cached
Calculate absolute feedback at a specified level of the system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
level
|
Optional[int]
|
Level of feedback to compute (None for all levels) |
None
|
method
|
Literal['combinations', 'polynomial']
|
Method for computing feedback ('combinations' or 'polynomial') |
'combinations'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Total number of feedback terms at specified levels |
References
- Dambacher, J.M., Luh, H.-K., Li, H.W., Rossignol, P.A. (2003). Qualitative stability and ambiguity in model ecosystems. The American Naturalist 161, 876–888.
Examples:
from qmm import load_digraph, absolute_feedback
absolute_feedback(load_digraph("snowshoe_rp"), level=2)
# Matrix([[3]])
absolute_feedback(load_digraph("snowshoe_rp"))
# Matrix([
# [1],
# [2],
# [3],
# [3]])
Source code in qmm/core/stability.py
weighted_feedback
cached
Calculate ratio of net to total feedback terms at each level of the system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
level
|
Optional[int]
|
Level to compute weighted feedback (None for all levels) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Weighted feedback metrics for each level |
References
- Dambacher, J.M., Luh, H.-K., Li, H.W., Rossignol, P.A. (2003). Qualitative stability and ambiguity in model ecosystems. The American Naturalist 161, 876–888.
Examples:
from qmm import load_digraph, weighted_feedback
weighted_feedback(load_digraph("snowshoe_rp"), level=2)
# Matrix([[-1]])
weighted_feedback(load_digraph("snowshoe_rp"))
# Matrix([
# [ -1],
# [ -1],
# [ -1],
# [-1/3]])
Source code in qmm/core/stability.py
feedback_metrics
cached
Calculate net, absolute and weighted feedback metrics at each level of the system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Feedback metrics for each system level |
References
- Dambacher, J.M., Luh, H.-K., Li, H.W., Rossignol, P.A. (2003). Qualitative stability and ambiguity in model ecosystems. The American Naturalist 161, 876–888.
Examples:
from qmm import load_digraph, feedback_metrics
feedback_metrics(load_digraph("snowshoe_rp"))
# Feedback level Net Absolute Positive Negative Weighted
# 0 0 -1 1 0 1 -1
# 1 1 -2 2 0 2 -1
# 2 2 -3 3 0 3 -1
# 3 3 -1 3 1 2 -1/3
Source code in qmm/core/stability.py
hurwitz_determinants
cached
Calculate Hurwitz determinants for analysing system stability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
level
|
Optional[int]
|
Level to compute determinants (None for all Hurwitz determinants) |
None
|
form
|
Literal['symbolic', 'signed']
|
Type of computation ('symbolic' or 'signed') |
'symbolic'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Hurwitz determinants at specified levels |
References
- Hurwitz, A. (1895). On the conditions under which an equation has only roots with negative real parts. Mathematische Annelen 65, 273–284.
- Puccia, C.J., Levins, R. (1985). Qualitative Modeling of Complex Systems: An Introduction to Loop Analysis and Time Averaging. Harvard University Press.
Examples:
from qmm import load_digraph, hurwitz_determinants
hurwitz_determinants(load_digraph("snowshoe_rp"), level=2, form='symbolic')
# Matrix([[a_C,P*a_P,C*a_P,P + a_C,P*a_P,R*a_R,C + a_C,R*a_R,C*a_R,R + a_P,P**2*a_R,R + a_P,P*a_R,R**2]])
Source code in qmm/core/stability.py
net_determinants
cached
Calculate net terms in Hurwitz determinants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
level
|
Optional[int]
|
Level to compute determinants (None for all Hurwitz determinants) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Net terms in Hurwitz determinants |
References
- Dambacher, J.M., Luh, H.-K., Li, H.W., Rossignol, P.A. (2003). Qualitative stability and ambiguity in model ecosystems. The American Naturalist 161, 876–888.
Examples:
from qmm import load_digraph, net_determinants
net_determinants(load_digraph("snowshoe_rp"), level=2)
# Matrix([[5]])
net_determinants(load_digraph("snowshoe_rp"))
# Matrix([
# [1],
# [2],
# [5],
# [5]])
Source code in qmm/core/stability.py
absolute_determinants
cached
Calculate absolute terms in Hurwitz determinants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
level
|
Optional[int]
|
Level to compute determinants (None for all Hurwitz determinants) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Absolute terms in Hurwitz determinants |
References
- Dambacher, J.M., Luh, H.-K., Li, H.W., Rossignol, P.A. (2003). Qualitative stability and ambiguity in model ecosystems. The American Naturalist 161, 876–888.
Examples:
from qmm import load_digraph, absolute_determinants
absolute_determinants(load_digraph("snowshoe_rp"), level=2)
# Matrix([[9]])
absolute_determinants(load_digraph("snowshoe_rp"))
# Matrix([
# [ 1],
# [ 2],
# [ 9],
# [27]])
Source code in qmm/core/stability.py
weighted_determinants
cached
Calculate ratio of net to total terms for Hurwitz determinants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
level
|
Optional[int]
|
Level to compute determinants (None for all Hurwitz determinants) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Ratio of net to total terms for Hurwitz determinants |
References
- Dambacher, J.M., Luh, H.-K., Li, H.W., Rossignol, P.A. (2003). Qualitative stability and ambiguity in model ecosystems. The American Naturalist 161, 876–888.
Examples:
from qmm import load_digraph, weighted_determinants
weighted_determinants(load_digraph("snowshoe_rp"), level=2)
# Matrix([[5/9]])
weighted_determinants(load_digraph("snowshoe_rp"))
# Matrix([
# [ 1],
# [ 1],
# [ 5/9],
# [5/27]])
Source code in qmm/core/stability.py
determinants_metrics
cached
Calculate net, absolute and weighted Hurwitz determinant metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Hurwitz determinant metrics |
References
- Dambacher, J.M., Luh, H.-K., Li, H.W., Rossignol, P.A. (2003). Qualitative stability and ambiguity in model ecosystems. The American Naturalist 161, 876–888.
Examples:
from qmm import load_digraph, determinants_metrics
determinants_metrics(load_digraph("snowshoe_rp"))
# Hurwitz determinant Net Absolute Weighted
# 0 0 1 1 1
# 1 1 2 2 1
# 2 2 5 9 5/9
# 3 3 5 27 5/27
Source code in qmm/core/stability.py
conditional_stability
cached
Analyse conditional stability metrics and model stability class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Conditional stability metrics and model class |
References
- Dambacher, J.M., Luh, H.-K., Li, H.W., Rossignol, P.A. (2003). Qualitative stability and ambiguity in model ecosystems. The American Naturalist 161, 876–888.
Examples:
from qmm import load_digraph, conditional_stability
conditional_stability(load_digraph("snowshoe_rp"))
# Test Definition Result
# 0 Weighted feedback Maximum weighted feedback (level 3) -0.33
# 1 Weighted determinant n-1 weighted determinant at level 0.56
# 2 Ratio to model-c system Ratio to a 'model-c' type system 1.7
# 3 Model class Class of the model based on conditional stability metrics Class I
Source code in qmm/core/stability.py
simulation_stability
Analyse stability using randomly sampled interaction strengths from a specified distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
n_sim
|
int
|
Number of simulations to perform (default 10000) |
10000
|
dist
|
Literal['uniform', 'weak', 'moderate', 'strong', 'uniform_two_oom']
|
Distribution to sample from (default 'uniform'): - "uniform": Uniform(0, 1) - no assumption about interaction strength - "weak": Beta(1, 3) - weak interactions predominate - "moderate": Beta(2, 2) - moderate interactions predominate - "strong": Beta(3, 1) - strong interactions predominate - "uniform_two_oom": Uniform(0.01, 1) |
'uniform'
|
seed
|
int
|
Random seed |
42
|
presample
|
Optional[ndarray]
|
Optional pre-sampled values of shape (n_sim, n, n) to use instead of random sampling |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Proportion of stable matrices and proportion that fail Hurwitz criteria |
References
- May, R.M. (1973). Qualitative Stability in Model Ecosystems. Ecology 54, 638–641.
- Dambacher, J.M., Luh, H.-K., Li, H.W., Rossignol, P.A. (2003). Qualitative stability and ambiguity in model ecosystems. The American Naturalist 161, 876–888.
Examples:
from qmm import load_digraph, simulation_stability
simulation_stability(load_digraph("snowshoe_rp"), n_sim=1000)
# Test Definition Result
# 0 Stable matrices Proportion where all eigenvalues have negative real parts 76.70%
# 1 Unstable matrices Proportion where one or more eigenvalues have positive real parts 23.30%
# 2 Hurwitz criterion i Proportion where polynomial coefficients are not all of the same sign 23.30%
# 3 Hurwitz criterion ii Proportion where Hurwitz determinants are not all positive 0.00%
# 4 Hurwitz criterion i only Proportion where only Hurwitz criterion i fails 23.30%
# 5 Hurwitz criterion ii only Proportion where only Hurwitz criterion ii fails 0.00%
Source code in qmm/core/stability.py
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 | |
qmm.core.press
Analyse direct and indirect effects of press perturbations.
adjoint_matrix
cached
Calculate elements of classical adjoint matrix for press perturbation response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
form
|
Literal['symbolic', 'signed']
|
Type of computation ('symbolic', 'signed') |
'symbolic'
|
perturb
|
Optional[str]
|
Node to perturb (None for full matrix) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Classical adjoint matrix elements |
References
- Dambacher, J.M., Li, H.W., Rossignol, P.A. (2002). Relevance of Community Structure in Assessing Indeterminacy of Ecological Predictions. Ecology 83, 1372–1385.
Examples:
from qmm import load_digraph, adjoint_matrix
adjoint_matrix(load_digraph("snowshoe_rp"), form='symbolic')
# Matrix([
# [ a_C,P*a_P,C, -a_P,P*a_R,C, a_C,P*a_R,C],
# [-a_C,P*a_P,R + a_C,R*a_P,P, a_P,P*a_R,R, -a_C,P*a_R,R],
# [ a_C,R*a_P,C, a_P,C*a_R,R - a_P,R*a_R,C, a_C,R*a_R,C]])
adjoint_matrix(load_digraph("snowshoe_rp"), form='symbolic', perturb='R')
# Matrix([
# [ a_C,P*a_P,C],
# [-a_C,P*a_P,R + a_C,R*a_P,P],
# [ a_C,R*a_P,C]])
adjoint_matrix(load_digraph("snowshoe_rp"), form='signed')
# Matrix([
# [1, -1, 1],
# [0, 1, -1],
# [1, 0, 1]])
adjoint_matrix(load_digraph("snowshoe_rp"), form='signed', perturb='R')
# Matrix([
# [1],
# [0],
# [1]])
Source code in qmm/core/press.py
absolute_feedback_matrix
cached
Calculate total number of both positive and negative terms for press perturbation response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
perturb
|
Optional[str]
|
Node to perturb (None for full matrix) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Absolute feedback matrix elements |
References
- Dambacher, J.M., Li, H.W., Rossignol, P.A. (2002). Relevance of Community Structure in Assessing Indeterminacy of Ecological Predictions. Ecology 83, 1372–1385.
Examples:
from qmm import load_digraph, absolute_feedback_matrix
absolute_feedback_matrix(load_digraph("snowshoe_rp"))
# Matrix([
# [1, 1, 1],
# [2, 1, 1],
# [1, 2, 1]])
absolute_feedback_matrix(load_digraph("snowshoe_rp"), perturb='R')
# Matrix([
# [1],
# [2],
# [1]])
Source code in qmm/core/press.py
weighted_predictions_matrix
cached
Calculate ratio of net to total terms for a press perturbation response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
as_nan
|
bool
|
Return NaN for undefined ratios |
True
|
as_abs
|
bool
|
Return absolute values |
False
|
perturb
|
Optional[str]
|
Node to perturb (None for full matrix) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Prediction weights |
References
- Dambacher, J.M., Li, H.W., Rossignol, P.A. (2002). Relevance of Community Structure in Assessing Indeterminacy of Ecological Predictions. Ecology 83, 1372–1385.
Examples:
from qmm import load_digraph, weighted_predictions_matrix
weighted_predictions_matrix(load_digraph("snowshoe_rp"))
# Matrix([
# [1, -1, 1],
# [0, 1, -1],
# [1, 0, 1]])
weighted_predictions_matrix(load_digraph("snowshoe_rp"), perturb='R')
# Matrix([
# [1],
# [0],
# [1]])
weighted_predictions_matrix(load_digraph("snowshoe_rp"), as_abs=True)
# Matrix([
# [1, 1, 1],
# [0, 1, 1],
# [1, 0, 1]])
weighted_predictions_matrix(load_digraph("snowshoe_rp"), as_abs=False)
# Matrix([
# [1, -1, 1],
# [0, 1, -1],
# [1, 0, 1]])
Source code in qmm/core/press.py
sign_determinacy_matrix
cached
Calculate probability of a correct sign prediction (matches adjoint).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
method
|
Literal['average', '95_bound']
|
Method for computing determinacy ('average', '95_bound') |
'average'
|
as_nan
|
bool
|
Return NaN for undefined ratios |
True
|
as_abs
|
bool
|
Return absolute values |
False
|
perturb
|
Optional[str]
|
Node to perturb (None for full matrix) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Probability of sign determinacy |
References
- Hosack, G.R., Hayes, K.R., Dambacher, J.M. (2008). Assessing Model Structure Uncertainty Through an Analysis of System Feedback and Bayesian Networks. Ecological Applications 18, 1070–1082.
Examples:
from qmm import load_digraph, sign_determinacy_matrix
sign_determinacy_matrix(load_digraph("snowshoe_rp"), method='average')
# Matrix([
# [ 1, -1, 1],
# [1/2, 1, -1],
# [ 1, 1/2, 1]])
sign_determinacy_matrix(load_digraph("snowshoe_rp"), method='average', perturb='R')
# Matrix([
# [ 1],
# [1/2],
# [ 1]])
sign_determinacy_matrix(load_digraph("snowshoe_rp"), method='average', as_abs=True)
# Matrix([
# [ 1, 1, 1],
# [1/2, 1, 1],
# [ 1, 1/2, 1]])
sign_determinacy_matrix(load_digraph("snowshoe_rp"), method='average', as_abs=False)
# Matrix([
# [ 1, -1, 1],
# [1/2, 1, -1],
# [ 1, 1/2, 1]])
Source code in qmm/core/press.py
numerical_simulations
cached
numerical_simulations(
G,
n_sim=10000,
dist="uniform",
seed=42,
as_nan=True,
as_abs=False,
positive_only=False,
match_adjoint=False,
)
Calculate proportion of positive and negative responses from stable simulations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
n_sim
|
int
|
Number of simulations |
10000
|
dist
|
Literal['uniform', 'weak', 'moderate', 'strong', 'uniform_two_oom']
|
Distribution for sampling ('uniform', 'weak', 'moderate', 'strong') |
'uniform'
|
seed
|
int
|
Random seed |
42
|
as_nan
|
bool
|
Return NaN for undefined ratios |
True
|
positive_only
|
bool
|
Return just the proportion of positive responses instead of sign-dominant proportions. |
False
|
as_abs
|
bool
|
Return absolute values |
False
|
match_adjoint
|
bool
|
Return proportion of simulations matching the sign of the adjoint. Values are always between 0 and 1. Entries where the adjoint sign is ambiguous (0) return 0.5. Incompatible with positive_only and as_abs. |
False
|
References
- Dambacher, J.M., Li, H.W., Rossignol, P.A. (2003). Qualitative predictions in model ecosystems. Ecological Modelling 161, 79–93.
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Average proportion of positive and negative responses |
Raises:
| Type | Description |
|---|---|
ValueError
|
If invalid parameter combinations are used. |
Examples:
from qmm import load_digraph, numerical_simulations
numerical_simulations(load_digraph("snowshoe_rp"), n_sim=1000, seed=42)
# Matrix([
# [ 1.0, -1.0, 1.0],
# [0.621, 1.0, -1.0],
# [ 1.0, 0.639, 1.0]])
numerical_simulations(load_digraph("snowshoe_rp"), n_sim=1000, seed=42, as_abs=True)
# Matrix([
# [ 1.0, 1.0, 1.0],
# [0.621, 1.0, 1.0],
# [ 1.0, 0.639, 1.0]])
numerical_simulations(load_digraph("snowshoe_rp"), n_sim=1000, seed=42, as_abs=False)
# Matrix([
# [ 1.0, -1.0, 1.0],
# [0.621, 1.0, -1.0],
# [ 1.0, 0.639, 1.0]])
numerical_simulations(load_digraph("snowshoe_rp"), n_sim=1000, seed=42, positive_only=True)
# Matrix([
# [ 1.0, 0.0, 1.0],
# [0.621, 1.0, 0.0],
# [ 1.0, 0.639, 1.0]])
Source code in qmm/core/press.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
qmm.core.prediction
Generate qualitative predictions of system response to press perturbations with thresholds for ambiguity.
qualitative_predictions
Create a sympy matrix of qualitative predictions with thresholds for ambiguity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
Graph input for the matrix generator |
required |
generator
|
Callable
|
Matrix generator function from core.press module |
numerical_simulations
|
t1
|
float
|
Lower threshold for predictions |
0.8
|
t2
|
float
|
Higher threshold for predictions |
0.95
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Qualitative predictions |
Raises:
| Type | Description |
|---|---|
ValueError
|
If generator is not callable or thresholds are invalid |
References
- Dambacher, J.M., Li, H.W., Rossignol, P.A. (2002). Relevance of Community Structure in Assessing Indeterminacy of Ecological Predictions. Ecology 83, 1372–1385.
- Dambacher, J.M., Li, H.W., Rossignol, P.A. (2003). Qualitative predictions in model ecosystems. Ecological Modelling 161, 79–93.
- Hosack, G.R., Hayes, K.R., Dambacher, J.M. (2008). Assessing Model Structure Uncertainty Through an Analysis of System Feedback and Bayesian Networks. Ecological Applications 18, 1070–1082.
Examples:
from qmm import load_digraph, qualitative_predictions, weighted_predictions_matrix
qualitative_predictions(load_digraph("snowshoe_rp"), generator=weighted_predictions_matrix, t1=0.5, t2=1.0)
# Matrix([
# [+, −, +],
# [?, +, −],
# [+, ?, +]])
Source code in qmm/core/prediction.py
table_of_predictions
Create a table of qualitative predictions with thresholds for ambiguity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
M
|
Union[Matrix, ndarray]
|
Matrix of predictions from press perturbation analysis |
required |
t1
|
float
|
Lower threshold for predictions |
0.8
|
t2
|
float
|
Higher threshold for predictions |
0.95
|
index
|
Optional[List[str]]
|
Row labels |
None
|
columns
|
Optional[List[str]]
|
Column labels |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Qualitative predictions |
References
- Dambacher, J.M., Li, H.W., Rossignol, P.A. (2002). Relevance of Community Structure in Assessing Indeterminacy of Ecological Predictions. Ecology 83, 1372–1385.
- Dambacher, J.M., Li, H.W., Rossignol, P.A. (2003). Qualitative predictions in model ecosystems. Ecological Modelling 161, 79–93.
- Hosack, G.R., Hayes, K.R., Dambacher, J.M. (2008). Assessing Model Structure Uncertainty Through an Analysis of System Feedback and Bayesian Networks. Ecological Applications 18, 1070–1082.
Examples:
from qmm import load_digraph, weighted_predictions_matrix, table_of_predictions
W = weighted_predictions_matrix(load_digraph("snowshoe_rp"))
nodes = ['R', 'C', 'P']
table_of_predictions(W, t1=0.5, t2=1.0, index=nodes, columns=nodes)
# R C P
# R + − +
# C ? + −
# P + ? +
Source code in qmm/core/prediction.py
compare_predictions
Compare predictions between alternative models or prediction methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
M1
|
DataFrame
|
First matrix of predictions |
required |
M2
|
DataFrame
|
Second matrix of predictions |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Combined predictions showing differences and agreements |
Examples:
from qmm import load_digraph, weighted_predictions_matrix, table_of_predictions, compare_predictions
G1 = load_digraph("snowshoe_rp")
G2 = G1.copy()
G2.remove_edge('C', 'P')
nodes = ['R', 'C', 'P']
W1 = weighted_predictions_matrix(G1)
W2 = weighted_predictions_matrix(G2)
pred1 = table_of_predictions(W1, t1=0.5, t2=1.0, index=nodes, columns=nodes)
pred2 = table_of_predictions(W2, t1=0.5, t2=1.0, index=nodes, columns=nodes)
compare_predictions(pred1, pred2)
# R C P
# R +, 0 − +
# C ? + −
# P +, 0 ?, − +
Source code in qmm/core/prediction.py
qmm.core.helper
Utility functions for model development and analysis.
list_to_digraph
Convert an adjacency matrix to a directed graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
Union[List[List[int]], ndarray]
|
A square matrix (list of lists or numpy array) representing the adjacency matrix. Non-zero values indicate edges, where the value represents the sign of the edge. |
required |
ids
|
Optional[List[str]]
|
Optional list of node identifiers. If None, nodes will be labeled 1 to n. |
None
|
Returns:
| Type | Description |
|---|---|
DiGraph
|
nx.DiGraph: A NetworkX directed graph with signed edges. |
Examples:
from qmm import list_to_digraph
G = list_to_digraph([[-1, -1, 0], [1, 0, -1], [1, 1, -1]])
list(G.nodes())
# ['1', '2', '3']
list(G.edges(data='sign'))
# [('1', '1', -1), ('1', '2', 1), ('1', '3', 1), ('2', '1', -1), ('2', '3', 1), ('3', '2', -1), ('3', '3', -1)]
Source code in qmm/core/helper.py
load_digraph
Load a built-in example model as a signed directed graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Name of the built-in model to load. Available models: - "snowshoe": Simple 3-node predator-prey model (R, C, P) - "snowshoe_rp": Snowshoe model with an added positive R->P link - "snowshoe_io": Snowshoe_rp model with input/output nodes - "chain": 5-node linear chain with self-effects - "mesocosm": 8-node complex ecosystem model |
required |
Returns:
| Type | Description |
|---|---|
DiGraph
|
nx.DiGraph: A NetworkX directed graph with signed edges. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If model name is not recognized. |
Examples:
from qmm import load_digraph
G = load_digraph("snowshoe_rp")
list(G.nodes())
# ['R', 'C', 'P']
list(G.edges(data='sign'))
# [('R', 'R', -1), ('R', 'C', 1), ('R', 'P', 1), ('C', 'R', -1), ('C', 'P', 1), ('P', 'C', -1), ('P', 'P', -1)]
Source code in qmm/core/helper.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
digraph_to_list
Convert a directed graph to an adjacency matrix string representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
A NetworkX directed graph with signed edges. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
String representation of the adjacency matrix. |
Examples:
from qmm import load_digraph, digraph_to_list
digraph_to_list(load_digraph("snowshoe_rp"))
# '[[0, -1, 1], [1, -1, 1], [-1, 0, -1]]'
Source code in qmm/core/helper.py
get_nodes
Get nodes of a specific type from a directed graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX directed graph to extract nodes from. |
required |
node_type
|
Literal['state', 'input', 'output', 'all']
|
Type of nodes to extract ('state', 'input', 'output', or 'all'). |
'state'
|
labels
|
bool
|
If True, return node labels instead of node ids. |
False
|
Returns:
| Type | Description |
|---|---|
List[Union[str, Dict[str, Any]]]
|
List of node identifiers or dictionaries containing node data. |
Examples:
from qmm import load_digraph, get_nodes
get_nodes(load_digraph("snowshoe_rp"), "state")
# ['R', 'C', 'P']
Source code in qmm/core/helper.py
get_weight
Calculate weight matrix by dividing net effect by absolute effect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net
|
Matrix
|
Matrix of net terms. |
required |
absolute
|
Matrix
|
Matrix of absolute terms. |
required |
no_effect
|
Union[Basic, float]
|
Value to use when absolute terms is 0 (default: sympy.nan). |
nan
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sympy.Matrix: Matrix of weights. |
Examples:
import sympy as sp
from qmm import get_weight
net = sp.Matrix([[2, -2], [1, 0]])
absolute = sp.Matrix([[4, 2], [1, 0]])
get_weight(net, absolute)
# Matrix([
# [1/2, -1],
# [ 1, nan]])
Source code in qmm/core/helper.py
get_positive
Calculate matrix of positive terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net
|
Matrix
|
Matrix of net terms. |
required |
absolute
|
Matrix
|
Matrix of absolute terms. |
required |
Returns:
| Type | Description |
|---|---|
Matrix
|
sympy.Matrix: Matrix of positive terms. |
Examples:
import sympy as sp
from qmm import get_positive
net = sp.Matrix([[3, -2], [1, 0]])
absolute = sp.Matrix([[4, 2], [1, 0]])
get_positive(net, absolute)
# Matrix([
# [3, 0],
# [1, 0]])
Source code in qmm/core/helper.py
get_negative
Calculate matrix of negative terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net
|
Matrix
|
Matrix of net terms. |
required |
absolute
|
Matrix
|
Matrix of absolute terms. |
required |
Returns:
| Type | Description |
|---|---|
Matrix
|
sympy.Matrix: Matrix of negative terms. |
Examples:
import sympy as sp
from qmm import get_negative
net = sp.Matrix([[3, -2], [1, 0]])
absolute = sp.Matrix([[4, 2], [1, 0]])
get_negative(net, absolute)
# Matrix([
# [0, 2],
# [0, 0]])
Source code in qmm/core/helper.py
sign_determinacy
Calculate sign determinacy matrix from prediction weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wmat
|
Matrix
|
Matrix of prediction weights. |
required |
tmat
|
Matrix
|
Matrix of absolute feedback. |
required |
method
|
Literal['average', '95_bound']
|
Method to use for probability calculation ('average' or '95_bound'). |
'average'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sympy.Matrix: Probability of sign determinacy. |
References
- Hosack, G.R., Hayes, K.R., Dambacher, J.M. (2008). Assessing Model Structure Uncertainty Through an Analysis of System Feedback and Bayesian Networks. Ecological Applications 18, 1070–1082.
Examples:
from qmm import load_digraph, weighted_predictions_matrix, absolute_feedback_matrix, sign_determinacy
G = load_digraph("snowshoe_rp")
wmat = weighted_predictions_matrix(G)
tmat = absolute_feedback_matrix(G)
sign_determinacy(wmat, tmat, method='average')
# Matrix([
# [ 1, -1, 1],
# [1/2, 1, -1],
# [ 1, 1/2, 1]])
Source code in qmm/core/helper.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
perm
Compute the permanent of a square matrix.
The permanent is similar to the determinant but uses only addition (no sign alternation). This implementation is based on the algorithms from thewalrus library (https://github.com/XanaduAI/thewalrus).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
ndarray
|
A square numpy array (float or complex). |
required |
method
|
Literal['bbfg', 'ryser']
|
Algorithm to use - "bbfg" for BBFG formula (default, faster) or "ryser" for Ryser formula. Any other value uses Ryser. |
'bbfg'
|
decompose
|
bool
|
Dulmage-Mendelsohn decomposition (default True) for sparse matrices. |
True
|
Returns:
| Type | Description |
|---|---|
float
|
The permanent of matrix A. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a numpy array. |
ValueError
|
If matrix is not square or contains NaNs. |
References
- Ryser, H.J. (1963). Combinatorial Mathematics.
- Glynn, D.G. (2010). The permanent of a square matrix. European Journal of Combinatorics 31, 1887–1891.
Examples:
import numpy as np
from qmm.core.helper import perm
perm(np.array([[1, 2], [3, 4]]), method='bbfg')
# 10
Source code in qmm/core/helper.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | |
get_dashed_alternatives
Generate all alternative model structures based on dashed edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph with potentially dashed edges (edges with dashes=True attribute) |
required |
combinations
|
bool
|
If True, return all 2^n combinations of dashed edges. If False, return base graph (no dashed edges) plus variants with each single dashed edge added. |
True
|
Returns:
| Type | Description |
|---|---|
List[DiGraph]
|
List[nx.DiGraph]: List of graph variants with different dashed edge configurations. If no dashed edges exist, returns a list containing only the original graph. |
References
- Raymond, B., McInnes, J., Dambacher, J.M., Way, S., Bergstrom, D.M. (2011). Qualitative modelling of invasive species eradication on subantarctic Macquarie Island. Journal of Applied Ecology 48, 181–191.
Examples:
from qmm import load_digraph
from qmm.core.helper import get_dashed_alternatives
G_mod = load_digraph("snowshoe_rp").copy()
G_mod.remove_edge('C', 'P')
G_mod.add_edge('C', 'P', sign=1, dashes=True)
variants = get_dashed_alternatives(G_mod, combinations=True)
len(variants)
# 2
Source code in qmm/core/helper.py
Extension module
qmm.extensions.senstability
Analyse the sensitivity of system stability to direct effects within feedback cycles.
structural_sensitivity
cached
Calculate contribution of direct effects to stabilising and destabilising feedback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
level
|
Optional[int]
|
Feedback level (None for highest level) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Ratio of net to total feedback terms for each direct effect |
References
- Hosack, G.R., Li, H.W., Rossignol, P.A. (2009). Sensitivity of system stability to model structure. Ecological Modelling 220, 1054–1062.
Examples:
from qmm import structural_sensitivity, load_digraph
structural_sensitivity(load_digraph("snowshoe_rp"))
# Matrix([
# [-a_C,P*a_P,C*a_R,R, a_C,P*a_P,R*a_R,C - a_C,R*a_P,P*a_R,C, 0],
# [-a_C,R*a_P,P*a_R,C, 0, -a_C,P*a_P,C*a_R,R + a_C,P*a_P,R*a_R,C],
# [ a_C,P*a_P,R*a_R,C, -a_C,P*a_P,C*a_R,R, -a_C,R*a_P,P*a_R,C]])
Source code in qmm/extensions/senstability.py
net_structural_sensitivity
cached
Calculate net contribution of direct effects to system feedback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
level
|
Optional[int]
|
Feedback level (None for highest level) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Net feedback terms containing each direct effect |
References
- Hosack, G.R., Li, H.W., Rossignol, P.A. (2009). Sensitivity of system stability to model structure. Ecological Modelling 220, 1054–1062.
Examples:
from qmm import net_structural_sensitivity, load_digraph
net_structural_sensitivity(load_digraph("snowshoe_rp"))
# Matrix([
# [-1, 0, 0],
# [-1, 0, 0],
# [ 1, -1, -1]])
Source code in qmm/extensions/senstability.py
absolute_structural_sensitivity
cached
Calculate total contribution of direct effects to system feedback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
level
|
Optional[int]
|
Feedback level (None for highest level) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Total feedback terms containing each direct effect |
References
- Hosack, G.R., Li, H.W., Rossignol, P.A. (2009). Sensitivity of system stability to model structure. Ecological Modelling 220, 1054–1062.
Examples:
from qmm import absolute_structural_sensitivity, load_digraph
absolute_structural_sensitivity(load_digraph("snowshoe_rp"))
# Matrix([
# [1, 2, 0],
# [1, 0, 2],
# [1, 1, 1]])
Source code in qmm/extensions/senstability.py
weighted_structural_sensitivity
cached
Calculate weighted structual sensitvity for each direct effect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
level
|
Optional[int]
|
Feedback level (None for highest level) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Weighted structural sensitivity of each direct effect |
References
- Hosack, G.R., Li, H.W., Rossignol, P.A. (2009). Sensitivity of system stability to model structure. Ecological Modelling 220, 1054–1062.
Examples:
from qmm import weighted_structural_sensitivity, load_digraph
weighted_structural_sensitivity(load_digraph("snowshoe_rp"))
# Matrix([
# [-1, 0, nan],
# [-1, nan, 0],
# [ 1, -1, -1]])
Source code in qmm/extensions/senstability.py
qmm.extensions.life
Analyse change in life expectancy from press perturbations.
birth_matrix
cached
Create matrix of direct effects on birth rate from press perturbations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
form
|
Literal['symbolic', 'signed']
|
Type of computation ('symbolic', 'signed') |
'symbolic'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Positive direct effects on birth rate |
References
- Dambacher, J.M., Levins, R., Rossignol, P.A. (2005). Life expectancy change in perturbed communities: Derivation and qualitative analysis. Mathematical Biosciences 197, 1–14.
Examples:
from qmm import birth_matrix, load_digraph
birth_matrix(load_digraph("snowshoe_rp"), form='symbolic')
# Matrix([
# [ 0, 0, 0],
# [a_C,R, 0, 0],
# [a_P,R, a_P,C, 0]])
birth_matrix(load_digraph("snowshoe_rp"), form='signed')
# Matrix([
# [0, 0, 0],
# [1, 0, 0],
# [1, 1, 0]])
Source code in qmm/extensions/life.py
death_matrix
cached
Create matrix of direct effects on death rate from press perturbations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
form
|
Literal['symbolic', 'signed']
|
Type of computation ('symbolic', 'signed') |
'symbolic'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Positive direct effects on death rate |
References
- Dambacher, J.M., Levins, R., Rossignol, P.A. (2005). Life expectancy change in perturbed communities: Derivation and qualitative analysis. Mathematical Biosciences 197, 1–14.
Examples:
from qmm import death_matrix, load_digraph
death_matrix(load_digraph("snowshoe_rp"), form='symbolic')
# Matrix([
# [a_R,R, a_R,C, 0],
# [ 0, 0, a_C,P],
# [ 0, 0, a_P,P]])
death_matrix(load_digraph("snowshoe_rp"), form='signed')
# Matrix([
# [1, 1, 0],
# [0, 0, 1],
# [0, 0, 1]])
Source code in qmm/extensions/life.py
life_expectancy_change
cached
Calculate change in life expectancy from press perturbations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
type
|
Literal['birth', 'death']
|
Change in birth or death rate ('birth' or 'death') |
'birth'
|
form
|
Literal['symbolic', 'signed']
|
Type of computation ('symbolic', 'signed') |
'symbolic'
|
perturb
|
Optional[str]
|
Node to perturb (None for full matrix) |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Change in life expectancy for each component |
References
- Dambacher, J.M., Levins, R., Rossignol, P.A. (2005). Life expectancy change in perturbed communities: Derivation and qualitative analysis. Mathematical Biosciences 197, 1–14.
Examples:
from qmm import life_expectancy_change, load_digraph
life_expectancy_change(load_digraph("snowshoe_rp"), form='symbolic', type='birth')
# Matrix([
# [-a_C,P*a_P,C*a_R,R + a_C,P*a_P,R*a_R,C - a_C,R*a_P,P*a_R,C, 0, 0],
# [ -a_C,P*a_C,R*a_P,C, -a_C,P*a_P,C*a_R,R + a_C,P*a_P,R*a_R,C, -a_C,P*a_C,R*a_R,C],
# [ -a_C,R*a_P,C*a_P,P, -a_P,C*a_P,P*a_R,R + a_P,P*a_P,R*a_R,C, -a_C,R*a_P,P*a_R,C]])
life_expectancy_change(load_digraph("snowshoe_rp"), form='symbolic', type='death')
# Matrix([
# [ 0, 0, 0],
# [-a_C,P*a_C,R*a_P,C, a_C,R*a_P,P*a_R,C, -a_C,P*a_C,R*a_R,C],
# [-a_C,R*a_P,C*a_P,P, -a_P,C*a_P,P*a_R,R + a_P,P*a_P,R*a_R,C, a_C,P*a_P,C*a_R,R - a_C,P*a_P,R*a_R,C]])
Source code in qmm/extensions/life.py
net_life_expectancy_change
cached
Calculate net terms in life expectancy change from press perturbations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
type
|
Literal['birth', 'death']
|
Change in birth or death rate ('birth' or 'death') |
'birth'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Net life expectancy change for each component |
References
- Dambacher, J.M., Levins, R., Rossignol, P.A. (2005). Life expectancy change in perturbed communities: Derivation and qualitative analysis. Mathematical Biosciences 197, 1–14.
Examples:
from qmm import net_life_expectancy_change, load_digraph
net_life_expectancy_change(load_digraph("snowshoe_rp"), type='birth')
# Matrix([
# [-1, 0, 0],
# [-1, 0, -1],
# [-1, 0, -1]])
net_life_expectancy_change(load_digraph("snowshoe_rp"), type='death')
# Matrix([
# [ 0, 0, 0],
# [-1, 1, -1],
# [-1, 0, 0]])
Source code in qmm/extensions/life.py
absolute_life_expectancy_change
cached
Calculate absolute terms in life expectancy change from press perturbations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
type
|
Literal['birth', 'death']
|
Change in birth or death rate ('birth' or 'death') |
'birth'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Absolute life expectancy change for each component |
References
- Dambacher, J.M., Levins, R., Rossignol, P.A. (2005). Life expectancy change in perturbed communities: Derivation and qualitative analysis. Mathematical Biosciences 197, 1–14.
Examples:
from qmm import absolute_life_expectancy_change, load_digraph
absolute_life_expectancy_change(load_digraph("snowshoe_rp"), type='birth')
# Matrix([
# [3, 0, 0],
# [1, 2, 1],
# [1, 2, 1]])
absolute_life_expectancy_change(load_digraph("snowshoe_rp"), type='death')
# Matrix([
# [0, 0, 0],
# [1, 1, 1],
# [1, 2, 2]])
Source code in qmm/extensions/life.py
weighted_predictions_life_expectancy
cached
Calculate ratio of net to total change in life expectancy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
type
|
Literal['birth', 'death']
|
Change in birth or death rate ('birth' or 'death') |
'birth'
|
as_nan
|
bool
|
Return NaN for undefined ratios |
True
|
as_abs
|
bool
|
Return absolute values |
False
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Net-to-total ratios for life expectancy predictions |
References
- Dambacher, J.M., Levins, R., Rossignol, P.A. (2005). Life expectancy change in perturbed communities: Derivation and qualitative analysis. Mathematical Biosciences 197, 1–14.
Examples:
from qmm import weighted_predictions_life_expectancy, load_digraph
weighted_predictions_life_expectancy(load_digraph("snowshoe_rp"), type='birth', as_abs=False, as_nan=True)
# Matrix([
# [-1/3, nan, nan],
# [ -1, 0, -1],
# [ -1, 0, -1]])
weighted_predictions_life_expectancy(load_digraph("snowshoe_rp"), type='birth', as_abs=True, as_nan=False)
# Matrix([
# [1/3, 1, 1],
# [ 1, 0, 1],
# [ 1, 0, 1]])
weighted_predictions_life_expectancy(load_digraph("snowshoe_rp"), type='death', as_abs=False, as_nan=True)
# Matrix([
# [nan, nan, nan],
# [ -1, 1, -1],
# [ -1, 0, 0]])
weighted_predictions_life_expectancy(load_digraph("snowshoe_rp"), type='death', as_abs=True, as_nan=False)
# Matrix([
# [1, 1, 1],
# [1, 1, 1],
# [1, 0, 0]])
Source code in qmm/extensions/life.py
qmm.extensions.paths
Analyse causal pathways, cycles and complementary feedback.
get_cycles
cached
Find all feedback cycles in the signed digraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Products of interactions along each cycle |
References
- Mason, S.J. (1953). Feedback Theory-Some Properties of Signal Flow Graphs. Proceedings of the IRE 41, 1144–1156.
- Levins, R. (1974). The qualitative analysis of partially specified systems. Annals of the New York Academy of Sciences 231, 123–138.
- Puccia, C.J., Levins, R. (1985). Qualitative Modeling of Complex Systems: An Introduction to Loop Analysis and Time Averaging. Harvard University Press.
Examples:
from qmm import get_cycles, load_digraph
get_cycles(load_digraph("snowshoe_rp"))
# Matrix([
# [ -a_P,P],
# [ -a_R,R],
# [ -a_C,P*a_P,C],
# [ -a_C,R*a_R,C],
# [a_C,P*a_P,R*a_R,C]])
Source code in qmm/extensions/paths.py
cycles_table
cached
Find all feedback cycles in the signed digraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Table with cycle length, path representation, and sign |
References
- Mason, S.J. (1953). Feedback Theory-Some Properties of Signal Flow Graphs. Proceedings of the IRE 41, 1144–1156.
- Levins, R. (1974). The qualitative analysis of partially specified systems. Annals of the New York Academy of Sciences 231, 123–138.
- Puccia, C.J., Levins, R. (1985). Qualitative Modeling of Complex Systems: An Introduction to Loop Analysis and Time Averaging. Harvard University Press.
Examples:
from qmm import cycles_table, load_digraph
cycles_table(load_digraph("snowshoe_rp"))
# Length Cycle Sign
# 0 1 P $\multimap$ P −
# 1 1 R $\multimap$ R −
# 2 2 C $\rightarrow$ P $\multimap$ C −
# 3 2 C $\multimap$ R $\rightarrow$ C −
# 4 3 C $\multimap$ R $\rightarrow$ P $\multimap$ C +
Source code in qmm/extensions/paths.py
get_paths
cached
Find all causal pathways between two nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
source
|
str
|
Source node |
required |
target
|
str
|
Target node (can be same as source for self-effect) |
required |
form
|
Literal['symbolic', 'signed', 'binary']
|
Type of path products ('symbolic', 'signed', or 'binary') |
'symbolic'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Products of interactions along each path |
References
- Mason, S.J. (1953). Feedback Theory-Some Properties of Signal Flow Graphs. Proceedings of the IRE 41, 1144–1156.
- Levins, R. (1974). The qualitative analysis of partially specified systems. Annals of the New York Academy of Sciences 231, 123–138.
- Puccia, C.J., Levins, R. (1985). Qualitative Modeling of Complex Systems: An Introduction to Loop Analysis and Time Averaging. Harvard University Press.
Examples:
from qmm import get_paths, load_digraph
get_paths(load_digraph("snowshoe_io"), 'Inp1', 'Out1', form='symbolic')
# Matrix([
# [a_C,R*a_P,C*b_R,Inp1*c_Out1,P],
# [ -a_C,R*b_R,Inp1*c_Out1,C],
# [ -a_P,C*b_C,Inp1*c_Out1,P],
# [ b_C,Inp1*c_Out1,C]])
get_paths(load_digraph("snowshoe_io"), 'Inp1', 'Out1', form='signed')
# Matrix([
# [ 1],
# [-1],
# [-1],
# [ 1]])
get_paths(load_digraph("snowshoe_io"), 'Inp1', 'Out1', form='binary')
# Matrix([
# [1],
# [1],
# [1],
# [1]])
Source code in qmm/extensions/paths.py
paths_table
cached
Create table of paths between nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
source
|
str
|
Source node |
required |
target
|
str
|
Target node |
required |
Returns:
| Type | Description |
|---|---|
Optional[DataFrame]
|
Optional[pd.DataFrame]: DataFrame containing path information or None if no paths exist |
References
- Mason, S.J. (1953). Feedback Theory-Some Properties of Signal Flow Graphs. Proceedings of the IRE 41, 1144–1156.
- Levins, R. (1974). The qualitative analysis of partially specified systems. Annals of the New York Academy of Sciences 231, 123–138.
- Puccia, C.J., Levins, R. (1985). Qualitative Modeling of Complex Systems: An Introduction to Loop Analysis and Time Averaging. Harvard University Press.
Examples:
from qmm import paths_table, load_digraph
paths_table(load_digraph("snowshoe_io"), 'Inp1', 'Out1')
# Length Path Sign
# 0 4 Inp1 $\rightarrow$ R $\rightarrow$ C $\rightarrow$ P $\rightarrow$ Out1 +
# 1 3 Inp1 $\rightarrow$ R $\rightarrow$ C $\multimap$ Out1 −
# 2 3 Inp1 $\multimap$ C $\rightarrow$ P $\rightarrow$ Out1 −
# 3 2 Inp1 $\multimap$ C $\multimap$ Out1 +
Source code in qmm/extensions/paths.py
complementary_feedback
cached
Calculate feedback from state nodes not on paths between source and target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
source
|
str
|
Source node |
required |
target
|
str
|
Target node (can be same as source for self-effect) |
required |
form
|
Literal['symbolic', 'signed', 'binary']
|
Type of feedback ('symbolic', 'signed', or 'binary') |
'symbolic'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Feedback cycles in complementary subsystem |
References
- Mason, S.J. (1953). Feedback Theory-Some Properties of Signal Flow Graphs. Proceedings of the IRE 41, 1144–1156.
- Levins, R. (1974). The qualitative analysis of partially specified systems. Annals of the New York Academy of Sciences 231, 123–138.
- Puccia, C.J., Levins, R. (1985). Qualitative Modeling of Complex Systems: An Introduction to Loop Analysis and Time Averaging. Harvard University Press.
Examples:
from qmm import complementary_feedback, load_digraph
complementary_feedback(load_digraph("snowshoe_io"), 'Inp1', 'Out1', form='symbolic')
# Matrix([
# [ -1],
# [ -a_P,P],
# [ -a_R,R],
# [-a_P,P*a_R,R]])
complementary_feedback(load_digraph("snowshoe_io"), 'Inp1', 'Out1', form='signed')
# Matrix([
# [-1],
# [-1],
# [-1],
# [-1]])
complementary_feedback(load_digraph("snowshoe_io"), 'Inp1', 'Out1', form='binary')
# Matrix([
# [1],
# [1],
# [1],
# [1]])
Source code in qmm/extensions/paths.py
system_paths
cached
Calculate combined effect of paths and complementary feedback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
source
|
str
|
Source node |
required |
target
|
str
|
Target node (can be same as source for self-effect) |
required |
form
|
Literal['symbolic', 'signed', 'binary']
|
Type of computation ('symbolic', 'signed', or 'binary') |
'symbolic'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Total effects including paths and feedback |
References
- Mason, S.J. (1953). Feedback Theory-Some Properties of Signal Flow Graphs. Proceedings of the IRE 41, 1144–1156.
- Levins, R. (1974). The qualitative analysis of partially specified systems. Annals of the New York Academy of Sciences 231, 123–138.
- Puccia, C.J., Levins, R. (1985). Qualitative Modeling of Complex Systems: An Introduction to Loop Analysis and Time Averaging. Harvard University Press.
Examples:
from qmm import system_paths, load_digraph
system_paths(load_digraph("snowshoe_io"), 'Inp1', 'Out1', form='symbolic')
# Matrix([
# [ a_C,R*a_P,C*b_R,Inp1*c_Out1,P],
# [-a_C,R*a_P,P*b_R,Inp1*c_Out1,C],
# [-a_P,C*a_R,R*b_C,Inp1*c_Out1,P],
# [ a_P,P*a_R,R*b_C,Inp1*c_Out1,C]])
system_paths(load_digraph("snowshoe_io"), 'Inp1', 'Out1', form='signed')
# Matrix([
# [ 1],
# [-1],
# [-1],
# [ 1]])
system_paths(load_digraph("snowshoe_io"), 'Inp1', 'Out1', form='binary')
# Matrix([
# [1],
# [1],
# [1],
# [1]])
Source code in qmm/extensions/paths.py
weighted_paths
cached
Calculate ratio of net to total path effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
source
|
str
|
Source node |
required |
target
|
str
|
Target node |
required |
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Net-to-total ratios for path predictions |
Examples:
from qmm import weighted_paths, load_digraph
weighted_paths(load_digraph("snowshoe_io"), 'Inp1', 'Out1')
# Matrix([
# [ 1],
# [-1],
# [-1],
# [ 1]])
Source code in qmm/extensions/paths.py
path_metrics
cached
Calculate comprehensive metrics for paths between nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
source
|
str
|
Source node |
required |
target
|
str
|
Target node |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Metrics including path length, sign, and feedback |
Examples:
from qmm import path_metrics, load_digraph
path_metrics(load_digraph("snowshoe_io"), 'Inp1', 'Out1')
# Length Path Path sign Complementary subsystem Net feedback Absolute feedback Positive feedback Negative feedback Weighted feedback Weighted path System path
# 0 4 Inp1, R, C, P, Out1 + None -1 1 0 1 -1 1 1
# 1 3 Inp1, R, C, Out1 − P -1 1 0 1 -1 -1 -1
# 2 3 Inp1, C, P, Out1 − R -1 1 0 1 -1 -1 -1
# 3 2 Inp1, C, Out1 + R, P -1 1 0 1 -1 1 1
Source code in qmm/extensions/paths.py
qmm.extensions.effects
Analyse cumulative effects from perturbation scenarios with multiple-inputs and multiple-outputs.
define_input_output
Classify nodes as state, input or output from topology (any pre-set category is overwritten).
Sources (and source-chains) become inputs, sinks (and sink-chains) outputs; a self-loop or feedback cycle keeps a node as state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
remove_disconnected
|
bool
|
Remove all but the largest weakly-connected component (warns about dropped nodes) |
True
|
Returns:
| Type | Description |
|---|---|
DiGraph
|
nx.DiGraph: Model with input, state and output classification |
Examples:
from qmm import define_input_output, load_digraph
G = load_digraph("snowshoe_io")
G_io = define_input_output(G)
(G_io.nodes['Inp1']['category'], G_io.nodes['Out1']['category'])
# ('input', 'output')
Source code in qmm/extensions/effects.py
direct_effects
cached
Calculate direct effects from the signed digraph structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
form
|
Literal['net', 'absolute', 'positive', 'negative']
|
Type of direct effects ('net', 'absolute', 'positive', 'negative') |
'net'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Direct effects for state/input columns and state/output rows |
Source code in qmm/extensions/effects.py
cumulative_effects
cached
Calculate cumulative effects to multiple inputs using state-space representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
form
|
Literal['symbolic', 'signed', 'binary']
|
Type of computation ('symbolic', 'signed', or 'binary') |
'symbolic'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Cumulative effects on state variables and outputs |
Examples:
from qmm import load_digraph, cumulative_effects
cumulative_effects(load_digraph("snowshoe_io"), form='symbolic')[3, 3]
# a_C,R*a_P,C*b_R,Inp1*c_Out1,P - a_C,R*a_P,P*b_R,Inp1*c_Out1,C - a_P,C*a_R,R*b_C,Inp1*c_Out1,P + a_P,P*a_R,R*b_C,Inp1*c_Out1,C
cumulative_effects(load_digraph("snowshoe_io"), form='signed')
# Matrix([
# [1, -1, 1, 2, -1],
# [1, 1, -1, 0, 1],
# [1, 1, 1, 0, -1],
# [0, 0, 2, 0, -2],
# [1, 1, -1, 0, 1]])
cumulative_effects(load_digraph("snowshoe_io"), form='binary')
# Matrix([
# [1, 1, 1, 2, 1],
# [1, 1, 1, 2, 1],
# [1, 1, 1, 2, 1],
# [2, 2, 2, 4, 2],
# [1, 1, 1, 2, 1]])
Source code in qmm/extensions/effects.py
net_effects
cached
Calculate net cumulative effects from multiple inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Net effects on state variables and outputs |
Examples:
from qmm import load_digraph, net_effects
net_effects(load_digraph("snowshoe_io"))
# Matrix([
# [1, -1, 1, 2, -1],
# [1, 1, -1, 0, 1],
# [1, 1, 1, 0, -1],
# [0, 0, 2, 0, -2],
# [1, 1, -1, 0, 1]])
Source code in qmm/extensions/effects.py
absolute_effects
cached
Calculate absolute effects from multiple inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Total effects on state variables and outputs |
Examples:
from qmm import load_digraph, absolute_effects
absolute_effects(load_digraph("snowshoe_io"))
# Matrix([
# [1, 1, 1, 2, 1],
# [1, 1, 1, 2, 1],
# [1, 1, 1, 2, 1],
# [2, 2, 2, 4, 2],
# [1, 1, 1, 2, 1]])
Source code in qmm/extensions/effects.py
weighted_effects
cached
Calculate ratio of net to total terms for predicting cumulative effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Ratio of net to total effects |
Examples:
from qmm import load_digraph, weighted_effects
weighted_effects(load_digraph("snowshoe_io"))
# Matrix([
# [1, -1, 1, 1, -1],
# [1, 1, -1, 0, 1],
# [1, 1, 1, 0, -1],
# [0, 0, 1, 0, -1],
# [1, 1, -1, 0, 1]])
Source code in qmm/extensions/effects.py
sign_determinacy_effects
cached
Calculate probability of correct sign prediction for cumulative effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
method
|
Literal['average', '95_bound']
|
Method for computing determinacy ('average', '95_bound') |
'average'
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Sign determinacy probabilities for effects |
Examples:
from qmm import load_digraph, sign_determinacy_effects
sign_determinacy_effects(load_digraph("snowshoe_io"), method='average')
# Matrix([
# [ 1, -1, 1, 1, -1],
# [ 1, 1, -1, 1/2, 1],
# [ 1, 1, 1, 1/2, -1],
# [1/2, 1/2, 1, 1/2, -1],
# [ 1, 1, -1, 1/2, 1]])
sign_determinacy_effects(load_digraph("snowshoe_io"), method='95_bound')
# Matrix([
# [ 1, -1, 1, 1, -1],
# [ 1, 1, -1, 1/2, 1],
# [ 1, 1, 1, 1/2, -1],
# [1/2, 1/2, 1, 1/2, -1],
# [ 1, 1, -1, 1/2, 1]])
Source code in qmm/extensions/effects.py
get_simulations
cached
get_simulations(
G,
n_sim=10000,
dist="uniform",
seed=42,
perturb=None,
observe=None,
presample=None,
return_samples=False,
average_uncertain=False,
)
Calculate average proportion of positive and negative effects from stable numerical simulations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
n_sim
|
int
|
Number of simulations |
10000
|
dist
|
Literal['uniform', 'weak', 'moderate', 'strong', 'uniform_two_oom']
|
Distribution for sampling |
'uniform'
|
seed
|
int
|
Random seed |
42
|
perturb
|
Optional[Tuple[str, int]]
|
Optional perturbations (node, sign) |
None
|
observe
|
Optional[Tuple[Tuple[str, int], ...]]
|
Optional observations (node, sign) |
None
|
presample
|
Optional[Callable[[Tuple[Symbol, ...]], Dict[Symbol, Any]]]
|
Optional callable that receives the tuple of free symbols and returns a mapping of symbol substitutions to apply before sampling. |
None
|
return_samples
|
bool
|
If True, include dict mapping symbol names to arrays of sampled values |
False
|
average_uncertain
|
bool
|
If True, sample edges marked dashes=True in/out each draw (structure averaging) |
False
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict containing effects, valid_sims, all_nodes, tmat, prop_stable, attempts, and optionally samples. |
Examples:
from qmm import get_simulations, load_digraph
result = get_simulations(load_digraph("snowshoe_io"), n_sim=1000, perturb=('Inp1', 1))
result['effects'][0]
# array([ 1.29385476, 2.55918625, 3.12917778, -1.74767706, 2.48217995])
Source code in qmm/extensions/effects.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | |
simulation_effects
simulation_effects(
G,
n_sim=10000,
dist="uniform",
seed=42,
positive_only=False,
presample=None,
average_uncertain=False,
)
Performs numerical simulations of cumulative effects using random interaction strengths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
n_sim
|
int
|
Number of simulations |
10000
|
dist
|
Literal['uniform', 'weak', 'moderate', 'strong', 'uniform_two_oom']
|
Distribution for sampling ("uniform", "weak", "moderate", "strong") |
'uniform'
|
seed
|
int
|
Random seed |
42
|
positive_only
|
bool
|
Return just the proportion of positive responses instead of sign-dominant proportions |
False
|
presample
|
Optional[Callable[[Tuple[Symbol, ...]], Dict[Symbol, Any]]]
|
Optional callable passed through to get_simulations |
None
|
average_uncertain
|
bool
|
Passed through to get_simulations (structure averaging over uncertain links) |
False
|
Returns:
| Type | Description |
|---|---|
Matrix
|
SymPy Matrix containing simulation results |
Examples:
from qmm import load_digraph, simulation_effects
simulation_effects(load_digraph("snowshoe_io"), n_sim=1000)
# Matrix([
# [ 1.0, -1.0, 1.0, 1.0, -1.0],
# [ 1.0, 1.0, -1.0, -0.513, 1.0],
# [ 1.0, 1.0, 1.0, -0.513, -1.0],
# [-0.517, -0.517, 1.0, 0.526, -1.0],
# [ 1.0, 1.0, -1.0, -0.513, 1.0]])
simulation_effects(load_digraph("snowshoe_io"), n_sim=1000, positive_only=True)
# Matrix([
# [ 1.0, 0.0, 1.0, 1.0, 0.0],
# [ 1.0, 1.0, 0.0, 0.487, 1.0],
# [ 1.0, 1.0, 1.0, 0.487, 0.0],
# [0.483, 0.483, 1.0, 0.526, 0.0],
# [ 1.0, 1.0, 0.0, 0.487, 1.0]])
Source code in qmm/extensions/effects.py
simulations_table
simulations_table(
G,
perturb,
observe="",
n_sim=10000,
dist="uniform",
seed=42,
combinations=True,
presample=None,
)
Summarise simulation effects across model variants for each response node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
perturb
|
str
|
Node and sign to perturb (perturbation string) |
required |
observe
|
str
|
Observation string (node:sign, comma-separated allowed) to filter simulations |
''
|
n_sim
|
int
|
Number of simulations |
10000
|
dist
|
Literal['uniform', 'weak', 'moderate', 'strong', 'uniform_two_oom']
|
Distribution for sampling |
'uniform'
|
seed
|
int
|
Random seed |
42
|
combinations
|
bool
|
If True, evaluate every combination of dashed edges |
True
|
presample
|
Optional[Callable[[Tuple[Symbol, ...]], Dict[Symbol, Any]]]
|
Optional callable passed through to get_simulations |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Table of counts for negative, no effect, and positive responses |
Examples:
from qmm import load_digraph, simulations_table
simulations_table(load_digraph("snowshoe_io"), perturb='Inp1:+', n_sim=1000)
# model effect_on negative no_effect positive valid_sims stable_sims attempts
# 0 1 R 0 0 1000 1000 1000 1000
# 1 1 C 513 0 487 1000 1000 1000
# 2 1 P 513 0 487 1000 1000 1000
# 3 1 Out1 474 0 526 1000 1000 1000
# 4 1 Out2 513 0 487 1000 1000 1000
Source code in qmm/extensions/effects.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |
table_of_direct_effects
Create a table of direct effects with state/input columns and state/output rows.
Source code in qmm/extensions/effects.py
table_of_effects
Source code in qmm/extensions/effects.py
qmm.extensions.indicators
Identify informative indicators from press perturbations.
mutual_information
cached
mutual_information(
models,
perturb,
n_sim=10000,
seed=42,
include_null=False,
average_uncertain=False,
)
Calculate mutual information of variables for alternative models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
models
|
Union[DiGraph, List[DiGraph]]
|
One or more NetworkX DiGraphs representing alternative models |
required |
perturb
|
str
|
Node and sign to perturb (can be comma-separated for multiple perturbations) |
required |
n_sim
|
int
|
Number of simulations |
10000
|
seed
|
int
|
Random seed |
42
|
include_null
|
bool
|
If True, include a null model with equal probability (1/3) of positive, negative, or NaN response across simulations |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Mutual information for indicator selection |
References
- Hosack, G.R., Hayes, K.R., Dambacher, J.M. (2008). Assessing Model Structure Uncertainty Through an Analysis of System Feedback and Bayesian Networks. Ecological Applications 18, 1070–1082.
- Melbourne-Thomas, J., Wotherspoon, S., Raymond, B., Constable, A. (2012). Comprehensive evaluation of model uncertainty in qualitative network analyses. Ecological Monographs 82, 505–519.
Examples:
from qmm import mutual_information, load_digraph
G1 = load_digraph("snowshoe_rp")
G2 = G1.copy()
G2.remove_edge('C', 'P')
mutual_information((G1, G2), perturb='R:+', n_sim=1000)
# Node Mutual Information
# 0 R 0.513827
# 1 P 0.456516
# 2 C 0.153711
Source code in qmm/extensions/indicators.py
qmm.extensions.validation
Validate qualitative predictions of system response to press perturbations from observations.
marginal_likelihood
cached
marginal_likelihood(
G,
perturb,
observe,
n_sim=10000,
dist="uniform",
seed=42,
average_uncertain=False,
)
Calculate proportion of simulations matching qualitative observations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
perturb
|
str
|
Perturbation string (node:sign, comma-separated allowed) |
required |
observe
|
str
|
Observation string (node:sign, comma-separated allowed) |
required |
n_sim
|
int
|
Number of simulations |
10000
|
dist
|
Literal['uniform', 'weak', 'moderate', 'strong', 'uniform_two_oom']
|
Distribution for sampling ('uniform', 'weak', 'moderate', 'strong', 'uniform_two_oom') |
'uniform'
|
seed
|
int
|
Random seed |
42
|
average_uncertain
|
bool
|
Passed through to get_simulations (structure averaging over uncertain links) |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Marginal likelihood |
References
- Raymond, B., McInnes, J., Dambacher, J.M., Way, S., Bergstrom, D.M. (2011). Qualitative modelling of invasive species eradication on subantarctic Macquarie Island. Journal of Applied Ecology 48, 181–191.
- Melbourne-Thomas, J., Wotherspoon, S., Raymond, B., Constable, A. (2012). Comprehensive evaluation of model uncertainty in qualitative network analyses. Ecological Monographs 82, 505–519.
Examples:
from qmm import marginal_likelihood, load_digraph
marginal_likelihood(load_digraph("snowshoe_io"), perturb='Inp1:+', observe='Out1:+', n_sim=1000)
# 0.526
Source code in qmm/extensions/validation.py
model_validation
cached
Compare marginal likelihoods from alternative model structures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
perturb
|
str
|
Perturbation string (node:sign, comma-separated allowed) |
required |
observe
|
str
|
Observation string (node:sign, comma-separated allowed) |
required |
n_sim
|
int
|
Number of simulations |
10000
|
dist
|
Literal['uniform', 'weak', 'moderate', 'strong', 'uniform_two_oom']
|
Distribution for sampling |
'uniform'
|
seed
|
int
|
Random seed |
42
|
combinations
|
bool
|
If True, evaluate every combination of dashed edges. If False, only compare the full model vs. all dashed edges removed. |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Marginal likelihood comparison for requested dashed-edge configurations. |
References
- Raymond, B., McInnes, J., Dambacher, J.M., Way, S., Bergstrom, D.M. (2011). Qualitative modelling of invasive species eradication on subantarctic Macquarie Island. Journal of Applied Ecology 48, 181–191.
- Melbourne-Thomas, J., Wotherspoon, S., Raymond, B., Constable, A. (2012). Comprehensive evaluation of model uncertainty in qualitative network analyses. Ecological Monographs 82, 505–519.
Examples:
from qmm import model_validation, load_digraph
import networkx as nx
G = nx.DiGraph(load_digraph("snowshoe_io"))
G.add_edge('R', 'P', sign=1, dashes=True)
model_validation(G, perturb='Inp1:+', observe='Out1:+', n_sim=1000, combinations=False)
# Marginal likelihood R $\rightarrow$ P
# 0 0.815 ✓
# 1 0.526
Source code in qmm/extensions/validation.py
posterior_predictions
cached
posterior_predictions(
G,
perturb,
observe="",
n_sim=10000,
dist="uniform",
seed=42,
positive_only=False,
presample=None,
average_uncertain=False,
)
Calculate model predictions conditioned on observations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
perturb
|
str
|
Perturbation string (node:sign, comma-separated allowed) |
required |
observe
|
str
|
Observation string (node:sign, comma-separated allowed) |
''
|
n_sim
|
int
|
Number of simulations |
10000
|
dist
|
Literal['uniform', 'weak', 'moderate', 'strong', 'uniform_two_oom']
|
Distribution for sampling |
'uniform'
|
seed
|
int
|
Random seed |
42
|
positive_only
|
bool
|
Return just the proportion of positive responses instead of sign-dominant proportions |
False
|
presample
|
Optional[Callable[[Tuple[Symbol, ...]], Dict[Symbol, Any]]]
|
Optional callable passed through to get_simulations |
None
|
average_uncertain
|
bool
|
Passed through to get_simulations (structure averaging over uncertain links) |
False
|
Returns:
| Type | Description |
|---|---|
Matrix
|
sp.Matrix: Predictions conditioned on observations |
References
- Raymond, B., McInnes, J., Dambacher, J.M., Way, S., Bergstrom, D.M. (2011). Qualitative modelling of invasive species eradication on subantarctic Macquarie Island. Journal of Applied Ecology 48, 181–191.
- Melbourne-Thomas, J., Wotherspoon, S., Raymond, B., Constable, A. (2012). Comprehensive evaluation of model uncertainty in qualitative network analyses. Ecological Monographs 82, 505–519.
Examples:
from qmm import posterior_predictions, load_digraph
posterior_predictions(load_digraph("snowshoe_io"), perturb='Inp1:+', observe='Out1:+', n_sim=1000)
# Matrix([
# [ 1.0],
# [-0.52851711026616],
# [-0.52851711026616],
# [ 1.0],
# [-0.52851711026616]])
Source code in qmm/extensions/validation.py
diagnose_observations
Identify possible perturbations from marginal likelihoods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
DiGraph
|
NetworkX DiGraph representing signed digraph model |
required |
observe
|
str
|
Observation string (node:sign, comma-separated allowed) |
required |
perturb_nodes
|
Union[str, List[str]]
|
Node subset to test - comma-separated string, 'state', 'input', or list of nodes |
None
|
n_sim
|
int
|
Number of simulations |
10000
|
dist
|
Literal['uniform', 'weak', 'moderate', 'strong', 'uniform_two_oom']
|
Distribution for sampling |
'uniform'
|
seed
|
int
|
Random seed |
42
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Ranked perturbations matching observations |
Examples:
from qmm import diagnose_observations, load_digraph
diagnose_observations(load_digraph("snowshoe_io"), observe='Out1:+', perturb_nodes='input', n_sim=1000)
# Input Sign Marginal likelihood
# 0 Inp2 - 1.000
# 1 Inp1 + 0.526
# 2 Inp1 - 0.474
# 3 Inp2 + 0.000
Source code in qmm/extensions/validation.py
bayes_factors
Calculate Bayes factors from the ratio of marginal likelihoods of alternative models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G_list
|
Union[List[DiGraph], Tuple[DiGraph, ...]]
|
List or tuple of NetworkX DiGraphs representing alternative models |
required |
perturb
|
str
|
Perturbation string (node:sign, comma-separated allowed) |
required |
observe
|
str
|
Observation string (node:sign, comma-separated allowed) |
required |
n_sim
|
int
|
Number of simulations |
10000
|
dist
|
Literal['uniform', 'weak', 'moderate', 'strong', 'uniform_two_oom']
|
Distribution for sampling ('uniform', 'weak', 'moderate', 'strong', 'uniform_two_oom') |
'uniform'
|
seed
|
int
|
Random seed |
42
|
names
|
Optional[List[str]]
|
Optional list of model names |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: DataFrame containing Bayes factors |
References
- Raymond, B., McInnes, J., Dambacher, J.M., Way, S., Bergstrom, D.M. (2011). Qualitative modelling of invasive species eradication on subantarctic Macquarie Island. Journal of Applied Ecology 48, 181–191.
- Melbourne-Thomas, J., Wotherspoon, S., Raymond, B., Constable, A. (2012). Comprehensive evaluation of model uncertainty in qualitative network analyses. Ecological Monographs 82, 505–519.
Examples:
from qmm import bayes_factors, load_digraph
G1 = load_digraph("snowshoe_io")
G2 = G1.copy()
G2.remove_edge('C', 'P')
bayes_factors([G1, G2], perturb='Inp1:+', observe='Out1:+', n_sim=1000)
# Model comparison Likelihood 1 Likelihood 2 Bayes factor
# 0 Model A/Model B 0.526 0.474 1.109705