Java Code Examples for com.google.common.primitives.Doubles#toArray()
The following examples show how to use
com.google.common.primitives.Doubles#toArray() .
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: IntensityPlotDataset.java From mzmine3 with GNU General Public License v2.0 | 6 votes |
public Number getStdDevValue(int row, int column) { Feature[] peaks = getPeaks(xValues[column], selectedRows[row]); // if we have only 1 peak, there is no standard deviation if (peaks.length == 1) return 0; HashSet<Double> values = new HashSet<Double>(); for (int i = 0; i < peaks.length; i++) { if (peaks[i] == null) continue; if (yAxisValueSource == YAxisValueSource.HEIGHT) values.add(peaks[i].getHeight()); if (yAxisValueSource == YAxisValueSource.AREA) values.add(peaks[i].getArea()); if (yAxisValueSource == YAxisValueSource.RT) values.add(peaks[i].getRT()); } double doubleValues[] = Doubles.toArray(values); double std = MathUtils.calcStd(doubleValues); return std; }
Example 2
Source File: MutableDistribution.java From java-monitoring-client-library with Apache License 2.0 | 6 votes |
/** Constructs an empty Distribution with the specified {@link DistributionFitter}. */ public MutableDistribution(DistributionFitter distributionFitter) { this.distributionFitter = checkNotNull(distributionFitter); ImmutableSortedSet<Double> boundaries = distributionFitter.boundaries(); checkArgument(boundaries.size() > 0); checkArgument(Ordering.natural().isOrdered(boundaries)); this.intervalCounts = TreeRangeMap.create(); double[] boundariesArray = Doubles.toArray(distributionFitter.boundaries()); // Add underflow and overflow intervals this.intervalCounts.put(Range.lessThan(boundariesArray[0]), 0L); this.intervalCounts.put(Range.atLeast(boundariesArray[boundariesArray.length - 1]), 0L); // Add finite intervals for (int i = 1; i < boundariesArray.length; i++) { this.intervalCounts.put(Range.closedOpen(boundariesArray[i - 1], boundariesArray[i]), 0L); } }
Example 3
Source File: ConvertCollectionToArray.java From levelup-java-examples with Apache License 2.0 | 6 votes |
@Test public void convert_collection_of_objects_to_primitive_array_with_guava () { List<Double> searchEngineMarketShare = Lists.newArrayList(); searchEngineMarketShare.add(67.1); searchEngineMarketShare.add(16.9); searchEngineMarketShare.add(11.8); searchEngineMarketShare.add(2.7); searchEngineMarketShare.add(1.6); double[] searchEngineMarketShareArray = Doubles.toArray(searchEngineMarketShare); logger.info(Arrays.toString(searchEngineMarketShareArray)); assertEquals(5, searchEngineMarketShareArray.length); }
Example 4
Source File: SliceSamplerUnitTest.java From gatk-protected with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Test slice sampling of a normal distribution. Checks that input mean and standard deviation are recovered * by 10000 samples to a relative error of 0.5% and 2%, respectively. */ @Test public void testSliceSamplingOfNormalDistribution() { rng.setSeed(RANDOM_SEED); final double mean = 5.; final double standardDeviation = 0.75; final NormalDistribution normalDistribution = new NormalDistribution(mean, standardDeviation); final Function<Double, Double> normalLogPDF = normalDistribution::logDensity; final double xInitial = 1.; final double xMin = Double.NEGATIVE_INFINITY; final double xMax = Double.POSITIVE_INFINITY; final double width = 0.5; final int numSamples = 10000; final SliceSampler normalSampler = new SliceSampler(rng, normalLogPDF, xMin, xMax, width); final double[] samples = Doubles.toArray(normalSampler.sample(xInitial, numSamples)); final double sampleMean = new Mean().evaluate(samples); final double sampleStandardDeviation = new StandardDeviation().evaluate(samples); Assert.assertEquals(relativeError(sampleMean, mean), 0., 0.005); Assert.assertEquals(relativeError(sampleStandardDeviation, standardDeviation), 0., 0.02); }
Example 5
Source File: SliceSamplerUnitTest.java From gatk with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Tests slice sampling of a monotonic beta distribution as an example of sampling of a bounded random variable. * Checks that input mean and variance are recovered by 10000 samples to a relative error of 0.5% and 2%, * respectively. */ @Test public void testSliceSamplingOfMonotonicBetaDistribution() { rng.setSeed(RANDOM_SEED); final double alpha = 10.; final double beta = 1.; final BetaDistribution betaDistribution = new BetaDistribution(alpha, beta); final Function<Double, Double> betaLogPDF = betaDistribution::logDensity; final double mean = betaDistribution.getNumericalMean(); final double variance = betaDistribution.getNumericalVariance(); final double xInitial = 0.5; final double xMin = 0.; final double xMax = 1.; final double width = 0.1; final int numSamples = 10000; final SliceSampler betaSampler = new SliceSampler(rng, betaLogPDF, xMin, xMax, width); final double[] samples = Doubles.toArray(betaSampler.sample(xInitial, numSamples)); final double sampleMean = new Mean().evaluate(samples); final double sampleVariance = new Variance().evaluate(samples); Assert.assertEquals(relativeError(sampleMean, mean), 0., 0.005); Assert.assertEquals(relativeError(sampleVariance, variance), 0., 0.02); }
Example 6
Source File: ATM.java From TimeIsMoney with GNU General Public License v3.0 | 6 votes |
/** * Creates a new atm instance with the {@link de.Linus122.TimeIsMoney.Main} class. * * @param plugin The {@link de.Linus122.TimeIsMoney.Main} class that implements {@link org.bukkit.plugin.java.JavaPlugin}. */ public ATM(Main plugin) { this.plugin = plugin; plugin.getServer().getPluginManager().registerEvents(this, plugin); plugin.getCommand("atm").setExecutor(this); if (!bankAccountsFile.exists()) { try { bankAccountsFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } bankAccountsConfig = YamlConfiguration.loadConfiguration(bankAccountsFile); worths = Doubles.toArray(Main.finalconfig.getDoubleList("atm_worth_gradation")); }
Example 7
Source File: MinibatchSliceSamplerUnitTest.java From gatk with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Tests slice sampling of a normal posterior = uniform prior x normal likelihood from 1000 data points. * Checks that input mean and standard deviation are recovered from the posterior of the mean parameter * by 500 burn-in samples + 1000 samples to a relative error of 1% and 5%, respectively. */ @Test public void testSliceSamplingOfNormalPosterior() { rng.setSeed(RANDOM_SEED); final double mean = 5.; final double standardDeviation = 0.75; final NormalDistribution normalDistribution = new NormalDistribution(rng, mean, standardDeviation); final BiFunction<Double, Double, Double> normalLogLikelihood = (d, x) -> new NormalDistribution(null, x, standardDeviation).logDensity(d); final List<Double> data = Doubles.asList(normalDistribution.sample(NUM_DATA_POINTS)); final double xInitial = 1.; final double xMin = Double.NEGATIVE_INFINITY; final double xMax = Double.POSITIVE_INFINITY; final double width = 0.5; final int numBurnInSamples = 500; final int numSamples = 1500; final MinibatchSliceSampler<Double> normalSampler = new MinibatchSliceSampler<>( rng, data, UNIFORM_LOG_PRIOR, normalLogLikelihood, xMin, xMax, width, MINIBATCH_SIZE, APPROX_THRESHOLD); final double[] samples = Doubles.toArray(normalSampler.sample(xInitial, numSamples).subList(numBurnInSamples, numSamples)); final double sampleMean = new Mean().evaluate(samples); final double sampleStandardDeviation = new StandardDeviation().evaluate(samples); Assert.assertEquals(relativeError(sampleMean, mean), 0., 0.01); Assert.assertEquals(relativeError(sampleStandardDeviation, standardDeviation / Math.sqrt(NUM_DATA_POINTS)), 0., 0.05); }
Example 8
Source File: Geoshape.java From titan1withtp3.1 with Apache License 2.0 | 5 votes |
private double[] convertCollection(Collection<Object> c) { List<Double> numbers = c.stream().map(o -> { if (!(o instanceof Number)) { throw new IllegalArgumentException("Collections may only contain numbers to create a Geoshape"); } return ((Number) o).doubleValue(); }).collect(Collectors.toList()); return Doubles.toArray(numbers); }
Example 9
Source File: DoubleArray.java From Strata with Apache License 2.0 | 5 votes |
/** * Obtains an instance from a collection of {@code Double}. * <p> * The order of the values in the returned array is the order in which elements are returned * from the iterator of the collection. * * @param collection the collection to initialize from * @return an array containing the values from the collection in iteration order */ public static DoubleArray copyOf(Collection<Double> collection) { if (collection.size() == 0) { return EMPTY; } if (collection instanceof ImmList) { return ((ImmList) collection).underlying; } return new DoubleArray(Doubles.toArray(collection)); }
Example 10
Source File: GibbsSamplerSingleGaussianUnitTest.java From gatk with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Tests Bayesian inference of a Gaussian model via MCMC. Recovery of input values for the variance and mean * global parameters is checked. In particular, the mean and standard deviation of the posteriors for * both parameters must be recovered to within a relative error of 1% and 10%, respectively, in 250 samples * (after 250 burn-in samples have been discarded). */ @Test public void testRunMCMCOnSingleGaussianModel() { //Create new instance of the Modeller helper class, passing all quantities needed to initialize state and data. final GaussianModeller modeller = new GaussianModeller(VARIANCE_INITIAL, MEAN_INITIAL, datapointsList); //Create a GibbsSampler, passing the total number of samples (including burn-in samples) //and the model held by the Modeller. final GibbsSampler<GaussianParameter, ParameterizedState<GaussianParameter>, GaussianDataCollection> gibbsSampler = new GibbsSampler<>(NUM_SAMPLES, modeller.model); //Run the MCMC. gibbsSampler.runMCMC(); //Get the samples of each of the parameter posteriors (discarding burn-in samples) by passing the //parameter name, type, and burn-in number to the getSamples method. final double[] varianceSamples = Doubles.toArray(gibbsSampler.getSamples(GaussianParameter.VARIANCE, Double.class, NUM_BURN_IN)); final double[] meanSamples = Doubles.toArray(gibbsSampler.getSamples(GaussianParameter.MEAN, Double.class, NUM_BURN_IN)); //Check that the statistics---i.e., the means and standard deviations---of the posteriors //agree with those found by emcee/analytically to a relative error of 1% and 10%, respectively. final double variancePosteriorCenter = new Mean().evaluate(varianceSamples); final double variancePosteriorStandardDeviation = new StandardDeviation().evaluate(varianceSamples); Assert.assertEquals(relativeError(variancePosteriorCenter, VARIANCE_TRUTH), 0., RELATIVE_ERROR_THRESHOLD_FOR_CENTERS); Assert.assertEquals( relativeError(variancePosteriorStandardDeviation, VARIANCE_POSTERIOR_STANDARD_DEVIATION_TRUTH), 0., RELATIVE_ERROR_THRESHOLD_FOR_STANDARD_DEVIATIONS); final double meanPosteriorCenter = new Mean().evaluate(meanSamples); final double meanPosteriorStandardDeviation = new StandardDeviation().evaluate(meanSamples); Assert.assertEquals(relativeError(meanPosteriorCenter, MEAN_TRUTH), 0., RELATIVE_ERROR_THRESHOLD_FOR_CENTERS); Assert.assertEquals( relativeError(meanPosteriorStandardDeviation, MEAN_POSTERIOR_STANDARD_DEVIATION_TRUTH), 0., RELATIVE_ERROR_THRESHOLD_FOR_STANDARD_DEVIATIONS); }
Example 11
Source File: GibbsSamplerCopyRatioUnitTest.java From gatk-protected with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * Tests Bayesian inference of a toy copy-ratio model via MCMC. * <p> * Recovery of input values for the variance global parameter and the segment-level mean parameters is checked. * In particular, the mean and standard deviation of the posterior for the variance must be recovered to within * a relative error of 1% and 5%, respectively, in 500 samples (after 250 burn-in samples have been discarded). * </p> * <p> * Furthermore, the number of truth values for the segment-level means falling outside confidence intervals of * 1-sigma, 2-sigma, and 3-sigma given by the posteriors in each segment should be roughly consistent with * a normal distribution (i.e., ~32, ~5, and ~0, respectively; we allow for errors of 10, 5, and 2). * Finally, the mean of the standard deviations of the posteriors for the segment-level means should be * recovered to within a relative error of 5%. * </p> * <p> * With these specifications, this unit test is not overly brittle (i.e., it should pass for a large majority * of randomly generated data sets), but it is still brittle enough to check for correctness of the sampling * (for example, specifying a sufficiently incorrect likelihood will cause the test to fail). * </p> */ @Test public void testRunMCMCOnCopyRatioSegmentedGenome() { //Create new instance of the Modeller helper class, passing all quantities needed to initialize state and data. final CopyRatioModeller modeller = new CopyRatioModeller(VARIANCE_INITIAL, MEAN_INITIAL, COVERAGES_FILE, NUM_TARGETS_PER_SEGMENT_FILE); //Create a GibbsSampler, passing the total number of samples (including burn-in samples) //and the model held by the Modeller. final GibbsSampler<CopyRatioParameter, CopyRatioState, CopyRatioDataCollection> gibbsSampler = new GibbsSampler<>(NUM_SAMPLES, modeller.model); //Run the MCMC. gibbsSampler.runMCMC(); //Check that the statistics---i.e., the mean and standard deviation---of the variance posterior //agree with those found by emcee/analytically to a relative error of 1% and 5%, respectively. final double[] varianceSamples = Doubles.toArray(gibbsSampler.getSamples(CopyRatioParameter.VARIANCE, Double.class, NUM_BURN_IN)); final double variancePosteriorCenter = new Mean().evaluate(varianceSamples); final double variancePosteriorStandardDeviation = new StandardDeviation().evaluate(varianceSamples); Assert.assertEquals(relativeError(variancePosteriorCenter, VARIANCE_TRUTH), 0., RELATIVE_ERROR_THRESHOLD_FOR_CENTERS); Assert.assertEquals(relativeError(variancePosteriorStandardDeviation, VARIANCE_POSTERIOR_STANDARD_DEVIATION_TRUTH), 0., RELATIVE_ERROR_THRESHOLD_FOR_STANDARD_DEVIATIONS); //Check statistics---i.e., the mean and standard deviation---of the segment-level mean posteriors. //In particular, check that the number of segments where the true mean falls outside confidence intervals //is roughly consistent with a normal distribution. final List<Double> meansTruth = loadList(MEANS_TRUTH_FILE, Double::parseDouble); final int numSegments = meansTruth.size(); final List<SegmentMeans> meansSamples = gibbsSampler.getSamples(CopyRatioParameter.SEGMENT_MEANS, SegmentMeans.class, NUM_BURN_IN); int numMeansOutsideOneSigma = 0; int numMeansOutsideTwoSigma = 0; int numMeansOutsideThreeSigma = 0; final List<Double> meanPosteriorStandardDeviations = new ArrayList<>(); for (int segment = 0; segment < numSegments; segment++) { final int j = segment; final double[] meanInSegmentSamples = Doubles.toArray(meansSamples.stream().map(s -> s.get(j)).collect(Collectors.toList())); final double meanPosteriorCenter = new Mean().evaluate(meanInSegmentSamples); final double meanPosteriorStandardDeviation = new StandardDeviation().evaluate(meanInSegmentSamples); meanPosteriorStandardDeviations.add(meanPosteriorStandardDeviation); final double absoluteDifferenceFromTruth = Math.abs(meanPosteriorCenter - meansTruth.get(segment)); if (absoluteDifferenceFromTruth > meanPosteriorStandardDeviation) { numMeansOutsideOneSigma++; } if (absoluteDifferenceFromTruth > 2 * meanPosteriorStandardDeviation) { numMeansOutsideTwoSigma++; } if (absoluteDifferenceFromTruth > 3 * meanPosteriorStandardDeviation) { numMeansOutsideThreeSigma++; } } final double meanPosteriorStandardDeviationsMean = new Mean().evaluate(Doubles.toArray(meanPosteriorStandardDeviations)); Assert.assertEquals(numMeansOutsideOneSigma, 100 - 68, DELTA_NUMBER_OF_MEANS_ALLOWED_OUTSIDE_1_SIGMA); Assert.assertEquals(numMeansOutsideTwoSigma, 100 - 95, DELTA_NUMBER_OF_MEANS_ALLOWED_OUTSIDE_2_SIGMA); Assert.assertTrue(numMeansOutsideThreeSigma <= DELTA_NUMBER_OF_MEANS_ALLOWED_OUTSIDE_3_SIGMA); Assert.assertEquals( relativeError(meanPosteriorStandardDeviationsMean, MEAN_POSTERIOR_STANDARD_DEVIATION_MEAN_TRUTH), 0., RELATIVE_ERROR_THRESHOLD_FOR_STANDARD_DEVIATIONS); }
Example 12
Source File: SegmentMergeUtils.java From gatk-protected with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * Given a segment specified by an index, returns a pair of scores for adjacent segments based on 2-sample * Kolmogorov-Smirnov tests of the observed alternate-allele fractions; except for edge cases, the sum of the * scores will be unity. All segments are assumed to be on the same chromosome. * If any of the three segments is missing SNPs, both scores are Double.NEGATIVE_INFINITY. * @param segments list of segments * @param snps SNP-allele-count data to be segmented * @param index index of the center segment to consider * @return scores for adjacent segments based on 2-sample Kolmogorov-Smirnov tests */ private static Pair<Double, Double> calculateSNPScores(final List<SimpleInterval> segments, final TargetCollection<AllelicCount> snps, final int index) { final SimpleInterval leftSegment = segments.get(index - 1); final SimpleInterval centerSegment = segments.get(index); final SimpleInterval rightSegment = segments.get(index + 1); //check if any segment is missing SNPs if (snps.targetCount(leftSegment) == 0 || snps.targetCount(centerSegment) == 0 || snps.targetCount(rightSegment) == 0) { return Pair.of(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); } //calculate Kolmogorov-Smirnov distances based on alternate-allele-fractions in each segment final double[] leftAAFs = Doubles.toArray(calculateAAFs(leftSegment, snps)); final double[] centerAAFs = Doubles.toArray(calculateAAFs(centerSegment, snps)); final double[] rightAAFs = Doubles.toArray(calculateAAFs(rightSegment, snps)); try { //if not enough alternate-allele fractions in any segment to use Apache Commons implementation of //kolmogorovSmirnovStatistic, kolmogorovSmirnovDistance will throw an unchecked //InsufficientDataException and the code after the catch block will be executed final double leftKSDistance = kolmogorovSmirnovDistance(leftAAFs, centerAAFs); final double rightKSDistance = kolmogorovSmirnovDistance(centerAAFs, rightAAFs); //edge case (divide-by-zero) if (leftKSDistance == 0. && rightKSDistance == 0.) { //this is unlikely to occur using the Apache Commons implementation of kolmogorovSmirnovStatistic return Pair.of(0.5, 0.5); } //if alternate-allele fractions in all segments are too similar or do not overlap appreciably, //will not return anything here and the code after the catch block will be executed if (Math.abs(leftKSDistance - rightKSDistance) > SNP_KOLMOGOROV_SMIRNOV_DISTANCE_DIFFERENCE_THRESHOLD && (leftKSDistance < SNP_KOLMOGOROV_SMIRNOV_DISTANCE_THRESHOLD || rightKSDistance < SNP_KOLMOGOROV_SMIRNOV_DISTANCE_THRESHOLD)) { final double leftSqrtN = sqrtN(leftAAFs, centerAAFs); final double rightSqrtN = sqrtN(centerAAFs, rightAAFs); return Pair.of( 1. - leftKSDistance * leftSqrtN / (leftKSDistance * leftSqrtN + rightKSDistance * rightSqrtN), 1. - rightKSDistance * rightSqrtN / (leftKSDistance * leftSqrtN + rightKSDistance * rightSqrtN)); } } catch (final InsufficientDataException e) { //do nothing here, continue below to use Hodges-Lehmann scores instead } //use Hodges-Lehmann scores computed using inverse minor-allele fractions //(which, ideally, are proportional to total copy ratio) //will be executed after an InsufficientDataException or alternate-allele fractions in all segments //are too similar or do not overlap appreciably final double[] leftInverseMAFs = Doubles.toArray(calculateInverseMAFs(leftSegment, snps)); final double[] centerInverseMAFs = Doubles.toArray(calculateInverseMAFs(centerSegment, snps)); final double[] rightInverseMAFs = Doubles.toArray(calculateInverseMAFs(rightSegment, snps)); return calculateHodgesLehmannScores(leftInverseMAFs, centerInverseMAFs, rightInverseMAFs); }
Example 13
Source File: TreePredictor.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
public double[] getThreshold(){ return Doubles.toArray(getNodeAttribute("threshold")); }
Example 14
Source File: CVDataset.java From mzmine2 with GNU General Public License v2.0 | 4 votes |
public CVDataset(PeakList alignedPeakList, ParameterSet parameters) { int numOfRows = alignedPeakList.getNumberOfRows(); RawDataFile selectedFiles[] = parameters.getParameter(CVParameters.dataFiles).getValue(); PeakMeasurementType measurementType = parameters.getParameter(CVParameters.measurementType).getValue(); // Generate title for the dataset datasetTitle = "Correlation of variation analysis"; datasetTitle = datasetTitle.concat(" ("); if (measurementType == PeakMeasurementType.AREA) datasetTitle = datasetTitle.concat("CV of peak areas"); else datasetTitle = datasetTitle.concat("CV of peak heights"); datasetTitle = datasetTitle.concat(" in " + selectedFiles.length + " files"); datasetTitle = datasetTitle.concat(")"); logger.finest("Computing: " + datasetTitle); // Loop through rows of aligned feature list Vector<Double> xCoordsV = new Vector<Double>(); Vector<Double> yCoordsV = new Vector<Double>(); Vector<Double> colorCoordsV = new Vector<Double>(); Vector<PeakListRow> peakListRowsV = new Vector<PeakListRow>(); for (int rowIndex = 0; rowIndex < numOfRows; rowIndex++) { PeakListRow row = alignedPeakList.getRow(rowIndex); // Collect available peak intensities for selected files Vector<Double> peakIntensities = new Vector<Double>(); for (int fileIndex = 0; fileIndex < selectedFiles.length; fileIndex++) { Feature p = row.getPeak(selectedFiles[fileIndex]); if (p != null) { if (measurementType == PeakMeasurementType.AREA) peakIntensities.add(p.getArea()); else peakIntensities.add(p.getHeight()); } } // If there are at least two measurements available for this peak // then calc CV and include this peak in the plot if (peakIntensities.size() > 1) { double[] ints = Doubles.toArray(peakIntensities); Double cv = MathUtils.calcCV(ints); Double rt = row.getAverageRT(); Double mz = row.getAverageMZ(); xCoordsV.add(rt); yCoordsV.add(mz); colorCoordsV.add(cv); peakListRowsV.add(row); } } // Finally store all collected values in arrays xCoords = Doubles.toArray(xCoordsV); yCoords = Doubles.toArray(yCoordsV); colorCoords = Doubles.toArray(colorCoordsV); peakListRows = peakListRowsV.toArray(new PeakListRow[0]); }
Example 15
Source File: SummarizeCorpusScores2016.java From tac-kbp-eal with MIT License | 4 votes |
@Override public double[] apply(final CorpusScores input) { return Doubles .toArray(transform(input.queryFPercentiles().get("F1").rawData(), Percentifier.INSTANCE)); }
Example 16
Source File: SomaticClusteringModel.java From gatk with BSD 3-Clause "New" or "Revised" License | 4 votes |
private void initializeClusters() { Utils.validate(!clustersHaveBeenInitialized, "Clusters have already been initialized."); final double[] somaticProbs = data.stream().mapToDouble(this::probabilityOfSomaticVariant).toArray(); double previousBIC = Double.NEGATIVE_INFINITY; for (int cluster = 0; cluster < MAX_BINOMIAL_CLUSTERS; cluster++) { final double[] oldLogClusterWeights = Arrays.copyOf(logClusterWeights, logClusterWeights.length); final double[] backgroundProbsGivenSomatic = data.stream().mapToDouble(datum -> backgroundProbGivenSomatic(datum.getTotalCount(), datum.getAltCount())).toArray(); final double[] backGroundProbs = MathArrays.ebeMultiply(somaticProbs, backgroundProbsGivenSomatic); final double[] alleleFractionQuantiles = calculateAlleleFractionQuantiles(); // calculate how much total probability is assigned to the background cluster, then split off a peak from the background final double[] totalQuantileBackgroundResponsibilities = calculateQuantileBackgroundResponsibilities(alleleFractionQuantiles, backGroundProbs); final List<Pair<Double, Double>> peaksAndMasses = calculatePeaksAndMasses(alleleFractionQuantiles, totalQuantileBackgroundResponsibilities); if (peaksAndMasses.isEmpty()) { break; } final Pair<Double, Double> biggestPeakAndMass = peaksAndMasses.stream().sorted(Comparator.comparingDouble(Pair<Double, Double>::getRight).reversed()).findFirst().get(); if (biggestPeakAndMass.getLeft() < alleleFractionQuantiles[Math.min(MIN_QUANTILE_INDEX_FOR_MAKING_CLUSTER, alleleFractionQuantiles.length-1)]) { break; } final double totalMass = peaksAndMasses.stream().mapToDouble(Pair::getRight).sum(); final double fractionOfBackgroundToSplit = Math.min(MAX_FRACTION_OF_BACKGROUND_TO_SPLIT_OFF, biggestPeakAndMass.getRight() / totalMass); final double newClusterLogWeight = Math.log(fractionOfBackgroundToSplit) + logClusterWeights[0]; final double newBackgroundWeight = Math.log1p(fractionOfBackgroundToSplit) + logClusterWeights[0]; clusters.add(new BinomialCluster(biggestPeakAndMass.getLeft())); final List<Double> newLogWeights = new ArrayList<>(Doubles.asList(logClusterWeights)); newLogWeights.add(newClusterLogWeight); newLogWeights.set(0, newBackgroundWeight); logClusterWeights = Doubles.toArray(newLogWeights); for (int n = 0; n < NUM_ITERATIONS; n++) { performEMIteration(false); } final double[] logLikelihoodsGivenSomatic = data.stream().mapToDouble(datum -> logLikelihoodGivenSomatic(datum.getTotalCount(), datum.getAltCount())).toArray(); final double weightedLogLikelihood = MathUtils.sum(MathArrays.ebeMultiply(somaticProbs, logLikelihoodsGivenSomatic)); final double effectiveSomaticCount = MathUtils.sum(somaticProbs); final double numParameters = 2 * clusters.size(); // if splitting off the peak worsened the BIC score, remove the new peak and we're done final double currentBIC = weightedLogLikelihood - numParameters * Math.log(effectiveSomaticCount); if (currentBIC < previousBIC) { clusters.remove(clusters.size() - 1); logClusterWeights = oldLogClusterWeights; break; } previousBIC = currentBIC; } clustersHaveBeenInitialized = true; }
Example 17
Source File: Tree.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
public double[] getThreshold(){ return Doubles.toArray(getNodeAttribute("threshold")); }
Example 18
Source File: Tree.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
public double[] getValues(){ return Doubles.toArray((List)getArray("values")); }
Example 19
Source File: CVDataset.java From mzmine3 with GNU General Public License v2.0 | 4 votes |
public CVDataset(PeakList alignedPeakList, ParameterSet parameters) { int numOfRows = alignedPeakList.getNumberOfRows(); RawDataFile selectedFiles[] = parameters.getParameter(CVParameters.dataFiles).getValue(); PeakMeasurementType measurementType = parameters.getParameter(CVParameters.measurementType).getValue(); // Generate title for the dataset datasetTitle = "Correlation of variation analysis"; datasetTitle = datasetTitle.concat(" ("); if (measurementType == PeakMeasurementType.AREA) datasetTitle = datasetTitle.concat("CV of peak areas"); else datasetTitle = datasetTitle.concat("CV of peak heights"); datasetTitle = datasetTitle.concat(" in " + selectedFiles.length + " files"); datasetTitle = datasetTitle.concat(")"); logger.finest("Computing: " + datasetTitle); // Loop through rows of aligned feature list Vector<Double> xCoordsV = new Vector<Double>(); Vector<Double> yCoordsV = new Vector<Double>(); Vector<Double> colorCoordsV = new Vector<Double>(); Vector<PeakListRow> peakListRowsV = new Vector<PeakListRow>(); for (int rowIndex = 0; rowIndex < numOfRows; rowIndex++) { PeakListRow row = alignedPeakList.getRow(rowIndex); // Collect available peak intensities for selected files Vector<Double> peakIntensities = new Vector<Double>(); for (int fileIndex = 0; fileIndex < selectedFiles.length; fileIndex++) { Feature p = row.getPeak(selectedFiles[fileIndex]); if (p != null) { if (measurementType == PeakMeasurementType.AREA) peakIntensities.add(p.getArea()); else peakIntensities.add(p.getHeight()); } } // If there are at least two measurements available for this peak // then calc CV and include this peak in the plot if (peakIntensities.size() > 1) { double[] ints = Doubles.toArray(peakIntensities); Double cv = MathUtils.calcCV(ints); Double rt = row.getAverageRT(); Double mz = row.getAverageMZ(); xCoordsV.add(rt); yCoordsV.add(mz); colorCoordsV.add(cv); peakListRowsV.add(row); } } // Finally store all collected values in arrays xCoords = Doubles.toArray(xCoordsV); yCoords = Doubles.toArray(yCoordsV); colorCoords = Doubles.toArray(colorCoordsV); peakListRows = peakListRowsV.toArray(new PeakListRow[0]); }
Example 20
Source File: SparseLocalDateDoubleTimeSeries.java From Strata with Apache License 2.0 | 3 votes |
/** * Obtains a time-series from matching arrays of dates and values. * <p> * The two arrays must be the same size and must be sorted from earliest to latest. * * @param dates the date list * @param values the value list * @return the time-series */ static SparseLocalDateDoubleTimeSeries of(Collection<LocalDate> dates, Collection<Double> values) { ArgChecker.noNulls(dates, "dates"); ArgChecker.noNulls(values, "values"); LocalDate[] datesArray = dates.toArray(new LocalDate[dates.size()]); double[] valuesArray = Doubles.toArray(values); validate(datesArray, valuesArray); return createUnsafe(datesArray, valuesArray); }