How can I generate random variates from a from a PDF that I have defined?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite
1












My problem is that Pick evaluates all the elements of a list at once. In my code I need a similar function that evaluates each element of a list.
I need to generate 1000000 numbers for a PDF.



My code is:



n = 1000000;
yteste = RandomReal[0, 2/3, 2 n];
xteste = RandomReal[-1, 1, 2 n];

f[x_] := 2/3 (1 - Abs[x]^3)

If[Length[xaceitos] <= n,
AppendTo[xaceitos, Pick[xteste, UnitStep[f[xteste] - yteste], 1]],
Nothing]


Is there any other function that can do it?










share|improve this question























  • Can you explain what you want your code to produce? What is the expected form of xaceitos, for instance?
    – J. M. is somewhat okay.♦
    2 hours ago










  • I need to generate n numbers that follow the PDF f[x], this is the monte carlo Method. The problem is, when i use the rejection method i only receive less than n numbers. I need to receive exactly n numbers.
    – Hugo David Costa Pereira
    2 hours ago







  • 1




    That is slightly clearer, but only a little. So, you want to sample under the curve of f[x] for Abs[x] < 1? If so: dist = ProbabilityDistribution[f[x], x, -1, 1]; xaceitos = RandomVariate[dist, n]; Show[Histogram[xaceitos, Automatic, "PDF"], Plot[f[x], x, -1, 1]]
    – J. M. is somewhat okay.♦
    2 hours ago











  • Yes, exactly. The curve of the histogram[xaceitos] must have the same shape o f[x].
    – Hugo David Costa Pereira
    2 hours ago














up vote
2
down vote

favorite
1












My problem is that Pick evaluates all the elements of a list at once. In my code I need a similar function that evaluates each element of a list.
I need to generate 1000000 numbers for a PDF.



My code is:



n = 1000000;
yteste = RandomReal[0, 2/3, 2 n];
xteste = RandomReal[-1, 1, 2 n];

f[x_] := 2/3 (1 - Abs[x]^3)

If[Length[xaceitos] <= n,
AppendTo[xaceitos, Pick[xteste, UnitStep[f[xteste] - yteste], 1]],
Nothing]


Is there any other function that can do it?










share|improve this question























  • Can you explain what you want your code to produce? What is the expected form of xaceitos, for instance?
    – J. M. is somewhat okay.♦
    2 hours ago










  • I need to generate n numbers that follow the PDF f[x], this is the monte carlo Method. The problem is, when i use the rejection method i only receive less than n numbers. I need to receive exactly n numbers.
    – Hugo David Costa Pereira
    2 hours ago







  • 1




    That is slightly clearer, but only a little. So, you want to sample under the curve of f[x] for Abs[x] < 1? If so: dist = ProbabilityDistribution[f[x], x, -1, 1]; xaceitos = RandomVariate[dist, n]; Show[Histogram[xaceitos, Automatic, "PDF"], Plot[f[x], x, -1, 1]]
    – J. M. is somewhat okay.♦
    2 hours ago











  • Yes, exactly. The curve of the histogram[xaceitos] must have the same shape o f[x].
    – Hugo David Costa Pereira
    2 hours ago












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





My problem is that Pick evaluates all the elements of a list at once. In my code I need a similar function that evaluates each element of a list.
I need to generate 1000000 numbers for a PDF.



My code is:



n = 1000000;
yteste = RandomReal[0, 2/3, 2 n];
xteste = RandomReal[-1, 1, 2 n];

f[x_] := 2/3 (1 - Abs[x]^3)

If[Length[xaceitos] <= n,
AppendTo[xaceitos, Pick[xteste, UnitStep[f[xteste] - yteste], 1]],
Nothing]


Is there any other function that can do it?










share|improve this question















My problem is that Pick evaluates all the elements of a list at once. In my code I need a similar function that evaluates each element of a list.
I need to generate 1000000 numbers for a PDF.



My code is:



n = 1000000;
yteste = RandomReal[0, 2/3, 2 n];
xteste = RandomReal[-1, 1, 2 n];

f[x_] := 2/3 (1 - Abs[x]^3)

If[Length[xaceitos] <= n,
AppendTo[xaceitos, Pick[xteste, UnitStep[f[xteste] - yteste], 1]],
Nothing]


Is there any other function that can do it?







probability-or-statistics






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 6 mins ago









m_goldberg

82.3k869190




82.3k869190










asked 2 hours ago









Hugo David Costa Pereira

212




212











  • Can you explain what you want your code to produce? What is the expected form of xaceitos, for instance?
    – J. M. is somewhat okay.♦
    2 hours ago










  • I need to generate n numbers that follow the PDF f[x], this is the monte carlo Method. The problem is, when i use the rejection method i only receive less than n numbers. I need to receive exactly n numbers.
    – Hugo David Costa Pereira
    2 hours ago







  • 1




    That is slightly clearer, but only a little. So, you want to sample under the curve of f[x] for Abs[x] < 1? If so: dist = ProbabilityDistribution[f[x], x, -1, 1]; xaceitos = RandomVariate[dist, n]; Show[Histogram[xaceitos, Automatic, "PDF"], Plot[f[x], x, -1, 1]]
    – J. M. is somewhat okay.♦
    2 hours ago











  • Yes, exactly. The curve of the histogram[xaceitos] must have the same shape o f[x].
    – Hugo David Costa Pereira
    2 hours ago
















  • Can you explain what you want your code to produce? What is the expected form of xaceitos, for instance?
    – J. M. is somewhat okay.♦
    2 hours ago










  • I need to generate n numbers that follow the PDF f[x], this is the monte carlo Method. The problem is, when i use the rejection method i only receive less than n numbers. I need to receive exactly n numbers.
    – Hugo David Costa Pereira
    2 hours ago







  • 1




    That is slightly clearer, but only a little. So, you want to sample under the curve of f[x] for Abs[x] < 1? If so: dist = ProbabilityDistribution[f[x], x, -1, 1]; xaceitos = RandomVariate[dist, n]; Show[Histogram[xaceitos, Automatic, "PDF"], Plot[f[x], x, -1, 1]]
    – J. M. is somewhat okay.♦
    2 hours ago











  • Yes, exactly. The curve of the histogram[xaceitos] must have the same shape o f[x].
    – Hugo David Costa Pereira
    2 hours ago















Can you explain what you want your code to produce? What is the expected form of xaceitos, for instance?
– J. M. is somewhat okay.♦
2 hours ago




Can you explain what you want your code to produce? What is the expected form of xaceitos, for instance?
– J. M. is somewhat okay.♦
2 hours ago












I need to generate n numbers that follow the PDF f[x], this is the monte carlo Method. The problem is, when i use the rejection method i only receive less than n numbers. I need to receive exactly n numbers.
– Hugo David Costa Pereira
2 hours ago





I need to generate n numbers that follow the PDF f[x], this is the monte carlo Method. The problem is, when i use the rejection method i only receive less than n numbers. I need to receive exactly n numbers.
– Hugo David Costa Pereira
2 hours ago





1




1




That is slightly clearer, but only a little. So, you want to sample under the curve of f[x] for Abs[x] < 1? If so: dist = ProbabilityDistribution[f[x], x, -1, 1]; xaceitos = RandomVariate[dist, n]; Show[Histogram[xaceitos, Automatic, "PDF"], Plot[f[x], x, -1, 1]]
– J. M. is somewhat okay.♦
2 hours ago





That is slightly clearer, but only a little. So, you want to sample under the curve of f[x] for Abs[x] < 1? If so: dist = ProbabilityDistribution[f[x], x, -1, 1]; xaceitos = RandomVariate[dist, n]; Show[Histogram[xaceitos, Automatic, "PDF"], Plot[f[x], x, -1, 1]]
– J. M. is somewhat okay.♦
2 hours ago













Yes, exactly. The curve of the histogram[xaceitos] must have the same shape o f[x].
– Hugo David Costa Pereira
2 hours ago




Yes, exactly. The curve of the histogram[xaceitos] must have the same shape o f[x].
– Hugo David Costa Pereira
2 hours ago










1 Answer
1






active

oldest

votes

















up vote
3
down vote













As @JM says, just use ProbabilityDistribution:



dist = ProbabilityDistribution[f[x], x, -1, 1];

random = RandomVariate[dist, 10^6];
Length[random]



1000000




Visualization:



Histogram[random, Automatic, "PDF"]


enter image description here






share|improve this answer




















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "387"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182581%2fhow-can-i-generate-random-variates-from-a-from-a-pdf-that-i-have-defined%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    As @JM says, just use ProbabilityDistribution:



    dist = ProbabilityDistribution[f[x], x, -1, 1];

    random = RandomVariate[dist, 10^6];
    Length[random]



    1000000




    Visualization:



    Histogram[random, Automatic, "PDF"]


    enter image description here






    share|improve this answer
























      up vote
      3
      down vote













      As @JM says, just use ProbabilityDistribution:



      dist = ProbabilityDistribution[f[x], x, -1, 1];

      random = RandomVariate[dist, 10^6];
      Length[random]



      1000000




      Visualization:



      Histogram[random, Automatic, "PDF"]


      enter image description here






      share|improve this answer






















        up vote
        3
        down vote










        up vote
        3
        down vote









        As @JM says, just use ProbabilityDistribution:



        dist = ProbabilityDistribution[f[x], x, -1, 1];

        random = RandomVariate[dist, 10^6];
        Length[random]



        1000000




        Visualization:



        Histogram[random, Automatic, "PDF"]


        enter image description here






        share|improve this answer












        As @JM says, just use ProbabilityDistribution:



        dist = ProbabilityDistribution[f[x], x, -1, 1];

        random = RandomVariate[dist, 10^6];
        Length[random]



        1000000




        Visualization:



        Histogram[random, Automatic, "PDF"]


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 hours ago









        Carl Woll

        58.4k275150




        58.4k275150



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182581%2fhow-can-i-generate-random-variates-from-a-from-a-pdf-that-i-have-defined%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery