Esempio n. 1
0
def discretizeBernoulliFromConditionalProbability(conditionalProbability,
                                                  conditioningDistribution,
                                                  ticks,
                                                  useSlowIntegration=True,
                                                  nodesNumber=32):
    conditioningDimension = conditioningDistribution.getDimension()
    if useSlowIntegration:
        # Accurate but slow
        integrator = ot.IteratedQuadrature()
    else:
        # Less accurate for non-smooth integrand but fast
        ot.ResourceMap.SetAsUnsignedInteger(
            "GaussLegendre-DefaultMarginalIntegrationPointsNumber",
            nodesNumber)
        integrator = ot.GaussLegendre(conditioningDimension)

    # Add the range bounds to the given ticks
    lower = list(conditioningDistribution.getRange().getLowerBound())
    upper = list(conditioningDistribution.getRange().getUpperBound())
    # Add the range bounds to the given ticks
    lower = conditioningDistribution.getRange().getLowerBound()
    upper = conditioningDistribution.getRange().getUpperBound()
    expandedTicks = [0] * len(ticks)
    for i in range(conditioningDimension):
        expandedTicks[i] = [lower[i]] + ticks[i] + [upper[i]]
    # Now perform the full discretization
    lengths = [(len(t) - 1) for t in expandedTicks]
    tuples = ot.Tuples(lengths).generate()
    probabilitiesTrue = [0] * len(tuples)

    def kernel(x):
        x = np.array(x)
        return conditionalProbability(x) * np.array(
            conditioningDistribution.computePDF(x[:, 0:conditioningDimension]))

    for i in range(len(tuples)):
        tuple = tuples[i]
        aConditioning = [
            expandedTicks[j][tuple[j]] for j in range(conditioningDimension)
        ]
        bConditioning = [
            expandedTicks[j][tuple[j] + 1]
            for j in range(conditioningDimension)
        ]
        den = conditioningDistribution.computeProbability(
            ot.Interval(aConditioning, bConditioning))
        if den > 0.0:
            num = integrator.integrate(
                ot.PythonFunction(conditioningDimension, 1,
                                  func_sample=kernel),
                ot.Interval(aConditioning, bConditioning))[0]
            probabilitiesTrue[i] = min(1.0, num / den)
        probabilities = ot.Point([1.0 - p for p in probabilitiesTrue] +
                                 probabilitiesTrue)
    return probabilities
Esempio n. 2
0
def discretizeFromConditionalDensity(conditionalDensity,
                                     conditioningDistribution,
                                     ticks,
                                     useSlowIntegration=True,
                                     nodesNumber=32):
    fullDimension = conditioningDistribution.getDimension() + 1
    if useSlowIntegration:
        # Accurate but slow
        integrator = ot.IteratedQuadrature()
    else:
        # Less accurate for non-smooth integrand but fast
        ot.ResourceMap.SetAsUnsignedInteger(
            "GaussLegendre-DefaultMarginalIntegrationPointsNumber",
            nodesNumber)
        integrator = ot.GaussLegendre(fullDimension)
    # Add the range bounds to the given ticks
    lower = list(conditioningDistribution.getRange().getLowerBound())
    upper = list(conditioningDistribution.getRange().getUpperBound())
    # For the conditioned variable it has to be estimated. We assume that the given
    # tick range is a correct margin to get the lower and upper bounds
    conditionedMin = min(ticks[fullDimension - 1])
    conditionedMax = max(ticks[fullDimension - 1])
    delta = conditionedMax - conditionedMin
    lower = lower + [conditionedMin - delta]
    upper = upper + [conditionedMax + delta]
    expandedTicks = [0] * fullDimension
    for i in range(fullDimension):
        expandedTicks[i] = [lower[i]] + ticks[i] + [upper[i]]
    # Now perform the full discretization
    lengths = [(len(t) - 1) for t in expandedTicks]
    tuples = ot.Tuples(lengths).generate()
    probabilities = ot.Point(len(tuples))

    def kernel(x):
        x = np.array(x)
        return conditionalDensity(x) * np.array(
            conditioningDistribution.computePDF(x[:, 0:fullDimension - 1]))

    for i in range(len(tuples)):
        tuple = tuples[i]
        aFull = [expandedTicks[j][tuple[j]] for j in range(fullDimension)]
        bFull = [expandedTicks[j][tuple[j] + 1] for j in range(fullDimension)]
        num = integrator.integrate(
            ot.PythonFunction(fullDimension, 1, func_sample=kernel),
            ot.Interval(aFull, bFull))[0]
        probabilities[i] = num
    return probabilities
Esempio n. 3
0
import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View


f = ot.SymbolicFunction(['x'], ['sin(x)'])
a = -2.5
b = 4.5
algo = ot.GaussLegendre([20])
value, nodes = algo.integrateWithNodes(f, ot.Interval(a, b))

g = f.draw(a, b, 512)
lower = ot.Cloud(nodes, ot.Sample(nodes.getSize(), 1))
lower.setColor("magenta")
lower.setPointStyle('circle')
g.add(lower)

fig = plt.figure(figsize=(8, 4))
plt.suptitle(
    r"GaussLegendre example: $\int_{-5/2}^{9/2}\sin(t)\,dt=$" + str(value[0]))
axis = fig.add_subplot(111)
axis.set_xlim(auto=True)
View(g, figure=fig, axes=[axis], add_legend=False)