commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kinow <...@git.apache.org>
Subject [GitHub] commons-rng pull request #5: Feature rng 37
Date Tue, 10 Oct 2017 04:59:24 GMT
Github user kinow commented on a diff in the pull request:

    https://github.com/apache/commons-rng/pull/5#discussion_r143630250
  
    --- Diff: commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratGaussianSampler.java
---
    @@ -0,0 +1,152 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *      http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.commons.rng.sampling.distribution;
    +
    +import org.apache.commons.rng.UniformRandomProvider;
    +
    +/**
    + * Gaussian Sampling by
    + * <a href="https://en.wikipedia.org/wiki/Ziggurat_algorithm">Ziggurat algorithm</a>
    + *
    + * <p>Based on
    + * "The Ziggurat Method for Generating Random Variables"<br>
    + * by George Marsaglia and Wai Wan Tsang</p>
    + *
    + * @see <a href="http://www.jstatsoft.org/article/view/v005i08/ziggurat.pdf">Ziggurat
Method for Generating Random Variables</a>
    + *
    + * @since 1.1
    + */
    +
    +public class ZigguratGaussianSampler
    +    extends SamplerBase
    +    implements NormalizedGaussianSampler {
    +
    +    /**
    +     * Generates values from Gaussian (normal) probability distribution
    +     * It uses two tables, integers KN and reals WN. Some 99% of the time,
    +     * the required x is produced by:
    +     * generate a random 32-bit integer j and let i be the index formed from
    +     * the rightmost 8 bits of j. If j < k_i return x = j * w_i.
    +     */
    +
    +    private static final int[] KN = new int[128];
    +    private static final double[] WN = new double[128];
    +    private static final double[] FN = new double[128];
    +
    +    /**
    +     * Initialize tables.
    +     */
    +    static {
    +        /**
    +         * Filling the tables.
    +         * k_0 = 2^32 * r * f(dn) / vn
    +         * k_i = 2^32 * ( x_{i-1} / x_i )
    +         * w_0 = .5^32 * vn / f(dn)
    +         * w_i = .5^32 * x_i
    +         * where dn - the rightmost x_i
    +         * vn - the area of the rectangle
    +         * f(dn) = exp(-.5 * dn * dn)
    +         */
    +        final double m = 2147483648.0; // 2^31
    +
    +        /* provides z(r) = 0, where z(r): x_255 = r, vn = r*f(r)+integral_r^inf f(x)dx
*/
    +        final double vn = 9.91256303526217e-3;
    +
    +        double dn = 3.442619855899;
    +        double tn = dn;
    +        double e = Math.exp(-.5 * dn * dn);
    +        final double q = vn / e;
    +
    +        KN[0] = (int) ((dn / q) * m);
    +        KN[1] = 0;
    +
    +        WN[0] = q / m;
    +        WN[127] = dn / m;
    +
    +        FN[0] = 1.0;
    +        FN[127] = e;
    +
    +        for (int i = 126; i >= 1; i--){
    +            e = Math.exp(-.5 *dn * dn);
    +            dn = Math.sqrt(-2. * Math.log(vn / dn + e));
    +            KN[i+1] = (int) ((dn / tn) * m);
    +            tn = dn;
    +            FN[i] = e;
    +            WN[i] = dn / m;
    +        }
    +    }
    +
    +    /**
    +     * @param rng Generator of uniformly distributed random numbers.
    +     */
    +    public ZigguratGaussianSampler(UniformRandomProvider rng) {
    +        super(rng);
    +    }
    +
    +    /** {@inheritDoc} */
    +    @Override
    +    public double sample() {
    +        int j = nextInt();
    +        int i = j & 127;
    +        return (j < KN[i]) ? j * WN[i] : nfix(j,i);
    +    }
    +
    +    /** get the value from the tail of the distribution
    --- End diff --
    
    Normally it's in the next line, starting with upper case. Not really important though.
Some components have user guides, but many point to the Javadocs as user guide :-) that's
why we focus so much on the text that is put here @cur4so 


---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message