0
$\begingroup$

I would like to sampling data from a distribution Sqrt[x^2 + y^2]. I used the code as follows:

a = ProbabilityDistribution[Sqrt[x^2 + y^2], {x, 0, 1}, {y, 0, 1}];
RandomVariate[a, 10]

However, mathematica fails to produce the result.

If I use this code, then it works.

a = ProbabilityDistribution[x^2 + y^2, {x, 0, 1}, {y, 0, 1}];
RandomVariate[a, 10];

It seems that these two are not so different, except for the second one does not have square root. Can anyone explain this?

$\endgroup$
2
  • $\begingroup$ In terms of a tractable formula, a square root can make a big difference. But in any event, Mathematica 10.4 gives me a warning rather than "fails to produce the result": For Mathematica 10.4 I get the following warning: "Sampling from ProbabilityDistribution[$\sqrt{x1^2+x2^2},\{x1, 0 , 1\}, \{x2, 0, 1\}$] is not implemented." $\endgroup$
    – JimB
    Aug 31, 2018 at 5:24
  • $\begingroup$ Neither the pdf $f(x,y) = \sqrt(x^2 + y^2)$ nor the pdf $g(x,y) = x^2 + y^2$ is valid or well-defined, as neither integrates to unity. $\endgroup$
    – wolfies
    Aug 31, 2018 at 13:07

1 Answer 1

1
$\begingroup$

Use TransformedDistribution

aDist = TransformedDistribution[
   Sqrt[x^2 + y^2], {x \[Distributed] UniformDistribution[{0, 1}], 
    y \[Distributed] UniformDistribution[{0, 1}]}];

SeedRandom[0]

RandomVariate[aDist, 10]

(* {0.671686, 1.03103, 1.13352, 0.855141, 0.984159, 1.23482, 0.908538, 0.637849, \
0.551954, 0.735437} *)

Note that Sqrt[x^2+y^2] is a scalar and unlike your second example, RandomVariate[a, 10] should return a list of 10 scalars rather than a list of 10 pairs.

Plot[Evaluate@PDF[aDist, x], {x, 0, Sqrt[2]},
 MaxRecursion -> 10]

enter image description here

If x and y are standard normal rather than uniform then

aDist2 = TransformedDistribution[
  Sqrt[x^2 + y^2], {x \[Distributed] NormalDistribution[], 
   y \[Distributed] NormalDistribution[]}]

(* RayleighDistribution[1] *)
$\endgroup$
2
  • $\begingroup$ I don't follow your comment that $\sqrt(x^2 + y^2)$ is scalar: the pdf $f(x,y) = \sqrt(x^2 + y^2)$ defines a bivariate space, and random variates drawn from that distribution should be bivariate pairs. [ If the OP is after a scalar -- well ... who knows ... maybe he is ... but that is not what he has set up using ProbabilityDistribution $\endgroup$
    – wolfies
    Aug 31, 2018 at 13:10
  • $\begingroup$ @wolfies - since the “PDF” was not scaled properly to be a valid PDF and the OP did not use the option Method -> "Normalize", I guessed that the OP meant to find the distribution for a = Sqrt[x^2 + y^2]. If it turns out that this is not what is intended then I will delete my answer. $\endgroup$
    – Bob Hanlon
    Aug 31, 2018 at 14:27

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.