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?<
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
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
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.