Calculating expectation for a custom distribution in Mathematica

房东的猫 提交于 2019-12-20 01:41:18

问题


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

Can one extend the functionality of PDF, CDF, FindDistributionParameters etc in Mathematica?

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]

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.

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.

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


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.


Many thanks to all.


回答1:


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

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

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 should give False is parameters explicitly violate assumptions, and DistributionParameterAssumptions should return the boolean expression representing assumptions on parameters of your distribution.




回答2:


I'm not sure I really understand your question... The expected value or the mean, is the first moment of the distribution and can be calculated as

expectation := Integrate[x #, {x,-Infinity,Infinity}]&;

and use it as expectation[f[x]], where f[x] is your pdf.

Your last code snippet doesn't work for me. I don't know if it is v8 code or if it is custom defined or if you're trying to say that is what you'd like your function to be like...

You can also try looking into Mathematica's ExpectedValue function.

ExpectedValue[x, NormalDistribution[m, s], x]
Out[1] = m



回答3:


The following page contains some tips on enabling custom distributions (i.e. written from scratch without TransformedDisribution or ProbabilityDistribution) for use in CopulaDistribution, RandomVariate, etc: https://mathematica.stackexchange.com/questions/20067/efficient-generation-of-random-variates-from-a-copula-distribution/26169#26169



来源:https://stackoverflow.com/questions/6296455/calculating-expectation-for-a-custom-distribution-in-mathematica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!