Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generate random numbers - specify range and mean

    Hello everyone,

    Trust everyone is keeping safe. Apologies if this might be a silly question - is there a way generate a set of random numbers (e.g. 100) within a given range (e.g [0 30] but with a specific mean (e.g. (e.g. 8.5)? Any help will be appreciated. Thank you!

    Best,

    Chiara

  • #2
    The question is not silly, in fact it is rather complicated (and probably impossible to answer) at the level of generality you are asking it.

    But if I take your examples given as what you want to achieve here it is how:

    Code:
    . set obs 1000000
    number of observations (_N) was 0, now 1,000,000
    
    . gen beta = 30*rbeta(1,2.52941176471)
    
    . summ beta
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
            beta |  1,000,000    8.500319    6.353481    .000027    29.8619

    Comment


    • #3
      Thank you for your response and the suggestion. Could you kindly give me a bit more information on
      Code:
      rbeta(1,2.52941176471)
      Best,

      Chiara

      Comment


      • #4
        I did the following:

        1) You asked me for a random variable within a certain range, the example you gave was [0,30]. I know that the Beta distribution is between [0,1], but I can fix this by multiplying by 30, then I get your range.

        2) I also know that the Mean(Beta) = a/(a+b). So I solved the equation 30*a/(a+b) = 8.5. There are multiple solutions to that, but if I fix a = 1, then b has to be = 2.52941176471

        3) So I went and generated a Beta random variable with parameters a=1 and b=2.52941176471.

        And viola (as the French say) I got a number which had the properties you gave as an example.




        Originally posted by Chiara Piazzo View Post
        Thank you for your response and the suggestion. Could you kindly give me a bit more information on
        Code:
        rbeta(1,2.52941176471)
        Best,

        Chiara

        Comment


        • #5
          From the identity


          8.5 = .(8.5/30) * 30 + (21.5/30) * 0


          it follows that a distribution which is 30 8.5/30 of the time and 0 otherwise satisfies your criteria. The mean and the range don't fix the distribution in general.

          But as seriously suggested and implemented by Joro Kolev, a beta distribution is certainly a flexible family which may be adequate for your purposes.

          PS I think that the French (at least those in my school texts) say voilĂ . But then again my school texts told me that the French say Quelle horreur! when annoyed, and I think those texts lied to me. My sampling of friends and colleagues indicates that faced with (say)

          Code:
          invalid syntax
          r(198);
          Code:
          
          


          the fraction of French users saying "Quelle horreur!" is precisely 0.

          Comment


          • #6
            This is excellent, Nick, a two point distribution the way how you construct it does the job here, so in this context I over-complicated unnecessarily. It did not even come to me, because what I have done in my own research is having such two point distributions given, and then trying to come up with a continuous distributions with the same (or as close as possible) corresponding moments. (E.g., in the wild bootstrap there are those Mammen and Radamacher two point distributions, so then how do we come up with something that is continuous and has similar moments.)

            PS.

            The joke which I tried to crack makes an allusion to a clueless person who tries to look sophisticated by insinuating that he knows French and tries to insert voila, but because he is clueless he says violin, fiddle.

            The joke that you constructed was pretty awesome. Indeed, what they teach us in school is not exactly what the French say when they hit their finger with a hammer.



            Originally posted by Nick Cox View Post
            From the identity


            8.5 = .(8.5/30) * 30 + (21.5/30) * 0


            it follows that a distribution which is 30 8.5/30 of the time and 0 otherwise satisfies your criteria. The mean and the range don't fix the distribution in general.

            But as seriously suggested and implemented by Joro Kolev, a beta distribution is certainly a flexible family which may be adequate for your purposes.

            PS. I think that the French (at least those in my school texts) say voilĂ . But then again my school texts told me that the French say Quelle horreur! when annoyed, and I think those texts lied to me. My sampling of friends and colleagues indicates that faced with (say)

            Code:
            invalid syntax
            r(198);
            Code:
            
            


            the fraction of French users saying "Quelle horreur!" is precisely 0.

            Comment


            • #7
              Dear Joro Kolev and Nick Cox,

              Thank you so much! This is great - I am playing around with your suggestions.

              I have a question on this - what if I wanted to expand the range of values and include negative numbers [-60 20]? How would things change? Thanks again!

              Best,

              Chiara

              P.S. Nick - thanks for the excellent joke

              Comment


              • #8
                The two point distribution that Nick described is easier to handle. So you solve for p

                p*(-60) + (1-p)*20 = 8.5,
                giving p = 0.14375.

                Code:
                clear 
                
                set obs 1000000
                
                gen x = cond(runiform()<0.14375, -60, 20)
                
                summ x
                
                . summ x
                
                    Variable |        Obs        Mean    Std. Dev.       Min        Max
                -------------+---------------------------------------------------------
                           x |  1,000,000     8.50864    28.05812        -60         20

                Originally posted by Chiara Piazzo View Post
                Dear Joro Kolev and Nick Cox,

                Thank you so much! This is great - I am playing around with your suggestions.

                I have a question on this - what if I wanted to expand the range of values and include negative numbers [-60 20]? How would things change? Thanks again!

                Best,

                Chiara

                P.S. Nick - thanks for the excellent joke

                Comment


                • #9
                  This is great, thanks again!

                  Best

                  CP

                  Comment


                  • #10
                    Sorry spoke too soon. The following code only generates values of -60 and 20. Is this what is supposed to happen?

                    Code:
                    gen x = cond(runiform()<0.14375, -60, 20)
                    Thanks and best.

                    Chiara

                    Comment


                    • #11
                      Yes, it is a two point distributing where the value of -60 happens with probability of 0.14375, and the value of 20 with probability (1-0.14375). It has a range of -60 to 20, and a mean of 8.5 as you asked.

                      Originally posted by Chiara Piazzo View Post
                      Sorry spoke too soon. The following code only generates values of -60 and 20. Is this what is supposed to happen?

                      Code:
                      gen x = cond(runiform()<0.14375, -60, 20)
                      Thanks and best.

                      Chiara


                      Comment


                      • #12
                        Thank you!

                        Comment

                        Working...
                        X