Announcement

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

  • randomization process coding

    Hi Everyone,

    I have 13 events, and I want to have a probability for each.

    - Define the Partial_Sum(j)=sum(p(k) to p(j)), where j=1,2,...13 with Partial_Sum(14)=1
    - To sample from this discrete distribution, a random variable X is drawn from a continuous uniform distribution on the interval (0,1)
    - For each value of j check which of the following N mutual events is true:
    * X<=Partial_Sum(1)
    * Partial_Sum(j-1)<X<=Partial_Sum(j) for j=2,3,...,13

    I need to write a code for this. Could you please help me with this?







  • #2
    I've illustrated an approach with my own made up set of 13 partial sums.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear*
    input int index float partial_sum
     1 .14785433
     2  .1692771
     3 .19177543
     4 .19815916
     5 .21698804
     6  .2227877
     7  .2450673
     8 .25898004
     9  .6292577
    10  .7572148
    11  .8604344
    12  .8686824
    13         1
    end
    tempfile partial_sums
    save `partial_sums'
    
    //    SIMULATE 100 DRAWS FROM THE PDF DEFINED IN `partial_sums'
    clear
    set seed 1234
    set obs 100
    gen x = runiform()
    gen int obs_no = _n
    
    cross using `partial_sums'
    by obs_no (index), sort: egen wanted = min(cond(partial_sum >= x, index, .))
    keep obs_no x wanted
    duplicates drop

    Comment


    • #3
      Thank you so much, Clyde!

      Comment

      Working...
      X