List of usage examples for org.apache.commons.math3.distribution NormalDistribution NormalDistribution
public NormalDistribution(double mean, double sd, double inverseCumAccuracy) throws NotStrictlyPositiveException
From source file:fr.inria.zenith.randomWalk.RandomWalkTsG.java
public static String[] randomWalk(int length) { NormalDistribution n = new NormalDistribution(new JDKRandomGenerator(), 0, 1); //mean 0 std 1 variance 1 String[] ts = new String[length]; double[] tstemp = new double[length]; double[] e = new double[length - 1]; for (int i = 0; i < e.length; i++) { e[i] = n.sample();/* w w w . j av a2s . co m*/ } ts[0] = "0.0"; for (int i = 1; i < length; i++) { ts[i] = (tstemp[i] = tstemp[i - 1] + e[i - 1]) + ""; } return ts; }
From source file:com.insightml.models.clusters.GaussianMixtureModelsTest.java
@Test public void test() { final Well19937c rnd = new Well19937c(0); final double[] data = Vectors.append(new NormalDistribution(rnd, 10, 5).sample(50), new NormalDistribution(rnd, 4000, 100).sample(75)); final Point[] points = new Point[data.length]; for (int i = 0; i < points.length; ++i) { points[i] = new Point(data[i]); }// www .j ava2 s . c o m final Triple<IContDistribution[], double[], double[][]> out = new GaussianMixtureModels().run(points, 2, 10); final Components result = new Components(out.getFirst(), out.getSecond()); System.err.println(result); Assert.assertEquals(-685.3993, result.incompleteLogLikelihood(data), 0.0001); Assert.assertEquals(-690.2276, result.bic(data), 0.0001); }
From source file:com.vsthost.rnd.jdeoptim.evolution.strategies.SandboxStrategy.java
@Override public void regenerate(Population population, Problem problem, Objective objective) { // Get the best member of the population: final double[] bestMember = population.getBestMember(); // Define the new population data and scores as we don't want to override old one within the loop: final double[][] newPopData = new double[population.getSize()][]; final double[] newPopScores = new double[population.getSize()]; final boolean[] newPopFlags = new boolean[population.getSize()]; // Iterate over the current population: for (int c = 0; c < population.getSize(); c++) { // Are we going to adjust CR and F? if (this.c > 0) { // Yes. We will not adjust the CR first: this.cr = new NormalDistribution(this.randomGenerator, this.meanCR, 0.1).sample(); // Check and reset CR: this.cr = this.cr > 1 ? 1 : (this.cr < 0 ? 0 : this.cr); // OK, now we will adjust F: do {/*from w w w . j a v a 2 s. co m*/ // Get the new F: this.f = new CauchyDistribution(this.randomGenerator, this.meanF, 0.1).sample(); // Check and reset F if required: this.f = this.f > 1 ? 1 : this.f; } while (this.f <= 0); } // Get the candidate as the base of the next candidate (a.k.a. trial): final double[] trial = population.getMemberCopy(c); // Get the score of the candidate: final double oldScore = population.getScore(c); // Get 2 random member indices from the population which are distinct: int[] randomMembers = Utils.pickRandom(Utils.sequence(population.getSize()), 2, new int[] { c }, this.randomGenerator); // Get the random members: final double[] randomMember1 = population.getMember(randomMembers[0]); final double[] randomMember2 = population.getMember(randomMembers[1]); // Iterate over all member elements and do the trick: for (int i = 0; i < population.getDimension(); i++) { // Any manipulation? if (probability.sample() < this.cr) { // Yes, we will proceed with a change: trial[i] = bestMember[i] + this.f * (probability.sample() + 0.0001) * (randomMember1[i] - randomMember2[i]); } } // Apply limits in case that we have violated: for (int i = 0; i < trial.length; i++) { // Check lower limit: if (trial[i] < problem.getLower()[i]) { trial[i] = problem.getLower()[i]; } // Check upper limit: else if (trial[i] > problem.getUpper()[i]) { trial[i] = problem.getUpper()[i]; } } // OK, we are done with the trial. We will now check if we have a // better candidate. If yes, we will replace the old member with the trial, // if not we will just skip. Compute the score: final double newScore = objective.apply(trial); // Check the new score against the old one and act accordingly: if (newScore < oldScore) { // Yes, our trial is a better candidate. Replace: newPopData[c] = trial; newPopScores[c] = newScore; newPopFlags[c] = true; // We will now re-adjust for CR and F. this.goodCR += this.cr / ++this.goodNPCount; this.goodF += this.f; this.goodF2 += Math.pow(this.f, 2); } else { newPopFlags[c] = false; } // Re-compute mean CR and F if required: if (this.c > 0 && this.goodF != 0) { this.meanCR = (1 - this.c) * this.meanCR + this.c * this.goodCR; this.meanF = (1 - this.c) * this.meanF + this.c * this.goodF2 / this.goodF; } } // Now, override the population: for (int i = 0; i < population.getSize(); i++) { if (newPopFlags[i]) { population.setMember(i, newPopData[i], newPopScores[i]); } } }
From source file:com.vsthost.rnd.jdeoptim.evolution.strategies.Strategy3.java
@Override public void regenerate(Population population, Problem problem, Objective objective) { // Setup a uniform integer distribution for candidate element selection: UniformIntegerDistribution elementSampling = new UniformIntegerDistribution(this.randomGenerator, 0, population.getDimension() - 1); // Get the best member of the population: final double[] bestMember = population.getBestMember(); // Define the new population data and scores as we don't want to override old one within the loop: final double[][] newPopData = new double[population.getSize()][]; final double[] newPopScores = new double[population.getSize()]; final boolean[] newPopFlags = new boolean[population.getSize()]; // Iterate over the current population: for (int c = 0; c < population.getSize(); c++) { // Are we going to adjust CR and F? if (this.c > 0) { // Yes. We will not adjust the CR first: this.cr = new NormalDistribution(this.randomGenerator, this.meanCR, 0.1).sample(); // Check and reset CR: this.cr = this.cr > 1 ? 1 : (this.cr < 0 ? 0 : this.cr); // OK, now we will adjust F: do {/*from www . ja va2s .co m*/ // Get the new F: this.f = new CauchyDistribution(this.randomGenerator, this.meanF, 0.1).sample(); // Check and reset F if required: this.f = this.f > 1 ? 1 : this.f; } while (this.f <= 0.0); } // Get the candidate as the base of the next candidate (a.k.a. trial): final double[] trial = population.getMemberCopy(c); // Get the score of the candidate: final double oldScore = population.getScore(c); // Get two random candidate indices: final int[] randomCandidates = Utils.pickRandom(Utils.sequence(population.getSize()), 2, new int[] { c }, this.randomGenerator); // Get the index of element of candidate to start with: int j = elementSampling.sample(); // Set the counter for preventing overflowing dimension: int k = 0; // Iterate and set elements: do { // Get the jitter: final double jitter = (probability.sample() * this.jitterFactor) + this.f; // Get the respective element of the best candidate: final double bestest = bestMember[j]; // Get the random candidate elements: final double random1 = population.getMember(randomCandidates[0])[j]; final double random2 = population.getMember(randomCandidates[1])[j]; // Override trial: trial[j] = bestest + jitter * (random1 - random2); // Move to the next element: j = (j + 1) % population.getDimension(); // Increment k: k++; } while (probability.sample() < this.cr && k < population.getDimension()); // We have an interim trial. We will now truncate: for (int i = 0; i < trial.length; i++) { // Are we truncating? if (precision == 0) { continue; } // OK, truncate: trial[i] = DMatrixUtils.roundDoubleToClosest(trial[i], this.precision); } // Apply limits in case that we have violated: for (int i = 0; i < trial.length; i++) { // Check lower limit: if (trial[i] < problem.getLower()[i]) { if (this.bounceBack) { trial[i] = problem.getLower()[i] + probability.sample() * (problem.getUpper()[i] - problem.getLower()[i]); } else { trial[i] = problem.getLower()[i]; } } // Check upper limit: if (trial[i] > problem.getUpper()[i]) { if (this.bounceBack) { trial[i] = problem.getUpper()[i] - probability.sample() * (problem.getUpper()[i] - problem.getLower()[i]); } else { trial[i] = problem.getUpper()[i]; } } } // We will now check if we have a // better candidate. If yes, we will replace the old member with the trial, // if not we will just skip. Compute the score: final double newScore = objective.apply(trial); // Check the new score against the old one and act accordingly: if (newScore < oldScore) { // Yes, our trial is a better candidate. Replace: newPopData[c] = trial; newPopScores[c] = newScore; newPopFlags[c] = true; // We will now re-adjust for CR and F. this.goodCR += this.cr / ++this.goodNPCount; this.goodF += this.f; this.goodF2 += Math.pow(this.f, 2); } else { newPopFlags[c] = false; } } // Re-compute mean CR and F if required: if (this.c > 0 && this.goodF != 0) { this.meanCR = (1 - this.c) * this.meanCR + this.c * this.goodCR; this.meanF = (1 - this.c) * this.meanF + this.c * this.goodF2 / this.goodF; } // Now, override the population: for (int i = 0; i < population.getSize(); i++) { if (newPopFlags[i]) { population.setMember(i, newPopData[i], newPopScores[i]); } } }
From source file:edu.cmu.tetrad.util.RandomUtil.java
/** * Sets the seed to the given value.//from w w w . ja v a 2 s . com * * @param seed A long value. Once this seed is set, the behavior of the random number generator is deterministic, so * setting the seed can be used to repeat previous behavior. */ public void setSeed(long seed) { // Apache offers several random number generators; picking one that works pretty well. randomGenerator = new Well44497b(seed); normal = new NormalDistribution(randomGenerator, 0, 1); this.seed = seed; }
From source file:edu.cmu.tetrad.sem.LargeScaleSimulation.java
/** * This simulates data by picking random values for the exogenous terms and * percolating this information down through the SEM, assuming it is * acyclic. Works, but will hang for cyclic models, and is very slow for * large numbers of variables (probably due to the heavyweight lookups of * various values--could be improved). The model must be acyclic, or else * this will spin.//from w ww. j a v a 2 s . co m */ public DataSet simulateDataRecursive(int sampleSize) { if (tierIndices == null) { List<Node> nodes = graph.getNodes(); tierIndices = new int[nodes.size()]; for (int j = 0; j < nodes.size(); j++) { tierIndices[j] = j; } } int size = variableNodes.size(); setupModel(size); class SimulateTask extends RecursiveTask<Boolean> { private final int from; private final int to; private double[][] all; private int chunk; public SimulateTask(int from, int to, double[][] all, int chunk) { this.from = from; this.to = to; this.all = all; this.chunk = chunk; } @Override protected Boolean compute() { if (from - to > chunk) { int mid = from + to / 2; SimulateTask left = new SimulateTask(from, mid, all, chunk); SimulateTask right = new SimulateTask(mid, to, all, chunk); left.fork(); right.compute(); left.join(); return true; } else { for (int i = from; i < to; i++) { NormalDistribution normal = new NormalDistribution(new Well1024a(++seed), 0, 1);//sqrt(errorVars[col])); normal.sample(); if (verbose && (i + 1) % 50 == 0) System.out.println("Simulating " + (i + 1)); for (int col : tierIndices) { double value = normal.sample() * sqrt(errorVars[col]); for (int j = 0; j < parents[col].length; j++) { value += all[parents[col][j]][i] * coefs[col][j]; } value += means[col]; all[col][i] = value; } } return true; } } } if (graph instanceof TimeLagGraph) { sampleSize += 200; } double[][] all = new double[variableNodes.size()][sampleSize]; int chunk = sampleSize / ForkJoinPoolInstance.getInstance().getPool().getParallelism() + 1; ForkJoinPoolInstance.getInstance().getPool().invoke(new SimulateTask(0, sampleSize, all, chunk)); if (graph instanceof TimeLagGraph) { int[] rem = new int[200]; for (int i = 0; i < 200; ++i) { rem[i] = i; } BoxDataSet dat = new BoxDataSet(new VerticalDoubleDataBox(all), variableNodes); dat.removeRows(rem); return dat; } return new BoxDataSet(new VerticalDoubleDataBox(all), variableNodes); }
From source file:edu.cmu.tetrad.util.RandomUtil.java
/** * Returns Normal PDF value for a normal with the given mean and standard deviation. * * @param mean The mean of the normal to be used. * @param sd The standard deviation of the normal to be used. * @param value The domain value for the PDF. * @return Ibid./*from www . j a v a2s . co m*/ */ public double normalPdf(double mean, double sd, double value) { return new NormalDistribution(randomGenerator, mean, sd).density(value); }
From source file:MannWhitneyUTest.java
/** * @param Umin/*from ww w . jav a2 s . com*/ * smallest Mann-Whitney U value * @param n1 * number of subjects in first sample * @param n2 * number of subjects in second sample * @return two-sided asymptotic p-value * @throws ConvergenceException * if the p-value can not be computed due to a convergence error * @throws MaxCountExceededException * if the maximum number of iterations is exceeded */ private double calculateAsymptoticPValue(final double Umin, final int n1, final int n2) throws ConvergenceException, MaxCountExceededException { System.out.println("6"); /* * long multiplication to avoid overflow (double not used due to * efficiency and to avoid precision loss) */ final long n1n2prod = (long) n1 * n2; // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation final double EU = n1n2prod / 2.0; final double VarU = n1n2prod * (n1 + n2 + 1) / 12.0; final double z = (Umin - EU) / FastMath.sqrt(VarU); // No try-catch or advertised exception because args are valid // pass a null rng to avoid unneeded overhead as we will not sample from // this distribution final NormalDistribution standardNormal = new NormalDistribution(null, 0, 1); return 2 * standardNormal.cumulativeProbability(z); }
From source file:edu.cmu.tetrad.sem.LargeScaleSimulation.java
/** * Simulates data using the model X = (I - B)Y^-1 * e. Errors are uncorrelated. * * @param sampleSize The nubmer of samples to draw. */// ww w . j a va 2s .c o m public DataSet simulateDataReducedForm(int sampleSize) { if (sampleSize < 1) throw new IllegalArgumentException("Sample size must be >= 1: " + sampleSize); int size = variableNodes.size(); setupModel(size); NormalDistribution normal = new NormalDistribution(new Well1024a(++seed), 0, 1); TetradMatrix B = new TetradMatrix(getCoefficientMatrix()); TetradMatrix iMinusBInv = TetradAlgebra.identity(B.rows()).minus(B).inverse(); double[][] all = new double[variableNodes.size()][sampleSize]; for (int row = 0; row < sampleSize; row++) { TetradVector e = new TetradVector(B.rows()); for (int j = 0; j < e.size(); j++) { e.set(j, normal.sample() * sqrt(errorVars[j])); } TetradVector x = iMinusBInv.times(e); for (int j = 0; j < x.size(); j++) { all[j][row] = x.get(j); } } List<Node> continuousVars = new ArrayList<>(); for (Node node : getVariableNodes()) { final ContinuousVariable var = new ContinuousVariable(node.getName()); var.setNodeType(node.getNodeType()); continuousVars.add(var); } BoxDataSet boxDataSet = new BoxDataSet(new VerticalDoubleDataBox(all), continuousVars); return DataUtils.restrictToMeasured(boxDataSet); }
From source file:hivemall.anomaly.ChangeFinder2DTest.java
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);/*from ww w .j av a2 s.c om*/ 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++; } } }