List of usage examples for org.apache.commons.math3.distribution PoissonDistribution sample
@Override public int sample()
From source file:DecorateMutationsSNP.java
/** * The procedure computes for each edge of the ARG the number of mutations occurred in that period of time (length of the edge) by means of Poisson and Normal distributions * @param mu real value that is SNP mutation rate * @param arg instance of the class PopulationARG representing the ARG of a single population * @throws ErrorTimesInputFileException this exception is thrown if there is an inconsistency in time parameters setting * @see PopulationARG/*from w w w .j a va2 s .c om*/ */ public static void decoratingWithMutations(double mu, PopulationARG arg) throws ErrorTimesInputFileException { //For each leaf of arg create a set of mutations in the node for (int j = 0; j < arg.getExtantUnits(); j++) { arg.getNodeSet().get(j).setLeaf(true); arg.getNodeSet().get(j).setMutation_set(new TreeSet<Double>()); } Iterator<Integer> it_edges = arg.getGraphEdges().keySet().iterator(); double lamda = 0; //System.out.println("------- SNPs Decoration of Population "+arg.getId_pop()+" ------------ "); //For each edge in the ARG, compute the number of mutations and decorate the solids while (it_edges.hasNext()) { Integer keyE = it_edges.next(); if (arg.getGraphEdges().get(keyE).getId_fath() != -1) { double density = arg.getGraphEdges().get(keyE).computeDensity(); double time = arg.getGraphEdges().get(keyE).getTime(); //*** if time is negative then throw the exception that //there is probably an error in how the time parameters are specified in the input file ***// if (time <= 0) { throw new ErrorTimesInputFileException(); } //System.out.println("Time: "+arg.getGraphEdges().get(keyE).getTime()); double n = arg.getG() * density; double p = mu * arg.getN() * time; //******* Compute the # of mutations by Poisson Distribution ****** lamda = n * p; PoissonDistribution Prand = new PoissonDistribution(lamda); int randomPois = Prand.sample(); arg.getGraphEdges().get(keyE).setnMut(randomPois); if (randomPois == 0) GenerateAdmixturePopulations .setNum_edges_without_mut(GenerateAdmixturePopulations.getNum_edges_without_mut() + 1); else GenerateAdmixturePopulations.setNum_edges_more_than_one_mut( GenerateAdmixturePopulations.getNum_edges_more_than_one_mut() + 1); //For each mutation we assign a position for (int i = 0; i < arg.getGraphEdges().get(keyE).getnMut(); i++) { //maps the real solids in (0,1) in an interval (0,densityTotal) //by creating a new list of segments. double start = 0; ArrayList<Interval> tempSegs = new ArrayList<Interval>(); for (int j = 0; j < arg.getGraphEdges().get(keyE).getSegments().size(); j++) { double lengthInt = arg.getGraphEdges().get(keyE).getSegments().get(j).getEnd() - arg.getGraphEdges().get(keyE).getSegments().get(j).getStart(); tempSegs.add(new Interval(start, start + lengthInt)); start = start + lengthInt; } double x = Math.random() * arg.getGraphEdges().get(keyE).getDensity(); boolean found = false; int index = 0; while (!found && index < tempSegs.size()) { if (x >= tempSegs.get(index).getStart() && x < tempSegs.get(index).getEnd()) { found = true; } else { index++; } } double offset = x - tempSegs.get(index).getStart(); double posMutation = arg.getGraphEdges().get(keyE).getSegments().get(index).getStart() + offset; arg.getGraphEdges().get(keyE).getSegments().get(index).getPosMutations().add(posMutation); //add the mutation to the set of the all mutations GenerateAdmixturePopulations.getAllMutations().add(new Double(posMutation)); //Create the mutation object Mutation mut = new Mutation(); mut.setPositionID(posMutation); mut.setIDfather(arg.getGraphEdges().get(keyE).getId_fath()); mut.setIDson(arg.getGraphEdges().get(keyE).getId_son()); mut.setSegment(arg.getGraphEdges().get(keyE).getSegments().get(index)); mut.setLeaves(computeLeaves(arg.getGraphEdges().get(keyE).getId_son(), arg)); mut.setID_original_pop(arg.getId_pop()); arg.getSNPpositionsList().add(posMutation); arg.getMutationSet().put(posMutation, mut); GenerateAdmixturePopulations.getAllmutationSet().put(mut.getPositionID(), mut); //For each leaf (in the set), that has the mutation mut, add mut to the leaf TreeSet<Integer> leaves = mut.getLeaves(); Iterator<Integer> it_leaves = leaves.iterator(); while (it_leaves.hasNext()) { int l = it_leaves.next(); arg.getNodeSet().get(l).getMutation_set().add(mut.getPositionID()); } } //end of computing the exact position of each mutation } //end if the edge has a father } //end while for each edge of the ARG //System.out.println("------- End of SNPs Decoration of Population "+arg.getId_pop()+" ------------ "); }
From source file:com.cloudera.oryx.ml.serving.als.model.LoadTestALSModelFactory.java
public static ALSServingModel buildTestModel() { log.info("Building load test model..."); System.gc();/*from w ww . j a va 2s. c o m*/ long startMemory = JVMUtils.getUsedMemory(); RandomGenerator random = RandomManager.getRandom(); PoissonDistribution itemPerUserDist = new PoissonDistribution(random, AVG_ITEMS_PER_USER, PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS); ALSServingModel model = new ALSServingModel(FEATURES, true); long totalEntries = 0; for (int user = 0; user < USERS; user++) { String userID = "U" + user; model.setUserVector(userID, randomVector(random)); int itemsPerUser = itemPerUserDist.sample(); totalEntries += itemsPerUser; Collection<String> knownIDs = new ArrayList<>(itemsPerUser); for (int i = 0; i < itemsPerUser; i++) { knownIDs.add("I" + random.nextInt(ITEMS)); } model.addKnownItems(userID, knownIDs); } for (int item = 0; item < ITEMS; item++) { model.setItemVector("I" + item, randomVector(random)); } System.gc(); long endMemory = JVMUtils.getUsedMemory(); log.info("Built model over {} users, {} items, {} features, {} entries, using {}MB", USERS, ITEMS, FEATURES, totalEntries, (endMemory - startMemory) / 1_000_000); return model; }
From source file:com.cloudera.oryx.app.serving.als.model.LoadTestALSModelFactory.java
public static ALSServingModel buildTestModel() { log.info("Building load test model..."); System.gc();/*from w w w. j a v a 2 s . c o m*/ long startMemory = JVMUtils.getUsedMemory(); ALSServingModel model = new ALSServingModel(FEATURES, true, LSH_SAMPLE_RATE, new TestALSRescorerProvider()); AtomicLong totalEntries = new AtomicLong(); int numCores = Runtime.getRuntime().availableProcessors(); log.info("Adding {} users", USERS); AtomicInteger userCount = new AtomicInteger(); ExecUtils.doInParallel(numCores, i -> { RandomGenerator random = RandomManager.getRandom(((long) i << 32) ^ System.nanoTime()); PoissonDistribution itemPerUserDist = new PoissonDistribution(random, AVG_ITEMS_PER_USER, PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS); for (int user = userCount.getAndIncrement(); user < USERS; user = userCount.getAndIncrement()) { String userID = "U" + user; model.setUserVector(userID, VectorMath.randomVectorF(FEATURES, random)); int itemsPerUser = itemPerUserDist.sample(); totalEntries.addAndGet(itemsPerUser); Collection<String> knownIDs = new ArrayList<>(itemsPerUser); for (int item = 0; item < itemsPerUser; item++) { knownIDs.add("I" + random.nextInt(ITEMS)); } model.addKnownItems(userID, knownIDs); } }); log.info("Adding {} items", ITEMS); AtomicInteger itemCount = new AtomicInteger(); ExecUtils.doInParallel(numCores, i -> { RandomGenerator random = RandomManager.getRandom(((long) i << 32) ^ System.nanoTime()); for (int item = itemCount.getAndIncrement(); item < ITEMS; item = itemCount.getAndIncrement()) { model.setItemVector("I" + item, VectorMath.randomVectorF(FEATURES, random)); } }); System.gc(); long endMemory = JVMUtils.getUsedMemory(); log.info("Built model over {} users, {} items, {} features, {} entries, using {}MB", USERS, ITEMS, FEATURES, totalEntries, (endMemory - startMemory) / 1_000_000); log.info("Model: {}", model); return model; }
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 w ww . jav a 2 s .c o m*/ 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++; } } }
From source file:com.cloudera.oryx.app.serving.als.model.ALSServingModelTest.java
@Test public void testLSHEffect() { RandomGenerator random = RandomManager.getRandom(); PoissonDistribution itemPerUserDist = new PoissonDistribution(random, 20, PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS); int features = 20; ALSServingModel mainModel = new ALSServingModel(features, true, 1.0, null); ALSServingModel lshModel = new ALSServingModel(features, true, 0.5, null); int userItemCount = 20000; for (int user = 0; user < userItemCount; user++) { String userID = "U" + user; float[] vec = VectorMath.randomVectorF(features, random); mainModel.setUserVector(userID, vec); lshModel.setUserVector(userID, vec); int itemsPerUser = itemPerUserDist.sample(); Collection<String> knownIDs = new ArrayList<>(itemsPerUser); for (int i = 0; i < itemsPerUser; i++) { knownIDs.add("I" + random.nextInt(userItemCount)); }//from w w w. j a v a 2 s. c o m mainModel.addKnownItems(userID, knownIDs); lshModel.addKnownItems(userID, knownIDs); } for (int item = 0; item < userItemCount; item++) { String itemID = "I" + item; float[] vec = VectorMath.randomVectorF(features, random); mainModel.setItemVector(itemID, vec); lshModel.setItemVector(itemID, vec); } int numRecs = 10; Mean meanMatchLength = new Mean(); for (int user = 0; user < userItemCount; user++) { String userID = "U" + user; List<Pair<String, Double>> mainRecs = mainModel .topN(new DotsFunction(mainModel.getUserVector(userID)), null, numRecs, null) .collect(Collectors.toList()); List<Pair<String, Double>> lshRecs = lshModel .topN(new DotsFunction(lshModel.getUserVector(userID)), null, numRecs, null) .collect(Collectors.toList()); int i = 0; while (i < lshRecs.size() && i < mainRecs.size() && lshRecs.get(i).equals(mainRecs.get(i))) { i++; } meanMatchLength.increment(i); } log.info("Mean matching prefix: {}", meanMatchLength.getResult()); assertGreaterOrEqual(meanMatchLength.getResult(), 4.0); meanMatchLength.clear(); for (int item = 0; item < userItemCount; item++) { String itemID = "I" + item; List<Pair<String, Double>> mainRecs = mainModel .topN(new CosineAverageFunction(mainModel.getItemVector(itemID)), null, numRecs, null) .collect(Collectors.toList()); List<Pair<String, Double>> lshRecs = lshModel .topN(new CosineAverageFunction(lshModel.getItemVector(itemID)), null, numRecs, null) .collect(Collectors.toList()); int i = 0; while (i < lshRecs.size() && i < mainRecs.size() && lshRecs.get(i).equals(mainRecs.get(i))) { i++; } meanMatchLength.increment(i); } log.info("Mean matching prefix: {}", meanMatchLength.getResult()); assertGreaterOrEqual(meanMatchLength.getResult(), 5.0); }
From source file:ml.shifu.shifu.core.dtrain.lr.LogisticRegressionWorker.java
protected float sampleWeights(float label) { float sampleWeights = 1f; // sample negative or kFoldCV, sample rate is 1d double sampleRate = (modelConfig.getTrain().getSampleNegOnly() || this.isKFoldCV) ? 1d : modelConfig.getTrain().getBaggingSampleRate(); int classValue = (int) (label + 0.01f); if (!modelConfig.isBaggingWithReplacement()) { Random random = null;//from w w w. ja va 2s. com if (this.isStratifiedSampling) { random = baggingRandomMap.get(classValue); if (random == null) { random = DTrainUtils.generateRandomBySampleSeed(modelConfig.getTrain().getBaggingSampleSeed(), CommonConstants.NOT_CONFIGURED_BAGGING_SEED); baggingRandomMap.put(classValue, random); } } else { random = baggingRandomMap.get(0); if (random == null) { random = DTrainUtils.generateRandomBySampleSeed(modelConfig.getTrain().getBaggingSampleSeed(), CommonConstants.NOT_CONFIGURED_BAGGING_SEED); baggingRandomMap.put(0, random); } } if (random.nextDouble() <= sampleRate) { sampleWeights = 1f; } else { sampleWeights = 0f; } } else { // bagging with replacement sampling in training data set, take PoissonDistribution for sampling with // replacement if (this.isStratifiedSampling) { PoissonDistribution rng = this.baggingRngMap.get(classValue); if (rng == null) { rng = new PoissonDistribution(sampleRate); this.baggingRngMap.put(classValue, rng); } sampleWeights = rng.sample(); } else { PoissonDistribution rng = this.baggingRngMap.get(0); if (rng == null) { rng = new PoissonDistribution(sampleRate); this.baggingRngMap.put(0, rng); } sampleWeights = rng.sample(); } } return sampleWeights; }
From source file:gdsc.smlm.ij.plugins.EMGainAnalysis.java
/** * Randomly generate a histogram from poisson-gamma-gaussian samples * //from www .j av a2 s . c o m * @return The histogram */ private int[] simulateFromPoissonGammaGaussian() { // Randomly sample RandomGenerator random = new Well44497b(System.currentTimeMillis() + System.identityHashCode(this)); PoissonDistribution poisson = new PoissonDistribution(random, _photons, PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS); CustomGammaDistribution gamma = new CustomGammaDistribution(random, _photons, _gain, GammaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); final int steps = simulationSize; int[] sample = new int[steps]; for (int n = 0; n < steps; n++) { if (n % 64 == 0) IJ.showProgress(n, steps); // Poisson double d = poisson.sample(); // Gamma if (d > 0) { gamma.setShapeUnsafe(d); d = gamma.sample(); } // Gaussian d += _noise * random.nextGaussian(); // Convert the sample to a count sample[n] = (int) Math.round(d + _bias); } int max = Maths.max(sample); int[] h = new int[max + 1]; for (int s : sample) h[s]++; return h; }
From source file:gdsc.smlm.ij.plugins.CreateData.java
private float[] createBackground(RandomDataGenerator random) { float[] pixels2 = null; if (settings.background > 0) { if (random == null) random = new RandomDataGenerator(); createBackgroundPixels();/*from w w w . j av a2 s. c o m*/ pixels2 = Arrays.copyOf(backgroundPixels, backgroundPixels.length); // Add Poisson noise. if (uniformBackground) { // We can do N random samples thus ensuring the background average is constant. // Note: The number of samples must be Poisson distributed. final int samples = (int) random.nextPoisson(pixels2[0] * pixels2.length); // Only do sampling if the number of samples is valid if (samples >= 1) { pixels2 = new float[pixels2.length]; final int upper = pixels2.length - 1; for (int i = 0; i < samples; i++) { pixels2[random.nextInt(0, upper)] += 1; } } else { // If using a uniform illumination then we can use a fixed Poisson distribution PoissonDistribution dist = new PoissonDistribution(random.getRandomGenerator(), pixels2[0], PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS); for (int i = 0; i < pixels2.length; i++) { pixels2[i] = dist.sample(); } } } else { for (int i = 0; i < pixels2.length; i++) { pixels2[i] = random.nextPoisson(pixels2[i]); } } } else { pixels2 = new float[settings.size * settings.size]; } return pixels2; }
From source file:SimpleGui.QueryGenerator.java
/** * Creating specified number (numOfQuerySegments) of sub-queries * This should be first time to keep sub-queries set fixed for all inputs */// w ww .j ava2 s .com public ArrayList<String> generateSubQuerySamples(int numOfQuerySegments) throws IOException { /* File file = new File("//home//santhilata//Dropbox//CacheLearning//QGen//src//main//java//QueryInput" + "//SubQueries.txt"); FileWriter fw = new FileWriter(file); fw.flush(); */ DatabaseDetails[] ddetails = this.getConfiguration().getDistributedEnvironment().getDatabaseSchema(); int querySegmentSample = 0; String tempSTR = ""; ArrayList<String> queries = new ArrayList<>(numOfQuerySegments); createSQSS(); createPQSS(); for (int i = 0; i < numOfQuerySegments; i++) { String query = ""; ArrayList<String> tempListSQS = new ArrayList<>(); int noOfSQSInQuery = 0; // This variable is to select more than one sqss. This is to ensure we get data from multiple databases while (noOfSQSInQuery == 0) { noOfSQSInQuery = new Random().nextInt(ddetails.length) + 1; // upto no.of databases } int seed = new Random().nextInt(8); while (seed == 0) { seed = new Random().nextInt(8); } PoissonDistribution poisson = new PoissonDistribution(noOfSQS / seed); UniformIntegerDistribution uid = new UniformIntegerDistribution(noOfSQS / (seed + 1), noOfSQS / (seed)); //adding SQS in the query for (int j = 0; j < noOfSQSInQuery; j++) { // String tempSTR = sqss.get(poisson.sample()); if (querySegmentSample > sqss.size() - 1) querySegmentSample = 0; else { tempSTR = sqss.get(querySegmentSample); querySegmentSample++; } if (!tempListSQS.contains(tempSTR)) tempListSQS.add(tempSTR); else j--; } for (int j = 0; j < noOfSQSInQuery; j++) { query = query + "<" + tempListSQS.get(j) + ">"; } //till here added sqs in a query int tempAttr = new Random().nextInt(noOfSQSInQuery); //to ensure that first attribute is taken from the chosen subject query segments while ((tempAttr == 0)) { if (noOfSQSInQuery == 1) tempAttr = 1; else tempAttr = new Random().nextInt(noOfSQSInQuery); } String attr1 = ""; if (tempAttr == 1) attr1 = tempListSQS.get(0); else attr1 = tempListSQS.get(tempAttr); String attr2 = attr1 + "*"; // what is this star for? while (attr2.contains(attr1)) { //this is to avoid repetition of attributes in the predicates // System.out.println(attr2 +" "+attr1); PoissonDistribution poisson_PQS = new PoissonDistribution(pqss.size() / seed); int poisson_sample = poisson_PQS.sample(); while (poisson_sample >= pqss.size()) { poisson_sample = poisson_PQS.sample(); } attr2 = pqss.get(poisson_sample); // System.out.println("new attr2 "+attr2); } if (attr2 == null) { attr1 = attr1 + "," + conditionArray[new Random().nextInt(conditionArray.length)]; long cardinality = Math.round(Math.abs(new Random().nextGaussian() * 1000)); attr1 = attr1 + "," + conditionArray[new Random().nextInt(conditionArray.length)] + "-" + cardinality; } String str = "<" + attr1 + attr2 + ">"; query = query + "#" + str; queries.add(query); } /* for (String query: queries ) { fw.append(query+"\n"); } fw.close(); */ this.sub_queries = queries; return queries; }
From source file:SimpleGui.QueryGenerator.java
/** * Returns array of values in a given distribution * @param distribution//from www. ja va2s.c om * @param numQs * @param mean * @param variance * @param exponentialMean * @param u_lower * @param u_upper * @return */ public int[] getDistributionValues(String distribution, int numQs, double mean, double variance, double exponentialMean, int u_lower, int u_upper, double slope, int upperBoundary) { int[] values = new int[numQs]; switch (distribution) { case "Poisson": { RandomEngine engine = new DRand(); Poisson poisson = new Poisson(mean, engine); int poissonObs = poisson.nextInt(); Normal normal = new Normal(mean, variance, engine); for (int i = 0; i < numQs; i++) { double normalObs = normal.nextDouble(); int sample = (int) Math.abs(normalObs); while (sample >= upperBoundary) { normalObs = normal.nextDouble(); sample = (int) Math.abs(normalObs); } values[i] = sample; //System.out.println(values[i]+"from poisson"); //============================================= PoissonDistribution p = new PoissonDistribution(mean); int randomInt = p.sample(); // values[i] = randomInt; } // Arrays.sort(values); break; } case "Random": { Random randomGenerator = new Random(); for (int i = 0; i < numQs; i++) { int randomInt = randomGenerator.nextInt(numQs - 1); values[i] = randomInt + 1; // to avoid '0' } break; } case "Uniform": { for (int i = 0; i < numQs; i++) { UniformIntegerDistribution u = new UniformIntegerDistribution(u_lower, u_upper); int randomInt = u.sample(); while (randomInt >= upperBoundary) { randomInt = u.sample(); } if (randomInt == 0) values[i] = randomInt + 1; // to avoid random '0' else values[i] = randomInt; } break; } case "Exponential": { for (int i = 0; i < numQs; i++) { ExponentialDistribution e = new ExponentialDistribution(exponentialMean); double exponential = e.sample(); while (exponential >= upperBoundary - 1) { exponential = e.sample(); } values[i] = new Double(exponential).intValue() + 1; //to avoid random value '0' } break; } case "Grading": { // y=mx+c. increment and then decrement for positive slopes. int constant = 0; for (int i = 0; i < numQs; i++) { constant += slope; int nextVal = new Double(constant).intValue(); System.out.println(upperBoundary); if (nextVal >= upperBoundary || nextVal < 0) { slope = -1 * slope; i--; } else values[i] = nextVal; } break; } } // Arrays.sort(values); return values; }