# Expression for variance of a truncated distribution as a function of the point of truncation

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


Variance[dist]


Mathematica does not give me an expression. Doing

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


or

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?

• 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. Commented Apr 14, 2023 at 14:17

$Version (* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *) Clear["Global*"]  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 *)  • 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. – JimB Commented Apr 13, 2023 at 16:17 • 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).
– Jan
Commented Apr 17, 2023 at 10:39