Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.anhth12.distributions; import org.apache.commons.math3.distribution.ExponentialDistribution; import org.apache.commons.math3.distribution.NormalDistribution; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.distribution.UniformRealDistribution; import org.apache.commons.math3.random.RandomGenerator; import org.canova.api.util.MultiDimensionalMap; /** * * @author anhth12 */ public class Distributions { private static final MultiDimensionalMap<RandomGenerator, Double, RealDistribution> normalDistributions = MultiDimensionalMap .newHashBackedMap(); private static final MultiDimensionalMap<RandomGenerator, Double, RealDistribution> exponentialDist = MultiDimensionalMap .newHashBackedMap(); private static final MultiDimensionalMap<RandomGenerator, Double, RealDistribution> uniformDist = MultiDimensionalMap .newHashBackedMap(); public static RealDistribution exponential(RandomGenerator rng, double mean) { if (exponentialDist.get(rng, mean) == null) { RealDistribution ret = new ExponentialDistribution(rng, 1.0, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); exponentialDist.put(rng, mean, ret); } return exponentialDist.get(rng, mean); } public static RealDistribution normal(RandomGenerator rng, double std) { if (normalDistributions.get(rng, std) == null) { RealDistribution ret = new NormalDistribution(rng, 0, std, NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); normalDistributions.put(rng, std, ret); return ret; } return normalDistributions.get(rng, std); } public static RealDistribution uniform(RandomGenerator rng, double fanIn) { fanIn = Math.abs(fanIn); if (uniformDist.get(rng, fanIn) == null) { RealDistribution ret = new UniformRealDistribution(rng, -fanIn, fanIn); uniformDist.put(rng, fanIn, ret); return ret; } return uniformDist.get(rng, fanIn); } public static RealDistribution uniform(RandomGenerator rng, double min, double max) { return new UniformRealDistribution(rng, min, max); } public static RealDistribution uniform(RandomGenerator rng, int nIn, int nOut) { double fanIn = -4 * Math.sqrt(6. / (nOut + nIn)); return uniform(rng, fanIn); } public static RealDistribution uniform(RandomGenerator rng) { double fanIn = 0.1; return new UniformRealDistribution(rng, -fanIn, fanIn); } }