Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to generate graphs based on correlations of sequences #25933 #35009

Merged
merged 19 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/sage/graphs/generators/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# import from Sage library
from sage.graphs.graph import Graph
from math import sin, cos, pi
from numpy import corrcoef
from sage.matrix.constructor import Matrix


def BullGraph():
Expand Down Expand Up @@ -393,6 +395,43 @@ def CompleteGraph(n):
G.add_edges(((i, j) for i in range(n) for j in range(i + 1, n)))
return G

def CorrelationGraph(seqs, alpha, include_anticorrelation):
"""
Constructs and returns a correlation graph with a node corresponding to each sequence in `seqs`.

Edges are added between nodes where the corresponding sequences have a correlation coeffecient greater than alpha.

If include_anticorrelation is true, then edges are also added between nodes with correlation coeffecient less than -alpha.

EXAMPLES:

sage: from sage.graphs.generators.basic import CorrelationGraph
sage: data=[[1,2,3],[4,5,6],[7,8,9999]]
sage: CG1 = CorrelationGraph(data, 0.9, False)
sage: CG2 = CorrelationGraph(data, 0.9, True)
sage: CG3 = CorrelationGraph(data, 0.1, True)
sage: CG1.edges(sort=False)
[(0, 0, None), (0, 1, None), (1, 1, None), (2, 2, None)]
sage: CG2.edges(sort=False)
[(0, 0, None), (0, 1, None), (1, 1, None), (2, 2, None)]
sage: CG3.edges(sort=False)
[(0, 0, None), (0, 1, None), (0, 2, None), (1, 1, None), (1, 2, None), (2, 2, None)]

"""

# compute pairwise correlation coeffecients
corrs = corrcoef(seqs)

# compare against alpha to get adjacency matrix
if include_anticorrelation:
boolean_adjacency_matrix = abs(corrs)>=alpha
else:
boolean_adjacency_matrix = corrs>=alpha

adjacency_matrix = Matrix(boolean_adjacency_matrix.astype(int))

# call graph constructor
return Graph(adjacency_matrix, format="adjacency_matrix", name="Correlation Graph")

def CompleteBipartiteGraph(p, q, set_position=True):
r"""
Expand Down
2 changes: 2 additions & 0 deletions src/sage/graphs/graph_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def wrap_name(x):
"CompleteBipartiteGraph",
"CompleteGraph",
"CompleteMultipartiteGraph",
"CorrelationGraph",
"DiamondGraph",
"GemGraph",
"DartGraph",
Expand Down Expand Up @@ -2354,6 +2355,7 @@ def quadrangulations(self, order, minimum_degree=None, minimum_connectivity=None
CompleteGraph = staticmethod(basic.CompleteGraph)
CompleteBipartiteGraph = staticmethod(basic.CompleteBipartiteGraph)
CompleteMultipartiteGraph = staticmethod(basic.CompleteMultipartiteGraph)
CorrelationGraph = staticmethod(basic.CorrelationGraph)
DiamondGraph = staticmethod(basic.DiamondGraph)
GemGraph = staticmethod(basic.GemGraph)
DartGraph = staticmethod(basic.DartGraph)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/real_double.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ cdef class RealDoubleElement(FieldElement):
def round(self):
"""
Round ``self`` to the nearest integer.

This uses the convention of rounding half to even
(i.e., if the fractional part of ``self`` is `0.5`, then it
is rounded to the nearest even integer).
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/real_mpfr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2992,7 +2992,7 @@ cdef class RealNumber(sage.structure.element.RingElement):
"""
Round ``self`` to the nearest representable integer, rounding halfway
cases away from zero.

.. NOTE::

The rounding mode of the parent field does not affect the result.
Expand Down