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
|
#!/usr/bin/env python
import openturns as ot
import openturns.testing as ott
from math import sqrt
ot.PlatformInfo.SetNumericalPrecision(5)
distribution = ot.FisherSnedecor(4.5, 8.4)
size = 10000
sample = distribution.getSample(size)
factory = ot.FisherSnedecorFactory()
print("Distribution =", repr(distribution))
result = factory.buildEstimator(sample)
estimatedDistribution = result.getDistribution()
print("Estimated distribution =", repr(estimatedDistribution))
parameterDistribution = result.getParameterDistribution()
print("Parameter distribution =", parameterDistribution)
defaultDistribution = factory.build()
print("Default distribution =", defaultDistribution)
fromParameterDistribution = factory.build(distribution.getParameter())
print("Distribution from parameters =", fromParameterDistribution)
typedEstimatedDistribution = factory.buildAsFisherSnedecor(sample)
print("Typed estimated distribution =", typedEstimatedDistribution)
defaultTypedDistribution = factory.buildAsFisherSnedecor()
print("Default typed distribution =", defaultTypedDistribution)
typedFromParameterDistribution = factory.buildAsFisherSnedecor(
distribution.getParameter()
)
print("Typed distribution from parameters=", typedFromParameterDistribution)
# Various estimators
estimatedDistribution = factory.build(sample)
print("Estimated distribution with default estimator =", estimatedDistribution)
estimatedDistribution = factory.buildMethodOfMoments(sample)
print("Estimated distribution with moments=", estimatedDistribution)
estimatedDistribution = factory.buildMethodOfLikelihoodMaximization(sample)
print("Estimated distribution with likelihoodMax.=", estimatedDistribution)
# Build method of moments
print("Build method of moments")
size = 10000
distribution = ot.FisherSnedecor(4.5, 8.4)
sample = distribution.getSample(size)
factory = ot.FisherSnedecorFactory()
estimatedTN = factory.buildMethodOfMoments(sample)
exact_mu = sample.computeMean()[0]
exact_sigma2 = sample.computeCovariance()[0, 0]
computed_mu = estimatedTN.getMean()[0]
computed_sigma2 = estimatedTN.getCovariance()[0, 0]
ott.assert_almost_equal(exact_mu, computed_mu, 1.0e-15, 0.0)
ott.assert_almost_equal(exact_sigma2, computed_sigma2, 1.0e-15, 0.0)
# Build method of likelihood maximization
print("Build method of likelihood maximization")
size = 10000
distribution = ot.FisherSnedecor(4.5, 8.4)
sample = distribution.getSample(size)
factory = ot.FisherSnedecorFactory()
estimatedTN = factory.buildMethodOfLikelihoodMaximization(sample)
exact_d1 = distribution.getD1()
exact_d2 = distribution.getD2()
computed_d1 = estimatedTN.getD1()
computed_d2 = estimatedTN.getD2()
ott.assert_almost_equal(exact_d1, computed_d1, 0.0, 100.0 / sqrt(size))
ott.assert_almost_equal(exact_d2, computed_d2, 0.0, 100.0 / sqrt(size))
|