Note
Go to the end to download the full example code.
Non parametric Adaptive Importance Sampling (NAIS)¶
The objective is to evaluate a probability from the Non parametric Adaptive Importance Sampling (NAIS) technique.
We consider the Four Branch function use case (see The Four Branch function).
The function is a 2D limit state function with four distinct failure modes.
The standard benchmark uses independent standard normal inputs and parameter .
We want to evaluate the failure probability:
where is the Four Branch function and
follows
the standard 2-dimensional Normal distribution.
First, import the python modules:
import openturns as ot
import openturns.viewer as otv
Create the probabilistic model
¶
Load the Four Branch function use case:
from openturns.usecases import four_branch_function
fb = four_branch_function.FourBranchModel()
Create the input random vector :
Get the limit state function :
Draw the function to help to understand the shape of the limit state function:
graph = ot.Graph("Four Branch function", "x1", "x2")
graph.setLegendPosition("upper right")
drawfunction = g.draw([-8] * 2, [8] * 2, [100] * 2)
graph.add(drawfunction)
view = otv.View(graph)
In order to be able to get the NAIS samples used in the algorithm, it is necessary to transform the PythonFunction into a MemoizeFunction:
g = ot.MemoizeFunction(g)
Create the output random vector :
Y = ot.CompositeRandomVector(g, X)
Create the event
¶
threshold = 0.0
myEvent = ot.ThresholdEvent(Y, ot.Less(), threshold)
Evaluate the probability with the NAIS technique¶
In order to get all the inputs and outputs that realize the event, you have to mention it now:
algo.setKeepSample(True)
Now you can run the algorithm.
algo.run()
result = algo.getResult()
proba = result.getProbabilityEstimate()
print("Proba NAIS = ", proba)
print("Current coefficient of variation = ", result.getCoefficientOfVariation())
Proba NAIS = 0.004561496703722792
Current coefficient of variation = 0.061676386160111354
The confidence interval of level is:
ci95 = result.getProbabilityDistribution().computeBilateralConfidenceInterval(0.95)
print("Confidence interval (0.95) = ", ci95)
Confidence interval (0.95) = [0.00401009, 0.00511291]
You can also get the successive thresholds used by the algorithm:
levels = algo.getThresholdPerStep()
print("Levels of g = ", levels)
Levels of g = [1.3517,0.0425045,0]
Draw the NAIS samples used by the algorithm¶
You can get the number of steps with:
Ns = algo.getStepsNumber()
print("Number of steps= ", Ns)
Number of steps= 3
Get all the inputs where was evaluated at each step
list_subSamples = list()
for step in range(Ns):
list_subSamples.append(algo.getInputSample(step))
The following graph draws each NAIS sample and the frontier where
is the threshold at the step
:
graph = ot.Graph()
graph.setAxes(True)
graph.setGrid(True)
graph.setTitle("NAIS sampling: samples")
graph.setXTitle(r"$x_1$")
graph.setYTitle(r"$x_2$")
graph.setLegendPosition("lower left")
Add all the NAIS samples:
Add the frontiers where
is the threshold at the step
:
gIsoLines = g.draw([-5] * 2, [5] * 2, [128] * 2)
dr = gIsoLines.getDrawable(0)
for i, lv in enumerate(levels):
dr.setLevels([lv])
dr.setLineStyle("solid")
dr.setLegend(r"$g(X) = $" + str(round(lv, 2)))
dr.setLineWidth(3)
dr.setColor(col[i])
graph.add(dr)
Draw the frontiers only¶
The following graph enables to understand the progression of the algorithm:
graph = ot.Graph()
graph.setAxes(True)
graph.setGrid(True)
dr = gIsoLines.getDrawable(0)
for i, lv in enumerate(levels):
dr.setLevels([lv])
dr.setLineStyle("solid")
dr.setLegend(r"$g(X) = $" + str(round(lv, 2)))
dr.setLineWidth(3)
graph.add(dr)
graph.setColors(col)
graph.setLegendPosition("lower left")
graph.setTitle("NAIS sampling: thresholds")
graph.setXTitle(r"$x_1$")
graph.setYTitle(r"$x_2$")
_ = otv.View(graph)
Get all the input and output points that realized the event¶
The following lines are possible only if you have mentioned that you wanted to keep samples with the method algo.setKeepSample(True)
select = ot.NAIS.EVENT1 # points that realize the event
step = Ns - 1 # get the working sample from last iteration
inputEventSample = algo.getInputSample(step, select)
outputEventSample = algo.getOutputSample(step, select)
print("Number of event realizations = ", inputEventSample.getSize())
Number of event realizations = 571
Draw them! They are all in the event space.
graph = ot.Graph()
graph.setAxes(True)
graph.setGrid(True)
cloud = ot.Cloud(inputEventSample)
cloud.setPointStyle("bullet")
graph.add(cloud)
# A 500x500 grid is enough for the single level-0 isoline below
gIsoLines = g.draw([-5] * 2, [5] * 2, [500] * 2)
dr = gIsoLines.getDrawable(0)
dr.setLevels([0.0])
dr.setColor("red")
graph.add(dr)
_ = otv.View(graph)
otv.View.ShowAll()
OpenTURNS