org.apache.commons.math3.distribution.AbstractRealDistribution Java Examples

The following examples show how to use org.apache.commons.math3.distribution.AbstractRealDistribution. 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: FragPileupGen.java    From HMMRATAC with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns a new Distribution 
 * @param p a double specifying which distribution to use
 * @param m a double representing the mean for the desired distribution
 * @param l a double representing the standard deviation, or more generally, the lambda of the desired distribution
 * @return A new AbstractRealDistribution
 */
private AbstractRealDistribution getDist(double p,double m, double l){
	if (p == 1){
		//return new LaplaceDistribution(m,l);
		return new ExponentialDistribution(m);
	}
	if (p == 2){
		return new NormalDistribution(m,l);
	}
	if (p == 0.5 || p == 3){
		return new ExponentialDistribution(m);
	}
	if (p == 0){
		//return new ModifiedLaplaceDistribution(m,l);
		return new ExponentialDistribution(m);
	}
	else{
		return null;
	}
	
}
 
Example #2
Source File: pileup.java    From HMMRATAC with GNU General Public License v3.0 6 votes vote down vote up
private AbstractRealDistribution getDist(double p,double m, double l){
	if (p == 1){
		return new LaplaceDistribution(m,l);
		//return new ExponentialDistribution(m);
	}
	if (p == 2){
		return new NormalDistribution(m,l);
	}
	if (p == 0.5 || p == 3){
		return new ExponentialDistribution(m);
	}
	if (p == 0){
		//return new ModifiedLaplaceDistribution(m,l);
		return new ExponentialDistribution(m);
	}
	else{
		return null;
	}
	
}
 
Example #3
Source File: CurveEstimationTest.java    From finmath-lib with Apache License 2.0 6 votes vote down vote up
/**
 * Regression matrix (currently no test, just for inspection)
 */
@Test
public void testRegressionMatrix() {
	final double[] X = { 0.0 , 1.0 };
	final double[] Y = { 1.0 , 0.8 };

	if(isJBLASPresent) {
		// The following code only works if JBlas is present

		final AbstractRealDistribution kernel=new NormalDistribution();
		final double K=kernel.density(0.0);
		final DoubleMatrix R=new DoubleMatrix(new double[] {K*(Y[0]+Y[1]),Y[1]*(X[1]-X[0])*K} );
		final DoubleMatrix M=new DoubleMatrix(new double[][] {{2*K,K*(X[1]-X[0])},{K*(X[1]-X[0]),K*(X[1]-X[0])*(X[1]-X[0])}} );
		final double detM= M.get(0,0)*M.get(1, 1)-M.get(1,0)*M.get(1,0);
		DoubleMatrix MInv=new DoubleMatrix(new double[][] {{M.get(1, 1),-M.get(1,0)},{-M.get(1,0),M.get(0,0)}} );
		MInv=MInv.mul(1/detM);
		final DoubleMatrix a=MInv.mmul(R);
		System.out.println(R.toString());
		System.out.println(M.toString());
		System.out.println(a.toString());
	}
}
 
Example #4
Source File: CurveEstimationTest.java    From finmath-lib with Apache License 2.0 6 votes vote down vote up
/**
 * Regression matrix (currently no test, just for inspection)
 */
@Test
public void testRegressionMatrix() {
	final double[] X = { 0.0 , 1.0 };
	final double[] Y = { 1.0 , 0.8 };

	if(isJBLASPresent) {
		// The following code only works if JBlas is present

		final AbstractRealDistribution kernel=new NormalDistribution();
		final double K=kernel.density(0.0);
		final DoubleMatrix R=new DoubleMatrix(new double[] {K*(Y[0]+Y[1]),Y[1]*(X[1]-X[0])*K} );
		final DoubleMatrix M=new DoubleMatrix(new double[][] {{2*K,K*(X[1]-X[0])},{K*(X[1]-X[0]),K*(X[1]-X[0])*(X[1]-X[0])}} );
		final double detM= M.get(0,0)*M.get(1, 1)-M.get(1,0)*M.get(1,0);
		DoubleMatrix MInv=new DoubleMatrix(new double[][] {{M.get(1, 1),-M.get(1,0)},{-M.get(1,0),M.get(0,0)}} );
		MInv=MInv.mul(1/detM);
		final DoubleMatrix a=MInv.mmul(R);
		System.out.println(R.toString());
		System.out.println(M.toString());
		System.out.println(a.toString());
	}
}
 
Example #5
Source File: QuantityProbability.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
public QuantityProbability(Class<? extends RandomGenerator> randomClazz, int randomSeed, Class<? extends AbstractRealDistribution> distributionClazz, Object... distributionParameters) {
    this.randomSeed = randomSeed;
    this.random = ReflectionUtility.getInstance(randomClazz, randomSeed);
    this.distributionParameters = distributionParameters;
    distributionParameters = ArrayUtility.insert(0, distributionParameters, random);
    this.distribution = ReflectionUtility.getInstance(distributionClazz, distributionParameters);
}
 
Example #6
Source File: QuantityProbability.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public void afterLoad() {
    try {
        random = (RandomGenerator) ReflectionUtility.getInstance(Class.forName(randomClass), randomSeed);
        Object[] parameters = ArrayUtility.insert(0, distributionParameters, random);
        distribution = (AbstractRealDistribution) ReflectionUtility.getInstance(Class.forName(distributionClass), parameters);
    } catch (Exception exception) {
    }
}
 
Example #7
Source File: StatisticsUtilTest.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Generates a paired sample for two real distribution.
 *
 * @param dist0 The distribution to draw the first sample from.
 * @param dist1 The distribution to draw the second sample from.
 * @return The drawn samples according to the given distributions.
 */
private static double[][] generateDistributionSamples(final AbstractRealDistribution dist0, final AbstractRealDistribution dist1) {
	double[] sampleA = new double[SAMPLE_SIZE];
	double[] sampleB = new double[SAMPLE_SIZE];

	for (int i = 0; i < SAMPLE_SIZE; i++) {
		sampleA[i] = dist0.sample();
		sampleB[i] = dist1.sample();
	}
	return new double[][] { sampleA, sampleB };
}
 
Example #8
Source File: AdaptiveMetropolisSampler.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public double sample(final RandomGenerator rng, final Function<Double, Double> logPDF) {
    Utils.nonNull(rng);
    Utils.nonNull(logPDF);
    final AbstractRealDistribution normal = new NormalDistribution(rng, 0, 1);
    final double proposal = xCurrent + stepSize * normal.sample();
    final double acceptanceProbability = (proposal < lowerBound || upperBound < proposal) ? 0
            : Math.min(1, Math.exp(logPDF.apply(proposal) - logPDF.apply(xCurrent)));

    //adjust stepSize larger/smaller to decrease/increase the acceptance rate
    final double correctionFactor = (acceptanceProbability - optimalAcceptanceRate) * adjustmentRate * (timeScale / (timeScale + iteration));
    stepSize *= Math.exp(correctionFactor);
    iteration++;
    return rng.nextDouble() < acceptanceProbability ? proposal : xCurrent;
}
 
Example #9
Source File: AdaptiveMetropolisSampler.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public double sample(final RandomGenerator rng, final Function<Double, Double> logPDF) {
    Utils.nonNull(rng);
    Utils.nonNull(logPDF);
    final AbstractRealDistribution normal = new NormalDistribution(rng, 0, 1);
    final double proposal = xCurrent + stepSize * normal.sample();
    final double acceptanceProbability = (proposal < lowerBound || upperBound < proposal) ? 0
            : Math.min(1, Math.exp(logPDF.apply(proposal) - logPDF.apply(xCurrent)));

    //adjust stepSize larger/smaller to decrease/increase the acceptance rate
    final double correctionFactor = (acceptanceProbability - optimalAcceptanceRate) * adjustmentRate * (timeScale / (timeScale + iteration));
    stepSize *= Math.exp(correctionFactor);
    iteration++;
    return rng.nextDouble() < acceptanceProbability ? proposal : xCurrent;
}
 
Example #10
Source File: PatternSpikeGenerator.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
private static AbstractRealDistribution makeDist(DistributionType type, double mean, double sigma, int seed) {
    switch (type) {
        case LOGNORMAL:
            return new LogNormalDistribution(new Well19937c(seed), mean, sigma, 1.0E-9D);
        case EXPONENTIAL:
            return new ExponentialDistribution(new Well19937c(seed), mean, 1.0E-9D);
        case UNIFORM:
            return new UniformRealDistribution(new Well19937c(seed), mean - sigma, mean + sigma);
        case FIXED:
            return new UniformRealDistribution(new Well19937c(seed), mean, mean + 1.0E-9D);
        default:
            throw new IllegalArgumentException(String.format("Unsupported distribution type '%s", type));
    }
}
 
Example #11
Source File: OpenShopProblemGenerator.java    From AILibs with GNU Affero General Public License v3.0 4 votes vote down vote up
private int sampleMinInteger(final int min, final AbstractRealDistribution dist) {
	return Math.max(min, (int)Math.round(dist.sample()));
}
 
Example #12
Source File: ProbabilityEvaluator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public Object doWork(Object... values) throws IOException{

  Object first = null;
  Object second = null;
  Object third = null;

  if(values.length == 2) {
    first = values[0];
    second = values[1];

    if (null == first) {
      throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));
    }
    if (null == second) {
      throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - null found for the second value", toExpression(constructingFactory)));
    }
    if (!(first instanceof IntegerDistribution)) {
      throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the first value, expecting a IntegerDistributionm for probability at a specific value.", toExpression(constructingFactory), first.getClass().getSimpleName()));
    }
    if (!(second instanceof Number)) {
      throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the second value, expecting a Number", toExpression(constructingFactory), first.getClass().getSimpleName()));
    }

    IntegerDistribution d = (IntegerDistribution) first;
    Number predictOver = (Number) second;
    return d.probability(predictOver.intValue());

  } else if(values.length == 3) {
    first = values[0];
    second = values[1];
    third = values[2];

    if (!(first instanceof AbstractRealDistribution)) {
      throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the first value, expecting a RealDistribution for probability ranges", toExpression(constructingFactory), first.getClass().getSimpleName()));
    }
    if (!(second instanceof Number)) {
      throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the second value, expecting a Number", toExpression(constructingFactory), first.getClass().getSimpleName()));
    }

    if (!(third instanceof Number)) {
      throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the second value, expecting a Number", toExpression(constructingFactory), first.getClass().getSimpleName()));
    }

    AbstractRealDistribution realDistribution = (AbstractRealDistribution)first;
    Number start = (Number) second;
    Number end = (Number) third;
    return realDistribution.probability(start.doubleValue(), end.doubleValue());
  } else {
    throw new IOException("The probability function expects 2 or 3 parameters");
  }
}
 
Example #13
Source File: DistributionOffsetApache.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public DistributionOffsetApache(AbstractRealDistribution delegate, long min, long max)
{
    this.delegate = delegate;
    this.min = min;
    this.delta = max - min;
}
 
Example #14
Source File: DistributionBoundApache.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public DistributionBoundApache(AbstractRealDistribution delegate, long min, long max)
{
    this.delegate = delegate;
    this.min = min;
    this.max = max;
}