Пример #1
0
def test_multivariate_aggregation_with_unmatched_bins_and_dependence(
        multivariate_test_cdfs):
    """Check multivariate aggregation where the outcomes of each variable are completely different,
    and the variables are correlated."""

    marginal_cdf_p, marginal_cdf_v = multivariate_test_cdfs
    dim = len(marginal_cdf_p)

    # Make a correlation matrix with positive correlation between each pair of adjacent variables (needs at least 2D)
    R = ot.CorrelationMatrix(dim)
    for d in range(1, dim):
        R[d - 1, d] = 0.25
    cdf_p, cdf_v = multivariate_marginal_to_univariate_joint_cdf(
        marginal_cdf_p,
        marginal_cdfs_v=marginal_cdf_v,
        copula=ot.NormalCopula(R))
    assert all(np.diff(cdf_v) >= 0) and all(
        np.diff(cdf_p) >= 0)  # Check for non-decreasing cdf
    assert (cdf_v[0] >= 10 * dim
            and cdf_v[-1] <= 100 * dim)  # Check range of aggregated outcomes
    assert cdf_p[0] >= 0 and (cdf_p[-1] < 1 or cdf_p[-1] == approx(1)
                              )  # Check range of cumulative probabilities

    cdf_p_2, cdf_v_2 = multivariate_marginal_to_univariate_joint_cdf(
        marginal_cdf_p,
        marginal_cdfs_v=marginal_cdf_v,
        copula=ot.NormalCopula(R),
        n_draws=1000,
    )
    assert len(cdf_p_2) == 1000
Пример #2
0
def test_multivariate_aggregation():
    """Check different aggregation functions for multivariate aggregation, as well as a copula."""

    # For sum
    marginal_pdfs = [
        [1 / 3, 1 / 3, 1 / 3],
        [1 / 4, 1 / 4, 2 / 4],
        [1 / 6, 1 / 6, 2 / 3],
    ]
    marginal_cdfs = np.cumsum(marginal_pdfs, axis=1)
    cdf_p, cdf_v = multivariate_marginal_to_univariate_joint_cdf(marginal_cdfs,
                                                                 a=10,
                                                                 b=100)
    assert all(np.diff(cdf_v) >= 0) and all(
        np.diff(cdf_p) >= 0)  # Check for non-decreasing cdf
    assert cdf_v[
        0] == 30 and cdf_v[-1] == 300  # Check range of aggregated outcomes
    assert (cdf_p[0] == 1 / 3 * 1 / 4 * 1 / 6
            and cdf_p[-1] == 1)  # Check range of cumulative probabilities

    # For mean
    cdf_p, cdf_v = multivariate_marginal_to_univariate_joint_cdf(
        marginal_cdfs, agg_function=np.mean, a=10, b=100)
    assert all(np.diff(cdf_v) >= 0) and all(
        np.diff(cdf_p) >= 0)  # Check for non-decreasing cdf
    assert cdf_v[
        0] == 10 and cdf_v[-1] == 100  # Check range of aggregated outcomes
    assert (cdf_p[0] == 1 / 3 * 1 / 4 * 1 / 6
            and cdf_p[-1] == 1)  # Check range of cumulative probabilities

    # For a normal copula with a correlation matrix with positive correlation between 1st and 2nd variable
    R = ot.CorrelationMatrix(len(marginal_cdfs))
    R[0, 1] = 0.25
    cdf_p, cdf_v = multivariate_marginal_to_univariate_joint_cdf(
        marginal_cdfs, copula=ot.NormalCopula(R), a=10, b=100)
    assert all(np.diff(cdf_v) >= 0) and all(
        np.diff(cdf_p) >= 0)  # Check for non-decreasing cdf
    assert cdf_v[
        0] == 30 and cdf_v[-1] == 300  # Check range of aggregated outcomes
    assert (cdf_p[0] > 1 / 3 * 1 / 4 * 1 / 6
            and cdf_p[-1] == 1)  # Check range of cumulative probabilities

    # For a normal copula with a correlation matrix with negative correlation between 1st and 2nd variable
    R = ot.CorrelationMatrix(len(marginal_cdfs))
    R[0, 1] = -0.25
    cdf_p, cdf_v = multivariate_marginal_to_univariate_joint_cdf(
        marginal_cdfs, copula=ot.NormalCopula(R), a=10, b=100)
    assert all(np.diff(cdf_v) >= 0) and all(
        np.diff(cdf_p) >= 0)  # Check for non-decreasing cdf
    assert cdf_v[
        0] == 30 and cdf_v[-1] == 300  # Check range of aggregated outcomes
    assert (cdf_p[0] < 1 / 3 * 1 / 4 * 1 / 6
            and cdf_p[-1] == 1)  # Check range of cumulative probabilities
Пример #3
0
def generateSampleWithConditionalIndependance3(size=1000):
    dim = 5
    # 0T4 | 1,2,3 <=> r34 = (-r01*r13*r23*r24+r01*r14*r23^2+r02*r13^2*r24-r02*r13*r14*r23-r03*r12*r13*r24-r03*r12*r14
    # *r23+2*r04*r12*r13*r23+r01*r12*r24+r02*r12*r14+r03*r13*r14+r03*r23*r24-r04*r12^2-r04*r13^2-r04*r23^2-r01*r14-r02
    # *r24+r04)/(r01*r12*r23+r02*r12*r13-r03*r12^2-r01*r13-r02*r23+r03)
    R = ot.CorrelationMatrix(dim)
    R[0, 1] = 0.9
    R[0, 2] = 0.9
    R[0, 3] = 0.9
    R[0, 4] = 0.9
    R[1, 2] = 0.9
    R[1, 3] = 0.9
    R[1, 4] = 0.9
    R[2, 3] = 0.9
    R[2, 4] = 0.95
    R[3,
      4] = (-R[0, 1] * R[1, 3] * R[2, 3] * R[2, 4] +
            R[0, 1] * R[1, 4] * R[2, 3]**2 + R[0, 2] * R[1, 3]**2 * R[2, 4] -
            R[0, 2] * R[1, 3] * R[1, 4] * R[2, 3] - R[0, 3] * R[1, 2] *
            R[1, 3] * R[2, 4] - R[0, 3] * R[1, 2] * R[1, 4] * R[2, 3] +
            2 * R[0, 4] * R[1, 2] * R[1, 3] * R[2, 3] +
            R[0, 1] * R[1, 2] * R[2, 4] + R[0, 2] * R[1, 2] * R[1, 4] +
            R[0, 3] * R[1, 3] * R[1, 4] + R[0, 3] * R[2, 3] * R[2, 4] -
            R[0, 4] * R[1, 2]**2 - R[0, 4] * R[1, 3]**2 -
            R[0, 4] * R[2, 3]**2 - R[0, 1] * R[1, 4] - R[0, 2] * R[2, 4] +
            R[0, 4]) / (R[0, 1] * R[1, 2] * R[2, 3] +
                        R[0, 2] * R[1, 2] * R[1, 3] - R[0, 3] * R[1, 2]**2 -
                        R[0, 1] * R[1, 3] - R[0, 2] * R[2, 3] + R[0, 3])
    copula = ot.NormalCopula(R)
    copula.setDescription(["X" + str(i) for i in range(dim)])
    return copula.getSample(
        size)  # .exportToCSVFile("conditional_independence_04_123.csv")
Пример #4
0
def generateDataForSpecificInstance(size):
  R = ot.CorrelationMatrix(3)
  R[0, 1] = 0.5
  R[0, 2] = 0.45
  collection = [ot.FrankCopula(3.0), ot.NormalCopula(R), ot.ClaytonCopula(2.0)]
  copula = ot.ComposedCopula(collection)
  return copula.getSample(size)
Пример #5
0
def define_distribution():
    """
    Define the distribution of the training example (beam).
    Return a ComposedDistribution object from openTURNS
    """
    sample_E = ot.Sample.ImportFromCSVFile("sample_E.csv")
    kernel_smoothing = ot.KernelSmoothing(ot.Normal())
    bandwidth = kernel_smoothing.computeSilvermanBandwidth(sample_E)
    E = kernel_smoothing.build(sample_E, bandwidth)
    E.setDescription(['Young modulus'])

    F = ot.LogNormal()
    F.setParameter(ot.LogNormalMuSigma()([30000, 9000, 15000]))
    F.setDescription(['Load'])

    L = ot.Uniform(250, 260)
    L.setDescription(['Length'])

    I = ot.Beta(2.5, 4, 310, 450)
    I.setDescription(['Inertia'])

    marginal_distributions = [F, E, L, I]
    SR_cor = ot.CorrelationMatrix(len(marginal_distributions))
    SR_cor[2, 3] = -0.2
    copula = ot.NormalCopula(ot.NormalCopula.GetCorrelationFromSpearmanCorrelation(SR_cor))

    return(ot.ComposedDistribution(marginal_distributions, copula))
Пример #6
0
def test_marginal_distributions_with_residual_probability():
    """Aggregate three time slots with CDFs that are not fully specified.
    That means each has a residual probability that their outcome is higher than the highest value given.
    """

    # Make sure incomplete cdf functions can still be transformed (a higher outcome with cp=1 can be assumed to exist)
    marginal_cdfs = [
        [1 / 3, 2 / 4, 3 / 4],
        [1 / 4, 4 / 6, 5 / 6],
        [1 / 6, 5 / 9, 8 / 9],
    ]
    cdf_p, _ = multivariate_marginal_to_univariate_joint_cdf(marginal_cdfs)
    assert all(np.diff(cdf_p) >= 0)  # Check for non-decreasing cdf
    assert (
        cdf_p[-1] < 1
    )  # Check that the assumed outcome with cp=1 is not actually returned
    a = (
        cdf_p[-1] - cdf_p[-2]
    )  # The probability of the highest outcome for each of the three variables
    b = 1 / 4 * 1 / 6 * 3 / 9  # The probability for independent random variables
    assert a == approx(b)  # Check the expected outcome
    # Make a correlation matrix with negative correlation between the first and second variable
    R = ot.CorrelationMatrix(len(marginal_cdfs))
    R[0, 1] = -0.25
    cdf_p, _ = multivariate_marginal_to_univariate_joint_cdf(
        marginal_cdfs, copula=ot.NormalCopula(R))
    assert all(np.diff(cdf_p) >= 0)  # Check for non-decreasing cdf
    a = (
        cdf_p[-1] - cdf_p[-2]
    )  # The probability of the highest outcome for each of the three variables
    assert (
        a < b
    )  # Check the expected outcome is now lower (if x1 is high, than x2 is less likely to be high)
    # Make a correlation matrix with positive correlation between the first and second variable
    R = ot.CorrelationMatrix(len(marginal_cdfs))
    R[0, 1] = 0.25
    cdf_p, _ = multivariate_marginal_to_univariate_joint_cdf(
        marginal_cdfs, copula=ot.NormalCopula(R))
    assert all(np.diff(cdf_p) >= 0)  # Check for non-decreasing cdf
    a = (
        cdf_p[-1] - cdf_p[-2]
    )  # The probability of the highest outcome for each of the three variables
    assert (
        a > b
    )  # Check the expected outcome is now higher (if x1 is high, than x2 is likely to be high, too)
Пример #7
0
def generateSampleWithConditionalIndependance1(size=1000):
    dim = 3
    # 0T2 | 1 <=> r12 = r02/r01
    R = ot.CorrelationMatrix(dim)
    R[0, 1] = 0.95
    R[0, 2] = 0.9
    R[1, 2] = R[0, 2] / R[0, 1]
    copula = ot.NormalCopula(R)
    copula.setDescription(["X" + str(i) for i in range(dim)])
    return copula.getSample(
        size)  # ".exportToCSVFile("conditional_independence_02_1.csv")
def generate_gaussian_data(ndag, size, r=0.8):
    order = ndag.getTopologicalOrder()
    copulas = []
    for k in range(order.getSize()):
        d = 1 + ndag.getParents(k).getSize()
        R = ot.CorrelationMatrix(d)
        for i in range(d):
            for j in range(i):
                R[i, j] = r
        copulas.append(ot.NormalCopula(R))
    cbn = otagr.ContinuousBayesianNetwork(ndag, [ot.Uniform(0., 1.)]*ndag.getSize(), copulas)
    sample = cbn.getSample(size)
    return sample
Пример #9
0
def generateSampleWithConditionalIndependance2(size=1000):
    dim = 4
    # 0T3 | 1,2 <=> r23 = (r03*(r12^2-1)+r13*(r01-r02*r12)) / (r01*r12-r02)
    R = ot.CorrelationMatrix(dim)
    R[0, 1] = 0.9
    R[0, 2] = 0.9
    R[0, 3] = 0.9
    R[1, 2] = 0.9
    R[1, 3] = 0.95
    R[2, 3] = (R[0, 3] * (R[1, 2]**2 - 1.0) + R[1, 3] *
               (R[0, 1] - R[0, 2] * R[1, 2])) / (R[0, 1] * R[1, 2] - R[0, 2])
    copula = ot.NormalCopula(R)
    copula.setDescription(["X" + str(i) for i in range(dim)])
    return copula.getSample(
        size)  # .exportToCSVFile("conditional_independence_03_12.csv")
 def test_CrossCutDistribution2(self):
     # Create a Funky distribution
     corr = ot.CorrelationMatrix(2)
     corr[0, 1] = 0.2
     copula = ot.NormalCopula(corr)
     x1 = ot.Normal(-1.0, 1.0)
     x2 = ot.Normal(2.0, 1.0)
     x_funk = ot.ComposedDistribution([x1, x2], copula)
     # Create a Punk distribution
     x1 = ot.Normal(1.0, 1.0)
     x2 = ot.Normal(-2.0, 1.0)
     x_punk = ot.ComposedDistribution([x1, x2], copula)
     distribution = ot.Mixture([x_funk, x_punk], [0.5, 1.0])
     referencePoint = distribution.getMean()
     crossCut = otbenchmark.CrossCutDistribution(distribution)
     # Avoid failing on CircleCi
     # _tkinter.TclError: no display name and no $DISPLAY environment variable
     try:
         _ = crossCut.drawMarginalPDF()
         _ = crossCut.drawConditionalPDF(referencePoint)
     except Exception as e:
         print(e)
Пример #11
0
    def __init__(self):
        self.dim = 4  # number of inputs
        # Young's modulus E
        self.E = ot.Beta(0.9, 3.5, 65.0e9, 75.0e9)  # in N/m^2
        self.E.setDescription("E")
        self.E.setName("Young modulus")

        # Load F
        self.F = ot.LogNormal()  # in N
        self.F.setParameter(ot.LogNormalMuSigma()([300.0, 30.0, 0.0]))
        self.F.setDescription("F")
        self.F.setName("Load")

        # Length L
        self.L = ot.Uniform(2.5, 2.6)  # in m
        self.L.setDescription("L")
        self.L.setName("Length")

        # Moment of inertia I
        self.I = ot.Beta(2.5, 4.0, 1.3e-7, 1.7e-7)  # in m^4
        self.I.setDescription("I")
        self.I.setName("Inertia")

        # physical model
        self.model = ot.SymbolicFunction(['E', 'F', 'L', 'I'],
                                         ['F*L^3/(3*E*I)'])

        # correlation matrix
        self.R = ot.CorrelationMatrix(self.dim)
        self.R[2, 3] = -0.2
        self.copula = ot.NormalCopula(
            ot.NormalCopula.GetCorrelationFromSpearmanCorrelation(self.R))
        self.distribution = ot.ComposedDistribution(
            [self.E, self.F, self.L, self.I], self.copula)

        # special case of an independent copula
        self.independentDistribution = ot.ComposedDistribution(
            [self.E, self.F, self.L, self.I])
Пример #12
0
def hill_climbing(D, max_parents=4, restart=1):
    N = D.getDimension()
    # Compute the estimate of the gaussian copula
    kendall_tau = D.computeKendallTau()
    #print(kendall_tau)
    pearson_r = ot.CorrelationMatrix(np.sin((np.pi / 2) * kendall_tau))

    # Create the gaussian copula with parameters pearson_r
    # if pearson_r isn't PSD, a regularization is done
    eps = 1e-6
    done = False
    while not done:
        try:
            gaussian_copula = ot.NormalCopula(pearson_r)
            done = True
        except:
            print("Regularization")
            for i in range(pearson_r.getDimension()):
                for j in range(i):
                    pearson_r[i, j] /= 1 + eps

    # Initialization
    G = du.create_empty_dag(N)
    score = sc.bic_score(D, gaussian_copula, G)

    best_graph = G
    best_score = score

    for r in range(restart):
        if r != 0:
            G = du.create_random_dag(N, max_parents)
        G, score = one_hill_climbing(D, gaussian_copula, G, max_parents)
        if score > best_score:
            best_graph = G
            best_score = score

    return gaussian_copula, best_graph, best_score
Пример #13
0
#! /usr/bin/env python

import openturns as ot

ot.TESTPREAMBLE()

# Instantiate one distribution object
dim = 3
R = ot.CorrelationMatrix(dim)
for i in range(dim - 1):
    R[i, i + 1] = 0.25
copula = ot.SklarCopula(
    ot.Distribution(ot.Normal([1.0, 2.0, 3.0], [2.0, 3.0, 1.0], R)))
copulaRef = ot.NormalCopula(R)
print("Copula ", repr(copula))
print("Copula ", copula)
print("Mean      =", repr(copula.getMean()))
print("Mean (ref)=", repr(copulaRef.getMean()))
ot.ResourceMap.SetAsUnsignedInteger("GaussKronrod-MaximumSubIntervals", 20)
ot.ResourceMap.SetAsScalar("GaussKronrod-MaximumError",  1.0e-4)
print("Covariance      =", repr(copula.getCovariance()))
ot.ResourceMap.SetAsUnsignedInteger("GaussKronrod-MaximumSubIntervals", 100)
ot.ResourceMap.SetAsScalar("GaussKronrod-MaximumError",  1.0e-12)
print("Covariance (ref)=", repr(copulaRef.getCovariance()))

# Is this copula an elliptical distribution?
print("Elliptical distribution= ", copula.isElliptical())

# Is this copula elliptical ?
print("Elliptical copula= ", copula.hasEllipticalCopula())
Пример #14
0
# %%
graph.add(histo)
view = viewer.View(graph)

# %%
# Draw a cloud
# ------------
#
# The `Cloud` class creates clouds of bidimensional points. To demonstrate it, let us create two gaussian distributions in two dimensions.

# %%
# Create a Funky distribution
corr = ot.CorrelationMatrix(2)
corr[0, 1] = 0.2
copula = ot.NormalCopula(corr)
x1 = ot.Normal(-1., 1)
x2 = ot.Normal(2, 1)
x_funk = ot.ComposedDistribution([x1, x2], copula)

# %%
# Create a Punk distribution
x1 = ot.Normal(1., 1)
x2 = ot.Normal(-2, 1)
x_punk = ot.ComposedDistribution([x1, x2], copula)

# %%
# Let us mix these two distributions.

# %%
mixture = ot.Mixture([x_funk, x_punk], [0.5, 1.])
F.setDescription("F")
# Length L
L = ot.Uniform(250., 260.) # in cm
L.setDescription("L")
# Moment of inertia I
I = ot.Beta(2.5, 1.5, 310, 450) # in cm^4
I.setDescription("I")

# %%
# Finally, we define the dependency using a `NormalCopula`.

# %%
dim = 4 # number of inputs
R = ot.CorrelationMatrix(dim)
R[2, 3] = -0.2 
myCopula = ot.NormalCopula(ot.NormalCopula.GetCorrelationFromSpearmanCorrelation(R))
myDistribution = ot.ComposedDistribution([E, F, L, I], myCopula)

# %%
# Create the design of experiments
# --------------------------------

# %%
# We consider a simple Monte-Carlo sampling as a design of experiments. This is why we generate an input sample using the `getSample` method of the distribution. Then we evaluate the output using the `model` function.

# %%
sampleSize_train = 10
X_train = myDistribution.getSample(sampleSize_train)
Y_train = model(X_train)

# %%
Пример #16
0
        # fails outside notebook
        import pyAgrum.lib.notebook as gnb
        gnb.showInference(model, evs=evs, size=size)
    except ImportError:
        pass

# **Probabilistic model**

# Marginal distributions
Torque = ot.LogNormal(0.0, 0.25)
Angle = ot.TruncatedNormal(0.0, 2.0, -8.0, 8.0)
Joint = ot.Uniform(1.8, 2.2)

# Dependence
rho = 0.5
TorqueAngleCopula = ot.NormalCopula(ot.CorrelationMatrix(2, [1.0, rho, rho, 1.0]))
copula = ot.ComposedCopula([TorqueAngleCopula, ot.IndependentCopula(1)])

# Joint distribution if needed
TorqueAngle = ot.ComposedDistribution([Torque, Angle], TorqueAngleCopula)
fullDistribution = ot.ComposedDistribution([Torque, Angle, Joint], copula)

# Leakage angle (rd)
angleMax = 5.0

# Leakage joint (mm)
jointMin = 2.0
jointSpread = 0.1

# Vibration torque (kN.m)
torqueSpread = 2.0
"""
Create the ordinal sum of copulas
=================================
"""
# %%
# In this example we are going to create an ordinal sum of copulas.

# %%
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)

# %%
# Create a collection of copulas
collection = [ot.GumbelCopula(2), ot.NormalCopula(2)]

# %%
# Merge the copulas
bounds = [0.3]
copula = ot.OrdinalSumCopula(collection, bounds)
print(copula)

# %%
# Draw PDF
graph = copula.drawPDF([512]*2)
graph.setXTitle('x')
graph.setYTitle('y')
graph.setLegendPosition('')
view = viewer.View(graph)
plt.show()
Пример #18
0
def sample_from_conf(
    var_conf: dict, corr_conf: dict, n_sample: int, seed: int = None
) -> pd.DataFrame:
    """
    Generate dataset with n_sample form configuration file var_conf.

    Parameters
    ----------
    var_conf: dict
        Configuration file of the variables (correlations,
        marginal distributions, rounding)
    n_sample: int
        Number of row in output dataset
    seed: int, optional
        Optional seed for replicability

    Outputs
    -------
    df_sample: pd.DataFrame
        dataset generated from conf files
    """

    ## Retrieve target variable
    var_list = list(var_conf.keys())
    target_var = var_list[-1]
    i_target_var = len(var_list) - 1
    assert var_conf[target_var]["corr"] is None  # Make sure that correlation
    # parameter is set to None for the target variable.

    ## Extract var to i_var dict
    var_dict = {}
    for i_var, var in enumerate(var_list):
        var_dict[var] = i_var

    ## Define marginal distributions of each variable
    marginals = []
    for var in var_list:
        marginals.append(var_conf[var]["marg"])

    ## Define correlations with target variable
    R = ot.CorrelationMatrix(len(var_list))
    for i_var, var in enumerate(var_list):
        if var != target_var:
            R[i_var, i_target_var] = var_conf[var]["corr"]

    ## Define correlations within explanatory variables
    for key, value in corr_conf.items():

        i_min = min(var_dict[key[0]], var_dict[key[1]])
        i_max = max(var_dict[key[0]], var_dict[key[1]])

        R[i_min, i_max] = value

    ## Build distribution and sample
    copula = ot.NormalCopula(R)
    distribution = ot.ComposedDistribution(marginals, copula)

    if seed is not None:
        ot.RandomGenerator.SetSeed(seed)

    df_sample = pd.DataFrame(
        np.array(distribution.getSample(n_sample)), columns=var_list
    )

    ## Apply bounds
    for var in var_list:
        if var_conf[var]["bounds"] is not None:
            df_sample[var] = df_sample[var].clip(
                var_conf[var]["bounds"][0], var_conf[var]["bounds"][1]
            )

    ## Applys rounding
    for var in var_list:
        df_sample[var] = df_sample[var].round(var_conf[var]["round"])

    ## Apply post-processinf

    df_sample = post_process_generated_dataset(df_sample)

    return df_sample
Пример #19
0
#! /usr/bin/env python

from __future__ import print_function
import openturns as ot

ot.TESTPREAMBLE()

model = ot.SymbolicFunction(['x0', 'x1', 'x2', 'x3'], ['-(6+x0^2-x1+x2+3*x3)'])
dim = model.getInputDimension()
marginals = [ot.Normal(5.0, 3.0) for i in range(dim)]
distribution = ot.ComposedDistribution(
    marginals, ot.ComposedCopula([ot.ClaytonCopula(),
                                  ot.NormalCopula()]))
#distribution = ot.Normal([5]*dim, [3]*dim, ot.CorrelationMatrix(dim))
#distribution = ot.ComposedDistribution(marginals, ot.IndependentCopula(dim))
distribution.setDescription(['marginal' + str(i) for i in range(dim)])
vect = ot.RandomVector(distribution)
output = ot.CompositeRandomVector(model, vect)
event = ot.Event(output, ot.Greater(), 0.0)
solver = ot.Cobyla()
solver.setMaximumEvaluationNumber(200)
solver.setMaximumAbsoluteError(1.0e-10)
solver.setMaximumRelativeError(1.0e-10)
solver.setMaximumResidualError(1.0e-10)
solver.setMaximumConstraintError(1.0e-10)
algo = ot.FORM(solver, event, distribution.getMean())
algo.run()
result = algo.getResult()
hasoferReliabilityIndexSensitivity = result.getHasoferReliabilityIndexSensitivity(
)
print(hasoferReliabilityIndexSensitivity)
print('output_YO=', model.getOutputByName('Y0'))
print('input_XO=', model.getInputByName('X0'))
print('inputs names=', model.getInputNames())
print('outputs names=', model.getOutputNames())
print('hasY0', model.hasOutputNamed('Y0'))
print('hasX0', model.hasInputNamed('X0'))

# set attributs values
# in
model.setInputs(inputs)
model.setOutputs(outputs)
model.setDistribution('X1', ot.LogNormal())
model.setFiniteDifferenceStep('X1', 1e-5)
R = ot.CorrelationMatrix(2)
R[0, 1] = 0.25
model.setCopula(['X0', 'X1'], ot.NormalCopula(R))
print('inputs=', model.getInputs())
print('stochastic var=', model.getStochasticInputNames())
print('distribution=', model.getDistribution())
print('copula=', model.getCopula())
# out
model.setFormulas(['sin(X0)+8*X1+0.5'])
print('outputs=', model.getOutputs())

# add variables
# in
X2 = persalys.Input('X2', 10)
model.addInput(X2)
model.setInputName('X2', 'X_2')
print('inputs=', model.getInputs())
print('copula=', model.getCopula())
import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View

myColl = [ot.ClaytonCopula(0.3), ot.NormalCopula(3)]
myMergedCop = ot.ComposedCopula(myColl)
myMergedCop.setDescription(['$u_1$', '$u_2$', '$u_3$', '$u_4$', '$u_5$'])
graphPDF = myMergedCop.drawMarginal2DPDF(0, 1, [0.0] * 2, [1.0] * 2, [100] * 2)
graphPDF.setXTitle('$u_1$')
graphPDF.setYTitle('$u_2$')
graphCDF = myMergedCop.drawMarginal2DCDF(0, 1, [0.0] * 2, [1.0] * 2, [100] * 2)
graphCDF.setXTitle('$u_1$')
graphCDF.setYTitle('$u_2$')

fig = plt.figure(figsize=(10, 4))
pdf_axis = fig.add_subplot(121)
cdf_axis = fig.add_subplot(122)
pdf_axis.set_xlim(auto=True)
cdf_axis.set_xlim(auto=True)

View(graphPDF, figure=fig, axes=[pdf_axis], add_legend=True)
View(graphCDF, figure=fig, axes=[cdf_axis], add_legend=True)
fig.suptitle("ComposedCopula(Clayton(0.3), NormalCopula(3)): pdf and cdf")
Пример #22
0
"""
Assemble copulas
================
"""
# %%
# In this example we are going to merge a collection of independent copulas into one.
#

# %%
from __future__ import print_function
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)

# %%
# create a collection of copulas
R = ot.CorrelationMatrix(3)
R[0, 1] = 0.5
R[0, 2] = 0.25
collection = [ot.FrankCopula(3.0), ot.NormalCopula(R), ot.ClaytonCopula(2.0)]

# %%
# merge the copulas
copula = ot.ComposedCopula(collection)
print(copula)
              distribution_d70, distribution_h_exit,
              distribution_k,
              distribution_L, distribution_m_p]
distribution_p = ot.ComposedDistribution(marginals_p)

#create marginal distribution
marginals = [distribution_D, distribution_D_cover,
             distribution_d70, distribution_h_exit,
             distribution_i_ch, distribution_k,
             distribution_L, distribution_m_p, 
             distribution_m_u]

#create copula uplift
RS_u = ot.CorrelationMatrix(len(marginals_u))
R_u = ot.NormalCopula.GetCorrelationFromSpearmanCorrelation(RS_u)
copula_u = ot.NormalCopula(R_u)

#create copula heave
RS_h = ot.CorrelationMatrix(len(marginals_h))
R_h = ot.NormalCopula.GetCorrelationFromSpearmanCorrelation(RS_h)
copula_h = ot.NormalCopula(R_h)

#create copula piping
RS_p = ot.CorrelationMatrix(len(marginals_p))
R_p = ot.NormalCopula.GetCorrelationFromSpearmanCorrelation(RS_p)
copula_p = ot.NormalCopula(R_p)

#create copula
RS = ot.CorrelationMatrix(len(marginals))
R = ot.NormalCopula.GetCorrelationFromSpearmanCorrelation(RS)
copula = ot.NormalCopula(R)
Пример #24
0
# First test: comparison with a Normal distribution with block-diagonal
# correlation
R0 = ot.CorrelationMatrix(2)
R0[0, 1] = 0.5
R1 = ot.CorrelationMatrix(3)
R1[0, 1] = 0.2
R1[0, 2] = 0.1
R1[1, 2] = 0.15
R2 = ot.CorrelationMatrix(2)
R2[0, 1] = 0.3
collection = [ot.Normal([0.0]*2, [1.0]*2, R0),
              ot.Normal([0.0]*3, [1.0]*3, R1),
              ot.Normal([0.0]*2, [1.0]*2, R2)]
distribution = ot.BlockIndependentDistribution(collection)
copulaCollection = [ot.NormalCopula(R0),
                    ot.NormalCopula(R1),
                    ot.NormalCopula(R2)]
copula = ot.ComposedCopula(copulaCollection)
ref = ot.ComposedDistribution([ot.Normal(0.0, 1.0)]*7, copula)

# Define a point
point = [0.3]*distribution.getDimension()
print("Point= ", point)

# Show PDF and CDF of point
DDF = distribution.computeDDF(point)
print("ddf      =", DDF)
print("ddf (ref)=", ref.computeDDF(point))
PDF = distribution.computePDF(point)
print("pdf      =%.5f" % PDF)
import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View

myCop1 = ot.GumbelCopula(2)
myCop2 = ot.NormalCopula(2)
alpha = 0.3
myOrdSumCop = ot.OrdinalSumCopula([myCop1, myCop2], [alpha])
myOrdSumCop.setDescription(['$u_1$', '$u_2$'])
graphPDF = myOrdSumCop.drawPDF()
graphCDF = myOrdSumCop.drawCDF()

fig = plt.figure(figsize=(8, 4))
plt.suptitle("Ordinal Sum of Copulas: Gumbel(2) and Normal(2): pdf and cdf")
pdf_axis = fig.add_subplot(121)
cdf_axis = fig.add_subplot(122)
pdf_axis.set_xlim(auto=True)
cdf_axis.set_xlim(auto=True)

View(graphPDF, figure=fig, axes=[pdf_axis], add_legend=True)
View(graphCDF, figure=fig, axes=[cdf_axis], add_legend=True)
Пример #26
0
      [(b * b + a * b * rho) / covTh, b * b / covTh]]

# Model
inputName = ["X1", "X2", "a", "b"]
formula = ["a * X1 + b * X2"]

full = ot.SymbolicFunction(inputName, formula)
model = ot.ParametricFunction(full, [2, 3], [a, b])

# Input distribution
distribution = ot.ComposedDistribution([ot.Normal()] * inputDimension)

# Correlated input distribution
S = ot.CorrelationMatrix(inputDimension)
S[1, 0] = 0.3
R = ot.NormalCopula().GetCorrelationFromSpearmanCorrelation(S)
myCopula = ot.NormalCopula(R)
myCorrelatedInputDistribution = ot.ComposedDistribution(
    [ot.Normal()] * inputDimension, myCopula)

sample = myCorrelatedInputDistribution.getSample(2000)

# Orthogonal basis
enumerateFunction = ot.LinearEnumerateFunction(inputDimension)
productBasis = ot.OrthogonalProductPolynomialFactory(
    [ot.HermiteFactory()] * inputDimension, enumerateFunction)
# Adaptive strategy
adaptiveStrategy = ot.FixedStrategy(
    productBasis, enumerateFunction.getStrataCumulatedCardinal(4))
# Projection strategy
samplingSize = 250
Пример #27
0
#    0 & 0 & 1 & -0.2 \\
#    0 & 0 & -0.2 & 1 \\
#    \end{pmatrix}

# %%
# We implement this correlation:

# Create the Spearman correlation matrix of the input random vector
RS = ot.CorrelationMatrix(4)
RS[2, 3] = -0.2

# Evaluate the correlation matrix of the Normal copula from RS
R = ot.NormalCopula.GetCorrelationFromSpearmanCorrelation(RS)

# Create the Normal copula parametrized by R
mycopula = ot.NormalCopula(R)

# %%
# And we endly create the composed input probability distribution.
inputDistribution = ot.ComposedDistribution([E, F, L, I], mycopula)
inputDistribution.setDescription(("E", "F", "L", "I"))

# %%
# Create the event whose probability we want to estimate:

inputRandomVector = ot.RandomVector(inputDistribution)
outputVariableOfInterest = ot.CompositeRandomVector(model_fmu,
                                                    inputRandomVector)

threshold = 30
event = ot.ThresholdEvent(outputVariableOfInterest, ot.Greater(), threshold)
Пример #28
0
I = ot.Beta(2.5, 4, 310, 450)
I.setDescription(['Inertia'])

# We now fix the order of the marginal distributions in the joint distribution. Order must match in the implementation of the physical model (to come).

# In[6]:

marginal_distributions = [F, E, L, I]

# Let then define the dependence structure as a Normal copula with a single non-zero Spearman correlation between components 2 and 3 of the final random vector, that is $L$ and $I$.

# In[7]:

SR_cor = ot.CorrelationMatrix(len(marginal_distributions))
SR_cor[2, 3] = -0.2
copula = ot.NormalCopula(
    ot.NormalCopula.GetCorrelationFromSpearmanCorrelation(SR_cor))

# Eventually the input joint distribution is defined as a *composed distribution*.

# In[8]:

X_distribution = ot.ComposedDistribution(marginal_distributions, copula)

# ... And let's make a *random vector* out of this distribution.

# In[9]:

X_random_vector = ot.RandomVector(X_distribution)

# ## Limit-state function
import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View
if ot.Beta().__class__.__name__ == 'ComposedDistribution':
    correlation = ot.CorrelationMatrix(2)
    correlation[1, 0] = 0.25
    aCopula = ot.NormalCopula(correlation)
    marginals = [ot.Normal(1.0, 2.0), ot.Normal(2.0, 3.0)]
    distribution = ot.ComposedDistribution(marginals, aCopula)
elif ot.Beta().__class__.__name__ == 'CumulativeDistributionNetwork':
    distribution = ot.CumulativeDistributionNetwork([ot.Normal(2),ot.Dirichlet([0.5, 1.0, 1.5])], ot.BipartiteGraph([[0,1], [0,1]]))
elif ot.Beta().__class__.__name__ == 'Histogram':
    distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15])
else:
    distribution = ot.Beta()
dimension = distribution.getDimension()
if dimension == 1:
    distribution.setDescription(['$x$'])
    pdf_graph = distribution.drawPDF()
    cdf_graph = distribution.drawCDF()
    fig = plt.figure(figsize=(10, 4))
    plt.suptitle(str(distribution))
    pdf_axis = fig.add_subplot(121)
    cdf_axis = fig.add_subplot(122)
    View(pdf_graph, figure=fig, axes=[pdf_axis], add_legend=False)
    View(cdf_graph, figure=fig, axes=[cdf_axis], add_legend=False)
elif dimension == 2:
    distribution.setDescription(['$x_1$', '$x_2$'])
    pdf_graph = distribution.drawPDF()
    fig = plt.figure(figsize=(10, 5))
    plt.suptitle(str(distribution))
Пример #30
0
#
# It consists in detecting a linear relation between two scalar samples.

# %%
from __future__ import print_function
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)

# %%
# Generate a sample of dimension 3 with component 0 correlated to component 2
marginals = [ot.Normal()] * 3
S = ot.CorrelationMatrix(3)
S[0, 2] = 0.9
copula = ot.NormalCopula(S)
distribution = ot.ComposedDistribution(marginals, copula)
sample = distribution.getSample(30)

# %%
# Split it in two samples: firstSample of dimension=2, secondSample of dimension=1
firstSample = sample[:, :2]
secondSample = sample[:, 2]

# %%
# Test independance of each components of firstSample against secondSample
test_results = ot.LinearModelTest.FullRegression(firstSample, secondSample)
for i in range(len(test_results)):
    print('Component', i, 'is independent?', test_results[i].getBinaryQualityMeasure(),
          'p-value=%.6g' % test_results[i].getPValue(),
          'threshold=%.6g' % test_results[i].getThreshold())