本文整理汇总了Java中org.apache.commons.math3.stat.correlation.SpearmansCorrelation类的典型用法代码示例。如果您正苦于以下问题:Java SpearmansCorrelation类的具体用法?Java SpearmansCorrelation怎么用?Java SpearmansCorrelation使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SpearmansCorrelation类属于org.apache.commons.math3.stat.correlation包,在下文中一共展示了SpearmansCorrelation类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCorrelation
import org.apache.commons.math3.stat.correlation.SpearmansCorrelation; //导入依赖的package包/类
/**
* Creates two random double arrays, calculates covariance and different correlation coefficients from it
* @throws Exception
*/
@Test
public void testCorrelation() throws Exception {
double[] x = new double[10];
double[] y = new double[10];
for (int i = 0; i < x.length; i++) {
x[i] = Math.random()*100;
y[i] = Math.random()*100;
}
System.out.println("x: " + Arrays.toString(x));
System.out.println("y: " + Arrays.toString(y));
System.out.println("Covariance: " + new Covariance().covariance(x,y));
System.out.println("Pearson's Correlation: " + new PearsonsCorrelation().correlation(x,y));
System.out.println("Spearman's Correlation: " + new SpearmansCorrelation().correlation(x,y));
System.out.println("Kendall's Correlation: " + new KendallsCorrelation().correlation(x,y));
}
示例2: Correlation
import org.apache.commons.math3.stat.correlation.SpearmansCorrelation; //导入依赖的package包/类
public Correlation(final double[] x, final double[] y) {
arrays = new double[2][x.length];
for (int i = 0; i < x.length; ++i) {
arrays[0][i] = x[i];
arrays[1][i] = y[i];
}
final DoubleLinkedList x2 = new DoubleLinkedList();
final DoubleLinkedList y2 = new DoubleLinkedList();
for (int i = 0; i < x.length; ++i) {
if (!Double.isNaN(x[i]) && x[i] != Double.NEGATIVE_INFINITY) {
x2.add(x[i]);
y2.add(y[i]);
}
}
final double[] x2arr = x2.toArray();
final double[] y2arr = y2.toArray();
covariance = new Covariance().covariance(x2arr, y2arr);
pearson = new PearsonsCorrelation().correlation(x2arr, y2arr);
spearman = new SpearmansCorrelation().correlation(x2arr, y2arr);
mean = (Math.abs(pearson) + Math.abs(spearman)) / 2;
}
示例3: addSummary
import org.apache.commons.math3.stat.correlation.SpearmansCorrelation; //导入依赖的package包/类
private void addSummary(String title, double[] sum1, double[] sum2)
{
BlockRealMatrix rm = new BlockRealMatrix(sum1.length, 2);
rm.setColumn(0, sum1);
rm.setColumn(1, sum2);
SpearmansCorrelation sr = new SpearmansCorrelation(rm);
PearsonsCorrelation p1 = sr.getRankCorrelation();
PearsonsCorrelation p2 = new PearsonsCorrelation(rm);
StringBuilder sb = new StringBuilder(title);
sb.append(sum1.length).append('\t');
sb.append(Utils.rounded(p1.getCorrelationMatrix().getEntry(0, 1))).append('\t');
sb.append(Utils.rounded(p1.getCorrelationPValues().getEntry(0, 1))).append('\t');
sb.append(Utils.rounded(p2.getCorrelationMatrix().getEntry(0, 1))).append('\t');
sb.append(Utils.rounded(p2.getCorrelationPValues().getEntry(0, 1)));
twSummary.append(sb.toString());
}
示例4: calculateCorrelations
import org.apache.commons.math3.stat.correlation.SpearmansCorrelation; //导入依赖的package包/类
private void calculateCorrelations() {
double[][] denaned = removeNaNs(x, y);
if (denaned[0].length > 0) {
pearsonsCorrelation = new PearsonsCorrelation().correlation(denaned[0], denaned[1]);
spearmansCorrelation = new SpearmansCorrelation().correlation(denaned[0], denaned[1]);
}
correlationsCalculated = true;
}
示例5: spearmanr
import org.apache.commons.math3.stat.correlation.SpearmansCorrelation; //导入依赖的package包/类
/**
* Computes Spearman's rank correlation for pairs of arrays or columns of a matrix.
*
* @param x X data
* @param y Y data
* @return Spearman's rank correlation
*/
public static Array spearmanr(Array x, Array y) {
int m = x.getShape()[0];
int n = 1;
if (x.getRank() == 2)
n = x.getShape()[1];
double[][] aa = new double[m][n * 2];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n * 2; j++) {
if (j < n) {
aa[i][j] = x.getDouble(i * n + j);
} else {
aa[i][j] = y.getDouble(i * n + j - n);
}
}
}
RealMatrix matrix = new Array2DRowRealMatrix(aa, false);
SpearmansCorrelation cov = new SpearmansCorrelation(matrix);
RealMatrix mcov = cov.getCorrelationMatrix();
m = mcov.getColumnDimension();
n = mcov.getRowDimension();
Array r = Array.factory(DataType.DOUBLE, new int[]{m, n});
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
r.setDouble(i * n + j, mcov.getEntry(i, j));
}
}
return r;
}
示例6: getSpearman
import org.apache.commons.math3.stat.correlation.SpearmansCorrelation; //导入依赖的package包/类
public static double getSpearman(List<Double> list1, List<Double> list2)
{
SpearmansCorrelation correlation = new SpearmansCorrelation();
double c = correlation.correlation(getArray(list1),getArray(list2));
return c;
}
示例7: spearmanCorrelation
import org.apache.commons.math3.stat.correlation.SpearmansCorrelation; //导入依赖的package包/类
/**
* Calculates the Spearman correlation matrix for the centrality maps in the given list.
* @param graph The graph the centrality measures are based on.
* @param maps The list of centrality maps.
* @return The correlation matrix as a RealMatrix.
*/
public static RealMatrix spearmanCorrelation(CustomGraph graph, List<CentralityMap> maps) {
double[][] mapsValues = getCentralityValuesMatrix(graph, maps);
SpearmansCorrelation correlation = new SpearmansCorrelation();
RealMatrix result = correlation.computeCorrelationMatrix(mapsValues);
return result;
}
示例8: createCorrelationOfSamples
import org.apache.commons.math3.stat.correlation.SpearmansCorrelation; //导入依赖的package包/类
/**
* creates the correlation of Samples useing Pearson's,Kendall's and Spearman's correlation coefficient
* @param samples
* @param rank
* @param type
*/
public static void createCorrelationOfSamples(List<Sample> samples, String rank, String type) {
//We need the unified map to make sure the counts are properly aligned
LinkedList<TaxonNode> taxonNodeList = getUnifiedTaxonList(samples, rank);
//The matrix data needs to be double, since PearsonsCorrelation only takes double arrays
double[][] taxaCounts = new double[samples.size()][taxonNodeList.size()];
for (int sampleIndex = 0; sampleIndex < samples.size(); sampleIndex++) {
double[] currentSampleCounts = new double[taxonNodeList.size()];
for (int taxonIndex = 0; taxonIndex < currentSampleCounts.length; taxonIndex++) {
currentSampleCounts[taxonIndex] = samples.get(sampleIndex).getTaxonCountRecursive(taxonNodeList.get(taxonIndex));
}
taxaCounts[sampleIndex] = currentSampleCounts;
}
switch (type) {
case "pearson":
pearsonsCorrelation = new PearsonsCorrelation(taxaCounts);
correlationMatrix = pearsonsCorrelation.getCorrelationMatrix();
pValueMatrix = pearsonsCorrelation.getCorrelationPValues();
break;
case "spearman":
spearmansCorrelation = new SpearmansCorrelation(new BlockRealMatrix(taxaCounts));
correlationMatrix = spearmansCorrelation.getCorrelationMatrix();
pValueMatrix = spearmansCorrelation.getRankCorrelation().getCorrelationPValues();
break;
case "kendall":
kendallsCorrelation = new KendallsCorrelation(taxaCounts);
correlationMatrix = kendallsCorrelation.getCorrelationMatrix();
pValueMatrix = new PearsonsCorrelation(taxaCounts).getCorrelationPValues(); //No p-values available for kendall's correlation!
break;
}
//Correct the NaNs to 0.0s
for (int i = 0; i < correlationMatrix.getRowDimension(); i++) {
for (int j = 0; j < correlationMatrix.getColumnDimension(); j++) {
if (Double.isNaN(correlationMatrix.getEntry(i, j)))
correlationMatrix.setEntry(i, j, 0.0);
}
}
for (int i = 0; i < pValueMatrix.getRowDimension(); i++) {
for (int j = 0; j < pValueMatrix.getColumnDimension(); j++) {
if (Double.isNaN(pValueMatrix.getEntry(i, j)))
pValueMatrix.setEntry(i, j, 1.0);
}
}
}
示例9: CalcCorrV2
import org.apache.commons.math3.stat.correlation.SpearmansCorrelation; //导入依赖的package包/类
public double CalcCorrV2(XYPointCollection CollectionA, XYPointCollection CollectionB, int NoPointPerInterval) {
SpearmansCorrelation pearsonsCorrelation = new SpearmansCorrelation();
int num = Math.max(CollectionA.PointCount(), CollectionB.PointCount()) / 2;
float timeinterval = 2f / (float) NoPointPerInterval;
if (num < 6) {
return 0f;
}
double[] arrayA = new double[num];
double[] arrayB = new double[num];
float start = Math.max(CollectionA.Data.get(0).getX(), CollectionB.Data.get(0).getX());
int i = 0;
float low = start;
float up = start + timeinterval;
for (int j = 0; j < CollectionA.PointCount(); j++) {
while (CollectionA.Data.get(j).getX() > up) {
i++;
low = up;
up = low + timeinterval;
}
if (i >= num) {
break;
}
if (CollectionA.Data.get(j).getX() >= low && CollectionA.Data.get(j).getX() < up) {
if (CollectionA.Data.get(j).getY() > arrayA[i]) {
arrayA[i] = CollectionA.Data.get(j).getY();
}
}
}
i = 0;
low = start;
up = start + timeinterval;
for (int j = 0; j < CollectionB.PointCount(); j++) {
while (CollectionB.Data.get(j).getX() > up) {
i++;
low = up;
up = low + timeinterval;
}
if (i >= num) {
break;
}
if (CollectionB.Data.get(j).getX() >= low && CollectionB.Data.get(j).getX() < up) {
if (CollectionB.Data.get(j).getY() > arrayB[i]) {
arrayB[i] = CollectionB.Data.get(j).getY();
}
}
}
if (arrayA[0] == 0f) {
arrayA[0] = arrayA[1];
}
if (arrayB[0] == 0f) {
arrayB[0] = arrayB[1];
}
for (int idx = 1; idx < num - 1; idx++) {
if (arrayA[idx] == 0f) {
arrayA[idx] = (arrayA[idx - 1] + arrayA[idx + 1]) / 2;
}
if (arrayB[idx] == 0f) {
arrayB[idx] = (arrayB[idx - 1] + arrayB[idx + 1]) / 2;
}
}
if (arrayA[num - 1] == 0f) {
arrayA[num - 1] = arrayA[num - 2];
}
if (arrayB[num - 1] == 0f) {
arrayB[num - 1] = arrayB[num - 2];
}
double R2 = pearsonsCorrelation.correlation(arrayA, arrayB);
return R2;
}
示例10: spearmanCorrelation
import org.apache.commons.math3.stat.correlation.SpearmansCorrelation; //导入依赖的package包/类
/**
* Computes Spearman correlation by comparing order of two corpora vocabularies
*
* @param goldCorpus gold corpus
* @param otherCorpus other corpus
* @param topN how many entries from the gold corpus should be taken
* @throws IOException I/O exception
*/
public static void spearmanCorrelation(File goldCorpus, File otherCorpus,
int topN)
throws IOException
{
LinkedHashMap<String, Integer> gold = loadCorpusToRankedVocabulary(
new FileInputStream(goldCorpus));
LinkedHashMap<String, Integer> other = loadCorpusToRankedVocabulary(
new FileInputStream(otherCorpus));
double[][] matrix = new double[topN][];
if (gold.size() < topN) {
throw new IllegalArgumentException(
"topN (" + topN + ") cannot be greater than vocabulary size (" + gold.size()
+ ")");
}
Iterator<Map.Entry<String, Integer>> iterator = gold.entrySet().iterator();
int counter = 0;
while (counter < topN) {
Map.Entry<String, Integer> next = iterator.next();
String goldWord = next.getKey();
Integer goldValue = next.getValue();
// look-up position in other corpus
Integer otherValue = other.get(goldWord);
if (otherValue == null) {
// System.err.println("Word " + goldWord + " not found in the other corpus");
otherValue = Integer.MAX_VALUE;
}
matrix[counter] = new double[2];
matrix[counter][0] = goldValue;
matrix[counter][1] = otherValue;
counter++;
}
RealMatrix realMatrix = new Array2DRowRealMatrix(matrix);
SpearmansCorrelation spearmansCorrelation = new SpearmansCorrelation(realMatrix);
double pValue = spearmansCorrelation.getRankCorrelation().getCorrelationPValues()
.getEntry(0, 1);
double correlation = spearmansCorrelation.getRankCorrelation().getCorrelationMatrix()
.getEntry(0, 1);
System.out.println("Gold: " + goldCorpus.getName());
System.out.println("Other: " + otherCorpus.getName());
System.out.printf(Locale.ENGLISH, "Top N:\n%d\nCorrelation\n%.3f\np-value\n%.3f\n", topN,
correlation, pValue);
}
示例11: calc
import org.apache.commons.math3.stat.correlation.SpearmansCorrelation; //导入依赖的package包/类
/**
* If channel1 == channel2 (both res-tables or both gt-tables), avoid self-counting, i.e., distance to nearest neighbor must not be 0!
* On the other hand if comparing res-table with gt-table then self-counting is allowed even if the data in the tables are the same.
* */
private double[] calc(final double[][] mainChannelCoords, final KDTree<double[]> mainChannelTree, final KDTree<double[]> otherChannelTree, final double [][] neighborsInDistance, final double [] nearestNeighborDistances) {
final int lastRadiusIndex = squaredRadiusDomain.length - 1;
final double maxSquaredRadius = squaredRadiusDomain[lastRadiusIndex];
final double[] cbcResults = new double[mainChannelCoords.length];
final AtomicInteger count = new AtomicInteger(0);
IJ.showProgress(0);
Loop.withIndex(0, mainChannelCoords.length, new Loop.BodyWithIndex() {
@Override
public void run(int i) {
try {
double[] counts = calcNeighborCount(mainChannelCoords[i], mainChannelTree, squaredRadiusDomain, (firstChannelCoords == secondChannelCoords));
for(int j = 0; j < counts.length; j++) {
counts[j] = counts[j] / counts[lastRadiusIndex] * maxSquaredRadius / squaredRadiusDomain[j];
}
double[] counts2 = calcNeighborCount(mainChannelCoords[i], otherChannelTree, squaredRadiusDomain, (firstChannelCoords == secondChannelCoords));
nearestNeighborDistances[i] = getDistanceToNearestNeighbor(mainChannelCoords[i], otherChannelTree, (firstChannelCoords == secondChannelCoords));
double maxCount = counts2[lastRadiusIndex];
for(int j = 0; j < counts2.length; j++) {
neighborsInDistance[j][i] = counts2[j];
if(maxCount == 0) {
counts2[j] = 0;
} else {
counts2[j] = counts2[j] / maxCount * maxSquaredRadius / squaredRadiusDomain[j];
}
}
SpearmansCorrelation correlator = new SpearmansCorrelation();
double correlation;
try {
correlation = correlator.correlation(counts, counts2);
} catch (NotANumberException e) {
correlation = Double.NaN;
}
double[] nearestNeighbor = otherChannelTree.nearest(mainChannelCoords[i]);
double nnDistance = MathArrays.distance(nearestNeighbor, mainChannelCoords[i]);
double result = correlation * MathProxy.exp(-nnDistance / MathProxy.sqrt(maxSquaredRadius));
cbcResults[i] = result;
if(i % 1024 == 0) {
IJ.showProgress((double)count.addAndGet(1024) / (double)(mainChannelCoords.length));
}
} catch(KeySizeException ex) {
throw new RuntimeException(ex);
}
}
});
IJ.showProgress(1);
return cbcResults;
}
示例12: correlate
import org.apache.commons.math3.stat.correlation.SpearmansCorrelation; //导入依赖的package包/类
@Override
public double correlate(double[] val1, double[] val2) {
SpearmansCorrelation correlation = new SpearmansCorrelation();
return correlation.correlation(val1, val2);
}