本文整理汇总了Java中org.apache.commons.math3.distribution.MultivariateNormalDistribution类的典型用法代码示例。如果您正苦于以下问题:Java MultivariateNormalDistribution类的具体用法?Java MultivariateNormalDistribution怎么用?Java MultivariateNormalDistribution使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MultivariateNormalDistribution类属于org.apache.commons.math3.distribution包,在下文中一共展示了MultivariateNormalDistribution类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: GenerateInitCentroids
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
private void GenerateInitCentroids() {
centroids = loadCentroids();
Random random = new Random(12397238947287L);
List<Point> initCentroids = new ArrayList<>();
RandomGenerator point_random = new JDKRandomGenerator();
for (Point centroid : centroids) {
MultivariateNormalDistribution distribution = new MultivariateNormalDistribution(point_random, means, covariances);
double[] point = distribution.sample();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dimension - 1; i++) {
point[i] += centroid.location[i];
sb.append(point[i]).append(", ");
}
point[dimension - 1] += centroid.location[dimension - 1];
sb.append(point[dimension - 1]);
System.out.println(sb.toString());
}
}
示例2: consume
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
@Override
public void consume(List<Datum> records) throws Exception {
if (!hasConsumed) {
n = records.get(0).metrics().getDimension();
mean = new ArrayRealVector(n);
covV = new ArrayRealVector(n, 1d/n);
covM = new DiagonalMatrix(covV.toArray());
mnd = new MultivariateNormalDistribution(mean.toArray(), covM.getData());
mnd.reseedRandomGenerator(randomSeed);
randomProjectionMatrix = new BlockRealMatrix(mnd.sample(k));
hasConsumed = true;
}
for (Datum d: records){
metricVector = d.metrics();
transformedVector = randomProjectionMatrix.operate(metricVector);
output.add(new Datum(d,transformedVector));
}
}
示例3: testFitGaussian
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
@Test
public void testFitGaussian() {
MultivariateNormalDistribution mvNormal = getSample3dNormal();
int N = 1000000;
int k = 3;
List<double[]> testData = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
testData.add(mvNormal.sample());
}
long startTime = System.currentTimeMillis();
Gaussian fitted = new Gaussian().fit(testData);
long endTime = System.currentTimeMillis();
log.debug("Fitted {} in: {}", N, endTime - startTime);
assertArrayEquals(mvNormal.getMeans(), fitted.getMean(), 0.01);
for (int i = 0; i < k; i++) {
assertArrayEquals(
mvNormal.getCovariances().getRow(i),
fitted.getCovariance().getRow(i), 0.05);
}
}
示例4: getMultivariateNormal
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
@Override
public RandomDistribution<double[]> getMultivariateNormal(
final RandomNumberStream rng, final double[] means,
final double[][] covariances)
{
final MultivariateRealDistribution dist = new MultivariateNormalDistribution(
RandomNumberStream.Util.asCommonsRandomGenerator(rng), means,
covariances);
return new RandomDistribution<double[]>()
{
@Override
public double[] draw()
{
return dist.sample();
}
};
}
示例5: buildDistribution
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
@Override
public MultivariateNormalDistribution buildDistribution(Configuration config, Random r) {
final double w = config.getWidth();
final double h = config.getHeight();
double maxd = interpolator.value(config.getHotspotRadius(),
config.getHotspotFreedomDegrees());
double factor = 1/maxd;
if (covDistribution == null) {
RealMatrix m = new Array2DRowRealMatrix(new double[][]{
{factor, 0},
{0, factor}
});
covDistribution = new InverseWishartDistribution(m, config.getHotspotFreedomDegrees());
covDistribution.reseedRandomGenerator(r.nextLong());
}
double[] means = new double[]{
r.nextInt(config.getWidth()), r.nextInt(config.getHeight()),
};
double[][] covariance = getCovarianceMatrix();
MultivariateNormalDistribution distribution = new MultivariateNormalDistribution(
means, covariance);
distribution.reseedRandomGenerator(r.nextLong());
return distribution;
}
示例6: testGaussian
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
private double testGaussian(double scale) {
double[] means = new double[]{0,0};
double[][] covariances = new double[][]{
{scale, 0}, {0, scale}
};
MultivariateNormalDistribution uniform = new MultivariateNormalDistribution(means, covariances);
DescriptiveStatistics stats = new DescriptiveStatistics(NORMAL_SAMPLES);
for (int i=0; i<NORMAL_SAMPLES; i++) {
double[] xy = uniform.sample();
final double x = xy[0];
final double y = xy[1];
stats.addValue(Math.sqrt(x*x + y*y));
}
return stats.getPercentile(90);
}
示例7: GaussianRandomFeatures
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
public GaussianRandomFeatures(int D, int n, double gammak, boolean sine) {
_D = D;
_n = n;
_gammak = gammak;
_sine = sine;
// Initialize mnd
// The value for the variances are taken from: http://www.eecs.berkeley.edu/~brecht/papers/07.rah.rec.nips.pdf
_cov = new double[_n][_n];
_mu = new double[_n];
for(int i = 0; i < _n; i++) {
_mu[i] = 0;
for(int j = 0; j < _n; j++)
_cov[i][j] = (i == j) ? 2*_gammak : 0;
}
_mnd = new MultivariateNormalDistribution(_mu, _cov);
_ws = new double[_D][];
for (int i = 0; i < _D; i ++)
_ws[i] = _mnd.sample();
// Initialize urd
_lb = 0;
_ub = 2*Math.PI;
_urd = new UniformRealDistribution (_lb, _ub);
_bs = new double[_D];
for (int i = 0; i < _D; i ++)
_bs[i] = _urd.sample();
}
示例8: testIncompatibleIntialMixture
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
@Test(expected = DimensionMismatchException.class)
public void testIncompatibleIntialMixture() {
// Data has 3 columns
double[][] data = new double[][] {
{ 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }
};
double[] weights = new double[] { 0.5, 0.5 };
// These distributions are compatible with 2-column data, not 3-column
// data
MultivariateNormalDistribution[] mvns = new MultivariateNormalDistribution[2];
mvns[0] = new MultivariateNormalDistribution(new double[] {
-0.0021722935000328823, 3.5432892936887908 },
new double[][] {
{ 4.537422569229048, 3.5266152281729304 },
{ 3.5266152281729304, 6.175448814169779 } });
mvns[1] = new MultivariateNormalDistribution(new double[] {
5.090902706507635, 8.68540656355283 }, new double[][] {
{ 2.886778573963039, 1.5257474543463154 },
{ 1.5257474543463154, 3.3794567673616918 } });
// Create components and mixture
List<Pair<Double, MultivariateNormalDistribution>> components =
new ArrayList<Pair<Double, MultivariateNormalDistribution>>();
components.add(new Pair<Double, MultivariateNormalDistribution>(
weights[0], mvns[0]));
components.add(new Pair<Double, MultivariateNormalDistribution>(
weights[1], mvns[1]));
MixtureMultivariateNormalDistribution badInitialMix
= new MixtureMultivariateNormalDistribution(components);
MultivariateNormalMixtureExpectationMaximization fitter
= new MultivariateNormalMixtureExpectationMaximization(data);
fitter.fit(badInitialMix);
}
示例9: generate
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
public void generate(int sleep_frequency) throws InterruptedException {
// GenerateCentroids();
centroids = loadCentroids();
long time = System.currentTimeMillis();
ThroughputLog throughput = new ThroughputLog(this.getClass().getSimpleName());
Random centroid_random = new Random(2342342170123L);
RandomGenerator point_random = new JDKRandomGenerator();
point_random.setSeed(8624214);
for (long generated_points = 0; generated_points < POINT_NUM; generated_points++) {
int centroid_index = centroid_random.nextInt(centroids.size());
MultivariateNormalDistribution distribution = new MultivariateNormalDistribution(point_random, means, covariances);
double[] point = distribution.sample();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dimension - 1; i++) {
point[i] += centroids.get(centroid_index).location[i];
sb.append(point[i]).append("\t");
}
point[dimension - 1] += centroids.get(centroid_index).location[dimension - 1];
sb.append(point[dimension - 1]).append(Constant.TimeSeparator).append(System.currentTimeMillis());
throughput.execute();
ProducerRecord<String, String> newRecord = new ProducerRecord<>(TOPIC, sb.toString());
producer.send(newRecord);
// System.out.println(sb.toString());
// control data generate speed
if (sleep_frequency > 0 && generated_points % sleep_frequency == 0) {
Thread.sleep(1);
}
}
producer.close();
logger.info("LatencyLog: " + String.valueOf(System.currentTimeMillis() - time));
}
示例10: main
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
public static void main(String[] args) {
double[] means = new double[]{10, 10};
double[][] covariances = new double[][]{{3, 2}, {2, 3}};
for (int i = 0; i < 300; i++) {
MultivariateNormalDistribution distribution = new MultivariateNormalDistribution(means, covariances);
System.out.println(String.valueOf(distribution.sample()[0]) + ", " + String.valueOf(distribution.sample()[1]));
}
}
示例11: calculateLogLikelihood
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
private void calculateLogLikelihood() {
sum_log_likelihood_ = 0;
for (int ii = 0; ii < number_of_observations_; ii++) {
if (observations_ref_[ii] == null) {
continue;
}
//System.out.println(ii);
//System.out.println(predicted_observation_covariances_[ii]);
MultivariateNormalDistribution dist = new MultivariateNormalDistribution(
predicted_observation_means_[ii].getColumn(0).toArray(),
predicted_observation_covariances_[ii].toArray2());
sum_log_likelihood_ += Math.log(dist.density(observations_ref_[ii].getColumn(0).toArray()));
}
}
示例12: getSample3dNormal
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
private static MultivariateNormalDistribution getSample3dNormal() {
double[] mean = {1,2,3};
double[][] cov = {
{3,1,0},
{1,3,1},
{0,1,3}
};
MultivariateNormalDistribution mvNormal = new MultivariateNormalDistribution(mean, cov);
return mvNormal;
}
示例13: testMahalanobis
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
@Test
public void testMahalanobis() {
MultivariateNormalDistribution mvNormal = getSample3dNormal();
Gaussian gaussian = new Gaussian(mvNormal.getMeans(), mvNormal.getCovariances());
int N = 100000;
int k = 3;
double[][] testData = new double[N][k];
for (int i = 0; i < N; i++) {
testData[i] = mvNormal.sample();
}
double[] mScores = new double[N];
long startTime = System.currentTimeMillis();
for (int i = 0; i < N; i++) {
mScores[i] = gaussian.mahalanobis(testData[i]);
}
long endTime = System.currentTimeMillis();
log.debug("Mahalobis distance on {} in {}", N, endTime-startTime);
double[] dScores = new double[N];
startTime = System.currentTimeMillis();
for (int i = 0; i < N; i++) {
dScores[i] = -Math.log(mvNormal.density(testData[i]));
}
endTime = System.currentTimeMillis();
log.debug("LogPDF on {} in {}", N, endTime-startTime);
// Check that mahalonbis distance has same relative magnitude as -log(pdf)
for (int i = 1; i < N; i++) {
assertEquals(mScores[i] > mScores[i-1], dScores[i] > dScores[i-1]);
}
}
示例14: MultivariateNormal
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
public MultivariateNormal(RealVector mean, RealMatrix sigma) {
double[][] arrayOfMatrix = new double[sigma.getColumnDimension()][sigma.getRowDimension()];
for (int i = 0; i < sigma.getColumnDimension(); i++) {
arrayOfMatrix[i] = sigma.getRow(i);
}
distribution = new MultivariateNormalDistribution(mean.toArray(), arrayOfMatrix);
}
示例15: bivariateNormalTest
import org.apache.commons.math3.distribution.MultivariateNormalDistribution; //导入依赖的package包/类
@Test
public void bivariateNormalTest() throws Exception {
// Make sure we are close to bivariate normal
MacroBaseConf conf = new MacroBaseConf()
.set(MacroBaseConf.TRANSFORM_TYPE, "BAYESIAN_NORMAL")
.set(MacroBaseConf.DATA_LOADER_TYPE, "CSV_LOADER")
.set(MacroBaseConf.CSV_COMPRESSION, CSVIngester.Compression.GZIP)
.set(MacroBaseConf.CSV_INPUT_FILE, "src/test/resources/data/2d_standard_normal_100k.csv.gz")
.set(MacroBaseConf.METRICS, "XX, YY")
.set(MacroBaseConf.ATTRIBUTES, "");
double[] means = {0, 0};
double[][] variance = {{1, 0}, {0, 1}};
MultivariateNormalDistribution bivariateNormal = new MultivariateNormalDistribution(means, variance);
List<Datum> data = conf.constructIngester().getStream().drain();
assertEquals(100000, data.size());
BayesianNormalDensity bayesianNormal = new BayesianNormalDensity(conf);
bayesianNormal.train(data);
assertEquals(0, bayesianNormal.getMean().getEntry(0), 0.01);
assertEquals(0, bayesianNormal.getMean().getEntry(1), 0.01);
Datum d;
int index;
Random rand = new Random();
for (int i =0 ; i < 100; i++ ) {
index = rand.nextInt(data.size());
d = data.get(index);
assertEquals(bivariateNormal.density(d.metrics().toArray()), bayesianNormal.getDensity(d), 1e-3);
}
}