I am working on a project where I need to calculate variance of a truncated distribution. Suppose $X$ has some distribution and $x\in\mathbb{R}$. I am interested in $var(X|X\leq x)$. Given some distribution and values of its parameters, e.g., exponential with $\lambda=1$, how do I ask Mathematica for $var(X|X\leq x)$ such that I get a symbolic expression (function of $x$).

As an example, consider:

dist = TruncatedDistribution[{-Infinity, x}, ExponentialDistribution[1]]

When I ask for


Mathematica does not give me an expression. Doing

Expectation[z^2, z \[Distributed] dist] - Expectation[z, z \[Distributed] dist]^2


Moment[dist, 2] - Moment[dist, 1]^2

produces a symbolic expression. I am trying to understand why I get different answers and ultimately how to do things so that I get a symbolic expression (for as many distributions as possible).

The accepted answer here seems to suggest that Expectation works better than Mean (and thus, by extension Variance). Should I expect Moment and Expectation to work equally well (I would guess that Moment uses Expectation)? Are there even better ways I am unaware of?

  • $\begingroup$ There is a problem with notation in this question. You are seeking $Var(X | X \leq b)$ for some parameter $b$ .... not as the OP has written: $Var(X | X \leq x)$. Note that $X$ is a random variable, and $x$ is typically the outcome of that random variable, so you cannot condition the outcome of the variable on itself. $\endgroup$
    – wolfies
    Commented Apr 14, 2023 at 14:17

1 Answer 1


(* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *)


You just need to restrict x to being positive

dist = TruncatedDistribution[{-Infinity, x}, ExponentialDistribution[1]]

(* TruncatedDistribution[{0, x}, ExponentialDistribution[1]] *)

m = Assuming[x > 0, Mean[dist]]

(* (E^-x (-1 + E^x - x))/(1 - Cosh[x] + Sinh[x]) *)

sd = Assuming[x > 0, StandardDeviation[dist]]

(* Sqrt[-((2 + x^2 - 2 Cosh[x])/((-1 + E^x) (1 - Cosh[x] + Sinh[x])))] *)

var = Assuming[x > 0, Variance[dist]]

(* -((2 + x^2 - 2 Cosh[x])/((-1 + E^x) (1 - Cosh[x] + Sinh[x]))) *)

var == Assuming[x > 0, 
   Expectation[z^2, z \[Distributed] dist] - 
    Expectation[z, z \[Distributed] dist]^2] // Simplify

(* True *)

var == Assuming[x > 0, Moment[dist, 2] - Moment[dist, 1]^2] // Simplify

(* True *)
  • $\begingroup$ It seems the assumption of x being positive is because the distribution the OP picked only has positive support on positive real numbers. That restriction isn't necessary when, for example, a normal distribution is used. $\endgroup$
    – JimB
    Commented Apr 13, 2023 at 16:17
  • $\begingroup$ Thank you, this is ''easy'' solution to $Variance$ giving me different answer relative to $Expectation$ and $Moment$ (which never occurred to me would work and thus went untried). $\endgroup$
    – Jan
    Commented Apr 17, 2023 at 10:39

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.