Пример #1
0
 def test_DrawFilledEvent(self):
     # Distribution
     R = ot.Normal(4.0, 1.0)
     R.setDescription("R")
     S = ot.Normal(2.0, 1.0)
     S.setDescription("S")
     g = ot.SymbolicFunction(["R", "S"], ["R - S"])
     # Event
     distribution = ot.ComposedDistribution([R, S])
     inputRV = ot.RandomVector(distribution)
     outputRV = ot.CompositeRandomVector(g, inputRV)
     eventF = ot.ThresholdEvent(outputRV, ot.GreaterOrEqual(), 0)
     alpha = 1.0 - 1.0e-2
     (
         bounds,
         marginalProb,
     ) = distribution.computeMinimumVolumeIntervalWithMarginalProbability(
         alpha)
     #
     drawEvent = otbenchmark.DrawEvent(eventF)
     # Avoid failing on CircleCi
     # _tkinter.TclError: no display name and no $DISPLAY environment variable
     try:
         _ = drawEvent.fillEvent(bounds)
     except Exception as e:
         print(e)
Пример #2
0
 def test_DrawFilledEvent(self):
     # Distribution
     R = ot.Normal(4.0, 1.0)
     R.setDescription("R")
     S = ot.Normal(2.0, 1.0)
     S.setDescription("S")
     g = ot.SymbolicFunction(["R", "S"], ["R - S"])
     # Event
     inputvector = ot.ComposedDistribution([R, S])
     inputRV = ot.RandomVector(inputvector)
     outputRV = ot.CompositeRandomVector(g, inputRV)
     eventF = ot.Event(outputRV, ot.GreaterOrEqual(), 0)
     #
     # Bounds
     alphaMin = 0.01
     alphaMax = 1 - alphaMin
     lowerBound = ot.Point(
         [R.computeQuantile(alphaMin)[0],
          S.computeQuantile(alphaMin)[0]])
     upperBound = ot.Point(
         [R.computeQuantile(alphaMax)[0],
          S.computeQuantile(alphaMax)[0]])
     bounds = ot.Interval(lowerBound, upperBound)
     #
     drawEvent = otbenchmark.DrawEvent(eventF)
     graph = drawEvent.fillEvent(bounds)
     assert type(graph) is ot.Graph
def getXEvent(b, t, mu_S, covariance, R, delta_t):
    full = buildCrossing(b, t, mu_S, covariance, R, delta_t)
    X = ot.RandomVector(full)
    f1 = ot.SymbolicFunction(["R", "X1", "X2"], ["X1 - R"])
    e1 = ot.ThresholdEvent(ot.CompositeRandomVector(f1, X), ot.Less(), 0.0)
    f2 = ot.SymbolicFunction(["R", "X1", "X2"], ["X2 - R"])
    e2 = ot.ThresholdEvent(ot.CompositeRandomVector(f2, X),
                           ot.GreaterOrEqual(), 0.0)
    event = ot.IntersectionEvent([e1, e2])
    return X, event
Пример #4
0
 def test_DrawSample(self):
     # Distribution
     R = ot.Normal(4.0, 1.0)
     R.setDescription("R")
     S = ot.Normal(2.0, 1.0)
     S.setDescription("S")
     g = ot.SymbolicFunction(["R", "S"], ["R - S"])
     # Event
     inputvector = ot.ComposedDistribution([R, S])
     inputRV = ot.RandomVector(inputvector)
     outputRV = ot.CompositeRandomVector(g, inputRV)
     eventF = ot.Event(outputRV, ot.GreaterOrEqual(), 0)
     #
     sampleSize = 500
     inputSample = inputvector.getSample(sampleSize)
     outputSample = g(inputSample)
     #
     drawEvent = otbenchmark.DrawEvent(eventF)
     graph = drawEvent.drawSample(inputSample, outputSample)
     assert type(graph) is ot.Graph
Пример #5
0
 def test_DrawSample(self):
     # Distribution
     R = ot.Normal(4.0, 1.0)
     R.setDescription("R")
     S = ot.Normal(2.0, 1.0)
     S.setDescription("S")
     g = ot.SymbolicFunction(["R", "S"], ["R - S"])
     # Event
     distribution = ot.ComposedDistribution([R, S])
     inputRV = ot.RandomVector(distribution)
     outputRV = ot.CompositeRandomVector(g, inputRV)
     eventF = ot.ThresholdEvent(outputRV, ot.GreaterOrEqual(), 0)
     #
     sampleSize = 500
     drawEvent = otbenchmark.DrawEvent(eventF)
     _ = drawEvent.drawSampleCrossCut(sampleSize, 0, 1)
     # Avoid failing on CircleCi
     # _tkinter.TclError: no display name and no $DISPLAY environment variable
     try:
         _ = drawEvent.drawSample(sampleSize)
     except Exception as e:
         print(e)
import openturns as ot
import otrobopt
from matplotlib import pyplot as plt
from openturns.viewer import View

thetaDist = ot.Normal(2.0, 0.1)
if 'VarianceMeasure' == 'WorstCaseMeasure':
    thetaDist = ot.Uniform(-1.0, 4.0)
elif 'ChanceMeasure' in 'VarianceMeasure':
    thetaDist = ot.Normal(1.0, 1.0)

f_base = ot.Function(['x', 'theta'], ['y'], ['x*theta'])
f = ot.Function(f_base, [1], thetaDist.getMean())

if 'VarianceMeasure' == 'JointChanceMeasure':
    measure = otrobopt.JointChanceMeasure(f, thetaDist, ot.GreaterOrEqual(),
                                          0.95)
elif 'VarianceMeasure' == 'IndividualChanceMeasure':
    measure = otrobopt.IndividualChanceMeasure(f, thetaDist,
                                               ot.GreaterOrEqual(), [0.95])
elif 'VarianceMeasure' == 'MeanStandardDeviationTradeoffMeasure':
    measure = otrobopt.MeanStandardDeviationTradeoffMeasure(
        f, thetaDist, [0.8])
elif 'VarianceMeasure' == 'QuantileMeasure':
    measure = otrobopt.QuantileMeasure(f, thetaDist, 0.99)
else:
    measure = otrobopt.VarianceMeasure(f, thetaDist)

N = 10
experiment = ot.LHSExperiment(N)
factory = otrobopt.MeasureFactory(experiment)
ot.TESTPREAMBLE()

# Uncertain parameters
distribution = ot.Normal([1.0] * 3, [2.0] * 3, ot.CorrelationMatrix(3))
distribution.setName("Unnamed")
# Model
f = ot.SymbolicFunction(["x", "y", "z"], ["x-1.5*y+2*z"])
# Sampling
size = 100
inputSample = distribution.getSample(size)
outputSample = f(inputSample)
comparisonOperators = [
    ot.Less(), ot.LessOrEqual(),
    ot.Greater(),
    ot.GreaterOrEqual()
]
threshold = 3.0
ot.ResourceMap.SetAsUnsignedInteger(
    "SimulationSensitivityAnalysis-DefaultSampleMargin", 10)
for i in range(4):
    # Analysis based on an event
    X = ot.RandomVector(distribution)
    Y = ot.CompositeRandomVector(f, X)
    event = ot.ThresholdEvent(Y, comparisonOperators[i], threshold)
    algo = ot.SimulationSensitivityAnalysis(event, inputSample, outputSample)
    print("algo=", algo)
    # Perform the analysis
    print("Mean point in event domain=", algo.computeMeanPointInEventDomain())
    print("Importance factors at threshold ", threshold, " =",
          algo.computeImportanceFactors())
Пример #8
0
import openturns as ot
import otrobopt

# First test: theta of dimension 1
thetaDist = ot.Normal(2.0, 0.1)
f_base = ot.SymbolicFunction(['x', 'theta'], ['x*theta'])
f = ot.ParametricFunction(f_base, [1], [1.0])

x = [1.0]

measures = [otrobopt.MeanMeasure(f, thetaDist),
            otrobopt.VarianceMeasure(f, thetaDist),
            otrobopt.WorstCaseMeasure(f, ot.Uniform(-1.0, 4.0)),
            otrobopt.WorstCaseMeasure(f, ot.Uniform(-1.0, 4.0), False),
            otrobopt.JointChanceMeasure(
                f, ot.Normal(1.0, 1.0), ot.GreaterOrEqual(), 0.95),
            otrobopt.IndividualChanceMeasure(
                f, ot.Normal(1.0, 1.0), ot.GreaterOrEqual(), [0.95]),
            otrobopt.MeanStandardDeviationTradeoffMeasure(f, thetaDist, [0.8]),
            otrobopt.QuantileMeasure(f, thetaDist, 0.99)
            ]
aggregated = otrobopt.AggregatedMeasure(measures)
measures.append(aggregated)

for measure in measures:
    print(measure, '(continuous)', measure(x))
    N = 10000
    experiment = ot.LHSExperiment(N)
    factory = otrobopt.MeasureFactory(experiment)
    discretizedMeasure = factory.build(measure)
    print(discretizedMeasure, '(discretized LHS)', discretizedMeasure(x))
Пример #9
0
G = ot.RandomVector(g, X_random_vector)
G.setDescription(['Deviation'])

# In[16]:

G_sample = G.getSample(int(1e3))
G_hist = ot.VisualTest_DrawHistogram(G_sample)
G_hist.setXTitle(G.getDescription()[0])
_ = View(G_hist, bar_kwargs={'label': 'G_sample'})

# # Event

# In[17]:

event = ot.Event(G, ot.GreaterOrEqual(), 30.)
event.setName("deviation > 30 cm")

# # Estimation of the event probability using (crude) Monte Carlo sampling

# In[18]:

g.clearHistory()

# In[19]:

ot.RandomGenerator.SetSeed(0)

# In[20]:

# create a Monte Carlo algorithm
from __future__ import print_function
import openturns as ot
import otrobopt

# First test: theta of dimension 1
thetaDist = ot.Normal(2.0, 0.1)
f_base = ot.SymbolicFunction(['x', 'theta'], ['x*theta'])
f = ot.ParametricFunction(f_base, [1], [1.0])

x = [1.0]

measures = [otrobopt.MeanMeasure(f, thetaDist),
            otrobopt.VarianceMeasure(f, thetaDist),
            otrobopt.WorstCaseMeasure(f, ot.Uniform(-1.0, 4.0)),
            otrobopt.WorstCaseMeasure(f, ot.Uniform(-1.0, 4.0), False),
            otrobopt.JointChanceMeasure(f, ot.Normal(1.0, 1.0), ot.GreaterOrEqual(), 0.95),
            otrobopt.IndividualChanceMeasure(f, ot.Normal(1.0, 1.0), ot.GreaterOrEqual(), [0.95]),
            otrobopt.MeanStandardDeviationTradeoffMeasure(f, thetaDist, [0.8]),
            otrobopt.QuantileMeasure(f, thetaDist, 0.99)
            ]
aggregated = otrobopt.AggregatedMeasure(measures)
measures.append(aggregated)

for measure in measures:
    print(measure, '(continuous)', measure(x))
    N = 10000
    experiment = ot.LHSExperiment(N)
    factory = otrobopt.MeasureFactory(experiment)
    discretizedMeasure = factory.build(measure)
    print(discretizedMeasure, '(discretized LHS)', discretizedMeasure(x))
    N = 4
from __future__ import print_function
import openturns as ot

ot.TESTPREAMBLE()


# Uncertain parameters
distribution = ot.Normal([1.0] * 3, [2.0] * 3, ot.CorrelationMatrix(3))
distribution.setName("Unnamed")
# Model
f = ot.SymbolicFunction(["x", "y", "z"], ["x-1.5*y+2*z"])
# Sampling
size = 100
inputSample = distribution.getSample(size)
outputSample = f(inputSample)
comparisonOperators = [ot.Less(), ot.LessOrEqual(), ot.Greater(), ot.GreaterOrEqual()]
threshold = 3.0
ot.ResourceMap.SetAsUnsignedInteger(
    "SimulationSensitivityAnalysis-DefaultSampleMargin", 10)
for i in range(4):
    # Analysis based on an event
    X = ot.RandomVector(distribution)
    Y = ot.CompositeRandomVector(f, X)
    event = ot.ThresholdEvent(Y, comparisonOperators[i], threshold)
    algo = ot.SimulationSensitivityAnalysis(event, inputSample, outputSample)
    print("algo=", algo)
    # Perform the analysis
    print("Mean point in event domain=",
          algo.computeMeanPointInEventDomain())
    print("Importance factors at threshold ", threshold,
          " =", algo.computeImportanceFactors())
Пример #12
0
# coding: utf-8

import openturns as ot

R = ot.Normal(4., 1.)
R.setDescription("R")

S = ot.Normal(2., 1.)
S.setDescription("S")

g = ot.SymbolicFunction(["R","S"],["R-S"])

inputvector = ot.ComposedDistribution([R,S])
inputRV = ot.RandomVector(inputvector)
outputRV = ot.CompositeRandomVector(g, inputRV)
eventF = ot.Event(outputRV, ot.GreaterOrEqual(), 0) 

# Create the Monte-Carlo algorithm
algoProb = ot.ProbabilitySimulationAlgorithm(eventF)
algoProb.setMaximumOuterSampling(1000)
algoProb.setMaximumCoefficientOfVariation(0.01)
algoProb.run()

# Get the results
resultAlgo = algoProb.getResult()
neval = g.getEvaluationCallsNumber()
print("Number of function calls = %d" %(neval))
pf = resultAlgo.getProbabilityEstimate()
print("Failure Probability = %.4f" % (pf))
level = 0.95
c95 = resultAlgo.getConfidenceLength(level)
Пример #13
0
fileName = 'myStudy.xml'

# Create a Study Object
myStudy = ot.Study()
myStudy.setStorageManager(ot.XMLStorageManager(fileName))

thetaDist = ot.Normal(2.0, 0.1)
f_base = ot.SymbolicFunction(['x', 'theta'], ['x*theta'])
f = ot.ParametricFunction(f_base, [1], [1.0])

measures = [
    otrobopt.MeanMeasure(f, thetaDist),
    otrobopt.VarianceMeasure(f, thetaDist),
    otrobopt.WorstCaseMeasure(f, ot.Uniform(-1.0, 4.0), False),
    otrobopt.JointChanceMeasure(f, ot.Normal(1.0, 1.0), ot.GreaterOrEqual(),
                                0.95),
    otrobopt.IndividualChanceMeasure(f, ot.Normal(1.0, 1.0),
                                     ot.GreaterOrEqual(), [0.95]),
    otrobopt.MeanStandardDeviationTradeoffMeasure(f, thetaDist, [0.8]),
    otrobopt.QuantileMeasure(f, thetaDist, 0.99)
]
aggregated = otrobopt.AggregatedMeasure(measures)
measures.append(aggregated)

for measure in measures:
    myStudy.add('measure' + measure.__class__.__name__, measure)

measure2 = otrobopt.MeanMeasure(f, thetaDist)
measureFunction = otrobopt.MeasureFunction(measure2)
myStudy.add('measureFunction', measureFunction)
Пример #14
0
fileName = 'myStudy.xml'

# Create a Study Object
myStudy = ot.Study()
myStudy.setStorageManager(ot.XMLStorageManager(fileName))

thetaDist = ot.Normal(2.0, 0.1)
f_base = ot.SymbolicFunction(['x', 'theta'], ['x*theta'])
f = ot.ParametricFunction(f_base, [1], [1.0])

measures = [otrobopt.MeanMeasure(f, thetaDist),
            otrobopt.VarianceMeasure(f, thetaDist),
            otrobopt.WorstCaseMeasure(f, ot.Uniform(-1.0, 4.0), False),
            otrobopt.JointChanceMeasure(
                f, ot.Normal(1.0, 1.0), ot.GreaterOrEqual(), 0.95),
            otrobopt.IndividualChanceMeasure(
                f, ot.Normal(1.0, 1.0), ot.GreaterOrEqual(), [0.95]),
            otrobopt.MeanStandardDeviationTradeoffMeasure(f, thetaDist, [0.8]),
            otrobopt.QuantileMeasure(f, thetaDist, 0.99)
            ]
aggregated = otrobopt.AggregatedMeasure(measures)
measures.append(aggregated)

for measure in measures:
    myStudy.add('measure' + measure.__class__.__name__, measure)

measure2 = otrobopt.MeanMeasure(f, thetaDist)
measureFunction = otrobopt.MeasureFunction(measure2)
myStudy.add('measureFunction', measureFunction)