import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if (ot.GeneralizedPareto().__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.GeneralizedPareto().__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.GeneralizedPareto() 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()
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View pdf_graph = ot.Graph('PDF graph', 'x', 'PDF', True, 'topleft') cdf_graph = ot.Graph('CDF graph', 'x', 'CDF', True, 'topleft') palette = ot.Drawable.BuildDefaultPalette(10) for i, p in enumerate([-1.0, 0.0, 1.0]): distribution = ot.GeneralizedPareto(1.0, p, 0.0) pdf_curve = distribution.drawPDF().getDrawable(0) cdf_curve = distribution.drawCDF().getDrawable(0) pdf_curve.setColor(palette[i]) cdf_curve.setColor(palette[i]) pdf_curve.setLegend('xi={}'.format(p)) cdf_curve.setLegend('xi={}'.format(p)) pdf_graph.add(pdf_curve) cdf_graph.add(cdf_curve) fig = plt.figure(figsize=(10, 4)) pdf_axis = fig.add_subplot(121) cdf_axis = fig.add_subplot(122) View(pdf_graph, figure=fig, axes=[pdf_axis], add_legend=True) View(cdf_graph, figure=fig, axes=[cdf_axis], add_legend=True) fig.suptitle('GeneralizedPareto(1,xi,0)')
# Various estimators are provided by the GPD factory. Please refer to the :class:`~openturns.GeneralizedParetoFactory` class documentation for more information. # The selection is based on the sample size and compared to the `GeneralizedParetoFactory-SmallSize` key of the :class:`~openturns.ResourceMap`: # # %% print( ot.ResourceMap.GetAsUnsignedInteger("GeneralizedParetoFactory-SmallSize")) # %% # The small sample case # ^^^^^^^^^^^^^^^^^^^^^ # # In this case the default estimator is based on a probability weighted method of moments, with a fallback on the exponential regression method. # %% myDist = ot.GeneralizedPareto(1.0, 0.0, 0.0) N = 17 sample = myDist.getSample(N) # %% # We build our experiment sample of size `N`. myFittedDist = ot.GeneralizedParetoFactory().buildAsGeneralizedPareto(sample) print(myFittedDist) # %% # We draw the fitted distribution as well as an histogram to visualize the fit: graph = myFittedDist.drawPDF() graph.add(ot.HistogramFactory().build(sample).drawPDF()) graph.setTitle("Generalized Pareto distribution fitting on a sample") graph.setColors(["black", "red"]) graph.setLegends(["GPD fitting", "histogram"])
# %% # Define a distribution # --------------------- # %% import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) # %% sigma = 1.4 xi = 0.5 u = 0.1 distribution = ot.GeneralizedPareto(sigma, xi, u) # %% # Draw the survival of a distribution # ----------------------------------- # %% # The `computeCDF` and `computeSurvivalFunction` computes the CDF :math:`F` and survival :math:`S` of a distribution. # %% p1 = distribution.computeCDF(10.) p1 # %% p2 = distribution.computeSurvivalFunction(10.) p2
graphPDF = myDistribution.drawPDF() graphPDF.setTitle(r"PDF of the GEV with parameters $\mu = 0.0$, $\sigma = 1.0$ and $\xi = 0.0$ ") view = otv.View(graphPDF) graphCDF = myDistribution.drawCDF() graphCDF.setTitle(r"CDF of the GEV with parameters $\mu = 0.0$, $\sigma = 1.0$ and $\xi = 0.0$ ") view = otv.View(graphCDF) # %% # The Generalized Pareto Distribution (GPD) # ----------------------------------------- # # In this paragraph we turn to the definition of a :class:`~openturns.GeneralizedPareto` distribution. # For instance we build a generalized Pareto distribution with parameters :math:`\sigma = 1.0`, :math:`\xi = 0.0` and :math:`u = 0.0` : # myGPD = ot.GeneralizedPareto(1.0, 0.0, 0.0) # %% # We draw its PDF and CDF : graphPDF = myGPD.drawPDF() graphPDF.setTitle(r"PDF of the GPD with parameters $\sigma = 1.0$, $\xi = 0.0$ and $u = 0.0$ ") view = otv.View(graphPDF) #%% graphCDF = myGPD.drawCDF() graphCDF.setTitle(r"CDF of the GPD with parameters $\sigma = 1.0$, $\xi = 0.0$ and $u = 0.0$ ") view = otv.View(graphCDF) # %% # Display all figures plt.show()
x_obs = y_obs RWMHsampler = ot.RandomWalkMetropolisHastings(prior, initialState, instrumental) RWMHsampler.setLikelihood(conditional, y_obs, model, x_obs) print("Log-likelihood of thetaTrue = {!r}".format( RWMHsampler.computeLogLikelihood(thetaTrue))) # produces an error with current master branch real_504 = RWMHsampler.getRealization() print("With 504 observations, getRealization() produces {!r}".format( real_504[0])) ott.assert_almost_equal(real_504[0], 2.0) # this example throws in ot 1.19 as it tries to evaluate the likelihood outside the support of the prior # see MetropolisHastingsImplementation::computeLogPosterior obs = ot.TruncatedNormal(0.5, 0.5, 0.0, 10.0).getSample(50) likelihood = ot.GeneralizedPareto() prior = ot.ComposedDistribution( [ot.LogUniform(-1.40, 4.0), ot.Normal(), ot.Normal()]) proposals = [ ot.Uniform(-prior.getMarginal(k).getStandardDeviation()[0], +prior.getMarginal(k).getStandardDeviation()[0]) for k in range(prior.getDimension()) ] initialState = prior.getMean() mh_coll = [ ot.RandomWalkMetropolisHastings(prior, initialState, proposals[i], [i]) for i in range(2) ] for mh in mh_coll: mh.setLikelihood(likelihood, obs)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.GeneralizedPareto().__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.GeneralizedPareto().__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.GeneralizedPareto().__class__.__name__ == 'Histogram': distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15]) else: distribution = ot.GeneralizedPareto() 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() fig = plt.figure(figsize=(10, 5)) plt.suptitle(str(distribution))