Skip to content

Commit

Permalink
Added Python unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaudin47 committed Aug 21, 2023
1 parent 1a53cb6 commit 1ad076b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions python/test/t_MonteCarloExperiment_std.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#! /usr/bin/env python

import openturns as ot
import openturns.testing as ott
import math
from openturns.usecases import ishigami_function

ot.TESTPREAMBLE()

Expand All @@ -11,3 +14,34 @@
sample, weights = experiment.generateWithWeights()
print("sample = ", repr(sample))
print("weights = ", repr(weights))

# Test integrate
im = ishigami_function.IshigamiModel()
sampleSize = 100000
experiment = ot.MonteCarloExperiment(im.distributionX, sampleSize)
approximatedOutputMean = experiment.integrate(im.model)
rtol = 0.0
atol = 1.0 / math.sqrt(sampleSize)
ott.assert_almost_equal(approximatedOutputMean[0], im.expectation, rtol, atol)

# Test computeNorm
im = ishigami_function.IshigamiModel()
centeredIshigamiFunction = ot.SymbolicFunction(
['x1', 'x2', 'x3'],
['sin(x1) + 7 * (sin(x2)) ^ 2 + 0.1 * x3^4 * sin(x1) - 3.5'])
functionNorm = experiment.computeNorm(centeredIshigamiFunction)
exactFunctionNorm = math.sqrt(im.variance)
rtol = 0.0
atol = 10.0 / math.sqrt(sampleSize)
ott.assert_almost_equal(functionNorm[0], exactFunctionNorm, rtol, atol)

# Test computeError
im = ishigami_function.IshigamiModel()
partOfIshigamiFunction = ot.SymbolicFunction(
['x1', 'x2', 'x3'],
['7 * (sin(x2)) ^ 2 + 0.1 * x3^4 * sin(x1)'])
functionError = experiment.computeError(im.model, partOfIshigamiFunction)
exactError = math.sqrt(0.5)
rtol = 0.0
atol = 10.0 / math.sqrt(sampleSize)
ott.assert_almost_equal(functionError[0], exactError, rtol, atol)

0 comments on commit 1ad076b

Please sign in to comment.