⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 empiricaldistributionimpl.java

📁 Apache的common math数学软件包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }        /**         * Computes binStats         *          * @param min  minimum value         * @param delta  grid size         * @throws IOException if an IO error occurs         */        public void computeBinStats(double min, double delta)                throws IOException {            String str = null;            double val = 0.0d;            while ((str = inputStream.readLine()) != null) {                val = Double.parseDouble(str);                SummaryStatistics stats =                    (SummaryStatistics) binStats.get(findBin(min, val, delta));                stats.addValue(val);            }            inputStream.close();            inputStream = null;        }        /**         * Computes sampleStats         *          * @throws IOException if an IOError occurs         */        public void computeStats() throws IOException {            String str = null;            double val = 0.0;            sampleStats = new SummaryStatistics();            while ((str = inputStream.readLine()) != null) {                val = new Double(str).doubleValue();                sampleStats.addValue(val);            }            inputStream.close();            inputStream = null;        }    }    /**     * <code>DataAdapter</code> for data provided as array of doubles.     */    private class ArrayDataAdapter extends DataAdapter{                /** Array of input  data values */        private double[] inputArray;                /**         * Construct an ArrayDataAdapter from a double[] array         *          * @param in double[] array holding the data         */        public ArrayDataAdapter(double[] in){            super();            inputArray = in;        }        /**         * Computes sampleStats         *          * @throws IOException if an IO error occurs         */        public void computeStats() throws IOException {            sampleStats = new SummaryStatistics();            for (int i = 0; i < inputArray.length; i++) {                sampleStats.addValue(inputArray[i]);            }        }        /**         * Computes binStats         *          * @param min  minimum value         * @param delta  grid size         * @throws IOException  if an IO error occurs         */        public void computeBinStats(double min, double delta)            throws IOException {            for (int i = 0; i < inputArray.length; i++) {                SummaryStatistics stats =                    (SummaryStatistics) binStats.get(                            findBin(min, inputArray[i], delta));                stats.addValue(inputArray[i]);            }        }    }    /**     * Fills binStats array (second pass through data file).     *      * @param in object providing access to the data     * @throws IOException  if an IO error occurs     */    private void fillBinStats(Object in) throws IOException {        // Load array of bin upper bounds -- evenly spaced from min - max        double min = sampleStats.getMin();        double max = sampleStats.getMax();        double delta = (max - min)/(new Double(binCount)).doubleValue();        double[] binUpperBounds = new double[binCount];        binUpperBounds[0] = min + delta;        for (int i = 1; i< binCount - 1; i++) {            binUpperBounds[i] = binUpperBounds[i-1] + delta;        }        binUpperBounds[binCount -1] = max;        // Initialize binStats ArrayList        if (!binStats.isEmpty()) {            binStats.clear();        }        for (int i = 0; i < binCount; i++) {            SummaryStatistics stats = new SummaryStatistics();            binStats.add(i,stats);        }        // Filling data in binStats Array        DataAdapterFactory aFactory = new DataAdapterFactory();        DataAdapter da = aFactory.getAdapter(in);        try {            da.computeBinStats(min, delta);        } catch (Exception e) {            if(e instanceof RuntimeException){                throw new RuntimeException(e.getMessage());            }else{                throw new IOException(e.getMessage());            }        }        // Assign upperBounds based on bin counts        upperBounds = new double[binCount];        upperBounds[0] =        ((double)((SummaryStatistics)binStats.get(0)).getN())/        (double)sampleStats.getN();        for (int i = 1; i < binCount-1; i++) {            upperBounds[i] = upperBounds[i-1] +            ((double)((SummaryStatistics)binStats.get(i)).getN())/            (double)sampleStats.getN();        }        upperBounds[binCount-1] = 1.0d;    }        /**     * Returns the index of the bin to which the given value belongs     *      * @param min  the minimum value     * @param value  the value whose bin we are trying to find     * @param delta  the grid size     * @return the index of the bin containing the value     */    private int findBin(double min, double value, double delta) {        return Math.min(                Math.max((int) Math.ceil((value- min) / delta) - 1, 0),                 binCount - 1);        }    /**     * Generates a random value from this distribution.     *      * @return the random value.     * @throws IllegalStateException if the distribution has not been loaded     */    public double getNextValue() throws IllegalStateException {        if (!loaded) {            throw new IllegalStateException("distribution not loaded");        }        // Start with a uniformly distributed random number in (0,1)        double x = Math.random();        // Use this to select the bin and generate a Gaussian within the bin        for (int i = 0; i < binCount; i++) {           if (x <= upperBounds[i]) {               SummaryStatistics stats = (SummaryStatistics)binStats.get(i);               if (stats.getN() > 0) {                   if (stats.getStandardDeviation() > 0) {  // more than one obs                        return randomData.nextGaussian                            (stats.getMean(),stats.getStandardDeviation());                   } else {                       return stats.getMean(); // only one obs in bin                   }               }           }        }        throw new RuntimeException("No bin selected");    }    /**     * Returns a {@link StatisticalSummary} describing this distribution.     * <strong>Preconditions:</strong><ul>     * <li>the distribution must be loaded before invoking this method</li></ul>     *      * @return the sample statistics     * @throws IllegalStateException if the distribution has not been loaded     */    public StatisticalSummary getSampleStats() {        return sampleStats;    }    /**     * Returns the number of bins.     *      * @return the number of bins.     */    public int getBinCount() {        return binCount;    }    /**     * Returns an ArrayList of {@link SummaryStatistics} instances containing     * statistics describing the values in each of the bins.  The ArrayList is     * indexed on the bin number.     *      * @return List of bin statistics.     */    public List getBinStats() {        return binStats;    }    /**     * Returns (a fresh copy of) the array of upper bounds for the bins.       Bins are: <br/>     * [min,upperBounds[0]],(upperBounds[0],upperBounds[1]],...,     *  (upperBounds[binCount-1],max]     *      * @return array of bin upper bounds     */    public double[] getUpperBounds() {        int len = upperBounds.length;        double[] out = new double[len];        System.arraycopy(upperBounds, 0, out, 0, len);        return out;    }    /**     * Property indicating whether or not the distribution has been loaded.     *      * @return true if the distribution has been loaded     */    public boolean isLoaded() {        return loaded;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -