You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by cur4so <gi...@git.apache.org> on 2017/10/09 18:38:37 UTC
[GitHub] commons-rng pull request #5: Feature rng 37
GitHub user cur4so opened a pull request:
https://github.com/apache/commons-rng/pull/5
Feature rng 37
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/cur4so/commons-rng feature-RNG-37
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/commons-rng/pull/5.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #5
----
----
---
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
[GitHub] commons-rng issue #5: Feature rng 37
Posted by cur4so <gi...@git.apache.org>.
Github user cur4so commented on the issue:
https://github.com/apache/commons-rng/pull/5
now it's more clear @kinow :) Fixed. And rephrased the git commit message as Gilles suggested
---
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
[GitHub] commons-rng issue #5: Feature rng 37
Posted by cur4so <gi...@git.apache.org>.
Github user cur4so commented on the issue:
https://github.com/apache/commons-rng/pull/5
fixed
---
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
[GitHub] commons-rng issue #5: Feature rng 37
Posted by kinow <gi...@git.apache.org>.
Github user kinow commented on the issue:
https://github.com/apache/commons-rng/pull/5
Thank you @cur4so for the pull request.
Travis CI failed to build this pull request, but I suspect it may be related to some thing else.
```
Results :
Failed tests:
ContinuousSamplerParametricTest.testSampling:53->check:127 Gaussian deviate [Box-Muller normalized Gaussian deviate [rng=org.apache.commons.rng.core.source32.MersenneTwister]]: Too many failures for sample size = 20000 (3 out of 50 tests failed, chi2=[22.682000000000002, 24.367000000000004, 24.088]
```
I checked out your pull request locally (https://gist.github.com/piscisaureus/3342247). Then built it with `mvn clean site`. Finally, under commons-rng/commons-rng-sampling/target/site/, opened the index.html file in my browser.
There, under Project Reports, I can see that PMD has two new issues from this PR (I'll comment on them). And Checkstyle has 4 new errors, which I'll also add a note to the PR.
Can you have a look at these issues @cur4so ? Great work so far. Thanks a lot for your contribution, and patience :-)
Bruno
---
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
[GitHub] commons-rng pull request #5: Feature rng 37
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/commons-rng/pull/5
---
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
[GitHub] commons-rng pull request #5: Feature rng 37
Posted by kinow <gi...@git.apache.org>.
Github user kinow commented on a diff in the pull request:
https://github.com/apache/commons-rng/pull/5#discussion_r143859435
--- Diff: commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratGaussianSampler.java ---
@@ -0,0 +1,159 @@
+/*
+ * 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.
+ */
+
+ /* @param KN - an auxiliary table of integers, k_i = 2^32*(x_{i-1}/x_i) */
+ private static final int[] KN = new int[128];
+ /* @param WN - table of doubles, w_i = x_i/2^32 */
+ private static final double[] WN = new double[128];
+ /* @param FN - the function values table (normalized gaussian in this implementation)
+ * f_i(x_i) = exp(-x_i^2/2)
+ */
--- End diff --
This is almost fixed. What you added were multiline comments, ignored by javadoc. You have to use
```
/**
* This is a Javadoc. Here goes your text...
*/
```
Instead of
```
/**
* This is a multi-line comment. Here goes your text...
*/
```
Site reports still have issues with these comments above.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
[GitHub] commons-rng pull request #5: Feature rng 37
Posted by kinow <gi...@git.apache.org>.
Github user kinow commented on a diff in the pull request:
https://github.com/apache/commons-rng/pull/5#discussion_r143869091
--- Diff: commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratGaussianSampler.java ---
@@ -0,0 +1,161 @@
+/*
+ * 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.
+ */
+
+ /* @param KN - an auxiliary table of integers, k_i = 2^32*(x_{i-1}/x_i) */
+ private static final int[] KN = new int[128];
+ /* @param WN - table of doubles, w_i = x_i/2^32 */
+ private static final double[] WN = new double[128];
+ /**
+ * This is a Javadoc. @param FN - the function values table
+ * (normalized gaussian in this implementation)
+ * f_i(x_i) = exp(-x_i^2/2)
+ */
+ private static final double[] FN = new double[128];
--- End diff --
Oh, sorry, I think that was a bad example. What I meant to say, is that your comments had to be something like this:
```
/** An auxiliary table of integers, k_i = 2^32*(x_{i-1}/x_i) */
private static final int[] KN = new int[128];
/** Table of doubles, w_i = x_i/2^32 */
private static final double[] WN = new double[128];
/** The function values table (normalized gaussian in this implementation) f_i(x_i) = exp(-x_i^2/2) */
private static final double[] FN = new double[128];
```
It is the double * after the / character that marks the comment as Javadoc or not. And the param annotation is necessary only for method parameters.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
[GitHub] commons-rng pull request #5: Feature rng 37
Posted by kinow <gi...@git.apache.org>.
Github user kinow commented on a diff in the pull request:
https://github.com/apache/commons-rng/pull/5#discussion_r143630285
--- 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
+ * @param hz - start random integer
+ * @param iz - corresponding to hz cell's number
+ */
+ private double nfix(int hz, int iz) {
+ /* The start of the right tail */
+ final double r = 3.442619855899;
+
+ double uni;
+ double x;
+ double y;
+
+ while (true) {
+ uni = .5 + hz * .2328306e-9;
+ x = hz * WN[iz];
+ /* iz==0, handles the base strip */
+ if (iz == 0) {
+ /* return x from the tail */
+ do {
+ y = -Math.log(uni);
+ x = y * 0.2904764;
+ uni = .5 + nextInt() * .2328306e-9;
+ } while (y + y < x * x);
+ return (hz > 0) ? r + x : -r - x;
+ }
+ /* iz>0, handle the wedges of other strips */
+ if (FN[iz] + uni * (FN[iz - 1] - FN[iz]) < Math.exp(-.5 * x * x)) {
+ return x;
+ }
+ hz = nextInt();
+ iz = hz & 127;
+ if (Math.abs(hz) < KN[iz]) {
+ return (hz * WN[iz]);
--- End diff --
No need to add parentheses here. Checkstyle is unhappy with it.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
[GitHub] commons-rng issue #5: Feature rng 37
Posted by cur4so <gi...@git.apache.org>.
Github user cur4so commented on the issue:
https://github.com/apache/commons-rng/pull/5
thanks @kinow for your suggestions and patience. I've implemented them in the last commit
---
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
[GitHub] commons-rng pull request #5: Feature rng 37
Posted by kinow <gi...@git.apache.org>.
Github user kinow commented on a diff in the pull request:
https://github.com/apache/commons-rng/pull/5#discussion_r143630148
--- 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];
--- End diff --
We need Javadocs for these fields. Even though there is a comment above KN, that's treated only as KN's comment. Perhaps one simple, short, comment for each KN, WN, and FN variables.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
[GitHub] commons-rng issue #5: Feature rng 37
Posted by kinow <gi...@git.apache.org>.
Github user kinow commented on the issue:
https://github.com/apache/commons-rng/pull/5
Builds successfully in
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /opt/jdk1.8.0_131/jre
Default locale: en_NZ, platform encoding: UTF-8
OS name: "linux", version: "4.10.0-35-generic", arch: "amd64", family: "unix"
I suspect the Travis CI build is not related to the PR, though would need someone else's opinion too.
The coverage report indicates 99% line coverage, and 86% branch coverage. :clap:
---
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
[GitHub] commons-rng pull request #5: Feature rng 37
Posted by kinow <gi...@git.apache.org>.
Github user kinow commented on a diff in the pull request:
https://github.com/apache/commons-rng/pull/5#discussion_r143630188
--- 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
+ * @param hz - start random integer
+ * @param iz - corresponding to hz cell's number
+ */
--- End diff --
Need to add a @return Javadoc comment below the iz param.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
[GitHub] commons-rng pull request #5: Feature rng 37
Posted by kinow <gi...@git.apache.org>.
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