Verify if a given argument is a probability distribution

4 次查看(过去 30 天)
I would like a class to have as property a probability distribution (of any kind) built using makedist().
The class of a variable a = makedist('normal',0,1) would be prob.NormalDistribution, so I guess I would either:
  1. Check that the parent class of the input is prob (unsuccessful so far)
  2. Match the class name in a similar way to: startsWith(class(a),'prob.')
Could it be done using Property Validation in classes with the "mustBe*" syntax?

采纳的回答

Loïc Reymond
Loïc Reymond 2022-7-26
An existing thread had already answered this question 5 years ago, sorry for the duplicate... https://www.mathworks.com/matlabcentral/answers/364545-isa-probability-distribution?s_tid=answers_rc1-1_p1_MLT
In order to verify that an input argument is a distribution, the following statment would work:
mustBeA(Distribution, 'prob.ToolboxFittableParametricDistribution')

更多回答(1 个)

dpb
dpb 2022-7-26
编辑:dpb 2022-7-26
Interesting puzzle...I dunno, but let's see what we can find...
The class is the fully-qualified distribution name; in your above example 'prob.NormalDistribution'; not just the parent class.
Well, let's see -- some testing shows that mustBeA is case sensitive as well and must be eggzackly the matching name -- so, the following seems to work with the builtin distributions -- fortunately, makedist without arguments returns the list of allowed/known distributions.
>> plist=strcat('prob.',capitalize(makedist),'Distribution')
plist =
27×1 cell array
{'prob.BetaDistribution' }
{'prob.BinomialDistribution' }
{'prob.BirnbaumsaundersDistribution' }
{'prob.BurrDistribution' }
{'prob.ExponentialDistribution' }
{'prob.ExtremevalueDistribution' }
{'prob.GammaDistribution' }
{'prob.GeneralizedextremevalueDistribution'}
{'prob.GeneralizedparetoDistribution' }
{'prob.HalfnormalDistribution' }
{'prob.InversegaussianDistribution' }
{'prob.LogisticDistribution' }
{'prob.LoglogisticDistribution' }
{'prob.LognormalDistribution' }
{'prob.MultinomialDistribution' }
{'prob.NakagamiDistribution' }
{'prob.NegativebinomialDistribution' }
{'prob.NormalDistribution' }
{'prob.PiecewiselinearDistribution' }
{'prob.PoissonDistribution' }
{'prob.RayleighDistribution' }
{'prob.RicianDistribution' }
{'prob.StableDistribution' }
{'prob.TlocationscaleDistribution' }
{'prob.TriangularDistribution' }
{'prob.UniformDistribution' }
{'prob.WeibullDistribution' }
>> d=makedist('Normal');
>> mustBeA(d,plist)
>>
The above serves to make sure only it is one of the possible; you'd have to shorten the list to specific values to find out about a particular distribution in which case it's probably still just a simple as to do a string search.
I dunno what you do about added distributions; didn't 'spearmint with one of them...
  1 个评论
Loïc Reymond
Loïc Reymond 2022-7-26
Thanks for your input !
I tried this approach, but had some issues with the distributions having names that contain more than one word (such as GeneralizedExtremeValueDistribution, that becomes GeneralizedextremevalueDistribution) eventually endding up not matching the case-sensitivity of mustBeA.
Furthermore, I am unsure about how to initialize the equivalent of your plist variable so that it is available during the property validation step (see below). Would you have a workaround?
Also, what do you mean by "added distributions"?
classdef DummyClass
properties
Distribution {mustBeA(Distribution, plist)} %plist has to be initialized
end
methods
%...
end
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by