1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
"""
Specify a simulation algorithm
==============================
"""
# %%
# In this example we are going to parameterize a simulation algorithm:
#
# - parameters linked to the number of points generated
# - the precision of the probability estimator
# - the sample storage strategy
# - using callbacks to monitor progress and stopping criteria.
#
# %%
import openturns as ot
# %%
# Create the joint distribution of the parameters.
# %%
distribution_R = ot.LogNormalMuSigma(300.0, 30.0, 0.0).getDistribution()
distribution_F = ot.Normal(75e3, 5e3)
marginals = [distribution_R, distribution_F]
distribution = ot.JointDistribution(marginals)
# %%
# Create the model.
# %%
model = ot.SymbolicFunction(["R", "F"], ["R-F/(pi_*100.0)"])
# %%
# Create the event whose probability we want to estimate.
# %%
vect = ot.RandomVector(distribution)
G = ot.CompositeRandomVector(model, vect)
event = ot.ThresholdEvent(G, ot.Less(), 0.0)
# %%
# Create a Monte Carlo algorithm.
# %%
experiment = ot.MonteCarloExperiment()
algo = ot.ProbabilitySimulationAlgorithm(event, experiment)
# %%
# Criteria 1: Define the Maximum Coefficient of variation of the probability estimator.
# %%
algo.setMaximumCoefficientOfVariation(0.05)
# %%
# Criteria 2: Define the number of iterations of the simulation.
# %%
algo.setMaximumOuterSampling(int(1e4))
# %%
# The block size parameter represents the number of samples evaluated per iteration, useful for parallelization.
# %%
algo.setBlockSize(2)
# %%
# HistoryStrategy to store the values of the probability used to draw the convergence graph.
# %%
# Null strategy
algo.setConvergenceStrategy(ot.Null())
# Full strategy
algo.setConvergenceStrategy(ot.Full())
# Compact strategy: N points
N = 1000
algo.setConvergenceStrategy(ot.Compact(N))
# %%
# Use a callback to display the progress every 10%.
# %%
def progress(p):
if p >= progress.t:
progress.t += 10.0
print("progress=", p, "%")
return False
progress.t = 10.0
algo.setProgressCallback(progress)
# %%
# Use a callback to stop the simulation.
# %%
def stop():
# here we never stop, but we could
return False
algo.setStopCallback(stop)
# %%
algo.run()
# %%
# Retrieve results.
# %%
result = algo.getResult()
probability = result.getProbabilityEstimate()
print("Pf=", probability)
|