Announcement

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

  • Probability distribution histogram - binomial

    Hello,
    In case of binomial probability, where n = 100 and p= 0.6. I would like to compute binomial probability when x = 0, 1, ...., 100 and plot the histogram.
    I use the following command:

    set obs 100
    forvalue i=1/100 {
    scalar prob`i' = binomialp(100,`i',0.6)
    }
    How can I combine the prob1,..., prob100 into one variable to plot the binomial distribution?
    Thank You

  • #2
    No loop needed, and no reason to create scalars here.

    Code:
    clear*
    set obs 101
    gen prob = binomialp(100, _n-1, 0.6)
    gen k = _n-1
    graph twoway bar prob k
    By the way, what you are plotting here is not the binomial distribution function but the binomial probability density function.

    Comment


    • #3
      I think this can solve your problem:
      Code:
      set obs 100
      gen prob = .
      
      forvalue i=1/100 {
          replace prob = binomialp(100,`i',0.6) in `i'
      }

      Comment


      • #4
        While the solution in #3 will generate (essentially) the same data set as the code in #2, note that it contains an unnecessary loop carrying out the interpretation and execution of 100 separate -replace- commands, whereas #2 populates the same data (actually one more observation to account for the possibility that k = 0) in a single pass.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          While the solution in #3 will generate (essentially) the same data set as the code in #2, note that it contains an unnecessary loop carrying out the interpretation and execution of 100 separate -replace- commands, whereas #2 populates the same data (actually one more observation to account for the possibility that k = 0) in a single pass.
          Thank you for pointing out! I am not familiar with the function "binomialp".

          Comment


          • #6
            Thank You.

            Comment


            • #7
              Any possibility this code could be made suitable for a normal distribution with a mean of 0.5 and variance of 0.025?

              Comment


              • #8
                If interested in #7 please follow https://www.statalist.org/forums/for...stogram-normal

                Comment

                Working...
                X