DistributionFitTest[] 用于 Mathematica 中的自定义分布 [英] DistributionFitTest[] for custom distributions in Mathematica
问题描述
我有两个自定义分布的 PDF 和 CDF,一种为每个分布生成 RandomVariates 的方法,以及用于将参数拟合到数据的代码.我之前在以下位置发布了一些代码:
I have PDFs and CDFs for two custom distributions, a means of generating RandomVariates for each, and code for fitting parameters to data. Some of this code I've posted previously at:
其中一些如下:
nlDist /: PDF[nlDist[alpha_, beta_, mu_, sigma_],
x_] := (1/(2*(alpha + beta)))*alpha*
beta*(E^(alpha*(mu + (alpha*sigma^2)/2 - x))*
Erfc[(mu + alpha*sigma^2 - x)/(Sqrt[2]*sigma)] +
E^(beta*(-mu + (beta*sigma^2)/2 + x))*
Erfc[(-mu + beta*sigma^2 + x)/(Sqrt[2]*sigma)]);
nlDist /:
CDF[nlDist[alpha_, beta_, mu_, sigma_],
x_] := ((1/(2*(alpha + beta)))*((alpha + beta)*E^(alpha*x)*
Erfc[(mu - x)/(Sqrt[2]*sigma)] -
beta*E^(alpha*mu + (alpha^2*sigma^2)/2)*
Erfc[(mu + alpha*sigma^2 - x)/(Sqrt[2]*sigma)] +
alpha*E^((-beta)*mu + (beta^2*sigma^2)/2 + alpha*x + beta*x)*
Erfc[(-mu + beta*sigma^2 + x)/(Sqrt[2]*sigma)]))/
E^(alpha*x);
dplDist /: PDF[dplDist[alpha_, beta_, mu_, sigma_], x_] :=
PDF[nlDist[alpha, beta, mu, sigma], Log[x]]/x;
dplDist /: CDF[dplDist[alpha_, beta_, mu_, sigma_], x_] :=
CDF[nlDist[alpha, beta, mu, sigma], Log[x]];
nlDist /: DistributionDomain[nlDist[alpha_, beta_, mu_, sigma_]] :=
Interval[{-Infinity, Infinity}]
nlDist /:
Random`DistributionVector[
nlDist [alpha_, beta_, mu_, sigma_], n_, prec_] :=
RandomVariate[ExponentialDistribution[alpha], n,
WorkingPrecision -> prec] -
RandomVariate[ExponentialDistribution[beta], n,
WorkingPrecision -> prec] +
RandomVariate[NormalDistribution[mu, sigma], n,
WorkingPrecision -> prec];
dplDist /:
Random`DistributionVector[
dplDist[alpha_, beta_, mu_, sigma_], n_, prec_] :=
Exp[RandomVariate[ExponentialDistribution[alpha], n,
WorkingPrecision -> prec] -
RandomVariate[ExponentialDistribution[beta], n,
WorkingPrecision -> prec] +
RandomVariate[NormalDistribution[mu, sigma], n,
WorkingPrecision -> prec]];
如果有人需要查看代码,我可以发布更多代码,但我认为以上内容可以很好地说明目前的方法.
I can post more of the code if someone needs to see it, but I think the above gives a good sense of the approach so far.
现在我需要一种方法来将 DistributionFitTest[] 与这些分布一起使用,如下所示:
Now I need a way to use DistributionFitTest[] with these distributions in something like this:
DistributionFitTest[data, dplDist[3.77, 1.34, -2.65, 0.40],"HypothesisTestData"]
啊,但这行不通.相反,我收到一条错误消息,开头为:
Ah, but this doesn't work. Instead I get an error message that starts out as:
"论据dplDist[3.77,1.34,-2.65,0.4] 应该是一个有效的分布..."
"The argument dplDist[3.77,1.34,-2.65,0.4] should be a valid distribution..."
因此 DistributionFitTest[] 似乎无法将这些分布识别为分布.
So it appears that DistributionFitTest[] doesn't recognize these distributions as distributions.
我不认为在这种情况下使用 TagSet 会有什么帮助,除非可以使用 TagSet 为 DistributionFitTest[] 提供识别这些自定义分布所需的信息.
I don't see how using TagSet would help in this instance, unless one can use TagSet to give DistributionFitTest[] what it needs to identify these custom distributions.
谁能告诉我让这个工作的方法?我想将 DistributionFitTest[] 与这样的自定义分布一起使用,或者找到一些解决方法来评估拟合优度.
Can anyone advise me of a way to get this to work? I'd like to use DistributionFitTest[] with custom distributions like this or find some work around to assess goodness of fit.
Thx -- Jagra
Thx -- Jagra
推荐答案
由于这个问题已经出现很多次了,我认为现在是提供一些如何正确地为 v8 定制发行版的食谱的最佳时机.
Since this question has come up many times, I think it's prime time to furnish some recipes for how to properly cook a custom distribution for v8.
使用 TagSet
为您的发行版定义:
Use TagSet
to define for your distribution:
DistributionParameterQ
、DistributionParameterAssumptions
、DistributionDomain
- 定义
PDF
、CDF
、SurvivalFunction
、HazardFunction
- 通过编码Random`DistributionVector来定义随机数生成代码
这样做将使除参数估计外的所有内容都适用于您的分布.
Doing so will make everything but parameter estimation work for your distribution.
你的错误是 dplDist
没有 DistributionDomain
定义,nlDist
和 dplDist
都没有DistributionParameterQ
和 DistributionParameterAssumptions
定义.
Your mistake was that dplDist
had no DistributionDomain
definition, and both nlDist
and dplDist
did not have
DistributionParameterQ
and DistributionParameterAssumptions
definitions.
我在您的定义中添加了以下内容:
I added to your definitions the following:
dplDist /: DistributionDomain[dplDist[alpha_, beta_, mu_, sigma_]] :=
Interval[{-Infinity, Infinity}]
nlDist /:
DistributionParameterQ[nlDist[alpha_, beta_, mu_, sigma_]] := !
TrueQ[Not[
Element[{alpha, beta, sigma, mu}, Reals] && alpha > 0 &&
beta > 0 && sigma > 0]]
dplDist /:
DistributionParameterQ[dplDist[alpha_, beta_, mu_, sigma_]] := !
TrueQ[Not[
Element[{alpha, beta, sigma, mu}, Reals] && alpha > 0 &&
beta > 0 && sigma > 0]]
nlDist /:
DistributionParameterAssumptions[
nlDist[alpha_, beta_, mu_, sigma_]] :=
Element[{alpha, beta, sigma, mu}, Reals] && alpha > 0 && beta > 0 &&
sigma > 0
dplDist /:
DistributionParameterAssumptions[
dplDist[alpha_, beta_, mu_, sigma_]] :=
Element[{alpha, beta, sigma, mu}, Reals] && alpha > 0 && beta > 0 &&
sigma > 0
现在它起作用了:
In[1014]:= data = RandomVariate[dplDist[3.77, 1.34, -2.65, 0.40], 100];
In[1015]:= DistributionFitTest[data, dplDist[3.77, 1.34, -2.65, 0.40],
"HypothesisTestData"]
Out[1015]= HypothesisTestData[<<DistributionFitTest>>]
这篇关于DistributionFitTest[] 用于 Mathematica 中的自定义分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!