Java Code Examples for org.apache.commons.math3.distribution.NormalDistribution#sample()

The following examples show how to use org.apache.commons.math3.distribution.NormalDistribution#sample() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchurTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRandomDataNormalDistribution() {
    for (int run = 0; run < 100; run++) {
        Random r = new Random(System.currentTimeMillis());
        NormalDistribution dist = new NormalDistribution(0.0, r.nextDouble() * 5);

        // matrix size
        int size = r.nextInt(20) + 4;

        double[][] data = new double[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                data[i][j] = dist.sample();
            }
        }

        RealMatrix m = MatrixUtils.createRealMatrix(data);
        RealMatrix s = checkAEqualPTPt(m);
        checkSchurForm(s);
    }
}
 
Example 2
Source File: HessenbergTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRandomDataNormalDistribution() {
    for (int run = 0; run < 100; run++) {
        Random r = new Random(System.currentTimeMillis());
        NormalDistribution dist = new NormalDistribution(0.0, r.nextDouble() * 5);

        // matrix size
        int size = r.nextInt(20) + 4;

        double[][] data = new double[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                data[i][j] = dist.sample();
            }
        }

        RealMatrix m = MatrixUtils.createRealMatrix(data);
        RealMatrix h = checkAEqualPHPt(m);
        checkHessenbergForm(h);
    }
}
 
Example 3
Source File: EigenDecompositionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
@Ignore
public void testNormalDistributionUnsymmetricMatrix() {
    for (int run = 0; run < 100; run++) {
        Random r = new Random(System.currentTimeMillis());
        NormalDistribution dist = new NormalDistribution(0.0, r.nextDouble() * 5);

        // matrix size
        int size = r.nextInt(20) + 4;

        double[][] data = new double[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                data[i][j] = dist.sample();
            }
        }

        RealMatrix m = MatrixUtils.createRealMatrix(data);
        checkUnsymmetricMatrix(m);
    }
}
 
Example 4
Source File: EigenDecompositionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNormalDistributionUnsymmetricMatrix() {
    for (int run = 0; run < 100; run++) {
        Random r = new Random(System.currentTimeMillis());
        NormalDistribution dist = new NormalDistribution(0.0, r.nextDouble() * 5);

        // matrix size
        int size = r.nextInt(20) + 4;

        double[][] data = new double[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                data[i][j] = dist.sample();
            }
        }

        RealMatrix m = MatrixUtils.createRealMatrix(data);
        checkUnsymmetricMatrix(m);
    }
}
 
Example 5
Source File: HessenbergTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRandomDataNormalDistribution() {
    for (int run = 0; run < 100; run++) {
        Random r = new Random(System.currentTimeMillis());
        NormalDistribution dist = new NormalDistribution(0.0, r.nextDouble() * 5);

        // matrix size
        int size = r.nextInt(20) + 4;

        double[][] data = new double[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                data[i][j] = dist.sample();
            }
        }

        RealMatrix m = MatrixUtils.createRealMatrix(data);
        RealMatrix h = checkAEqualPHPt(m);
        checkHessenbergForm(h);
    }
}
 
Example 6
Source File: SchurTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRandomDataNormalDistribution() {
    for (int run = 0; run < 100; run++) {
        Random r = new Random(System.currentTimeMillis());
        NormalDistribution dist = new NormalDistribution(0.0, r.nextDouble() * 5);

        // matrix size
        int size = r.nextInt(20) + 4;

        double[][] data = new double[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                data[i][j] = dist.sample();
            }
        }

        RealMatrix m = MatrixUtils.createRealMatrix(data);
        RealMatrix s = checkAEqualPTPt(m);
        checkSchurForm(s);
    }
}
 
Example 7
Source File: HessenbergTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRandomDataNormalDistribution() {
    for (int run = 0; run < 100; run++) {
        Random r = new Random(System.currentTimeMillis());
        NormalDistribution dist = new NormalDistribution(0.0, r.nextDouble() * 5);

        // matrix size
        int size = r.nextInt(20) + 4;

        double[][] data = new double[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                data[i][j] = dist.sample();
            }
        }

        RealMatrix m = MatrixUtils.createRealMatrix(data);
        RealMatrix h = checkAEqualPHPt(m);
        checkHessenbergForm(h);
    }
}
 
Example 8
Source File: RegressionSynthesizer.java    From pyramid with Apache License 2.0 6 votes vote down vote up
public  RegDataSet gaussianMixture(){

        NormalDistribution leftGaussian = new NormalDistribution(0.2,0.01);

        NormalDistribution rightGaussian = new NormalDistribution(0.7,0.1);

        RegDataSet dataSet = RegDataSetBuilder.getBuilder()
                .numDataPoints(numDataPoints)
                .numFeatures(1)
                .dense(true)
                .missingValue(false)
                .build();
        for (int i=0;i<numDataPoints;i++){
            double featureValue = Sampling.doubleUniform(0,1);
            double label;
            if (featureValue>0.5){
                label = leftGaussian.sample();
            } else {
                label = rightGaussian.sample();
            }
            dataSet.setFeatureValue(i,0,featureValue);
            dataSet.setLabel(i,label);
        }
        return dataSet;
    }
 
Example 9
Source File: HessenbergTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRandomDataNormalDistribution() {
    for (int run = 0; run < 100; run++) {
        Random r = new Random(System.currentTimeMillis());
        NormalDistribution dist = new NormalDistribution(0.0, r.nextDouble() * 5);

        // matrix size
        int size = r.nextInt(20) + 4;

        double[][] data = new double[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                data[i][j] = dist.sample();
            }
        }

        RealMatrix m = MatrixUtils.createRealMatrix(data);
        RealMatrix h = checkAEqualPHPt(m);
        checkHessenbergForm(h);
    }
}
 
Example 10
Source File: RegressionSynthesizer.java    From pyramid with Apache License 2.0 6 votes vote down vote up
public  RegDataSet univarStepFeatureNoise(){
    NormalDistribution featureNoise = new NormalDistribution(0,0.1);

    RegDataSet dataSet = RegDataSetBuilder.getBuilder()
            .numDataPoints(numDataPoints)
            .numFeatures(1)
            .dense(true)
            .missingValue(false)
            .build();
    for (int i=0;i<numDataPoints;i++){
        double featureValue = Sampling.doubleUniform(0,1);
        double label;
        if (featureValue>0.5){
            label = 0.7;
        } else {
            label = 0.2;
        }
        label += noise.sample();
        featureValue+= featureNoise.sample();
        dataSet.setFeatureValue(i,0,featureValue);
        dataSet.setLabel(i,label);
    }
    return dataSet;
}
 
Example 11
Source File: SchurTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRandomDataNormalDistribution() {
    for (int run = 0; run < 100; run++) {
        Random r = new Random(System.currentTimeMillis());
        NormalDistribution dist = new NormalDistribution(0.0, r.nextDouble() * 5);

        // matrix size
        int size = r.nextInt(20) + 4;

        double[][] data = new double[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                data[i][j] = dist.sample();
            }
        }

        RealMatrix m = MatrixUtils.createRealMatrix(data);
        RealMatrix s = checkAEqualPTPt(m);
        checkSchurForm(s);
    }
}
 
Example 12
Source File: PediatricGrowthTrajectory.java    From synthea with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a BMI for the person one year later. BMIs are generated based on correlations
 * measured between years of age and differences in mean BMI. This takes into special
 * consideration people at or above the 95th percentile, as the growth charts start to break down.
 * @param person to generate the new BMI for
 * @param time current time
 * @param randomGenerator Apache Commons Math random thingy needed to sample a value
 */
public void generateNextYearBMI(Person person, long time,
                                  JDKRandomGenerator randomGenerator) {
  double age = person.ageInDecimalYears(time);
  double nextAgeYear = age + 1;
  String sex = (String) person.attributes.get(Person.GENDER);
  int nextRoundedYear = (int) Math.floor(nextAgeYear);
  YearInformation yi = yearCorrelations.get(Integer.toString(nextRoundedYear));
  double sigma = sigma(sex, age);
  Point lastPoint = this.tail();
  double currentBMI = lastPoint.bmi;
  double ezscore = extendedZScore(currentBMI, lastPoint.ageInMonths, sex, sigma);
  double mean = yi.correlation * ezscore + yi.diff;
  double sd = Math.sqrt(1 - Math.pow(yi.correlation, 2));
  NormalDistribution nextZDistro = new NormalDistribution(randomGenerator, mean, sd);
  double nextYearZscore = nextZDistro.sample();
  double nextYearPercentile = GrowthChart.zscoreToPercentile(nextYearZscore);
  double nextPointBMI = percentileToBMI(nextYearPercentile, lastPoint.ageInMonths + 12,
      sex, sigma(sex, nextAgeYear));
  Point nextPoint = new Point();
  nextPoint.timeInSimulation = lastPoint.timeInSimulation + ONE_YEAR;
  nextPoint.ageInMonths = lastPoint.ageInMonths + 12;
  nextPoint.bmi = nextPointBMI;
  this.trajectory.add(nextPoint);
}
 
Example 13
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static List<Vector2D> makeBlobs(int samples, int centers, double clusterStd,
                                       double min, double max, boolean shuffle, RandomGenerator random) {

    NormalDistribution dist = new NormalDistribution(random, 0.0, clusterStd, 1e-9);

    double range = max - min;
    Vector2D[] centerPoints = new Vector2D[centers];
    for (int i = 0; i < centers; i++) {
        double x = random.nextDouble() * range + min;
        double y = random.nextDouble() * range + min;
        centerPoints[i] = new Vector2D(x, y);
    }
    
    int[] nSamplesPerCenter = new int[centers];
    int count = samples / centers;
    Arrays.fill(nSamplesPerCenter, count);
    
    for (int i = 0; i < samples % centers; i++) {
        nSamplesPerCenter[i]++;
    }
    
    List<Vector2D> points = new ArrayList<Vector2D>();
    for (int i = 0; i < centers; i++) {
        for (int j = 0; j < nSamplesPerCenter[i]; j++) {
            Vector2D point = new Vector2D(dist.sample(), dist.sample());
            points.add(point.add(centerPoints[i]));
        }
    }
    
    if (shuffle) {
        Collections.shuffle(points, new RandomAdaptor(random));
    }

    return points;
}
 
Example 14
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static List<Vector2D> makeBlobs(int samples, int centers, double clusterStd,
                                       double min, double max, boolean shuffle, RandomGenerator random) {

    NormalDistribution dist = new NormalDistribution(random, 0.0, clusterStd, 1e-9);

    double range = max - min;
    Vector2D[] centerPoints = new Vector2D[centers];
    for (int i = 0; i < centers; i++) {
        double x = random.nextDouble() * range + min;
        double y = random.nextDouble() * range + min;
        centerPoints[i] = new Vector2D(x, y);
    }
    
    int[] nSamplesPerCenter = new int[centers];
    int count = samples / centers;
    Arrays.fill(nSamplesPerCenter, count);
    
    for (int i = 0; i < samples % centers; i++) {
        nSamplesPerCenter[i]++;
    }
    
    List<Vector2D> points = new ArrayList<Vector2D>();
    for (int i = 0; i < centers; i++) {
        for (int j = 0; j < nSamplesPerCenter[i]; j++) {
            Vector2D point = new Vector2D(dist.sample(), dist.sample());
            points.add(point.add(centerPoints[i]));
        }
    }
    
    if (shuffle) {
        Collections.shuffle(points, new RandomAdaptor(random));
    }

    return points;
}
 
Example 15
Source File: CoverageDropoutDetectorTest.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Object[][] getUnivariateGaussianTargetsWithDropout(final double sigma, final double dropoutRate) {
    Random rng = new Random(337);
    final RandomGenerator randomGenerator = RandomGeneratorFactory.createRandomGenerator(rng);
    NormalDistribution n = new NormalDistribution(randomGenerator, 1, sigma);
    final int numDataPoints = 10000;
    final int numEventPoints = 2000;

    // Randomly select dropoutRate of targets and reduce by 25%-75% (uniformly distributed)
    UniformRealDistribution uniformRealDistribution = new UniformRealDistribution(randomGenerator, 0, 1.0);
    final List<ReadCountRecord.SingleSampleRecord> targetList = new ArrayList<>();
    for (int i = 0; i < numDataPoints; i++){
        double coverage = n.sample() + (i < (numDataPoints - numEventPoints) ? 0.0 : 0.5);
        if (uniformRealDistribution.sample() < dropoutRate) {
            double multiplier = .25 + uniformRealDistribution.sample()/2;
            coverage = coverage * multiplier;
        }
        targetList.add(new ReadCountRecord.SingleSampleRecord(new Target("arbitrary_name", new SimpleInterval("chr1", 100 + 2*i, 101 + 2 * i)), coverage));
    }

    HashedListTargetCollection<ReadCountRecord.SingleSampleRecord> targets = new HashedListTargetCollection<>(targetList);

    List<ModeledSegment> segments = new ArrayList<>();
    segments.add(new ModeledSegment(new SimpleInterval("chr1", 100, 16050), 8000, 1));
    segments.add(new ModeledSegment(new SimpleInterval("chr1", 16100, 20200), 2000, 1.5));

    return new Object [] []{ {targets, segments}};
}
 
Example 16
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static List<Vector2D> makeBlobs(int samples, int centers, double clusterStd,
                                       double min, double max, boolean shuffle, RandomGenerator random) {

    NormalDistribution dist = new NormalDistribution(random, 0.0, clusterStd, 1e-9);

    double range = max - min;
    Vector2D[] centerPoints = new Vector2D[centers];
    for (int i = 0; i < centers; i++) {
        double x = random.nextDouble() * range + min;
        double y = random.nextDouble() * range + min;
        centerPoints[i] = new Vector2D(x, y);
    }
    
    int[] nSamplesPerCenter = new int[centers];
    int count = samples / centers;
    Arrays.fill(nSamplesPerCenter, count);
    
    for (int i = 0; i < samples % centers; i++) {
        nSamplesPerCenter[i]++;
    }
    
    List<Vector2D> points = new ArrayList<Vector2D>();
    for (int i = 0; i < centers; i++) {
        for (int j = 0; j < nSamplesPerCenter[i]; j++) {
            Vector2D point = new Vector2D(dist.sample(), dist.sample());
            points.add(point.add(centerPoints[i]));
        }
    }
    
    if (shuffle) {
        Collections.shuffle(points, new RandomAdaptor(random));
    }

    return points;
}
 
Example 17
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static Vector2D generateNoiseVector(NormalDistribution distribution) {
    return new Vector2D(distribution.sample(), distribution.sample());
}
 
Example 18
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static Vector2D generateNoiseVector(NormalDistribution distribution) {
    return new Vector2D(distribution.sample(), distribution.sample());
}
 
Example 19
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static Vector2D generateNoiseVector(NormalDistribution distribution) {
    return new Vector2D(distribution.sample(), distribution.sample());
}
 
Example 20
Source File: ChangeFinder2DTest.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
public void testSota5D() throws HiveException {
    final int DIM = 5;
    final int EXAMPLES = 20001;

    final Double[] x = new Double[DIM];
    final List<Double> xList = Arrays.asList(x);

    Parameters params = new Parameters();
    params.set(LossFunction.logloss);
    params.r1 = 0.01d;
    params.k = 10;
    params.T1 = 10;
    params.T2 = 10;
    PrimitiveObjectInspector oi = PrimitiveObjectInspectorFactory.javaDoubleObjectInspector;
    ListObjectInspector listOI = ObjectInspectorFactory.getStandardListObjectInspector(oi);
    final ChangeFinder2D cf = new ChangeFinder2D(params, listOI);
    final double[] outScores = new double[2];

    RandomGenerator rng1 = new Well19937c(31L);
    final UniformIntegerDistribution uniform = new UniformIntegerDistribution(rng1, 0, 10);
    RandomGenerator rng2 = new Well19937c(41L);
    final PoissonDistribution poissonEvent = new PoissonDistribution(rng2, 1000.d,
        PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS);
    final StringBuilder buf = new StringBuilder(256);

    println("# time x0 x1 x2 x3 x4 mean0 mean1 mean2 mean3 mean4 outlier change");
    FIN: for (int i = 0; i < EXAMPLES;) {
        int len = poissonEvent.sample();
        double data[][] = new double[DIM][len];
        double mean[] = new double[DIM];
        double sd[] = new double[DIM];
        for (int j = 0; j < DIM; j++) {
            mean[j] = uniform.sample() * 5.d;
            sd[j] = uniform.sample() / 10.d * 5.d + 1.d;
            if (i % 5 == 0) {
                mean[j] += 50.d;
            }
            NormalDistribution normDist =
                    new NormalDistribution(new Well19937c(i + j), mean[j], sd[j]);
            data[j] = normDist.sample(len);
            data[j][len / (j + 2) + DIM % (j + 1)] = mean[j] + (j + 4) * sd[j];
        }
        for (int j = 0; j < len; j++) {
            if (i >= EXAMPLES) {
                break FIN;
            }
            x[0] = data[0][j];
            x[1] = data[1][j];
            x[2] = data[2][j];
            x[3] = data[3][j];
            x[4] = data[4][j];
            cf.update(xList, outScores);
            buf.append(i)
               .append(' ')
               .append(x[0].doubleValue())
               .append(' ')
               .append(x[1].doubleValue())
               .append(' ')
               .append(x[2].doubleValue())
               .append(' ')
               .append(x[3].doubleValue())
               .append(' ')
               .append(x[4].doubleValue())
               .append(' ')
               .append(mean[0])
               .append(' ')
               .append(mean[1])
               .append(' ')
               .append(mean[2])
               .append(' ')
               .append(mean[3])
               .append(' ')
               .append(mean[4])
               .append(' ')
               .append(outScores[0])
               .append(' ')
               .append(outScores[1]);
            println(buf.toString());
            StringUtils.clear(buf);
            i++;
        }
    }
}