Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MATH-1600: NormalizedRandomGenerator as "DoubleSupplier #188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public RealMatrix getRootMatrix() {
public double[] get() {
// generate uncorrelated vector
for (int i = 0; i < normalized.length; ++i) {
normalized[i] = generator.nextNormalizedDouble();
normalized[i] = generator.getAsDouble();
}

// compute correlated vector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@

package org.apache.commons.math4.legacy.random;

import java.util.function.DoubleSupplier;

/**
* This interface represent a normalized random generator for
* scalars.
* Normalized generator provide null mean and unit standard deviation scalars.
* Supply a random scalar with null mean and unit standard deviation.
* <p>This supplier does <strong>not</strong> specify the shape of the
* distribution, it is the implementing class that provides it. The
* only contract here is to generate numbers with null mean and unit
* standard deviation.</p>
* @since 1.2
*/
public interface NormalizedRandomGenerator {

/** Generate a random scalar with null mean and unit standard deviation.
* <p>This method does <strong>not</strong> specify the shape of the
* distribution, it is the implementing class that provides it. The
* only contract here is to generate numbers with null mean and unit
* standard deviation.</p>
* @return a random scalar with null mean and unit standard deviation
*/
double nextNormalizedDouble();
public interface NormalizedRandomGenerator extends DoubleSupplier {

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public StableRandomGenerator(final UniformRandomProvider generator,
* @return a random scalar with zero location and unit scale
*/
@Override
public double nextNormalizedDouble() {
public double getAsDouble() {
// we need 2 uniform random numbers to calculate omega and phi
double omega = -AccurateMath.log(generator.nextDouble());
double phi = AccurateMath.PI * (generator.nextDouble() - 0.5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public UniformRandomGenerator(UniformRandomProvider generator) {
* @return a random scalar in the range \( [-\sqrt{3}, +\sqrt{3}] \).
*/
@Override
public double nextNormalizedDouble() {
public double getAsDouble() {
return SQRT3 * (2 * generator.nextDouble() - 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private NormalizedRandomGenerator gaussianRandom(final UniformRandomProvider rng
return new NormalizedRandomGenerator() {
/** {@inheritDoc} */
@Override
public double nextNormalizedDouble() {
public double getAsDouble() {
return n.sample();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public class StableRandomGeneratorTest {
* TODO: verify that tolerance this wide is really OK
*/
@Test
public void testNextDouble() {
public void testGetAsDouble() {
StableRandomGenerator generator = new StableRandomGenerator(rg, 1.3,
0.1);
double[] sample = new double[2 * sampleSize];
for (int i = 0; i < sample.length; ++i) {
sample[i] = generator.nextNormalizedDouble();
sample[i] = generator.getAsDouble();
}
Assert.assertEquals(0.0, StatUtils.mean(sample), 0.3);
}
Expand All @@ -57,7 +57,7 @@ public void testGaussianCase() {

double[] sample = new double[sampleSize];
for (int i = 0; i < sample.length; ++i) {
sample[i] = generator.nextNormalizedDouble();
sample[i] = generator.getAsDouble();
}
Assert.assertEquals(0.0, StatUtils.mean(sample), 0.02);
Assert.assertEquals(1.0, StatUtils.variance(sample), 0.02);
Expand All @@ -72,7 +72,7 @@ public void testCauchyCase() {
DescriptiveStatistics summary = new DescriptiveStatistics();

for (int i = 0; i < sampleSize; ++i) {
double sample = generator.nextNormalizedDouble();
double sample = generator.getAsDouble();
summary.addValue(sample);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void testMeanAndStandardDeviation() {
UniformRandomGenerator generator = new UniformRandomGenerator(rg);
double[] sample = new double[10000];
for (int i = 0; i < sample.length; ++i) {
sample[i] = generator.nextNormalizedDouble();
sample[i] = generator.getAsDouble();
}
Assert.assertEquals(0.0, StatUtils.mean(sample), 0.07);
Assert.assertEquals(1.0, StatUtils.variance(sample), 0.02);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private NormalizedRandomGenerator gaussianRandom(final UniformRandomProvider rng
return new NormalizedRandomGenerator() {
/** {@inheritDoc} */
@Override
public double nextNormalizedDouble() {
public double getAsDouble() {
return n.sample();
}
};
Expand Down