User Tools

Site Tools


documentation:math

The maths libraries (including distributions)

Introduction

Mathematics features can be found in various libraries embedded in Java and Capsis including:

  • java.math
  • org.apache.commons.math (v2)
  • org.apache.commons.math3 (v3)
  • capsis.lib.math

Statistical distributions

Apache library distributions

The Apache commons math package can be used in Capsis, it proposes a set of standard distributions. Two versions are available in Capsis:

Example of available discrete distributions

  • BinomialDistribution
  • HypergeometricDistribution
  • PascalDistribution (i.e. special case of Negative binomial)
  • PoissonDistribution
  • ZipfDistribution

Example of available continuous distributions

  • BetaDistribution
  • CauchyDistribution
  • ChiSquaredDistribution
  • ExponentialDistribution
  • FDistribution
  • GammaDistribution
  • NormalDistribution
  • TDistribution
  • WeibullDistribution

Example of use

PoissonDistributionImpl poisson = new PoissonDistributionImpl (mu);
int i = poisson.sample ();

Capsis distributions

Additional distributions are available in capsis.lib.math.distribution:

  • TNormalDistribution (truncated)
  • NegativeBinomialDistribution (added in August 2020)
Usage

Apache v3: it is possible to draw numbers directly from the Distribution object with the sample () method

// Create a BetaDistribution
double alpha = 1.69291 - 0.02622 * k;
double beta = 20.00094 * (1 - Math.exp(-0.02544 * k)) - 0.32242 * k;
BetaDistribution dist = new BetaDistribution(alpha, beta);
 
// Draw a random number in the distribution 
double zr = dist.sample();

Apache v2: If sample () is not available in the distribution, data can be generated with a RandomGenerator class:

  • ContinuousRandomGenerator : for continuous distributions (Normal, Gamma, …)
  • IntegerRandomGenerator : for discrete distributions (ie Binomial, Poisson, …)
import org.apache.commons.math.MathException;
import capsis.lib.math.data.ContinuousRandomGenerator;
import capsis.lib.math.distribution.TNormalDistributionImpl;
 
/*
 * Return an array of values from a Truncated normal distribution
 */
public double[] generate_data() throws MathException {
        //example mu=15, sigma=2, low=10, high=20
	TNormalDistributionImpl dist = new TNormalDistributionImpl(15., 2., 10., 20.);
	ContinuousRandomGenerator gen = new ContinuousRandomGenerator(dist);
 
        return gen.getValues(1000);
}
import org.apache.commons.math.MathException;
import capsis.lib.math.data.ContinuousRandomGenerator;
import capsis.lib.math.distribution.TNormalDistributionImpl;
 
/*
 * Return one single value from a Truncated normal distribution
 */
public double getValueInTrucatedNormalDistribution () throws MathException {
        //example mu=15, sigma=2, low=10, high=20
        double mu = 15;
        double sigma = 2;
        double low = 10;
        double high = 20;
	TNormalDistributionImpl dist = new TNormalDistributionImpl(mu, sigma, low, high);
	ContinuousRandomGenerator gen = new ContinuousRandomGenerator(dist);
 
        return gen.nextDouble ();
}
documentation/math.txt · Last modified: 2021/12/13 09:28 by 127.0.0.1