# When are If, Piecewise, Switch, and Which interchangeable and when are they not?

Experimenting with joint dependent distributions via the TransformedDistribution function, I used the following to derive a distribution where the second variate is distributed dependent on the value of the first variate (very simplified & contrived example follows).

distA = TransformedDistribution[{b,
If[b == 1, d1, d2]}, {b \[Distributed]
DiscreteUniformDistribution[{1, 2}],
d1 \[Distributed] UniformDistribution[{1, 2}],
d2 \[Distributed] UniformDistribution[{2, 3}]}]

distB = TransformedDistribution[{b,
Piecewise[{{d1, b == 1}, {d2, b == 2}}]}, {b \[Distributed]
DiscreteUniformDistribution[{1, 2}],
d1 \[Distributed] UniformDistribution[{1, 2}],
d2 \[Distributed] UniformDistribution[{2, 3}]}]

distC = TransformedDistribution[{b,
Switch[b, 1, d1, 2, d2]}, {b \[Distributed]
DiscreteUniformDistribution[{1, 2}],
d1 \[Distributed] UniformDistribution[{1, 2}],
d2 \[Distributed] UniformDistribution[{2, 3}]}]

distD = TransformedDistribution[{b,
Which[b == 1, d1, b == 2, d2]}, {b \[Distributed]
DiscreteUniformDistribution[{1, 2}],
d1 \[Distributed] UniformDistribution[{1, 2}],
d2 \[Distributed] UniformDistribution[{2, 3}]}]


The first two behave as I'd expect: Mean, Var, RandomVariate all do what they're supposed to. The latter two, while behaving as expected for the simple probability functions (e.g. Mean), puke on any attempt to sample with RandomVariate, with the message

TransformedDistribution::nnbprm: The valid numeric parameters of distribution TransformedDistribution[{\FormalX]1,Switch[\FormalX]1,1,\FormalX]2,2,\FormalX]3]},\FormalX]1,\FormalX]2,\FormalX]3}\Distributed]ProductDistribution[DiscreteUniformDistribution[{1,2}],UniformDistribution[{1,2}],UniformDistribution[{2,3}]]] are expected. Use DistributionParameterAssumptions to obtain the parameter assumptions. >>

I'm a bit puzzled by this, seems the forms in this case should result in equivalent behavior. Any insights?

-
Your use of If, Switch etc. are not equivalent to begin with. For example, try Switch[b, 1, d1, _, d2] if you want something almost equivalent to your If. (It is equivalent on numeric b). – Michael E2 Jan 26 '14 at 0:38
@MichaelE2: Not sure I understand you. Since b can only take values 1 or 2, they are logically precisely equivalent. – ciao Jan 26 '14 at 1:42
I think what @MichaelE2 means is that Switch and Which should (could) be setup to return a result at all times. In addition to the use of Blank[] in Switch you could try Which[test1, ...,test2, ..., True, output if all tests fail] – Mike Honeychurch Jan 26 '14 at 3:34
@MikeHoneychurch:Yes, I'm aware of the optional use of a fall-through pattern. Nonetheless, such a pattern is unneeded when the test value can only assume "valid" values. In the example with switch, it is always falling through, and that's what I'd like clarification on: it's as if switch and which never see the evaluated form of the test value. – ciao Jan 26 '14 at 3:42
I guess I mean that it seems Mathematica does not analyze the distributions, so you might have to use code that is equivalent or close enough, if you wish to use different conditional constructs. Perhaps the problem is related to this: Simplify[ Switch[b, 1, d1, 2, d2] == If[b == 1, d1, d2], b == 1 || b == 2 ] does not return True. (It does return True under the assumption b == 1, or b == 2, but not their disjunction.) – Michael E2 Jan 26 '14 at 5:17