当前位置: 首页>>代码示例>>Java>>正文


Java Covariance类代码示例

本文整理汇总了Java中org.apache.commons.math3.stat.correlation.Covariance的典型用法代码示例。如果您正苦于以下问题:Java Covariance类的具体用法?Java Covariance怎么用?Java Covariance使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Covariance类属于org.apache.commons.math3.stat.correlation包,在下文中一共展示了Covariance类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testCorrelation

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的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));
}
 
开发者ID:jmueller95,项目名称:CORNETTO,代码行数:20,代码来源:ApacheMathTest.java

示例2: MahalanobisDistance

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的package包/类
/**
 * Create a new instance from an array of m-dimensional samples, e.g.
 * samples = {{x1,y1}, {x2,y2},...,{xn,yn}} for a vector of n two-dimensional
 * samples.
 * @param samples A vector of length n with m-dimensional samples, i.e.
 *      samples[k][i] represents the i-th component of the k-th sample.
 * @param diagonalIncrement Quantity added to the diagonal values of the
 *      covariance matrix to avoid singularity. 
 */
public MahalanobisDistance(double[][] samples, double diagonalIncrement) {
	n = samples.length;
	m = samples[0].length;
	if (n < 1 || m < 1) {
		throw new IllegalArgumentException("dimension less than 1");
	}
	if (diagonalIncrement < 0) {
		throw new IllegalArgumentException("diagonal increment must be positive");
	}

	mean = makeMeanVector(samples);
	
	Covariance cov = new Covariance(samples, BIAS_CORRECTION);	
	RealMatrix S = cov.getCovarianceMatrix();
	
	// condition the covariance matrix to avoid singularity:
	S = conditionCovarianceMatrix(S);
	
	Cov = S.getData();
	// get the inverse covariance matrix
	iCov = MatrixUtils.inverse(S).getData();	// not always symmetric?
}
 
开发者ID:imagingbook,项目名称:imagingbook-common,代码行数:32,代码来源:MahalanobisDistance.java

示例3: Correlation

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的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;
}
 
开发者ID:ProfilingIO,项目名称:insight-ml,代码行数:22,代码来源:Correlation.java

示例4: testSampling

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的package包/类
/**
 * Test the accuracy of sampling from the distribution.
 */
@Test
public void testSampling() {
    final double[] mu = { -1.5, 2 };
    final double[][] sigma = { { 2, -1.1 },
                               { -1.1, 2 } };
    final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma);
    d.reseedRandomGenerator(50);

    final int n = 500000;

    final double[][] samples = d.sample(n);
    final int dim = d.getDimension();
    final double[] sampleMeans = new double[dim];

    for (int i = 0; i < samples.length; i++) {
        for (int j = 0; j < dim; j++) {
            sampleMeans[j] += samples[i][j];
        }
    }

    final double sampledValueTolerance = 1e-2;
    for (int j = 0; j < dim; j++) {
        sampleMeans[j] /= samples.length;
        Assert.assertEquals(mu[j], sampleMeans[j], sampledValueTolerance);
    }

    final double[][] sampleSigma = new Covariance(samples).getCovarianceMatrix().getData();
    for (int i = 0; i < dim; i++) {
        for (int j = 0; j < dim; j++) {
            Assert.assertEquals(sigma[i][j], sampleSigma[i][j], sampledValueTolerance);
        }
    }
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:37,代码来源:MultivariateNormalDistributionTest.java

示例5: getExpectedValue

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的package包/类
@Override
public Object getExpectedValue(int start, int length)
{
    if (length <= 0) {
        return null;
    }
    else if (length == 1) {
        return 0.;
    }
    Covariance covariance = new Covariance();
    return covariance.covariance(constructDoublePrimitiveArray(start + 5, length), constructDoublePrimitiveArray(start, length), false);
}
 
开发者ID:y-lan,项目名称:presto,代码行数:13,代码来源:TestCovariancePopAggregation.java

示例6: getExpectedValue

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的package包/类
@Override
public Object getExpectedValue(int start, int length)
{
    if (length <= 1) {
        return null;
    }
    return new Covariance().covariance(constructDoublePrimitiveArray(start + 5, length), constructDoublePrimitiveArray(start, length), true);
}
 
开发者ID:y-lan,项目名称:presto,代码行数:9,代码来源:TestCovarianceSampAggregation.java

示例7: transform

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的package包/类
/**
 * Transform a given sequence of values using the algorithm PLAA.
 *
 * @param values the sequence of values
 * @return the result of the transformation
 */
public MeanSlopePair[] transform(double[] values) {
    int len = values.length;
    if (len < segments) {
        throw new ArrayLengthIsTooSmallException(len, segments, true);
    }

    int modulo = len % segments;
    if (modulo != 0) {
        throw new ArrayLengthIsNotDivisibleException(len, segments);
    }

    MeanSlopePair[] reducedValues = new MeanSlopePair[segments];
    int segmentSize = len / segments;

    double[] x = new double[segmentSize];
    for (int i = 0; i < segmentSize; i++) {
        x[i] = i + 1;
    }

    double variance = new Variance(true).evaluate(x);
    for (int i = 0; i < segments; i++) {
        double[] y = new double[segmentSize];
        System.arraycopy(values, i * segmentSize, y, 0, segmentSize);

        double covariance = new Covariance().covariance(x, y, true);
        double mean = new Mean().evaluate(y);

        reducedValues[i] = new MeanSlopePair(mean, covariance / variance);
    }

    return reducedValues;
}
 
开发者ID:octavian-h,项目名称:time-series-math,代码行数:39,代码来源:PiecewiseLinearAggregateApproximation.java

示例8: testSampling

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的package包/类
/**
 * Test the accuracy of sampling from the distribution.
 */
@Test
public void testSampling() {
    final double[] mu = { -1.5, 2 };
    final double[][] sigma = { { 2, -1.1 },
                               { -1.1, 2 } };
    final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma);
    d.reseedRandomGenerator(50);

    final int n = 500000;

    final double[][] samples = d.sample(n);
    final int dim = d.getDimensions();
    final double[] sampleMeans = new double[dim];

    for (int i = 0; i < samples.length; i++) {
        for (int j = 0; j < dim; j++) {
            sampleMeans[j] += samples[i][j];
        }
    }

    final double sampledValueTolerance = 1e-2;
    for (int j = 0; j < dim; j++) {
        sampleMeans[j] /= samples.length;
        Assert.assertEquals(mu[j], sampleMeans[j], sampledValueTolerance);
    }

    final double[][] sampleSigma = new Covariance(samples).getCovarianceMatrix().getData();
    for (int i = 0; i < dim; i++) {
        for (int j = 0; j < dim; j++) {
            Assert.assertEquals(sigma[i][j], sampleSigma[i][j], sampledValueTolerance);
        }
    }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:37,代码来源:MultivariateNormalDistributionTest.java

示例9: reduceTraining

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的package包/类
public double[][] reduceTraining(double[][] data, String[] classLabels) {
   assert(data.length > 0);
   assert(data[0].length == super.inputSize);

   double[][] reducedData = changingValueReducer.reduceTraining(data, classLabels);

   RealMatrix dataMatrix = new Array2DRowRealMatrix(reducedData);

   // NOTE(eriq): The math says that we should first center our data.
   //  But, centering the data (subtracting the mean) gives up much worse results...
   // RealMatrix centeredData = dataMatrix.subtract(getMeanMatrix(dataMatrix));
   RealMatrix centeredData = dataMatrix;

   Covariance covariance = new Covariance(reducedData);
   EigenDecomposition eigenDecomp = new EigenDecomposition(covariance.getCovarianceMatrix());

   // Get the eigen vectors.
   // Transpose them (each eigen vector is now in a row).
   // Take the top |reducedFeatureVectorLength| vectors.
   // TODO(eriq): Verify |reducedFeatureVectorLength| > |num reduced features|.
   // NOTE(eriq): Are the eigen vectors along the vertical or horizontal.
   // transformationMatrix = eigenDecomp.getV().transpose();
   transformationMatrix = eigenDecomp.getV();

   // Get only the top |super.outputSize| eigen vectors.
   transformationMatrix = transformationMatrix.getSubMatrix(0, super.outputSize - 1, 0, reducedData[0].length - 1);

   RealMatrix finalData = transformationMatrix.multiply(centeredData.transpose()).transpose();

   return finalData.getData();
}
 
开发者ID:eriq-augustine,项目名称:jocr,代码行数:32,代码来源:KLTReducer.java

示例10: calculateCov

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的package包/类
public void calculateCov(double[] x, double[] y){
	double covariance = new Covariance().covariance(x, y, false);//take out false too
	System.out.println(covariance);
}
 
开发者ID:PacktPublishing,项目名称:Java-Data-Science-Cookbook,代码行数:5,代码来源:CovarianceTest.java

示例11: maximizeSharpe

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的package包/类
public void maximizeSharpe()
{

   int i,j; 
   int n_basket = returnsAll.size();      
   
   if(n_basket > 0)
   {
    double[][] data = new double[getN_obs()][n_basket];
    double[] means = new double[n_basket];
    
    for(i=0;i<n_basket;i++)
    {
     double[] ret = returnsAll.get(i);
     double[] mstd = mean_std(ret); 
     means[i] = mstd[0];
     
     for(j=0;j<getN_obs();j++)
     {data[j][i] = ret[j];}        
    }
    
    RealVector m = new ArrayRealVector(means, false);
    Covariance covComp = new Covariance(data);
    
    DecompositionSolver solver = new LUDecomposition(covComp.getCovarianceMatrix()).getSolver();
    RealVector sol = solver.solve(m);
    
    double[] w = sol.toArray(); 
    double sumw = 0;
    for(i=0;i<w.length;i++) 
    {
     if(w[i] < 0) 
     {w[i] = 0;} 
     sumw = sumw + w[i]; 
    }
    
    for(i=0;i<w.length;i++) 
    {w[i] = w[i]/sumw;}       
    
    for(i = 0; i < w.length; i++)
    {weightSliders[i].setValue((int)(100*(w[i])));}             
    
   } 
}
 
开发者ID:clisztian,项目名称:iMetrica,代码行数:45,代码来源:SimPanel.java

示例12: maximizeSharpe

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的package包/类
public static double[] maximizeSharpe(double[][] data, int n_basket, int nobs, int nonneg)
   {
   
      int i,j; 
      double[] means = new double[n_basket];
      double sum=0;
      RealVector sol; 
      double[] w = new double[n_basket]; 
       
      for(i=0;i<n_basket;i++)
      {
       sum=0;
       for(j=0;j<nobs;j++)
       {
        sum = sum + data[j][i];        
       }
       means[i] = sum/nobs;
       //System.out.println(means[i]);
      } 
       
      RealVector m = new ArrayRealVector(means, false);
      Covariance covComp = new Covariance(data);
       
      //LinearConstraint(double[] coefficients, Relationship relationship, double value)
      //public static final Relationship LEQ 
      //RealMatrix rm = covComp.scalarMultiply(10000);
      RealMatrix rm = covComp.getCovarianceMatrix();
//       rm = rm.scalarMultiply(1000000);
//       for(i=0;i<n_basket;i++)
//       {printRow(rm.getRow(i));}
      
       
      try
      { 
        DecompositionSolver solver = new QRDecomposition(rm).getSolver();
        sol = solver.solve(m);
        w = sol.toArray(); 
      }
      catch(SingularMatrixException sme) 
      {
       //System.out.println("Matrix singular: setting weights to uniform"); 
       w = new double[n_basket]; 
       for(i=0;i<n_basket;i++) {w[i] = 1.0/n_basket;}
      }
      
      double sumw = 0;
      for(i=0;i<w.length;i++) 
      {
        if(nonneg == 1)
        {if(w[i] < 0) {w[i] = 1.0/n_basket;}}
        else if(nonneg == 2)
        {w[i] = Math.abs(w[i]);}
        
        sumw = sumw + w[i]; 
      }
       
      for(i=0;i<w.length;i++) {w[i] = w[i]/sumw;}
    
      return w; 
  }
 
开发者ID:clisztian,项目名称:iMetrica,代码行数:61,代码来源:StrategyFilter.java

示例13: evolve

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的package包/类
@Override
public Solution[] evolve(Solution[] parents) {
	int k = parents.length;
	int n = parents[0].getNumberOfVariables();
	RealMatrix x = new Array2DRowRealMatrix(k, n);
	
	for (int i=0; i<k; i++) {
		x.setRow(i, EncodingUtils.getReal(parents[i]));
	}
	
	try {
		//perform Cholesky factorization and get the upper triangular matrix
		double jumpRate = Math.pow(jumpRateCoefficient / Math.sqrt(n), 2.0);

		RealMatrix chol = new CholeskyDecomposition(
					new Covariance(x.scalarMultiply(jumpRate))
					.getCovarianceMatrix()).getLT();
		
		//produce the offspring
		Solution[] offspring = new Solution[numberOfOffspring];
		
		for (int i=0; i<numberOfOffspring; i++) {
			Solution child = parents[PRNG.nextInt(parents.length)].copy();
			
			//apply adaptive metropolis step to solution
			RealVector muC = new ArrayRealVector(
					EncodingUtils.getReal(child));
			RealVector ru = new ArrayRealVector(n);
			
			for (int j=0; j<n; j++) {
				ru.setEntry(j, PRNG.nextGaussian());
			}
			
			double[] variables = muC.add(chol.preMultiply(ru)).toArray();
			
			//assign variables back to solution
			for (int j=0; j<n; j++) {
				RealVariable variable = (RealVariable)child.getVariable(j);
				double value = variables[j];

				if (value < variable.getLowerBound()) {
					value = variable.getLowerBound();
				} else if (value > variable.getUpperBound()) {
					value = variable.getUpperBound();
				}

				variable.setValue(value);
			}
			
			offspring[i] = child;
		}
		
		return offspring;
	} catch (Exception e) {
		return new Solution[0];
	}
}
 
开发者ID:Matsemann,项目名称:eamaster,代码行数:58,代码来源:AdaptiveMetropolis.java

示例14: calcSSIMOnWindow

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的package包/类
/**
 * Calculate the SSIM value on a window of pixels
 * @param pLumaOne luma values for first image
 * @param pLumaTwo luma values for second image
 * @param pWidth width of the two images
 * @param pHeight height of the two images
 * @param pWindowSize window size
 * @param pStartX start x coordinate for the window
 * @param pStartY start y coordinate for the window
 * @return SSIM for the window
 */
private static double calcSSIMOnWindow(final double[] pLumaOne, final double[] pLumaTwo, final int pWidth,
		final int pHeight, final int pWindowSize, final int pStartX, final int pStartY) {
	
	final double k1 = 0.01;
	final double k2 = 0.03;
	final double L = Math.pow(2, 8)-1;//255 (at least for the moment all pixel values are 8-bit)
	final double c1 = Math.pow(k1*L, 2); 
	final double c2 = Math.pow(k2*L, 2); 

	final int windowWidth = ((pWindowSize+pStartX)>pWidth)?(pWidth-pStartX):(pWindowSize);
	final int windowHeight = ((pWindowSize+pStartY)>pHeight)?(pHeight-pStartY):(pWindowSize);
	
	if(windowWidth<=0||windowHeight<=0) {
		System.out.println(pWidth+" "+pStartX+" "+windowWidth+" "+windowHeight);
		System.out.println(pHeight+" "+pStartY+" "+windowWidth+" "+windowHeight);
	}
	
	//store a temporary copy of the pixels
	double[] pixelsOne = new double[windowWidth*windowHeight];
	double[] pixelsTwo = new double[windowWidth*windowHeight];
	
	for(int h=0;h<windowHeight;h++) {
		for(int w=0;w<windowWidth;w++) {
			pixelsOne[(h*windowWidth)+w] = pLumaOne[(pStartY+h)*pWidth+pStartX+w];
			pixelsTwo[(h*windowWidth)+w] = pLumaTwo[(pStartY+h)*pWidth+pStartX+w];
		}
	}		
	
	final double ux = new Mean().evaluate(pixelsOne, 0, pixelsOne.length);
	final double uy = new Mean().evaluate(pixelsTwo, 0, pixelsTwo.length);
	final double o2x = new Variance().evaluate(pixelsOne);
	final double o2y = new Variance().evaluate(pixelsTwo);
	final double oxy = new Covariance().covariance(pixelsOne, pixelsTwo);
	
	final double num = (2*ux*uy+c1)*(2*oxy+c2);
	final double den = (ux*ux+uy*uy+c1)*(o2x+o2y+c2);
	
	final double ssim = num/den;
	
	return ssim;
}
 
开发者ID:bl-dpt,项目名称:dissimilar,代码行数:53,代码来源:DissimilarV2.java

示例15: getAutocorrelation

import org.apache.commons.math3.stat.correlation.Covariance; //导入依赖的package包/类
/**
 * Berechne die Autokorrelationsfunktion für die Zeitreihe in Abhängigkeit von lamda.
 *
 * @param timeSeries Dgie Zeitreihe für die die Autokorrelation berechnet werden soll.
 * @param lamda      Lamda-Operator.
 * @return Autokorrelationsfunktion.
 */
private static double getAutocorrelation(final double[] timeSeries, final int lamda) {
    final double covariance = new Covariance().covariance(Arrays.copyOfRange(timeSeries, 0, timeSeries.length - lamda),
            Arrays.copyOfRange(timeSeries, lamda, timeSeries.length), false);
    final double variance = new Variance(false).evaluate(timeSeries);
    return covariance / variance;
}
 
开发者ID:DHBW-Karlsruhe,项目名称:businesshorizon2,代码行数:14,代码来源:YuleWalkerModelCalculator.java


注:本文中的org.apache.commons.math3.stat.correlation.Covariance类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。