What is an elegant way of calculating this result (programmatically) in mathematica?

Let $X$ be a random variable with PDF

$$f_{X}(x) = \begin{cases} x/4, & \text{if $1<x\leq3$} \\ 0, & \text{otherwise} \end{cases}$$

Let $A$ be the event $\{X\geq2\}$

Find $f_{X|A}(x)$

Here is the answer done manually for convenience:

$$f_{X|A}(x) = \begin{cases} \frac{f_{X}(x)}{P(A)}, & \text{if $2\leq x\leq3$} \\ 0, & \text{otherwise} \end{cases}$$

$$P(A)=P(X\geq2)=\int_{2}^{3}{f_{X}(x) dx}=5/8$$

$$\implies f_{X|A}(x) = \begin{cases} \frac{2x}{5}, & \text{if $2\leq x\leq3$} \\ 0, & \text{otherwise} \end{cases}$$

I am looking for a programmatic solution that utilizes \[Conditioned] or something else as high-level.

TransformedDistribution and ProbabilityDistribution look like promising avenues, but I am unable to state my problem in terms of them.

I know I could write out the problem explicitly in low level code. But I am looking for an implicit, declarative implementation. Surely mathematica with all its high level abstraction power can do this?

here is a failed attempt

A = X >= 2;
conditionalDist = TransformedDistribution[X \[Conditioned] A, X \[Distributed]ProbabilityDistribution[x/4, {x, 1, 3}] ] ;
PDF[conditionalDist, y]

You can use combination of ProbabilityDistribution and TruncatedDistribution as follows:

f[x_] := x/4
dist = ProbabilityDistribution[f[x], {x, 1, 3}];
td = TruncatedDistribution[{2, Infinity}, dist];

PDF[td, x]

$\begin{cases} \frac{2 x}{5} & 2<x<3 \\ 0 & \text{True} \end{cases}$

Probability[Conditioned[x <= t, x > 2], Distributed[x, dist]] // TeXForm

$\begin{cases} 1 & t\geq 3 \\ \frac{1}{5} \left(t^2-4\right) & 2<t<3 \end{cases}$


Here's another method that uses the CDF to directly define the desired distribution and which might be a bit more intuitive/self-evident:

f[x_] := x/4
dist = ProbabilityDistribution[f[x], {x, 1, 3}];

   Probability[x < y \[Conditioned] x > 2, x \[Distributed] dist]
 {y, -\[Infinity], \[Infinity]}

ProbabilityDistribution[Piecewise[{{(2*\[FormalX])/5, 2 < \[FormalX] < 3}}, 0], {\[FormalX], -Infinity, Infinity}]

In other words: we just compute the CDF with Probability and then plonk it into ProbabilityDistribution while telling it that it's a CDF rather than a PDF. This is a useful trick to keep in mind, since the CDF is sometimes easier to work with since it's a proper probability.

  • $\begingroup$ nice! (I felt that there must be a more direct way out there!) $\endgroup$ Jul 2 '19 at 14:16

Sometimes "compact" can be considered elegant:

(x/4)/Integrate[x/4, {x, 2, 3}]
(* (2 x)/5 *)

If you need it in terms of Mathematica's ProbabilityDistribution:

d = ProbabilityDistribution[(x/4)/Integrate[x/4, {x, 2, 3}], {x, 2, 3}]
PDF[d, x]

Conditional density

CDF[d, x]

Conditional distribution function


I learned from @kglr that Probability can give you a conditional CDF. One can take the derivative of this to compute the desired conditional PDF.

A = X >= 2;
conditionalCDF = Probability[X <= y \[Conditioned] A, X \[Distributed] ProbabilityDistribution[x/4, {x, 1, 3}]];
D[conditionalCDF, y]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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