Return the covariance of the two value distributions.<br>
The covariance describes the linear relation between two random variables (how much they change together).
@return The covariance of the two value distributions
555974
public double getCovariance() {
		if(covariance == null) {
			Observation o = new Observation();
			for(int i=0; i<observationA.getObservationCount(); i++)
				o.addValue(observationA.getValueAt(i)*observationB.getValueAt(i));
			covariance = o.getExpectation()-observationA.getExpectation()*observationB.getExpectation();
		}
		return covariance;
	}
1308149
func covariance(data1, data2 Interface, mean1, mean2 float64) (res float64) {
	// calculate the sum of the squares
	n := data1.Len()
	for i := 0; i < n; i++ {
		delta1 := (data1.Get(i) - mean1)
		delta2 := (data2.Get(i) - mean2)
		res += (delta1*delta2 - res) / float64(i+1)
	}
	return
}
1269858
public Variable[] getRecursivelyDependentVariables() {
		ArrayList<Variable> depVars = new ArrayList<Variable>();
		for (Variable depVar : this.getDependentVariables()) {
			for (Variable depVar1 : depVar.getRecursivelyDependentVariables()) {
				depVars.add(depVar1);
			}
		}
		depVars.add(this);
		return depVars.toArray(new Variable[depVars.size()]);
	}
139639
public synchronized double getAverageMS()
    {
        double result = 0.0;

        for( Double d : m_times.m_values )
        {
            result += d;
        }

        return result / m_times.size();
    }
1042924
public double[][] getCovmat() {
    // Since we worked only on params with dofit=true, we need to expand the
    // matrix to cover all parameters.
    double[][] fullcov = new double[numparams][numparams];
    for(int i = 0, i2 = 0; i < numparams; i++) {
      for(int j = 0, j2 = 0; j < numparams; j++) {
        fullcov[i][j] = (dofit[i] && dofit[j]) ? covmat[i2][j2] : 0;
        if(dofit[j]) {
          j2++;
        }
      }
      if(dofit[i]) {
        i2++;
      }
    }
    return fullcov;
  }
626155
public synchronized DoubleVector generate() {
        DoubleVector termVector = new DenseVector(indexVectorLength);
        for (int i = 0; i < indexVectorLength; i++)
            termVector.set(i, mean + (randomGenerator.nextGaussian() * stdev));
        return termVector;
    }
517864
public Matrix getD () {
		Matrix X = new Matrix(n,n);
		double[][] D = X.getArray();
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				D[i][j] = 0.0;
			}
			D[i][i] = d[i];
			if (e[i] > 0) {
				D[i][i+1] = e[i];
			} else if (e[i] < 0) {
				D[i][i-1] = e[i];
			}
		}
		return X;
	}
145470
public double[] getLinearRegressionParameters(RandomVariable dependents) {

		RandomVariable[] basisFunctions = basisFunctionsEstimator.getBasisFunctions();

		synchronized (solverLock) {
			if(solver == null) {
				// Build XTX - the symmetric matrix consisting of the scalar products of the basis functions.
				double[][] XTX = new double[basisFunctions.length][basisFunctions.length];
				for(int i=0; i<basisFunctions.length; i++) {
					for(int j=i; j<basisFunctions.length; j++) {
						XTX[i][j] = basisFunctions[i].mult(basisFunctions[j]).getAverage();	// Scalar product
						XTX[j][i] = XTX[i][j];												// Symmetric matrix
					}
				}

				solver = new SingularValueDecomposition(new Array2DRowRealMatrix(XTX, false)).getSolver();
			}
		}

		// Build XTy - the projection of the dependents random variable on the basis functions.
		double[] XTy = new double[basisFunctions.length];
		for(int i=0; i<basisFunctions.length; i++) {
			XTy[i] = dependents.mult(basisFunctions[i]).getAverage();				// Scalar product
		}

		// Solve X^T X x = X^T y - which gives us the regression coefficients x = linearRegressionParameters
		double[] linearRegressionParameters = solver.solve(new ArrayRealVector(XTy)).toArray();

		return linearRegressionParameters;
	}
109209
def posterior_covariance_between_points(self, X1, X2):
        
        return self.posterior.covariance_between_points(self.kern, self.X, X1, X2)
1203803
private static double[] computePerDimensionVariances(Relation<? extends NumberVector> relation, double[] center, DBIDs neighborhood) {
    final int dim = center.length;
    double[] variances = new double[dim];
    for(DBIDIter iter = neighborhood.iter(); iter.valid(); iter.advance()) {
      NumberVector databaseObject = relation.get(iter);
      for(int d = 0; d < dim; d++) {
        final double deviation = databaseObject.doubleValue(d) - center[d];
        variances[d] += deviation * deviation;
      }
    }
    return VMath.timesEquals(variances, 1. / neighborhood.size());
  }
1030642
public double[] getLinearRegressionParameters(RandomVariable dependents) {

		RandomVariable localizerWeights = dependents.squared().sub(Math.pow(dependents.getStandardDeviation()*standardDeviations,2.0)).choose(new Scalar(0.0), new Scalar(1.0));

		// Localize basis functions
		RandomVariable[] basisFunctionsNonLocalized = getBasisFunctionsEstimator().getBasisFunctions();
		RandomVariable[] basisFunctions = new RandomVariable[basisFunctionsNonLocalized.length];
		for(int i=0; i<basisFunctions.length; i++) {
			basisFunctions[i] = basisFunctionsNonLocalized[i].mult(localizerWeights);
		}

		// Localize dependents
		dependents = dependents.mult(localizerWeights);

		// Build XTX - the symmetric matrix consisting of the scalar products of the basis functions.
		double[][] XTX = new double[basisFunctions.length][basisFunctions.length];
		for(int i=0; i<basisFunctions.length; i++) {
			for(int j=i; j<basisFunctions.length; j++) {
				XTX[i][j] = basisFunctions[i].mult(basisFunctions[j]).getAverage();	// Scalar product
				XTX[j][i] = XTX[i][j];												// Symmetric matrix
			}
		}

		DecompositionSolver solver = new SingularValueDecomposition(new Array2DRowRealMatrix(XTX, false)).getSolver();

		// Build XTy - the projection of the dependents random variable on the basis functions.
		double[] XTy = new double[basisFunctions.length];
		for(int i=0; i<basisFunctions.length; i++) {
			XTy[i] = dependents.mult(basisFunctions[i]).getAverage();				// Scalar product
		}

		// Solve X^T X x = X^T y - which gives us the regression coefficients x = linearRegressionParameters
		double[] linearRegressionParameters = solver.solve(new ArrayRealVector(XTy)).toArray();

		return linearRegressionParameters;
	}
402809
public double getRMSError() {
        double r = covarCalc.getSumSquaredDeviations()
                / Math.sqrt(varCalcX.getSumSquaredDeviations()
                * varCalcY.getSumSquaredDeviations());
        return Math.sqrt(1 - r) * varCalcX.getStdDev();
    }
1067128
def probability_density(self, X):
        
        self.check_fit()

        # make cov positive semi-definite
        covariance = self.covariance * np.identity(self.covariance.shape[0])
        return stats.multivariate_normal.pdf(X, cov=covariance)
125823
def MultivariateGaussian(mu: vertex_constructor_param_types, covariance: vertex_constructor_param_types, label: Optional[str]=None) -> Vertex:
    
    return Double(context.jvm_view().MultivariateGaussianVertex, label, cast_to_double_vertex(mu), cast_to_double_vertex(covariance))
691154
public double getVariance() {
        if (!DATA_LIST.isEmpty()) {
            double sum = 0;
            double sumOfSquares = 0;
            double average = 0;
            for (DataPoint dataPoint : DATA_LIST) {
                sumOfSquares += (dataPoint.getValue() * dataPoint.getValue());
                sum += dataPoint.getValue();
            }
            average = sum / DATA_LIST.size();
            return (sumOfSquares / DATA_LIST.size()) - average * average;
        }
        return 0;
    }
902241
public MeanVariance averageSymmetricGini() {
    MeanVariance mv = new MeanVariance();
    for(int i1 = 0; i1 < size1; i1++) {
      double purity = 0.0;
      if(contingency[i1][size2] > 0) {
        final double cs = contingency[i1][size2]; // sum, as double.
        for(int i2 = 0; i2 < size2; i2++) {
          double rel = contingency[i1][i2] / cs;
          purity += rel * rel;
        }
        mv.put(purity, cs);
      }
    }
    for(int i2 = 0; i2 < size2; i2++) {
      double purity = 0.0;
      if(contingency[size1][i2] > 0) {
        final double cs = contingency[size1][i2]; // sum, as double.
        for(int i1 = 0; i1 < size1; i1++) {
          double rel = contingency[i1][i2] / cs;
          purity += rel * rel;
        }
        mv.put(purity, cs);
      }
    }
    return mv;
  }
1138874
public double[] getRegressionCoefficients(RandomVariable value) {
		if(basisFunctions.length == 0) {
			return new double[] { };
		}
		else if(basisFunctions.length == 1) {
			/*
			 * Regression with one basis function is just a projection on that vector. <b,x>/<b,b>
			 */
			return new double[] { value.mult(basisFunctions[0]).getAverage() / basisFunctions[0].squared().getAverage() };
		}
		else if(basisFunctions.length == 2) {
			/*
			 * Regression with two basis functions can be solved explicitly if determinant != 0 (otherwise we will fallback to SVD)
			 */
			double a = basisFunctions[0].squared().getAverage();
			double b = basisFunctions[0].mult(basisFunctions[1]).average().squared().doubleValue();
			double c = b;
			double d = basisFunctions[1].squared().getAverage();

			double determinant =  (a * d - b * c);
			if(determinant != 0) {
				double x = value.mult(basisFunctions[0]).getAverage();
				double y = value.mult(basisFunctions[1]).getAverage();

				double alpha0 = (d * x - b * y) / determinant;
				double alpha1 = (a * y - c * x) / determinant;

				return new double[] { alpha0, alpha1 };
			}
		}

		/*
		 * General case
		 */

		// Build regression matrix
		double[][] BTB = new double[basisFunctions.length][basisFunctions.length];
		for(int i=0; i<basisFunctions.length; i++) {
			for(int j=0; j<=i; j++) {
				double covariance = basisFunctions[i].mult(basisFunctions[j]).getAverage();
				BTB[i][j] = covariance;
				BTB[j][i] = covariance;
			}
		}

		double[] BTX = new double[basisFunctions.length];
		for(int i=0; i<basisFunctions.length; i++) {
			double covariance = basisFunctions[i].mult(value).getAverage();
			BTX[i] = covariance;
		}

		return LinearAlgebra.solveLinearEquationLeastSquare(BTB, BTX);
	}