def Uniform(inf_bound, sup_bound=I_DEFAULT): """ Construction of a uniform distribution :Parameters: * inf_bound (int) : lower bound to the range of possible values (shift parameter) * sup_bound (int) : upper bound to the range of possilbe values .. plot:: :width: 50% :include-source: from openalea.stat_tool.distribution import Uniform b = Uniform(1,10) b.plot(legend_size=8) """ # Check parameters #todo: returns error if ((inf_bound < min_inf_bound) || # (inf_bound > MAX_INF_BOUND)) { assert inf_bound >= 0 assert sup_bound >= 0 assert inf_bound <= sup_bound assert (sup_bound - inf_bound) < MAX_DIFF_BOUND param = D_DEFAULT proba = D_DEFAULT cumul_threshold = CUMUL_THRESHOLD return _DiscreteParametricModel(UNIFORM, \ inf_bound, sup_bound, param, proba, cumul_threshold)
def NegativeBinomial(inf_bound, param=D_DEFAULT, \ proba=D_DEFAULT): """ Construction of a negative binomial distribution :Parameters: * inf_bound (int) : lower bound to the range of possible values (shift parameter) * param (int, float) : parameter of the Poisson distribution * proba (int, float) : probability of 'success' .. plot:: :width: 50% :include-source: from openalea.stat_tool.distribution import NegativeBinomial b = NegativeBinomial(1,2 ,.5) b.plot(legend_size=12) """ # Check parameters assert inf_bound >= 0 assert param > 0 assert proba <= 1. and proba > 0 assert (param * (1. - proba) / proba) <= MAX_MEAN sup_bound = I_DEFAULT return _DiscreteParametricModel(NEGATIVE_BINOMIAL, \ inf_bound, sup_bound, param, proba)
def Poisson(inf_bound, param=D_DEFAULT): """ Construction of a poisson distribution :Parameters: * `inf_bound` (int) : lower bound to the range of possible values (shift parameter) * `param` (int, float) : parameter of the Poisson distribution .. plot:: :width: 50% :include-source: from openalea.stat_tool.distribution import Poisson b = Poisson(1,1.5) b.plot(legend_size=8) """ assert inf_bound >= 0 assert param > inf_bound assert param > 0. and param < MAX_MEAN sup_bound = I_DEFAULT proba = D_DEFAULT return _DiscreteParametricModel(POISSON, \ inf_bound, sup_bound, param, proba)
def Binomial(inf_bound, sup_bound=I_DEFAULT, \ proba=D_DEFAULT): """ Construction of a binomial distribution :param float inf_bound: lower bound to the range of possible values (shift parameter) :param float sup_bound: upper bound to the range of possilbe values :param float proba: probability of `success` .. plot:: :width: 50% :include-source: from openalea.stat_tool.distribution import Binomial b = Binomial(0,10,0.5) b.plot(legend_size=8) """ # todo: seg fault when passing -1 as first arguments if there # is no assert here below # memory leak ? # todo: returns error if ((inf_bound < min_inf_bound) || # (inf_bound > MAX_INF_BOUND)) { error.CheckType([inf_bound, sup_bound, proba], [int, int, [int, float]]) assert inf_bound >= 0 assert inf_bound < sup_bound assert (sup_bound - inf_bound) <= MAX_DIFF_BOUND assert proba <= 1. and proba > 0 param = D_DEFAULT return(_DiscreteParametricModel(BINOMIAL.real, inf_bound, sup_bound, param, proba))
def test_constructors(self): h = Histogram([1, 2, 3, 4, 5, 6, 1, 2, 3]) assert h # from histogram dist = Distribution(h) assert dist #from parametric model pm = _DiscreteParametricModel(h) dist = Distribution(pm) assert dist
def estimate_nonparametric(histo): """ Estimate a non parametric distribution :Parameters: * histo (histogram, mixture_data, convolution_data, compound_data) :Usage: .. doctest:: :options: +SKIP >>> Estimate(histo, "NON-PARAMETRIC") >>> estimate_nonparametric(histo) """ return _DiscreteParametricModel(histo)
def Distribution(utype, *args): """ Construction of a parametric discrete distribution (either binomial, Poisson, negative binomial or uniform) from the name and the parameters of the distribution or from an ASCII file. A supplementary shift parameter (argument inf_bound) is required with respect to the usual definitions of these discrete distributions. Constraints over parameters are given in the file syntax corresponding to the type distribution(cf. File Syntax). :Parameters: * `inf_bound` (int) : lower bound to the range of possible values (shift parameter), * `sup_bound` (int) : upper bound to the range of possible values \ (only relevant for binomial or uniform distributions), * `param` (int, real) : parameter of either the Poisson distribution or \ the negative binomial distribution. * `proba` (int, float) : probability of success \ (only relevant for binomial or negative binomial distributions), * `file_name` (string). .. note:: the names of the parametric discrete distributions can be summarized by their first letters: * "B" ("BINOMIAL"), * "P" ("POISSON"), * "NB" ("NEGATIVE_BINOMIAL"), * "U" ("UNIFORM"), * "M" ("MULTINOMIAL"), :Returns: If the construction succeeds, an object of type distribution is returned, otherwise no object is returned. :Examples: .. doctest:: :options: +SKIP >>> Distribution("BINOMIAL", inf_bound, sup_bound, proba) >>> Distribution("POISSON", inf_bound, param) >>> Distribution("NEGATIVE_BINOMIAL", inf_bound, param, proba) >>> Distribution("UNIFORM", inf_bound, sup_bound) >>> Distribution(file_name) .. seealso:: :func:`~openalea.stat_tool.output.Save`, :func:`~openalea.stat_tool.estimate.Estimate` :func:`~openalea.stat_tool.simulate.Simulate`. """ # Constructor from Filename or Histogram or parametricmodel if(len(args) == 0): error.CheckType([utype], [[str, _DiscreteDistributionData, _DiscreteParametricModel]], arg_id=[1]) result = _DiscreteParametricModel(utype) # from parameters if len(args)>0: error.CheckArgumentsLength(args, 1) if utype in ["B", "BINOMIAL"]: result = Binomial(*args) elif utype in ["P", "POISSON"]: result = Poisson(*args) elif utype in ["M", "MULTINOMIAL"]: raise NotImplementedError("Multinomial not yet implemented") elif utype in ["NB", "NEGATIVE_BINOMIAL"]: result = NegativeBinomial(*args) elif utype in ["U", "UNIFORM"]: result = Uniform(*args) else: raise KeyError(" %s not found. Allowed keys are %s" % (utype, distribution_identifier_type.keys())) return result