javaexceptionnanjungapache-commons-math

SpearmansCorrelation throws NotANumberException when calculating correlation of eigenvector centrality


I am trying to calculate Spearman's correlation for shell indices and eigenvector centralities in Java. I need to export eigenvector values and Spearman's correlation to a .csv file. Here are the given Java files.

CentralitiesAnalyzer.java:

public abstract class CentralitiesAnalyzer implements Reporter {

    protected Vertex[] vertices;
    protected double[] xs;
    protected double[] ys;

    protected double correlation;
    protected List<Triple<String, Double, Double>> results;

    protected CentralitiesExporter exporter = new CentralitiesExporter();

    public CentralitiesAnalyzer() {
        this.results = new ArrayList<>();
    }

    protected void insertValues() {
        double min = Arrays.stream(ys).min().getAsDouble();
        double max = Arrays.stream(ys).max().getAsDouble();

        for (int i = 0; i < vertices.length; i++) {
            ys[i] = (ys[i] - min) / (max - min);

            String id = vertices[i].getId();
            double x = xs[i];
            double y = ys[i];
            results.add(new Triple<>(id, x, y));
        }
        
        SpearmansCorrelation sc = new SpearmansCorrelation();
        correlation = sc.correlation(xs, ys);
    }

    public abstract void analyze(Graph<Vertex, Edge> graph, Map<Vertex, Integer> shellIndices);

    @Override
    public void report(String folder) {
    }

}

EigenvectorAnalyzer.java:

public class EigenvectorAnalyzer extends CentralitiesAnalyzer {

    private void init(EigenvectorCentrality<Vertex, Edge> centrality, Map<Vertex, Integer> shellIndices) {
        this.vertices = shellIndices.keySet().toArray(Vertex[]::new);
        this.xs = new double[vertices.length];
        this.ys = new double[vertices.length];

        for (int i = 0; i < vertices.length; i++) {
            xs[i] = shellIndices.get(vertices[i]);
            ys[i] = centrality.getVertexScore(vertices[i]);
        }
    }

    public void analyze(Graph<Vertex, Edge> graph, Map<Vertex, Integer> shellIndices) {
        EigenvectorCentrality<Vertex, Edge> centrality = new EigenvectorCentrality<Vertex, Edge>(graph);
        init(centrality, shellIndices);
        insertValues();
    }

    @Override
    public void report(String folder) {
        String file = "eigenvector-centralities.csv";
        String header = "Node ID;Shell index;Eigenvector centrality";
        exporter.setData(results, correlation);
        exporter.exportToCSV(folder, file, header);
    }

}

When I was running the project, SpearmansCorrelation threw NotANumberException.

Excerpt from the console:

Shell index: 3.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 7.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 2.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 1.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 3.00, eigenvector centrality: 0.01
Shell index: 7.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 3.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 7.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 3.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 7.00, eigenvector centrality: 0.01
Shell index: 7.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 3.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 3.00, eigenvector centrality: 0.01
Shell index: 2.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 2.00, eigenvector centrality: 0.01
Shell index: 2.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 1.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 2.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 1.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 3.00, eigenvector centrality: 0.01
Shell index: 3.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 1.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 2.00, eigenvector centrality: 0.01
Shell index: 2.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 1.00, eigenvector centrality: 0.01
Shell index: 1.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 2.00, eigenvector centrality: 0.01
Shell index: 3.00, eigenvector centrality: 0.01
Shell index: 1.00, eigenvector centrality: 0.01
Shell index: 3.00, eigenvector centrality: 0.01
Shell index: 1.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 2.00, eigenvector centrality: 0.01
Shell index: 2.00, eigenvector centrality: 0.01
Shell index: 2.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 6.00, eigenvector centrality: 0.01
Shell index: 5.00, eigenvector centrality: 0.01
Shell index: 2.00, eigenvector centrality: 0.01
Shell index: 4.00, eigenvector centrality: 0.01
Shell index: 1.00, eigenvector centrality: 0.01
Shell index: 2.00, eigenvector centrality: 0.01
Shell index: 1.00, eigenvector centrality: 0.01
Exception in thread "main" org.apache.commons.math3.exception.NotANumberException: NaN is not allowed
    at org.apache.commons.math3.stat.ranking.NaturalRanking.rank(NaturalRanking.java:217)
    at org.apache.commons.math3.stat.correlation.SpearmansCorrelation.correlation(SpearmansCorrelation.java:185)
    at rs.ac.uns.pmf.analysis.centralities.CentralitiesAnalyzer.insertValues(CentralitiesAnalyzer.java:46)
    at rs.ac.uns.pmf.analysis.centralities.EigenvectorAnalyzer.analyze(EigenvectorAnalyzer.java:27)
    at rs.ac.uns.pmf.main.Reporting.analyzeCentralities(Reporting.java:48)
    at rs.ac.uns.pmf.main.Reporting.saveReport(Reporting.java:91)
    at rs.ac.uns.pmf.main.Main.main(Main.java:7)

How do I bypass it so that I get 0.0 instead?


Solution

  • You can simply add a condition to check if max is equal to min: if true, set correlation to 0.0, since there's no meaningful correlation with x in that case, like:

    protected void insertValues() {
        double min = Arrays.stream(ys).min().getAsDouble();
        double max = Arrays.stream(ys).max().getAsDouble();
    
        if (max == min) {
            correlation = 0.0;
        } else {
            for (int i = 0; i < vertices.length; i++) {
                ys[i] = (ys[i] - min) / (max - min);
    
                String id = vertices[i].getId();
                double x = xs[i];
                double y = ys[i];
                results.add(new Triple<>(id, x, y));
            }
    
            SpearmansCorrelation sc = new SpearmansCorrelation();
            correlation = sc.correlation(xs, ys);
        }
    }