I'd like to compute the following expression using Mathematica:


0 in here is just an example. I'm more interested in computing the probability for a generic expression of x:


As a result, I have obtained this:


That is not a numerical result. Am I doing something wrong in the line of code or it is not possible to obtain a precise result of the probability or a function of x in the generic case?

  • $\begingroup$ Just a two comments on your code: (1) The probability is 1 for your first line of code as the sum of two cosines will never be bigger than 2, (2) You should use lowercase letters for variables. $\endgroup$ – JimB Nov 14 '16 at 16:57
  • $\begingroup$ Take a look at Distributed as I think that's what you meant with "~". $\endgroup$ – corey979 Nov 14 '16 at 16:59

The correct syntax for a multivariate UniformDistribution is

dist = UniformDistribution[{{0, 2*Pi}, {0, 2*Pi}}];

Note that Distributed is \[Distributed]

Probability[Cos[X1] + Cos[X2] <= 5, {X1, X2} \[Distributed] dist]

(*  1  *)

Alternatively, you could write

Probability[Cos[X1] + Cos[X2] <= 5, {
  X1 \[Distributed] UniformDistribution[{0, 2*Pi}],
  X2 \[Distributed] UniformDistribution[{0, 2*Pi}]}]

(*  1  *)

A probability of 1 is as expected since

FunctionRange[Cos[X1] + Cos[X2], {X1, X2}, y, Reals] // Quiet

(*  -2. <= y <= 2.  *)


prob[val_?NumericQ] := NProbability[Cos[X1] + Cos[X2] <= val,
   {X1, X2} \[Distributed] dist];

Plot[prob[val], {val, -2, 2}]

enter image description here

  • $\begingroup$ Thank you for your answer, I know that the function range will be between -2 and 2. Do you know if there is the possibility to compute the probability for a generic value y such that: f(y) = P[cos(x1)+cos(x2)<=y]? Instead of compute it for a precise value. $\endgroup$ – Mattia Nov 14 '16 at 18:11
  • $\begingroup$ fwiw Probability will give an analytic result in terms of a parameter for the single variable case: Probability[ Cos[X1] <= a, {X1 \[Distributed] UniformDistribution[{0, 2*Pi}]}] $\endgroup$ – george2079 Nov 14 '16 at 18:51
  • $\begingroup$ Again, thank you for your edit. Before, I didn't ask you precisely. Is is also possible with Mathematica to obtain a close-form expression of the probability as a function of y. Thus, a function of the plot you have just added? $\endgroup$ – Mattia Nov 14 '16 at 19:45
  • $\begingroup$ Having played with this a bit I'm pretty well convinced there is not a closed form expression to be had, at least using the direct procedure Probability uses. You might want to try math.stackexchange.com. $\endgroup$ – george2079 Nov 14 '16 at 20:59

Here is a numerical solution for 0.5 (rather than 5):

d = TransformedDistribution[Cos[x1] + Cos[x2], 
    {x1 \[Distributed] UniformDistribution[{0, 2 π}], 
     x2 \[Distributed] UniformDistribution[{0, 2 π}]}];

NProbability[z < 0.5, z \[Distributed] d]
(* 0.6916875929659185 *)


I have not found an explicit formula but note that a numerical solution can be made as accurate as desired. An alternative is using integration over a region. Below is a comparison of 3 approaches:

z0 = 1.9;
d = TransformedDistribution[Cos[x1] + Cos[x2], {x1 , x2} \[Distributed] 
    UniformDistribution[{{0, 2 π}, {0, 2 π}}]];
Timing[NProbability[z < z0, z \[Distributed] d]]
(* {0.3432022,0.983881258047714} *)

Timing[NIntegrate[If[Cos[2 π x1] + Cos[2 π x2] <= z0, 1, 0], {x1, 0, 1}, {x2, 0, 1}]]
(* {0.2964019,0.983881258047714} *)

Timing[NIntegrate[4 If[Cos[2 π x1] + Cos[2 π x2] <= z0, 1, 0], {x1, 1/2, 1}, {x2, 1/2, 1}]]
(* {0.2028013,0.9838812580477139} *)
  • $\begingroup$ Is it possible to obtain a function of a parameter y such that: f(y) = P[cos(x1)+cos(x2)<=y]? Instead of compute a precise value. $\endgroup$ – Mattia Nov 14 '16 at 17:57
  • $\begingroup$ Maybe. Someone else looking at this might know way or the other. You could certainly develop a large table of values of the probability for $-2 \le y \le 2$. $\endgroup$ – JimB Nov 14 '16 at 18:00

Just a "simulation" approach. Note that Mma has little trouble generative samples from the TransformedDistribution.

td = TransformedDistribution[
  Cos[x] + Cos[y], {x, y} \[Distributed] 
   UniformDistribution[Table[{0, 2 Pi}, 2]]]
rv = RandomVariate[td, 100000];
ecdf = EmpiricalDistribution[rv];
is = 300;
Row[{Histogram[rv, Automatic, "CDF", Frame -> True, ImageSize -> is],
  Plot[CDF[ecdf, x], {x, -2, 2}, Frame -> True, ImageSize -> is],
   Table[NProbability[u < t, u \[Distributed] td], {t, -2, 2, 0.1}], 
   Frame -> True, ImageSize -> is],
  Plot[Evaluate[NProbability[u < t, u \[Distributed] td]], {t, -2, 2},
    Frame -> True, ImageSize -> is]}]

enter image description here


I edited the original code to deal with an issue that became clarified after discussion with Jim Baldwin. In using UniformDistribution[2] as the multivariate for TranformedDistribution with transform: Cos[2 Pi x]+ Cos[2 Pi y], the distribution could be sampled but the transformed distribution object was problematic (no formal x's) and NProbability was problematic.

  • $\begingroup$ Very good. Did you mean "Probability" has issues rather than "NProbability" ? $\endgroup$ – JimB Nov 15 '16 at 6:22
  • $\begingroup$ @JimBaldwin I just meant the application of NProbability and CDF on the transformed distribution yield "crazy" values...I just didn't have the time to drill down to understand why. Just thought I'd add these to the others. :) ( I voted for the other answers as they all work ) $\endgroup$ – ubpdqn Nov 15 '16 at 6:26
  • $\begingroup$ Sorry I'm being thick. I don't see any issues with NProbability in any of the answers. $\endgroup$ – JimB Nov 15 '16 at 6:30
  • $\begingroup$ @JimBaldwin no, no, no you are not being thick! I am not explaining myself well enough. I hope this illustrates. You might be able to help me with my thickness...I just took the path of least thought:) $\endgroup$ – ubpdqn Nov 15 '16 at 6:44
  • $\begingroup$ OK. From that example in your comment I take it you mean that NProbability can have issues - but maybe not for this particular distribution. Is there a link that defines td? (again, in the example you give in the comment) $\endgroup$ – JimB Nov 15 '16 at 6:55

Working toward a closed form solution:

g[a_] := NProbability[Cos[X1] + Cos[X2] <= a,
  {X1 \[Distributed] UniformDistribution[{0, 2*Pi}],
   X2 \[Distributed] UniformDistribution[{0, 2*Pi}]}]

Following the definitions given in the documentation for Probability this reduces to:

f[a_] :=
 Piecewise[{{1, a >= 0}, {(Pi - ArcCos[1 + a])/Pi, -2 < a < 0}}, 0] -
    ArcCos[a - Cos[X2]] Boole[-1 < a - Cos[X2] < 1], {X2, 0, 2 Pi}]/(2 Pi^2)

Sorry to skip a bunch of steps, but it's straightforward...

Plot[{f[a], g[a]}, {a, -2, 2},
 PlotStyle -> {{Thick, Red}, {Thin, Black}}]

Enter image description here

That last NIntegrate I think can't be done in closed form. (The second form is considerably faster though, so perhaps it is useful.)


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.