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
|
"""
Estimate moments iteratively
============================
"""
# %%
# In this example, we use the :class:`~openturns.IterativeMoments` class
# to compute iterative statistics.
# This class stores central moments up to a prescribed order iteratively.
# Then several statistics based on the moments are available depending on the
# chosen order.
# %%
import openturns as ot
import openturns.viewer as otv
# %%
# We first create a one-dimensional Gaussian random variable to generate data.
dim = 1
distNormal = ot.Normal(dim)
# %%
# Then we use the central moments up to order 4 with the
# :class:`~openturns.IterativeMoments` class by giving the order (here 4)
# and the dimension (here 1):
# %%
order = 4
iterMoments = ot.IterativeMoments(order, dim)
# %%
# We can now perform the simulations.
# The :class:`~openturns.IterativeMoments` object stores the central
# moments iteratively.
# We first increment the object with one :class:`~openturns.Point` at a time.
# At any given step the current mean is obtained thanks to
# the :meth:`~openturns.IterativeMoments.getMean` method and the
# current number of iterations is given by
# the :meth:`~openturns.IterativeMoments.getIterationNumber` method.
size = 2000
meanEvolution = ot.Sample()
for i in range(size):
point = distNormal.getRealization()
iterMoments.increment(point)
meanEvolution.add(iterMoments.getMean())
# %%
# We display the evolution of the mean.
iterationSample = ot.Sample.BuildFromPoint(range(1, size + 1))
curve = ot.Curve(iterationSample, meanEvolution)
graph = ot.Graph("Evolution of the mean", "iteration nb", "mean", True)
graph.add(curve)
graph.setLogScale(ot.GraphImplementation.LOGX)
view = otv.View(graph)
# %%
# We can also increment with a :class:`~openturns.Sample`.
# %%
sample = distNormal.getSample(size)
iterMoments.increment(sample)
# %%
# We print the total number of iterations and the mean.
print("Total number of iteration: ", iterMoments.getIterationNumber())
print("Mean: ", iterMoments.getMean())
# %%
# For the order of the `iterMoments` object is 4, we also have access to
# other statistics such as the variance (order 2), the skewness (order 3)
# or the kurtosis (order 4). For instance, a specified order of 3 would leave
# only the variance and the skewness available.
print("Variance: ", iterMoments.getVariance())
print("Skewness: ", iterMoments.getSkewness())
print("Kurtosis: ", iterMoments.getKurtosis())
otv.View.ShowAll()
|