Announcement

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

  • Random number from a triangular distribution

    Hi,

    I was wondering if it were possible to generate random numbers from a triangular distribution in Mata as, for example, for the uniform distribution with runiform()

    Any help would be greatly appreciated.

    Simone

  • #2
    A standard triangular distribution on (-1,1) can be generated as the difference between two independent standard uniform (0,1) variates. As such this Mata code will generate a random triangularly-distributed n-vector t as the difference between two uniforms. (The code then goes on to depict its histogram.)
    Code:
    cap preserve
    cap drop _all
    
    loc n=10000
    set obs `n'
    
    mata
    
    rseed(2345)
    
    u1=runiform(`n',1)
    u2=runiform(`n',1)
    t=u1-u2
    
    end
    
    getmata tvar=t
    
    hist tvar
    
    cap restore

    Comment


    • #3
      P.S. For repeated applications it might be worthwhile to define a Mata function:
      Code:
      mata
      
      function rtriangle(r,c) return(runiform(r,c)-runiform(r,c))
      
      end
      This function would return an rxc matrix of pseudo-random standard triangular variates.

      Comment

      Working...
      X