Skip to content

Commit

Permalink
Improved Halton sequence unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaudin47 committed May 12, 2024
1 parent 9d9b1c0 commit 21fcc1f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1,017 deletions.
2 changes: 1 addition & 1 deletion lib/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ ot_check_test (ProcessSample_std)
ot_check_test (RandomGenerator_std)
ot_check_test (SobolSequence_std)
ot_check_test (FaureSequence_std)
ot_check_test (HaltonSequence_std)
ot_check_test (HaltonSequence_std IGNOREOUT)
ot_check_test (HaselgroveSequence_std)
ot_check_test (ReverseHaltonSequence_std)
ot_check_test (CorrelationAnalysis_std IGNOREOUT)
Expand Down
27 changes: 19 additions & 8 deletions lib/test/t_HaltonSequence_std.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,42 @@ int main(int, char *[])

try
{
// Create a Halton sequence
// Create 6 points from a Halton sequence in dimension 1
Sample expected1D(6, 1);
expected1D[0] = Point({1.0 / 2.0});
expected1D[1] = Point({1.0 / 4.0});
expected1D[2] = Point({3.0 / 4.0});
expected1D[3] = Point({1.0 / 8.0});
expected1D[4] = Point({5.0 / 8.0});
expected1D[5] = Point({3.0 / 8.0});
HaltonSequence sequence1D(1);
fullprint << sequence1D << std::endl;
Sample haltonSample1D(sequence1D.generate(6));
assert_almost_equal(haltonSample1D, expected1D);

// Create a Halton sequence in dimension 15
HaltonSequence sequence(15);
fullprint << sequence << std::endl;

// Create a numerical sample of the sequence
Sample haltonSample(sequence.generate(10));
fullprint << haltonSample << std::endl;

// Create another Halton' sequence of dimension 2 to estimate Pi in [0; 1)^2
UnsignedInteger dimension = 2;
sequence = HaltonSequence(dimension);
UnsignedInteger pointInsideCircle = 0;
UnsignedInteger sampleSize = 1000;
UnsignedInteger sampleSize = std::pow((2 * 3), 4); // This is significant!
for(UnsignedInteger i = 0; i < sampleSize; ++i)
{
Point haltonPoint(sequence.generate());
fullprint << haltonPoint << std::endl;
if(haltonPoint.norm() < 1.0)
++ pointInsideCircle;
}
Scalar probabilityEstimate = 1.0 * pointInsideCircle / sampleSize;
Scalar probability = M_PI / 4.0;
Scalar relativeError = std::abs(probability - probabilityEstimate) / probability;
fullprint << "sample size=" << sampleSize << std::endl;
fullprint << "relative error to Pi=" << relativeError << std::endl;
fullprint << "computed probability =" << probabilityEstimate << std::endl;
fullprint << "expected probability =" << probability << std::endl;
Scalar rtol = 10.0 / sampleSize;
assert_almost_equal(probabilityEstimate, probability, rtol);
}

catch (TestFailed & ex)
Expand Down
Loading

0 comments on commit 21fcc1f

Please sign in to comment.