I'd like to understand how functions like Mean are overloaded to provide the right behavior for the distributions in Mathematica (like NormalDistribution or PoissonDistribution).

I originally assumed it was through the use of UpValues, but now I'm not so sure...

So, if I wanted to implement a function or distribution and define a behavior for it when another function like Mean is applied to it, how would I go about it? I know it's not the following:

f /: Mean[f[x_]] := 3 x

Desired behavior:


==> 5
==> 9
  • 1
    $\begingroup$ Have you seen ProbabilityDistribution? $\endgroup$
    – Edmund
    Dec 22, 2018 at 21:54
  • $\begingroup$ Nope - also really useful. Thanks! $\endgroup$
    – hmode
    Dec 23, 2018 at 1:39

2 Answers 2


Symbolic(!) distributions are recognized by their head, not by their PDF:

Mean[PDF[NormalDistribution[0, 1], x]] 

Mean[E^(-(x^2/2))/Sqrt[2 π]]

So instead of messing around with Mean, I would rather suggest

distro /: PDF[distro[μ_, σ_], x_] := E^(-((x - μ)^2/(2 σ^2)))/(Sqrt[2 π] σ);
distro /: Mean[distro[μ_, σ_]] := μ;
distro /: Variance[distro[μ_, σ_]] := σ^2
Mean[distro[0, 1]]
Variance[distro[0, 1]]




This works:

SetAttributes[Mean, HoldFirst];
f[x_] := 2 + x
f /: Mean[f[x_]] := 3 x

Since using Unprotect is not reccomended here's another way.

mean[x_] := Mean[x]
SetAttributes[mean, HoldFirst]
f[x_] := 2 + x
f /: mean[f[x_]] := 3 x    
  • 3
    $\begingroup$ It should be mentioned that SetAttributes[Mean, HoldFirst] is likely to break many internal functions that depend on Mean $\endgroup$
    – Jason B.
    Dec 22, 2018 at 15:05
  • $\begingroup$ Thanks for this solution - I did learn something from your answer but I was trying to avoid setting DownValues. $\endgroup$
    – hmode
    Dec 23, 2018 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.