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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
"""
Distribution manipulation
=========================
"""
# %%
# In this example we are going to exhibit some of the services exposed by the distribution objects:
#
# - ask for the dimension, with the method `getDimension` ;
# - extract the marginal distributions, with the method `getMarginal` ;
# - to ask for some properties, with `isContinuous`, `isDiscrete`, `isElliptical` ;
# - to get the copula, with the method `getCopula` ;
# - to ask for some properties on the copula, with the methods `hasIndependentCopula`, `hasEllipticalCopula` ;
# - to evaluate some moments, with `getMean`, `getStandardDeviation`, `getCovariance`, `getSkewness`, `getKurtosis` ;
# - to evaluate the roughness, with the method `getRoughness` ;
# - to get one realization or simultaneously :math:`n` realizations, with the method `getRealization`, `getSample` ;
# - to evaluate the probability content of a given interval, with the method `computeProbability` ;
# - to evaluate a quantile or a complementary quantile, with the method `computeQuantile` ;
# - to evaluate the characteristic function of the distribution ;
# - to evaluate the derivative of the CDF or PDF ;
# - to draw some curves.
# %%
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)
# %%
# Create an 1-d distribution
dist_1 = ot.Normal()
# Create a 2-d distribution
dist_2 = ot.JointDistribution(
[ot.Normal(), ot.Triangular(0.0, 2.0, 3.0)], ot.ClaytonCopula(2.3)
)
# Create a 3-d distribution
copula_dim3 = ot.Student(5.0, 3).getCopula()
dist_3 = ot.JointDistribution(
[ot.Normal(), ot.Triangular(0.0, 2.0, 3.0), ot.Exponential(0.2)], copula_dim3
)
# %%
# Get the dimension fo the distribution
dist_2.getDimension()
# %%
# Get the 2nd marginal
dist_2.getMarginal(1)
# %%
# Get a 2-d marginal
dist_3.getMarginal([0, 1]).getDimension()
# %%
# Ask some properties of the distribution
dist_1.isContinuous(), dist_1.isDiscrete(), dist_1.isElliptical()
# %%
# Get the copula
copula = dist_2.getCopula()
# %%
# Ask some properties on the copula
dist_2.hasIndependentCopula(), dist_2.hasEllipticalCopula()
# %%
# Get the mean vector of the distribution
dist_2.getMean()
# %%
# Get the standard deviation vector of the distribution
dist_2.getStandardDeviation()
# %%
# Get the covariance matrix of the distribution
dist_2.getCovariance()
# %%
# Get the skewness vector of the distribution
dist_2.getSkewness()
# %%
# Get the kurtosis vector of the distribution
dist_2.getKurtosis()
# %%
# Get the roughness of the distribution
dist_1.getRoughness()
# %%
# Get one realization
dist_2.getRealization()
# %%
# Get several realizations
dist_2.getSample(5)
# %%
# Evaluate the PDF at the mean point
dist_2.computePDF(dist_2.getMean())
# %%
# Evaluate the CDF at the mean point
dist_2.computeCDF(dist_2.getMean())
# %%
# Evaluate the complementary CDF
dist_2.computeComplementaryCDF(dist_2.getMean())
# %%
# Evaluate the survival function at the mean point
dist_2.computeSurvivalFunction(dist_2.getMean())
# %%
# Evaluate the PDF on a sample
dist_2.computePDF(dist_2.getSample(5))
# %%
# Evaluate the CDF on a sample
dist_2.computeCDF(dist_2.getSample(5))
# %%
# Evaluate the probability content of an 1-d interval
interval = ot.Interval(-2.0, 3.0)
dist_1.computeProbability(interval)
# %%
# Evaluate the probability content of a 2-d interval
interval = ot.Interval([0.4, -1], [3.4, 2])
dist_2.computeProbability(interval)
# %%
# Evaluate the quantile of order `p=90%`
dist_2.computeQuantile(0.90)
# %%
# and the quantile of order 1-p
dist_2.computeQuantile(0.90, True)
# %%
# Evaluate the quantiles of order p et q
# For example, the quantile `90%` and `95%`
dist_1.computeQuantile([0.90, 0.95])
# %%
# and the quantile of order 1-p and 1-q
dist_1.computeQuantile([0.90, 0.95], True)
# %%
# Evaluate the characteristic function of the distribution (only 1-d)
dist_1.computeCharacteristicFunction(dist_1.getMean()[0])
# %%
# Evaluate the derivatives of the PDF with respect to the parameters at mean
dist_2.computePDFGradient(dist_2.getMean())
# %%
# Evaluate the derivatives of the CDF with respect to the parameters at mean
dist_2.computeCDFGradient(dist_2.getMean())
# %%
# Draw PDF
graph = dist_1.drawPDF()
view = viewer.View(graph)
# %%
# Draw CDF
graph = dist_1.drawCDF()
view = viewer.View(graph)
# %%
# Draw an 1-d quantile curve
# Define the range and the number of points
qMin = 0.2
qMax = 0.6
nbrPoints = 101
quantileGraph = dist_1.drawQuantile(qMin, qMax, nbrPoints)
view = viewer.View(quantileGraph)
# %%
# Draw a 2-d quantile curve
# Define the range and the number of points
qMin = 0.3
qMax = 0.9
nbrPoints = 101
quantileGraph = dist_2.drawQuantile(qMin, qMax, nbrPoints)
view = viewer.View(quantileGraph)
plt.show()
|