forked from openturns/openturns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved unit test of Sobol' sequence
- Loading branch information
Showing
6 changed files
with
74 additions
and
2,027 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,62 @@ | ||
#! /usr/bin/env python | ||
|
||
import openturns as ot | ||
import openturns.testing as ott | ||
import math as m | ||
|
||
ot.TESTPREAMBLE() | ||
|
||
|
||
# Create 6 points from a Sobol' sequence in dimension 1 | ||
expected = ot.Sample( | ||
[ | ||
[1.0 / 2.0], | ||
[3.0 / 4.0], | ||
[1.0 / 4.0], | ||
[3.0 / 8.0], | ||
[7.0 / 8.0], | ||
[5.0 / 8.0], | ||
] | ||
) | ||
sequence = ot.SobolSequence(1) | ||
print(sequence) | ||
sobolSample = sequence.generate(6) | ||
ott.assert_almost_equal(sobolSample, expected) | ||
|
||
# Create 6 points from a Sobol' sequence in dimension 2 | ||
expected = ot.Sample( | ||
[ | ||
[1.0 / 2.0, 1.0 / 2.0], | ||
[3.0 / 4.0, 1.0 / 4.0], | ||
[1.0 / 4.0, 3.0 / 4.0], | ||
[3.0 / 8.0, 3.0 / 8.0], | ||
[7.0 / 8.0, 7.0 / 8.0], | ||
[5.0 / 8.0, 1.0 / 8.0], | ||
] | ||
) | ||
sequence = ot.SobolSequence(2) | ||
print(sequence) | ||
sobolSample = sequence.generate(6) | ||
ott.assert_almost_equal(sobolSample, expected) | ||
|
||
# Create a Sobol' sequence of maximum dimension | ||
sequence = ot.SobolSequence(ot.SobolSequence.MaximumDimension) | ||
print(sequence) | ||
|
||
# Create a numerical sample of the sequence | ||
sobolSample = sequence.generate(10) | ||
print(repr(sobolSample)) | ||
|
||
# Create another Sobol' sequence of dimension 2 to estimate Pi in [0 1)^2 | ||
# Create a Sobol' sequence of dimension 2 to estimate Pi in [0 1)^2 | ||
dimension = 2 | ||
sequence = ot.SobolSequence(dimension) | ||
pointInsideCircle = 0 | ||
sampleSize = 1000 | ||
sampleSize = 2 ** 11 # This is significant! | ||
for i in range(sampleSize): | ||
sobolPoint = sequence.generate() | ||
print(sobolPoint.__repr__()) | ||
if sobolPoint.norm() < 1.0: | ||
pointInsideCircle = pointInsideCircle + 1 | ||
pointInsideCircle += 1 | ||
probabilityEstimate = (1.0 * pointInsideCircle) / sampleSize | ||
probability = m.pi / 4.0 | ||
relativeError = abs(probability - probabilityEstimate) / probability | ||
print("sample size=", sampleSize) | ||
print("relative error to Pi=%e" % relativeError) | ||
print("computed probability =", probabilityEstimate) | ||
print("expected probability =", probability) | ||
rtol = 10.0 / sampleSize | ||
ott.assert_almost_equal(probability, probabilityEstimate, rtol) |