Repository: sagemath/sage

Issue:
Add ability to generate graphs based on correlations of sequences
<div id="comment:0"></div>

Given a list of labeled real-valued vectors and a significance level `alpha`, generate a graph with vertices corresponding to the list's labels where two vertices are connected by an edge if the corresponding vectors have a correlation coefficient that is positive or negative at the specified significance level. An object creating the graph should support the `plot` method, which will plot the generated graph using a circular layout.

CC:  @dcoudert

Component: **graph theory**

Keywords: **sagedays@icerm**

Author: **Robert Kingan**

_Issue created by migration from https://trac.sagemath.org/ticket/25933_




Discussion:
<div id="comment:1" align="right">comment:1</div>

Added branch name
Branch: **[correlation-graph](https://github.com/sagemath/sagetrac-mirror/tree/correlation-graph)**

Resolving patch:
diff --git a/src/sage/graphs/generators/basic.py b/src/sage/graphs/generators/basic.py
--- a/src/sage/graphs/generators/basic.py
+++ b/src/sage/graphs/generators/basic.py
@@ -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():
@@ -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"""
diff --git a/src/sage/graphs/graph_generators.py b/src/sage/graphs/graph_generators.py
--- a/src/sage/graphs/graph_generators.py
+++ b/src/sage/graphs/graph_generators.py
@@ -68,6 +68,7 @@ def wrap_name(x):
      "CompleteBipartiteGraph",
      "CompleteGraph",
      "CompleteMultipartiteGraph",
+     "CorrelationGraph",
      "DiamondGraph",
      "GemGraph",
      "DartGraph",
@@ -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)


Test patch: