Find Random Number Generator following the density $f (x) = frac{1 + alpha x}{2}$, $ −1 ≤ x ≤ 1$, $−1...












2












$begingroup$


How could random variables with the following density function be generated
from a uniform random number generator? $f (x) = frac{1 + alpha x}{2}$, $ −1 ≤ x ≤ 1$, $−1 ≤alpha ≤ 1$.



To find the Random number generator one first needs to find the distribution function and then the quantile function. However, I have troubles solving this on my own:



begin{align}
F(x) & = int_{-1}^x frac{1+alpha u}{2} du\
& = frac 12 Big( (x+frac{alpha x^2}{2})-(-1+frac{alpha}{2})Big)\
& = alpha x^2 + 2x + 2 - alpha
end{align}



I am not sure how to handle the $alpha$. For getting the quantile(=inverse) function i need to solve this last expression for x.










share|cite|improve this question











$endgroup$












  • $begingroup$
    I think you could use the quadratic formula to solve for x, treating a as a constant!
    $endgroup$
    – Zach
    Dec 10 '18 at 12:08
















2












$begingroup$


How could random variables with the following density function be generated
from a uniform random number generator? $f (x) = frac{1 + alpha x}{2}$, $ −1 ≤ x ≤ 1$, $−1 ≤alpha ≤ 1$.



To find the Random number generator one first needs to find the distribution function and then the quantile function. However, I have troubles solving this on my own:



begin{align}
F(x) & = int_{-1}^x frac{1+alpha u}{2} du\
& = frac 12 Big( (x+frac{alpha x^2}{2})-(-1+frac{alpha}{2})Big)\
& = alpha x^2 + 2x + 2 - alpha
end{align}



I am not sure how to handle the $alpha$. For getting the quantile(=inverse) function i need to solve this last expression for x.










share|cite|improve this question











$endgroup$












  • $begingroup$
    I think you could use the quadratic formula to solve for x, treating a as a constant!
    $endgroup$
    – Zach
    Dec 10 '18 at 12:08














2












2








2





$begingroup$


How could random variables with the following density function be generated
from a uniform random number generator? $f (x) = frac{1 + alpha x}{2}$, $ −1 ≤ x ≤ 1$, $−1 ≤alpha ≤ 1$.



To find the Random number generator one first needs to find the distribution function and then the quantile function. However, I have troubles solving this on my own:



begin{align}
F(x) & = int_{-1}^x frac{1+alpha u}{2} du\
& = frac 12 Big( (x+frac{alpha x^2}{2})-(-1+frac{alpha}{2})Big)\
& = alpha x^2 + 2x + 2 - alpha
end{align}



I am not sure how to handle the $alpha$. For getting the quantile(=inverse) function i need to solve this last expression for x.










share|cite|improve this question











$endgroup$




How could random variables with the following density function be generated
from a uniform random number generator? $f (x) = frac{1 + alpha x}{2}$, $ −1 ≤ x ≤ 1$, $−1 ≤alpha ≤ 1$.



To find the Random number generator one first needs to find the distribution function and then the quantile function. However, I have troubles solving this on my own:



begin{align}
F(x) & = int_{-1}^x frac{1+alpha u}{2} du\
& = frac 12 Big( (x+frac{alpha x^2}{2})-(-1+frac{alpha}{2})Big)\
& = alpha x^2 + 2x + 2 - alpha
end{align}



I am not sure how to handle the $alpha$. For getting the quantile(=inverse) function i need to solve this last expression for x.







probability probability-theory statistics probability-distributions inverse-function






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Dec 11 '18 at 6:04









user1101010

9011830




9011830










asked Dec 10 '18 at 10:57









user1607user1607

1718




1718












  • $begingroup$
    I think you could use the quadratic formula to solve for x, treating a as a constant!
    $endgroup$
    – Zach
    Dec 10 '18 at 12:08


















  • $begingroup$
    I think you could use the quadratic formula to solve for x, treating a as a constant!
    $endgroup$
    – Zach
    Dec 10 '18 at 12:08
















$begingroup$
I think you could use the quadratic formula to solve for x, treating a as a constant!
$endgroup$
– Zach
Dec 10 '18 at 12:08




$begingroup$
I think you could use the quadratic formula to solve for x, treating a as a constant!
$endgroup$
– Zach
Dec 10 '18 at 12:08










1 Answer
1






active

oldest

votes


















0












$begingroup$

Not sure how you get from your second line to the third, there is clearly error somewhere



begin{equation}
F(x) = frac{alpha}{4}(x^2 - 1) + frac{1}{2}(x+1)
end{equation}



with obvious properties $F(x=-1)=0$ and $F(x=1)=1$. Now assign $F(x)$ to RV $xi$ in the [0...1] range and find inverse:



begin{equation}
x = F^{-1}(xi)
end{equation}



Simple quadratic equation with solution:



begin{equation}
x = frac{2 ; sqrt{frac{1}{4} + alpha(frac{alpha}{4} + xi - frac{1}{2})} - 1}{alpha}
end{equation}



For the case when $alpha=0$, $x=2xi-1$



Easy to check:



begin{equation}
xi=0, ;then; x=-1
end{equation}

begin{equation}
xi=1, ;then; x=+1
end{equation}



Python code



import numpy as np
import matplotlib.pyplot as plt

def PDF(x, alpha):
return (1.0 + alpha*x) / 2

def sampleAlpha(n, alpha):
U01 = np.random.uniform(size=n)
if alpha == 0.0:
return 2.0*U01 - 1.0

D = 0.25 + alpha*(U01 + 0.25*alpha - 0.5)
return (2.0*np.sqrt(D) - 1.0) / alpha

nbins = 100
xmin = -1.0
xmax = 1.0
alpha = 0.3

t = np.linspace(xmin, xmax, nbins + 1)

q = sampleAlpha(100000, alpha)
h, bedges = np.histogram(q, bins = t, density = True)

p = PDF(t, alpha)

plt.bar(bedges[:-1], h, width = (xmax - xmin)/float(nbins), align='edge')
plt.plot(t, p, 'r') # plotting PDF
plt.xlim(xmin, xmax)
plt.show()


generates graphs like one below for sampled vs PDF



enter image description here






share|cite|improve this answer









$endgroup$














    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: "69"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3033771%2ffind-random-number-generator-following-the-density-f-x-frac1-alpha-x%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0












    $begingroup$

    Not sure how you get from your second line to the third, there is clearly error somewhere



    begin{equation}
    F(x) = frac{alpha}{4}(x^2 - 1) + frac{1}{2}(x+1)
    end{equation}



    with obvious properties $F(x=-1)=0$ and $F(x=1)=1$. Now assign $F(x)$ to RV $xi$ in the [0...1] range and find inverse:



    begin{equation}
    x = F^{-1}(xi)
    end{equation}



    Simple quadratic equation with solution:



    begin{equation}
    x = frac{2 ; sqrt{frac{1}{4} + alpha(frac{alpha}{4} + xi - frac{1}{2})} - 1}{alpha}
    end{equation}



    For the case when $alpha=0$, $x=2xi-1$



    Easy to check:



    begin{equation}
    xi=0, ;then; x=-1
    end{equation}

    begin{equation}
    xi=1, ;then; x=+1
    end{equation}



    Python code



    import numpy as np
    import matplotlib.pyplot as plt

    def PDF(x, alpha):
    return (1.0 + alpha*x) / 2

    def sampleAlpha(n, alpha):
    U01 = np.random.uniform(size=n)
    if alpha == 0.0:
    return 2.0*U01 - 1.0

    D = 0.25 + alpha*(U01 + 0.25*alpha - 0.5)
    return (2.0*np.sqrt(D) - 1.0) / alpha

    nbins = 100
    xmin = -1.0
    xmax = 1.0
    alpha = 0.3

    t = np.linspace(xmin, xmax, nbins + 1)

    q = sampleAlpha(100000, alpha)
    h, bedges = np.histogram(q, bins = t, density = True)

    p = PDF(t, alpha)

    plt.bar(bedges[:-1], h, width = (xmax - xmin)/float(nbins), align='edge')
    plt.plot(t, p, 'r') # plotting PDF
    plt.xlim(xmin, xmax)
    plt.show()


    generates graphs like one below for sampled vs PDF



    enter image description here






    share|cite|improve this answer









    $endgroup$


















      0












      $begingroup$

      Not sure how you get from your second line to the third, there is clearly error somewhere



      begin{equation}
      F(x) = frac{alpha}{4}(x^2 - 1) + frac{1}{2}(x+1)
      end{equation}



      with obvious properties $F(x=-1)=0$ and $F(x=1)=1$. Now assign $F(x)$ to RV $xi$ in the [0...1] range and find inverse:



      begin{equation}
      x = F^{-1}(xi)
      end{equation}



      Simple quadratic equation with solution:



      begin{equation}
      x = frac{2 ; sqrt{frac{1}{4} + alpha(frac{alpha}{4} + xi - frac{1}{2})} - 1}{alpha}
      end{equation}



      For the case when $alpha=0$, $x=2xi-1$



      Easy to check:



      begin{equation}
      xi=0, ;then; x=-1
      end{equation}

      begin{equation}
      xi=1, ;then; x=+1
      end{equation}



      Python code



      import numpy as np
      import matplotlib.pyplot as plt

      def PDF(x, alpha):
      return (1.0 + alpha*x) / 2

      def sampleAlpha(n, alpha):
      U01 = np.random.uniform(size=n)
      if alpha == 0.0:
      return 2.0*U01 - 1.0

      D = 0.25 + alpha*(U01 + 0.25*alpha - 0.5)
      return (2.0*np.sqrt(D) - 1.0) / alpha

      nbins = 100
      xmin = -1.0
      xmax = 1.0
      alpha = 0.3

      t = np.linspace(xmin, xmax, nbins + 1)

      q = sampleAlpha(100000, alpha)
      h, bedges = np.histogram(q, bins = t, density = True)

      p = PDF(t, alpha)

      plt.bar(bedges[:-1], h, width = (xmax - xmin)/float(nbins), align='edge')
      plt.plot(t, p, 'r') # plotting PDF
      plt.xlim(xmin, xmax)
      plt.show()


      generates graphs like one below for sampled vs PDF



      enter image description here






      share|cite|improve this answer









      $endgroup$
















        0












        0








        0





        $begingroup$

        Not sure how you get from your second line to the third, there is clearly error somewhere



        begin{equation}
        F(x) = frac{alpha}{4}(x^2 - 1) + frac{1}{2}(x+1)
        end{equation}



        with obvious properties $F(x=-1)=0$ and $F(x=1)=1$. Now assign $F(x)$ to RV $xi$ in the [0...1] range and find inverse:



        begin{equation}
        x = F^{-1}(xi)
        end{equation}



        Simple quadratic equation with solution:



        begin{equation}
        x = frac{2 ; sqrt{frac{1}{4} + alpha(frac{alpha}{4} + xi - frac{1}{2})} - 1}{alpha}
        end{equation}



        For the case when $alpha=0$, $x=2xi-1$



        Easy to check:



        begin{equation}
        xi=0, ;then; x=-1
        end{equation}

        begin{equation}
        xi=1, ;then; x=+1
        end{equation}



        Python code



        import numpy as np
        import matplotlib.pyplot as plt

        def PDF(x, alpha):
        return (1.0 + alpha*x) / 2

        def sampleAlpha(n, alpha):
        U01 = np.random.uniform(size=n)
        if alpha == 0.0:
        return 2.0*U01 - 1.0

        D = 0.25 + alpha*(U01 + 0.25*alpha - 0.5)
        return (2.0*np.sqrt(D) - 1.0) / alpha

        nbins = 100
        xmin = -1.0
        xmax = 1.0
        alpha = 0.3

        t = np.linspace(xmin, xmax, nbins + 1)

        q = sampleAlpha(100000, alpha)
        h, bedges = np.histogram(q, bins = t, density = True)

        p = PDF(t, alpha)

        plt.bar(bedges[:-1], h, width = (xmax - xmin)/float(nbins), align='edge')
        plt.plot(t, p, 'r') # plotting PDF
        plt.xlim(xmin, xmax)
        plt.show()


        generates graphs like one below for sampled vs PDF



        enter image description here






        share|cite|improve this answer









        $endgroup$



        Not sure how you get from your second line to the third, there is clearly error somewhere



        begin{equation}
        F(x) = frac{alpha}{4}(x^2 - 1) + frac{1}{2}(x+1)
        end{equation}



        with obvious properties $F(x=-1)=0$ and $F(x=1)=1$. Now assign $F(x)$ to RV $xi$ in the [0...1] range and find inverse:



        begin{equation}
        x = F^{-1}(xi)
        end{equation}



        Simple quadratic equation with solution:



        begin{equation}
        x = frac{2 ; sqrt{frac{1}{4} + alpha(frac{alpha}{4} + xi - frac{1}{2})} - 1}{alpha}
        end{equation}



        For the case when $alpha=0$, $x=2xi-1$



        Easy to check:



        begin{equation}
        xi=0, ;then; x=-1
        end{equation}

        begin{equation}
        xi=1, ;then; x=+1
        end{equation}



        Python code



        import numpy as np
        import matplotlib.pyplot as plt

        def PDF(x, alpha):
        return (1.0 + alpha*x) / 2

        def sampleAlpha(n, alpha):
        U01 = np.random.uniform(size=n)
        if alpha == 0.0:
        return 2.0*U01 - 1.0

        D = 0.25 + alpha*(U01 + 0.25*alpha - 0.5)
        return (2.0*np.sqrt(D) - 1.0) / alpha

        nbins = 100
        xmin = -1.0
        xmax = 1.0
        alpha = 0.3

        t = np.linspace(xmin, xmax, nbins + 1)

        q = sampleAlpha(100000, alpha)
        h, bedges = np.histogram(q, bins = t, density = True)

        p = PDF(t, alpha)

        plt.bar(bedges[:-1], h, width = (xmax - xmin)/float(nbins), align='edge')
        plt.plot(t, p, 'r') # plotting PDF
        plt.xlim(xmin, xmax)
        plt.show()


        generates graphs like one below for sampled vs PDF



        enter image description here







        share|cite|improve this answer












        share|cite|improve this answer



        share|cite|improve this answer










        answered Dec 11 '18 at 5:11









        Severin PappadeuxSeverin Pappadeux

        15318




        15318






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Mathematics Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3033771%2ffind-random-number-generator-following-the-density-f-x-frac1-alpha-x%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?