在 Mathematica 中计算自定义分布的期望 [英] Calculating expectation for a custom distribution in Mathematica

查看:187
本文介绍了在 Mathematica 中计算自定义分布的期望的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题建立在我在之前的一个问题上得到的很好的答案上:

This question builds on the great answers I got on an earlier question:

可以扩展功能吗Mathematica 中的 PDF、CDF、FindDistributionParameters 等?

首先,我有两个自定义发行版的 PDF 和 CDF:nlDist 和 dplDist,您可以从基于 nlDist 的代码 dplDist 中看到.

To start I have PDFs and CDFs for two custom distributions: nlDist and dplDist as you can see from the code dplDist builds upon nlDist.

    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]];

Plot[PDF[dplDist[3.77, 1.34, -2.65, 0.40], x], {x, 0, .3}, 
 PlotRange -> All]
Plot[CDF[dplDist[3.77, 1.34, -2.65, 0.40], x], {x, 0, .3}, 
 PlotRange -> All]

在我之前的问题中,joebolte 和 sasha 的回答以及使用 TagSet 的建议帮助我走到了这一步.现在,我的问题与 dplDist 有关.

In my earlier question, joebolte's and sasha's answers and recommendation to use TagSet helped me get this far. Now, my questions relate to the dplDist.

我现在需要从 PDF 的 x 轴上的某个点计算期望值.在生存分析中,他们将其称为平均剩余寿命.类似于以下内容:

I now need to calculate expectation from some point on the x axis of the PDF. In survival analysis they refer to this as mean residual life. Something like the following:

Expectation[X \[Conditioned] X > 0.1, 
  X \[Distributed] dplDist[3.77, 1.34, -2.65, 0.40]] - 0.1

这不起作用,本质上只是将输入作为文本返回.

This doesn't work, essentially just returning the inputs as text.

我了解如何使用 TagSet 为自定义分发定义 PDF 和 CDF,我该如何为 Expectation[] 执行类似的操作?

I understand how I can use TagSet to define PDFs and CDFs for custom distributions, how do I do something similar for Expectation[]?

我将在一个单独的问题中发布更多关于此后续行动的信息,但我还需要一个策略来计算 dplDist 相对于我已拟合分布的某些数据的拟合优度.

I'll post more about this follow up in a separate question, but I also need a strategy for calculating goodness of fit of the dplDist relative to some data to which I've fit the distribution.

非常感谢大家.

推荐答案

尽管您已经为 Mathematica 的自定义分发提供了 PDFCDF,但您还没有提供域,所以它不知道积分的边界,实际上是积分还是求和.添加使事情起作用:

Although you have provided both PDF and CDF for your custom distribution to Mathematica, you have not given the domain, so it does not know boundaries of integration, and in fact whether to integrate or sum. Adding that makes things work:

In[8]:= nlDist /: 
 DistributionDomain[nlDist[alpha_, beta_, mu_, sigma_]] := 
 Interval[{-Infinity, Infinity}]

In[9]:= NExpectation[Log@X \[Conditioned] Log@X > 0.1, 
  X \[Distributed] nlDist[3.77, 1.34, -2.65, 0.40]] - 0.1

Out[9]= 0.199329

将此与 ProbabilityDistribution 比较格式 ProbabilityDistribution[ pdf, {x, min, max}],其中明确指出域.

Compare this with ProbabilityDistribution that has the format ProbabilityDistribution[ pdf, {x, min, max}], where you explicitly indicate the domain.

为了让 ProbabilityExpectation 等符号求解器及其对应的数字求解器能够处理这些,还建议设置 DistributionParameterQDistributionParameterAssumptions 也是如此.

In order for symbolic solvers like Probability, Expectation and their numeric counterparts work on these, it is also advised to set DistributionParameterQ and DistributionParameterAssumptions as well.

DistributionParameterQ 应该给出 False 参数明确违反假设,并且 DistributionParameterAssumptions 应该返回表示分布参数假设的布尔表达式.

DistributionParameterQ should give False is parameters explicitly violate assumptions, and DistributionParameterAssumptions should return the boolean expression representing assumptions on parameters of your distribution.

这篇关于在 Mathematica 中计算自定义分布的期望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆