示例#1
0
 def test_getSample_getMean(self):
     """Test InverseWishart.getSample and InverseWishart.getMean"""
     d, Scale, DoF, N = self.dimension, self.Scale, self.DoF, int(1E+4)
     Identity = ot.CovarianceMatrix(d)
     Scale_wishart = ot.CovarianceMatrix(Scale.solveLinearSystem(Identity))
     inverse_wishart = ot.InverseWishart(Scale, DoF)
     sample_inverse = ot.Sample(N, (d * (d + 1)) // 2)
     sample = ot.Sample(N, (d * (d + 1)) // 2)
     for i in range(N):
         M_inverse = inverse_wishart.getRealizationAsMatrix()
         M = M_inverse.solveLinearSystem(Identity)
         indice = 0
         for j in range(d):
             for k in range(j + 1):
                 sample_inverse[i, indice] = M_inverse[k, j]
                 sample[i, indice] = M[k, j]
                 indice += 1
     mean_inverse = sample_inverse.computeMean()
     mean = sample.computeMean()
     theoretical_mean_inverse = inverse_wishart.getMean()
     theoretical_mean = (ot.Wishart(Scale_wishart, DoF)).getMean()
     indice, coefficient = 0, 1. / (DoF - d - 1)
     for j in range(d):
         for k in range(j + 1):
             assert_almost_equal(theoretical_mean_inverse[indice],
                                 coefficient * Scale[k, j])
             assert_almost_equal(theoretical_mean[indice],
                                 DoF * Scale_wishart[k, j])
             assert_almost_equal(mean_inverse[indice],
                                 coefficient * Scale[k, j], 0.15, 1.E-3)
             assert_almost_equal(mean[indice],
                                 DoF * Scale_wishart[k, j], 0.15, 1.E-3)
             indice += 1
示例#2
0
import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View
if (ot.Wishart().__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.Wishart().__class__.__name__ == 'CumulativeDistributionNetwork'):
    distribution = ot.CumulativeDistributionNetwork(
        [ot.Normal(2), ot.Dirichlet([0.5, 1.0, 1.5])],
        ot.BipartiteGraph([[0, 1], [0, 1]]))
else:
    distribution = ot.Wishart()
dimension = distribution.getDimension()
if dimension <= 2:
    if distribution.getDimension() == 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)
    else:
        distribution.setDescription(['$x_1$', '$x_2$'])
        pdf_graph = distribution.drawPDF()
        fig = plt.figure(figsize=(10, 5))
import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View
if ot.Wishart().__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.Wishart().__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.Wishart().__class__.__name__ == 'Histogram':
    distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15])
else:
    distribution = ot.Wishart()
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()
示例#4
0
import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View
if ot.Wishart().__class__.__name__ == 'Bernoulli':
    distribution = ot.Bernoulli(0.7)
elif ot.Wishart().__class__.__name__ == 'Binomial':
    distribution = ot.Binomial(5, 0.2)
elif ot.Wishart().__class__.__name__ == 'ComposedDistribution':
    copula = ot.IndependentCopula(2)
    marginals = [ot.Uniform(1.0, 2.0), ot.Normal(2.0, 3.0)]
    distribution = ot.ComposedDistribution(marginals, copula)
elif ot.Wishart().__class__.__name__ == 'CumulativeDistributionNetwork':
    coll = [ot.Normal(2), ot.Dirichlet([0.5, 1.0, 1.5])]
    distribution = ot.CumulativeDistributionNetwork(
        coll, ot.BipartiteGraph([[0, 1], [0, 1]]))
elif ot.Wishart().__class__.__name__ == 'Histogram':
    distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15])
elif ot.Wishart().__class__.__name__ == 'KernelMixture':
    kernel = ot.Uniform()
    sample = ot.Normal().getSample(5)
    bandwith = [1.0]
    distribution = ot.KernelMixture(kernel, bandwith, sample)
elif ot.Wishart().__class__.__name__ == 'MaximumDistribution':
    coll = [
        ot.Uniform(2.5, 3.5),
        ot.LogUniform(1.0, 1.2),
        ot.Triangular(2.0, 3.0, 4.0)
    ]
    distribution = ot.MaximumDistribution(coll)
elif ot.Wishart().__class__.__name__ == 'Multinomial':
    distribution = ot.Multinomial(5, [0.2])