Announcement

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

  • Probability distribution histogram - normal

    Hello everyone,
    Any codes that are suitable for a normal distribution histogram with a mean of 0.6 and variance of 0.03 with 100 observations?
    Last edited by Vinayak Nagaraja; 24 May 2021, 16:17.

  • #2
    If what you intend is drawing a random sample of 100 draws from an N(.6,.03) distribution then you might try something like this:
    Code:
    cap preserve
    cap drop _all
    set seed 2345
    set obs 100
    local s=sqrt(.03)
    local m=.6
    gen y=rnormal(`m',`s')
    hist y
    cap restore
    Different values of the seed will result in different random samples being drawn. See
    Code:
    help set seed

    Comment


    • #3
      Thanks, Mr Mullahy

      Comment


      • #4
        If you want to guarantee that the mean is exactly 0.6 and the standard deviation is exactly 0.03 you should transform the random values accordingly (it may happen that sample mean and sd are different):
        Code:
        clear
        set seed 9141163
        set obs 100
        
        gen x1 = rnormal(.6,.03)          // actually -gen x1 = rnormal()- would also do
        sum x1
        local m_1 : di %5.3f r(mean)
        local s_1 : di %6.4f r(sd)
        lab var x1 "mean=`m_1', sd=`s_1'"
         
        egen x2 = std(x1)                 // z-standardize x1
        replace x2 = .6 + x2*.03          // transform x2 to mean=.6 and sd=.03
        sum x2
        local m_2 : di %5.3f r(mean)
        local s_2 : di %6.4f r(sd)
        lab var x2 "mean=`m_2', sd=`s_2'"
        
        histogram x1, normal name(x1, replace)
        histogram x2, normal name(x2, replace)
        graph combine x1 x2, c(1) ycommon xcommon  // compare distribution of x1 and x2
        sum x1 x2                                  // compare mean and sd of x1 and x2
        Comparison of mean and of sd x1 and x2:
        Code:
        . sum x1 x2                                  // compare mean and sd of x1 and x2
        
            Variable |        Obs        Mean    Std. dev.       Min        Max
        -------------+---------------------------------------------------------
                  x1 |        100    .5920749    .0430892   .4955885    .706874
                  x2 |        100          .6         .03   .5328233   .6799266

        Comment

        Working...
        X